Further massage
[llpp.git] / main.ml
blobaff6dbaa40460297d313e997c7f22fdca9b26292
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 module Opaque :
29 sig
30 type t = private string
31 val of_string : string -> t
32 val to_string : t -> string
33 end
35 struct
36 type t = string
37 let of_string s = s
38 let to_string t = t
39 end
42 let (~<) = Opaque.of_string;;
43 let (~>) = Opaque.to_string;;
45 type params = (angle * fitmodel * trimparams
46 * texcount * sliceheight * memsize
47 * colorspace * fontpath * trimcachepath
48 * haspbo)
49 and width = int
50 and height = int
51 and leftx = int
52 and opaque = Opaque.t
53 and recttype = int
54 and pixmapsize = int
55 and angle = int
56 and trimmargins = bool
57 and interpagespace = int
58 and texcount = int
59 and sliceheight = int
60 and gen = int
61 and top = float
62 and dtop = float
63 and fontpath = string
64 and trimcachepath = string
65 and memsize = int
66 and aalevel = int
67 and irect = (int * int * int * int)
68 and trimparams = (trimmargins * irect)
69 and colorspace = | Rgb | Bgr | Gray
70 and fitmodel = | FitWidth | FitProportional | FitPage
71 and haspbo = bool
72 and uri = string
73 and caption = string
76 type x = int
77 and y = int
78 and tilex = int
79 and tiley = int
80 and tileparams = (x * y * width * height * tilex * tiley)
83 type link =
84 | Lnotfound
85 | Lfound of int
86 and linkdir =
87 | LDfirst
88 | LDlast
89 | LDfirstvisible of (int * int * int)
90 | LDleft of int
91 | LDright of int
92 | LDdown of int
93 | LDup of int
96 type pagewithlinks =
97 | Pwlnotfound
98 | Pwl of int
101 type keymap =
102 | KMinsrt of key
103 | KMinsrl of key list
104 | KMmulti of key list * key list
105 and key = int * int
106 and keyhash = (key, keymap) Hashtbl.t
107 and keystate =
108 | KSnone
109 | KSinto of (key list * key list)
112 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
113 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
115 type pipe = (Unix.file_descr * Unix.file_descr);;
117 external init : pipe -> params -> unit = "ml_init";;
118 external seltext : opaque -> (int * int * int * int) -> unit = "ml_seltext";;
119 external hassel : opaque -> bool = "ml_hassel";;
120 external copysel : Unix.file_descr -> opaque -> unit = "ml_copysel";;
121 external getpdimrect : int -> float array = "ml_getpdimrect";;
122 external whatsunder : opaque -> int -> int -> under = "ml_whatsunder";;
123 external markunder : opaque -> int -> int -> mark -> bool = "ml_markunder";;
124 external clearmark : opaque -> unit = "ml_clearmark";;
125 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
126 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
127 external measurestr : int -> string -> float = "ml_measure_string";;
128 external postprocess :
129 opaque -> int -> int -> int -> (int * string * int) -> int
130 = "ml_postprocess";;
131 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
132 external platform : unit -> platform = "ml_platform";;
133 external setaalevel : int -> unit = "ml_setaalevel";;
134 external realloctexts : int -> bool = "ml_realloctexts";;
135 external findlink : opaque -> linkdir -> link = "ml_findlink";;
136 external getlink : opaque -> int -> under = "ml_getlink";;
137 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
138 external getlinkcount : opaque -> int = "ml_getlinkcount";;
139 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
140 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
141 external getpbo : width -> height -> colorspace -> opaque = "ml_getpbo";;
142 external freepbo : opaque -> unit = "ml_freepbo";;
143 external unmappbo : opaque -> unit = "ml_unmappbo";;
144 external pbousable : unit -> bool = "ml_pbo_usable";;
145 external unproject : opaque -> int -> int -> (int * int) option
146 = "ml_unproject";;
147 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
148 external rectofblock : opaque -> int -> int -> float array option
149 = "ml_rectofblock";;
150 external fz_version : unit -> string = "ml_fz_version";;
151 external begintiles : unit -> unit = "ml_begintiles";;
152 external endtiles : unit -> unit = "ml_endtiles";;
154 let platform_to_string = function
155 | Punknown -> "unknown"
156 | Plinux -> "Linux"
157 | Posx -> "OSX"
158 | Psun -> "Sun"
159 | Pfreebsd -> "FreeBSD"
160 | Pdragonflybsd -> "DragonflyBSD"
161 | Popenbsd -> "OpenBSD"
162 | Pnetbsd -> "NetBSD"
163 | Pcygwin -> "Cygwin"
166 let platform = platform ();;
168 let now = Unix.gettimeofday;;
170 let selfexec = ref "";;
172 let popen cmd fda =
173 if platform = Pcygwin
174 then (
175 let sh = "/bin/sh" in
176 let args = [|sh; "-c"; cmd|] in
177 let rec std si so se = function
178 | [] -> si, so, se
179 | (fd, 0) :: rest -> std fd so se rest
180 | (fd, -1) :: rest ->
181 Unix.set_close_on_exec fd;
182 std si so se rest
183 | (_, n) :: _ ->
184 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
186 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
187 ignore (Unix.create_process sh args si so se)
189 else popen cmd fda;
192 type mpos = int * int
193 and mstate =
194 | Msel of (mpos * mpos)
195 | Mpan of mpos
196 | Mscrolly | Mscrollx
197 | Mzoom of (int * int)
198 | Mzoomrect of (mpos * mpos)
199 | Mnone
202 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
203 and onkey = string -> int -> te
204 and ondone = string -> unit
205 and histcancel = unit -> unit
206 and onhist = ((histcmd -> string) * histcancel)
207 and histcmd = HCnext | HCprev | HCfirst | HClast
208 and cancelonempty = bool
209 and te =
210 | TEstop
211 | TEdone of string
212 | TEcont of string
213 | TEswitch of textentry
216 type 'a circbuf =
217 { store : 'a array
218 ; mutable rc : int
219 ; mutable wc : int
220 ; mutable len : int
224 let bound v minv maxv =
225 max minv (min maxv v);
228 let cbnew n v =
229 { store = Array.create n v
230 ; rc = 0
231 ; wc = 0
232 ; len = 0
236 let cbcap b = Array.length b.store;;
238 let cbput b v =
239 let cap = cbcap b in
240 b.store.(b.wc) <- v;
241 b.wc <- (b.wc + 1) mod cap;
242 b.rc <- b.wc;
243 b.len <- min (b.len + 1) cap;
246 let cbempty b = b.len = 0;;
248 let cbgetg b circular dir =
249 if cbempty b
250 then b.store.(0)
251 else
252 let rc = b.rc + dir in
253 let rc =
254 if circular
255 then (
256 if rc = -1
257 then b.len-1
258 else (
259 if rc >= b.len
260 then 0
261 else rc
264 else bound rc 0 (b.len-1)
266 b.rc <- rc;
267 b.store.(rc);
270 let cbget b = cbgetg b false;;
271 let cbgetc b = cbgetg b true;;
273 let drawstring size x y s =
274 Gl.enable `blend;
275 Gl.enable `texture_2d;
276 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
277 ignore (drawstr size x y s);
278 Gl.disable `blend;
279 Gl.disable `texture_2d;
282 let drawstring1 size x y s =
283 drawstr size x y s;
286 let drawstring2 size x y fmt =
287 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
290 type page =
291 { pageno : int
292 ; pagedimno : int
293 ; pagew : int
294 ; pageh : int
295 ; pagex : int
296 ; pagey : int
297 ; pagevw : int
298 ; pagevh : int
299 ; pagedispx : int
300 ; pagedispy : int
301 ; pagecol : int
305 let debugl l =
306 dolog "l %d dim=%d {" l.pageno l.pagedimno;
307 dolog " WxH %dx%d" l.pagew l.pageh;
308 dolog " vWxH %dx%d" l.pagevw l.pagevh;
309 dolog " pagex,y %d,%d" l.pagex l.pagey;
310 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
311 dolog " column %d" l.pagecol;
312 dolog "}";
315 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
316 dolog "rect {";
317 dolog " x0,y0=(% f, % f)" x0 y0;
318 dolog " x1,y1=(% f, % f)" x1 y1;
319 dolog " x2,y2=(% f, % f)" x2 y2;
320 dolog " x3,y3=(% f, % f)" x3 y3;
321 dolog "}";
324 type multicolumns = multicol * pagegeom
325 and singlecolumn = pagegeom
326 and splitcolumns = columncount * pagegeom
327 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
328 and multicol = columncount * covercount * covercount
329 and pdimno = int
330 and columncount = int
331 and covercount = int;;
333 type scrollb = int;;
334 let scrollbvv = 1;;
335 let scrollbhv = 2;;
337 type conf =
338 { mutable scrollbw : int
339 ; mutable scrollh : int
340 ; mutable scrollb : scrollb
341 ; mutable icase : bool
342 ; mutable preload : bool
343 ; mutable pagebias : int
344 ; mutable verbose : bool
345 ; mutable debug : bool
346 ; mutable scrollstep : int
347 ; mutable hscrollstep : int
348 ; mutable maxhfit : bool
349 ; mutable crophack : bool
350 ; mutable autoscrollstep : int
351 ; mutable maxwait : float option
352 ; mutable hlinks : bool
353 ; mutable underinfo : bool
354 ; mutable interpagespace : interpagespace
355 ; mutable zoom : float
356 ; mutable presentation : bool
357 ; mutable angle : angle
358 ; mutable cwinw : int
359 ; mutable cwinh : int
360 ; mutable savebmarks : bool
361 ; mutable fitmodel : fitmodel
362 ; mutable trimmargins : trimmargins
363 ; mutable trimfuzz : irect
364 ; mutable memlimit : memsize
365 ; mutable texcount : texcount
366 ; mutable sliceheight : sliceheight
367 ; mutable thumbw : width
368 ; mutable jumpback : bool
369 ; mutable bgcolor : (float * float * float)
370 ; mutable bedefault : bool
371 ; mutable tilew : int
372 ; mutable tileh : int
373 ; mutable mustoresize : memsize
374 ; mutable checkers : bool
375 ; mutable aalevel : int
376 ; mutable urilauncher : string
377 ; mutable pathlauncher : string
378 ; mutable colorspace : colorspace
379 ; mutable invert : bool
380 ; mutable colorscale : float
381 ; mutable redirectstderr : bool
382 ; mutable ghyllscroll : (int * int * int) option
383 ; mutable columns : columns
384 ; mutable beyecolumns : columncount option
385 ; mutable selcmd : string
386 ; mutable paxcmd : string
387 ; mutable updatecurs : bool
388 ; mutable keyhashes : (string * keyhash) list
389 ; mutable hfsize : int
390 ; mutable pgscale : float
391 ; mutable usepbo : bool
392 ; mutable wheelbypage : bool
393 ; mutable stcmd : string
394 ; mutable riani : bool
395 ; mutable pax : (float * int * int) ref option
396 ; mutable paxmark : mark
398 and columns =
399 | Csingle of singlecolumn
400 | Cmulti of multicolumns
401 | Csplit of splitcolumns
404 type anchor = pageno * top * dtop;;
406 type outlinekind =
407 | Onone
408 | Oanchor of anchor
409 | Ouri of uri
410 | Olaunch of launchcommand
411 | Oremote of (filename * pageno)
412 | Oremotedest of (filename * destname)
413 and outline = (caption * outlinelevel * outlinekind)
414 and outlinelevel = int
417 type rect = float * float * float * float * float * float * float * float;;
419 type tile = opaque * pixmapsize * elapsed
420 and elapsed = float;;
421 type pagemapkey = pageno * gen;;
422 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
423 and row = int
424 and col = int;;
426 let emptyanchor = (0, 0.0, 0.0);;
428 type infochange = | Memused | Docinfo | Pdim;;
430 class type uioh = object
431 method display : unit
432 method key : int -> int -> uioh
433 method button : int -> bool -> int -> int -> int -> uioh
434 method multiclick : int -> int -> int -> int -> uioh
435 method motion : int -> int -> uioh
436 method pmotion : int -> int -> uioh
437 method infochanged : infochange -> unit
438 method scrollpw : (int * float * float)
439 method scrollph : (int * float * float)
440 method modehash : keyhash
441 method eformsgs : bool
442 end;;
444 type mode =
445 | Birdseye of (conf * leftx * pageno * pageno * anchor)
446 | Textentry of (textentry * onleave)
447 | View
448 | LinkNav of linktarget
449 and onleave = leavetextentrystatus -> unit
450 and leavetextentrystatus = | Cancel | Confirm
451 and helpitem = string * int * action
452 and action =
453 | Noaction
454 | Action of (uioh -> uioh)
455 and linktarget =
456 | Ltexact of (pageno * int)
457 | Ltgendir of int
460 let isbirdseye = function Birdseye _ -> true | _ -> false;;
461 let istextentry = function Textentry _ -> true | _ -> false;;
463 type currently =
464 | Idle
465 | Loading of (page * gen)
466 | Tiling of (
467 page * opaque * colorspace * angle * gen * col * row * width * height
469 | Outlining of outline list
472 let emptykeyhash = Hashtbl.create 0;;
473 let nouioh : uioh = object (self)
474 method display = ()
475 method key _ _ = self
476 method multiclick _ _ _ _ = self
477 method button _ _ _ _ _ = self
478 method motion _ _ = self
479 method pmotion _ _ = self
480 method infochanged _ = ()
481 method scrollpw = (0, nan, nan)
482 method scrollph = (0, nan, nan)
483 method modehash = emptykeyhash
484 method eformsgs = false
485 end;;
487 type state =
488 { mutable sr : Unix.file_descr
489 ; mutable sw : Unix.file_descr
490 ; mutable wsfd : Unix.file_descr
491 ; mutable errfd : Unix.file_descr option
492 ; mutable stderr : Unix.file_descr
493 ; mutable errmsgs : Buffer.t
494 ; mutable newerrmsgs : bool
495 ; mutable w : int
496 ; mutable x : int
497 ; mutable y : int
498 ; mutable anchor : anchor
499 ; mutable ranchors : (string * string * anchor * string) list
500 ; mutable maxy : int
501 ; mutable layout : page list
502 ; pagemap : (pagemapkey, opaque) Hashtbl.t
503 ; tilemap : (tilemapkey, tile) Hashtbl.t
504 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
505 ; mutable pdims : (pageno * width * height * leftx) list
506 ; mutable pagecount : int
507 ; mutable currently : currently
508 ; mutable mstate : mstate
509 ; mutable searchpattern : string
510 ; mutable rects : (pageno * recttype * rect) list
511 ; mutable rects1 : (pageno * recttype * rect) list
512 ; mutable text : string
513 ; mutable winstate : Wsi.winstate list
514 ; mutable mode : mode
515 ; mutable uioh : uioh
516 ; mutable outlines : outline array
517 ; mutable bookmarks : outline list
518 ; mutable path : string
519 ; mutable password : string
520 ; mutable nameddest : string
521 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
522 ; mutable memused : memsize
523 ; mutable gen : gen
524 ; mutable throttle : (page list * int * float) option
525 ; mutable autoscroll : int option
526 ; mutable ghyll : (int option -> unit)
527 ; mutable help : helpitem array
528 ; mutable docinfo : (int * string) list
529 ; mutable texid : GlTex.texture_id option
530 ; hists : hists
531 ; mutable prevzoom : (float * int)
532 ; mutable progress : float
533 ; mutable redisplay : bool
534 ; mutable mpos : mpos
535 ; mutable keystate : keystate
536 ; mutable glinks : bool
537 ; mutable prevcolumns : (columns * float) option
538 ; mutable winw : int
539 ; mutable winh : int
540 ; mutable reprf : (unit -> unit)
541 ; mutable origin : string
542 ; mutable roam : (unit -> unit)
543 ; mutable bzoom : bool
544 ; mutable traw : [`float] Raw.t
545 ; mutable vraw : [`float] Raw.t
547 and hists =
548 { pat : string circbuf
549 ; pag : string circbuf
550 ; nav : anchor circbuf
551 ; sel : string circbuf
555 let defconf =
556 { scrollbw = 7
557 ; scrollh = 12
558 ; scrollb = scrollbhv lor scrollbvv
559 ; icase = true
560 ; preload = true
561 ; pagebias = 0
562 ; verbose = false
563 ; debug = false
564 ; scrollstep = 24
565 ; hscrollstep = 24
566 ; maxhfit = true
567 ; crophack = false
568 ; autoscrollstep = 2
569 ; maxwait = None
570 ; hlinks = false
571 ; underinfo = false
572 ; interpagespace = 2
573 ; zoom = 1.0
574 ; presentation = false
575 ; angle = 0
576 ; cwinw = 900
577 ; cwinh = 900
578 ; savebmarks = true
579 ; fitmodel = FitProportional
580 ; trimmargins = false
581 ; trimfuzz = (0,0,0,0)
582 ; memlimit = 32 lsl 20
583 ; texcount = 256
584 ; sliceheight = 24
585 ; thumbw = 76
586 ; jumpback = true
587 ; bgcolor = (0.5, 0.5, 0.5)
588 ; bedefault = false
589 ; tilew = 2048
590 ; tileh = 2048
591 ; mustoresize = 256 lsl 20
592 ; checkers = true
593 ; aalevel = 8
594 ; urilauncher =
595 (match platform with
596 | Plinux | Pfreebsd | Pdragonflybsd
597 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
598 | Posx -> "open \"%s\""
599 | Pcygwin -> "cygstart \"%s\""
600 | Punknown -> "echo %s")
601 ; pathlauncher = "lp \"%s\""
602 ; selcmd =
603 (match platform with
604 | Plinux | Pfreebsd | Pdragonflybsd
605 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
606 | Posx -> "pbcopy"
607 | Pcygwin -> "wsel"
608 | Punknown -> "cat")
609 ; paxcmd = "cat"
610 ; colorspace = Rgb
611 ; invert = false
612 ; colorscale = 1.0
613 ; redirectstderr = false
614 ; ghyllscroll = None
615 ; columns = Csingle [||]
616 ; beyecolumns = None
617 ; updatecurs = false
618 ; hfsize = 12
619 ; pgscale = 1.0
620 ; usepbo = false
621 ; wheelbypage = false
622 ; stcmd = "echo SyncTex"
623 ; riani = false
624 ; pax = None
625 ; paxmark = Mark_word
626 ; keyhashes =
627 let mk n = (n, Hashtbl.create 1) in
628 [ mk "global"
629 ; mk "info"
630 ; mk "help"
631 ; mk "outline"
632 ; mk "listview"
633 ; mk "birdseye"
634 ; mk "textentry"
635 ; mk "links"
636 ; mk "view"
641 let wtmode = ref false;;
642 let cxack = ref false;;
644 let findkeyhash c name =
645 try List.assoc name c.keyhashes
646 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
649 let conf = { defconf with angle = defconf.angle };;
651 let pgscale h = truncate (float h *. conf.pgscale);;
653 type fontstate =
654 { mutable fontsize : int
655 ; mutable wwidth : float
656 ; mutable maxrows : int
660 let fstate =
661 { fontsize = 14
662 ; wwidth = nan
663 ; maxrows = -1
667 let geturl s =
668 let colonpos = try String.index s ':' with Not_found -> -1 in
669 let len = String.length s in
670 if colonpos >= 0 && colonpos + 3 < len
671 then (
672 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
673 then
674 let schemestartpos =
675 try String.rindex_from s colonpos ' '
676 with Not_found -> -1
678 let scheme =
679 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
681 match scheme with
682 | "http" | "ftp" | "mailto" ->
683 let epos =
684 try String.index_from s colonpos ' '
685 with Not_found -> len
687 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
688 | _ -> ""
689 else ""
691 else ""
694 let gotouri uri =
695 if emptystr conf.urilauncher
696 then print_endline uri
697 else (
698 let url = geturl uri in
699 if emptystr url
700 then Printf.eprintf "obtained empty url from uri %S\n" uri
701 else
702 let re = Str.regexp "%s" in
703 let command = Str.global_replace re url conf.urilauncher in
704 try popen command []
705 with exn ->
706 Printf.eprintf
707 "failed to execute `%s': %s\n" command (exntos exn);
708 flush stderr;
712 let version () =
713 Printf.sprintf "llpp version %s, fitz %s, ocaml %s (%s/%dbit)"
714 Help.version (fz_version ()) Sys.ocaml_version
715 (platform_to_string platform) Sys.word_size
718 let makehelp () =
719 let strings = version () :: "" :: Help.keys in
720 Array.of_list (
721 List.map (fun s ->
722 let url = geturl s in
723 if nonemptystr url
724 then (s, 0, Action (fun u -> gotouri url; u))
725 else (s, 0, Noaction)
726 ) strings);
729 let noghyll _ = ();;
730 let firstgeomcmds = "", [];;
731 let noreprf () = ();;
732 let noroam () = ();;
734 let state =
735 { sr = Unix.stdin
736 ; sw = Unix.stdin
737 ; wsfd = Unix.stdin
738 ; errfd = None
739 ; stderr = Unix.stderr
740 ; errmsgs = Buffer.create 0
741 ; newerrmsgs = false
742 ; x = 0
743 ; y = 0
744 ; w = 0
745 ; anchor = emptyanchor
746 ; ranchors = []
747 ; layout = []
748 ; maxy = max_int
749 ; tilelru = Queue.create ()
750 ; pagemap = Hashtbl.create 10
751 ; tilemap = Hashtbl.create 10
752 ; pdims = []
753 ; pagecount = 0
754 ; currently = Idle
755 ; mstate = Mnone
756 ; rects = []
757 ; rects1 = []
758 ; text = ""
759 ; mode = View
760 ; winstate = []
761 ; searchpattern = ""
762 ; outlines = [||]
763 ; bookmarks = []
764 ; path = ""
765 ; password = ""
766 ; nameddest = ""
767 ; geomcmds = firstgeomcmds
768 ; hists =
769 { nav = cbnew 10 emptyanchor
770 ; pat = cbnew 10 ""
771 ; pag = cbnew 10 ""
772 ; sel = cbnew 10 ""
774 ; memused = 0
775 ; gen = 0
776 ; throttle = None
777 ; autoscroll = None
778 ; ghyll = noghyll
779 ; help = makehelp ()
780 ; docinfo = []
781 ; texid = None
782 ; prevzoom = (1.0, 0)
783 ; progress = -1.0
784 ; uioh = nouioh
785 ; redisplay = true
786 ; mpos = (-1, -1)
787 ; keystate = KSnone
788 ; glinks = false
789 ; prevcolumns = None
790 ; winw = -1
791 ; winh = -1
792 ; reprf = noreprf
793 ; origin = ""
794 ; roam = noroam
795 ; bzoom = false
796 ; traw = Raw.create_static `float 8
797 ; vraw = Raw.create_static `float 8
801 let hscrollh () =
802 if (conf.scrollb land scrollbhv = 0)
803 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
804 then 0
805 else conf.scrollbw
808 let vscrollw () =
809 if (conf.scrollb land scrollbvv = 0)
810 then 0
811 else conf.scrollbw
814 let wadjsb w = w - vscrollw ();;
816 let setfontsize n =
817 fstate.fontsize <- n;
818 fstate.wwidth <- measurestr fstate.fontsize "w";
819 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
822 let vlog fmt =
823 if conf.verbose
824 then
825 Printf.kprintf prerr_endline fmt
826 else
827 Printf.kprintf ignore fmt
830 let launchpath () =
831 if emptystr conf.pathlauncher
832 then print_endline state.path
833 else (
834 let re = Str.regexp "%s" in
835 let command = Str.global_replace re state.path conf.pathlauncher in
836 try popen command []
837 with exn ->
838 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
839 flush stderr;
843 module Ne = struct
844 type 'a t = | Res of 'a | Exn of exn;;
846 let res f =
847 try Res (f ())
848 with exn -> Exn exn
851 let clo fd f =
852 try tempfailureretry Unix.close fd
853 with exn -> f (exntos exn)
856 let dup fd =
857 try Res (tempfailureretry Unix.dup fd)
858 with exn -> Exn exn
861 let dup2 fd1 fd2 =
862 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
863 with exn -> Exn exn
865 end;;
867 let redirectstderr () =
868 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
869 if conf.redirectstderr
870 then
871 match Ne.res Unix.pipe with
872 | Ne.Exn exn ->
873 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
875 | Ne.Res (r, w) ->
876 begin match Ne.dup Unix.stderr with
877 | Ne.Exn exn ->
878 dolog "failed to dup stderr: %s" (exntos exn);
879 Ne.clo r (clofail "pipe/r");
880 Ne.clo w (clofail "pipe/w");
882 | Ne.Res dupstderr ->
883 begin match Ne.dup2 w Unix.stderr with
884 | Ne.Exn exn ->
885 dolog "failed to dup2 to stderr: %s" (exntos exn);
886 Ne.clo dupstderr (clofail "stderr duplicate");
887 Ne.clo r (clofail "redir pipe/r");
888 Ne.clo w (clofail "redir pipe/w");
890 | Ne.Res () ->
891 state.stderr <- dupstderr;
892 state.errfd <- Some r;
893 end;
895 else (
896 state.newerrmsgs <- false;
897 begin match state.errfd with
898 | Some fd ->
899 begin match Ne.dup2 state.stderr Unix.stderr with
900 | Ne.Exn exn ->
901 dolog "failed to dup2 original stderr: %s" (exntos exn)
902 | Ne.Res () ->
903 Ne.clo fd (clofail "dup of stderr");
904 state.errfd <- None;
905 end;
906 | None -> ()
907 end;
908 prerr_string (Buffer.contents state.errmsgs);
909 flush stderr;
910 Buffer.clear state.errmsgs;
914 module G =
915 struct
916 let postRedisplay who =
917 if conf.verbose
918 then prerr_endline ("redisplay for " ^ who);
919 state.redisplay <- true;
921 end;;
923 let getopaque pageno =
924 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
925 with Not_found -> None
928 let putopaque pageno opaque =
929 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
932 let pagetranslatepoint l x y =
933 let dy = y - l.pagedispy in
934 let y = dy + l.pagey in
935 let dx = x - l.pagedispx in
936 let x = dx + l.pagex in
937 (x, y);
940 let onppundermouse g x y d =
941 let rec f = function
942 | l :: rest ->
943 begin match getopaque l.pageno with
944 | Some opaque ->
945 let x0 = l.pagedispx in
946 let x1 = x0 + l.pagevw in
947 let y0 = l.pagedispy in
948 let y1 = y0 + l.pagevh in
949 if y >= y0 && y <= y1 && x >= x0 && x <= x1
950 then
951 let px, py = pagetranslatepoint l x y in
952 match g opaque l px py with
953 | Some res -> res
954 | None -> f rest
955 else f rest
956 | _ ->
957 f rest
959 | [] -> d
961 f state.layout
964 let getunder x y =
965 let g opaque l px py =
966 if state.bzoom
967 then (
968 match rectofblock opaque px py with
969 | Some a ->
970 let rect = (a.(0),a.(2),a.(1),a.(2),a.(1),a.(3),a.(0),a.(3)) in
971 state.rects <- [l.pageno, l.pageno mod 3, rect];
972 G.postRedisplay "getunder";
973 | None -> ()
975 match whatsunder opaque px py with
976 | Unone -> None
977 | under -> Some under
979 onppundermouse g x y Unone
982 let unproject x y =
983 let g opaque l x y =
984 match unproject opaque x y with
985 | Some (x, y) -> Some (Some (l.pageno, x, y))
986 | None -> None
988 onppundermouse g x y None;
991 let showtext c s =
992 state.text <- Printf.sprintf "%c%s" c s;
993 G.postRedisplay "showtext";
996 let pipesel opaque cmd =
997 if hassel opaque
998 then
999 match Ne.res Unix.pipe with
1000 | Ne.Exn exn ->
1001 showtext '!'
1002 (Printf.sprintf "pipesel can not create pipe: %s" (exntos exn));
1003 | Ne.Res (r, w) ->
1004 let doclose what fd =
1005 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
1007 let popened =
1008 try popen cmd [r, 0; w, -1]; true
1009 with exn ->
1010 dolog "can not execute %S: %s" cmd (exntos exn);
1011 false
1013 if popened
1014 then (
1015 copysel w opaque;
1016 G.postRedisplay "pipesel";
1018 else doclose "pipesel pipe/w" w;
1019 doclose "pipesel pipe/r" r;
1022 let paxunder x y =
1023 let g opaque l px py =
1024 if markunder opaque px py conf.paxmark
1025 then (
1026 Some (fun () ->
1027 match getopaque l.pageno with
1028 | None -> ()
1029 | Some opaque -> pipesel opaque conf.paxcmd
1032 else None
1034 G.postRedisplay "paxunder";
1035 if conf.paxmark = Mark_page
1036 then
1037 List.iter (fun l ->
1038 match getopaque l.pageno with
1039 | None -> ()
1040 | Some opaque -> clearmark opaque) state.layout;
1041 state.roam <-
1042 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
1045 let selstring s =
1046 match Ne.res Unix.pipe with
1047 | Ne.Exn exn ->
1048 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
1049 | Ne.Res (r, w) ->
1050 let clo cap fd =
1051 Ne.clo fd (fun msg ->
1052 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
1055 let popened =
1056 try popen conf.selcmd [r, 0; w, -1]; true
1057 with exn ->
1058 showtext '!'
1059 (Printf.sprintf "failed to execute %s: %s"
1060 conf.selcmd (exntos exn));
1061 false
1063 if popened
1064 then (
1066 let l = String.length s in
1067 let n = tempfailureretry (Unix.write w s 0) l in
1068 if n != l
1069 then
1070 showtext '!'
1071 (Printf.sprintf
1072 "failed to write %d characters to sel pipe, wrote %d"
1075 with exn ->
1076 showtext '!'
1077 (Printf.sprintf "failed to write to sel pipe: %s"
1078 (exntos exn)
1081 else dolog "%s" s;
1082 clo "selstring pipe/r" r;
1083 clo "selstring pipe/w" w;
1086 let undertext = function
1087 | Unone -> "none"
1088 | Ulinkuri s -> s
1089 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
1090 | Utext s -> "font: " ^ s
1091 | Uunexpected s -> "unexpected: " ^ s
1092 | Ulaunch s -> "launch: " ^ s
1093 | Unamed s -> "named: " ^ s
1094 | Uremote (filename, pageno) ->
1095 Printf.sprintf "%s: page %d" filename (pageno+1)
1096 | Uremotedest (filename, destname) ->
1097 Printf.sprintf "%s: destination %S" filename destname
1100 let updateunder x y =
1101 match getunder x y with
1102 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
1103 | Ulinkuri uri ->
1104 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1105 Wsi.setcursor Wsi.CURSOR_INFO
1106 | Ulinkgoto (pageno, _) ->
1107 if conf.underinfo
1108 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
1109 Wsi.setcursor Wsi.CURSOR_INFO
1110 | Utext s ->
1111 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1112 Wsi.setcursor Wsi.CURSOR_TEXT
1113 | Uunexpected s ->
1114 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
1115 Wsi.setcursor Wsi.CURSOR_INHERIT
1116 | Ulaunch s ->
1117 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
1118 Wsi.setcursor Wsi.CURSOR_INHERIT
1119 | Unamed s ->
1120 if conf.underinfo then showtext 'n' ("amed: " ^ s);
1121 Wsi.setcursor Wsi.CURSOR_INHERIT
1122 | Uremote (filename, pageno) ->
1123 if conf.underinfo then showtext 'r'
1124 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
1125 Wsi.setcursor Wsi.CURSOR_INFO
1126 | Uremotedest (filename, destname) ->
1127 if conf.underinfo then showtext 'r'
1128 (Printf.sprintf "emote destination: %s (%S)" filename destname);
1129 Wsi.setcursor Wsi.CURSOR_INFO
1132 let showlinktype under =
1133 if conf.underinfo
1134 then
1135 match under with
1136 | Unone -> ()
1137 | under ->
1138 let s = undertext under in
1139 showtext ' ' s
1142 let addchar s c =
1143 let b = Buffer.create (String.length s + 1) in
1144 Buffer.add_string b s;
1145 Buffer.add_char b c;
1146 Buffer.contents b;
1149 module type TextEnumType =
1151 type t
1152 val name : string
1153 val names : string array
1154 end;;
1156 module TextEnumMake (Ten : TextEnumType) =
1157 struct
1158 let names = Ten.names;;
1159 let to_int (t : Ten.t) = Obj.magic t;;
1160 let to_string t = names.(to_int t);;
1161 let of_int n : Ten.t = Obj.magic n;;
1162 let of_string s =
1163 let rec find i =
1164 if i = Array.length names
1165 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
1166 else (
1167 if Ten.names.(i) = s
1168 then of_int i
1169 else find (i+1)
1171 in find 0;;
1172 end;;
1174 module CSTE = TextEnumMake (struct
1175 type t = colorspace;;
1176 let name = "colorspace";;
1177 let names = [|"rgb"; "bgr"; "gray"|];;
1178 end);;
1180 module MTE = TextEnumMake (struct
1181 type t = mark;;
1182 let name = "mark";;
1183 let names = [|"page"; "block"; "line"; "word"|];;
1184 end);;
1186 module FMTE = TextEnumMake (struct
1187 type t = fitmodel;;
1188 let name = "fitmodel";;
1189 let names = [|"width"; "proportional"; "page"|];;
1190 end);;
1192 let intentry_with_suffix text key =
1193 let c =
1194 if key >= 32 && key < 127
1195 then Char.chr key
1196 else '\000'
1198 match Char.lowercase c with
1199 | '0' .. '9' ->
1200 let text = addchar text c in
1201 TEcont text
1203 | 'k' | 'm' | 'g' ->
1204 let text = addchar text c in
1205 TEcont text
1207 | _ ->
1208 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1209 TEcont text
1212 let multicolumns_to_string (n, a, b) =
1213 if a = 0 && b = 0
1214 then Printf.sprintf "%d" n
1215 else Printf.sprintf "%d,%d,%d" n a b;
1218 let multicolumns_of_string s =
1220 (int_of_string s, 0, 0)
1221 with _ ->
1222 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1223 if a > 1 || b > 1
1224 then failwith "subtly broken"; (n, a, b)
1228 let readcmd fd =
1229 let s = "xxxx" in
1230 let n = tempfailureretry (Unix.read fd s 0) 4 in
1231 if n != 4 then error "incomplete read(len) = %d" n;
1232 let len = 0
1233 lor (Char.code s.[0] lsl 24)
1234 lor (Char.code s.[1] lsl 16)
1235 lor (Char.code s.[2] lsl 8)
1236 lor (Char.code s.[3] lsl 0)
1238 let s = String.create len in
1239 let n = tempfailureretry (Unix.read fd s 0) len in
1240 if n != len then error "incomplete read(data) %d vs %d" n len;
1244 let btod b = if b then 1 else 0;;
1246 let wcmd fmt =
1247 let b = Buffer.create 16 in
1248 Buffer.add_string b "llll";
1249 Printf.kbprintf
1250 (fun b ->
1251 let s = Buffer.contents b in
1252 let n = String.length s in
1253 let len = n - 4 in
1254 (* dolog "wcmd %S" (String.sub s 4 len); *)
1255 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1256 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1257 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1258 s.[3] <- Char.chr (len land 0xff);
1259 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1260 if n' != n then error "write failed %d vs %d" n' n;
1261 ) b fmt;
1264 let calcips h =
1265 let d = state.winh - h in
1266 max conf.interpagespace ((d + 1) / 2)
1269 let rowyh (c, coverA, coverB) b n =
1270 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1271 then
1272 let _, _, vy, (_, _, h, _) = b.(n) in
1273 (vy, h)
1274 else
1275 let n' = n - coverA in
1276 let d = n' mod c in
1277 let s = n - d in
1278 let e = min state.pagecount (s + c) in
1279 let rec find m miny maxh = if m = e then miny, maxh else
1280 let _, _, y, (_, _, h, _) = b.(m) in
1281 let miny = min miny y in
1282 let maxh = max maxh h in
1283 find (m+1) miny maxh
1284 in find s max_int 0
1287 let calcheight () =
1288 match conf.columns with
1289 | Cmulti ((_, _, _) as cl, b) ->
1290 if Array.length b > 0
1291 then
1292 let y, h = rowyh cl b (Array.length b - 1) in
1293 y + h + (if conf.presentation then calcips h else 0)
1294 else 0
1295 | Csingle b ->
1296 if Array.length b > 0
1297 then
1298 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1299 y + h + (if conf.presentation then calcips h else 0)
1300 else 0
1301 | Csplit (_, b) ->
1302 if Array.length b > 0
1303 then
1304 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1305 y + h
1306 else 0
1309 let getpageywh pageno =
1310 let pageno = bound pageno 0 (state.pagecount-1) in
1311 match conf.columns with
1312 | Csingle b ->
1313 if Array.length b = 0
1314 then 0, 0, 0
1315 else
1316 let (_, _, y, (_, w, h, _)) = b.(pageno) in
1317 let y =
1318 if conf.presentation
1319 then y - calcips h
1320 else y
1322 y, w, h
1323 | Cmulti (cl, b) ->
1324 if Array.length b = 0
1325 then 0, 0, 0
1326 else
1327 let y, h = rowyh cl b pageno in
1328 let (_, _, _, (_, w, _, _)) = b.(pageno) in
1329 let y =
1330 if conf.presentation
1331 then y - calcips h
1332 else y
1334 y, w, h
1335 | Csplit (c, b) ->
1336 if Array.length b = 0
1337 then 0, 0, 0
1338 else
1339 let n = pageno*c in
1340 let (_, _, y, (_, w, h, _)) = b.(n) in
1341 y, w / c, h
1344 let getpageyh pageno =
1345 let y,_,h = getpageywh pageno in
1346 y, h;
1349 let getpagedim pageno =
1350 let rec f ppdim l =
1351 match l with
1352 | (n, _, _, _) as pdim :: rest ->
1353 if n >= pageno
1354 then (if n = pageno then pdim else ppdim)
1355 else f pdim rest
1357 | [] -> ppdim
1359 f (-1, -1, -1, -1) state.pdims
1362 let getpagey pageno = fst (getpageyh pageno);;
1364 let nogeomcmds cmds =
1365 match cmds with
1366 | s, [] -> emptystr s
1367 | _ -> false
1370 let page_of_y y =
1371 let ((c, coverA, coverB) as cl), b =
1372 match conf.columns with
1373 | Csingle b -> (1, 0, 0), b
1374 | Cmulti (c, b) -> c, b
1375 | Csplit (_, b) -> (1, 0, 0), b
1377 if Array.length b = 0
1378 then -1
1379 else
1380 let rec bsearch nmin nmax =
1381 if nmin > nmax
1382 then bound nmin 0 (state.pagecount-1)
1383 else
1384 let n = (nmax + nmin) / 2 in
1385 let vy, h = rowyh cl b n in
1386 let y0, y1 =
1387 if conf.presentation
1388 then
1389 let ips = calcips h in
1390 let y0 = vy - ips in
1391 let y1 = vy + h + ips in
1392 y0, y1
1393 else (
1394 if n = 0
1395 then 0, vy + h + conf.interpagespace
1396 else
1397 let y0 = vy - conf.interpagespace in
1398 y0, y0 + h + conf.interpagespace
1401 if y >= y0 && y < y1
1402 then (
1403 if c = 1
1404 then n
1405 else (
1406 if n > coverA
1407 then
1408 if n < state.pagecount - coverB
1409 then ((n-coverA)/c)*c + coverA
1410 else n
1411 else n
1414 else (
1415 if y > y0
1416 then bsearch (n+1) nmax
1417 else bsearch nmin (n-1)
1420 bsearch 0 (state.pagecount-1);
1423 let layoutN ((columns, coverA, coverB), b) y sh =
1424 let sh = sh - (hscrollh ()) in
1425 let rec fold accu n =
1426 if n = Array.length b
1427 then accu
1428 else
1429 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1430 if (vy - y) > sh &&
1431 (n = coverA - 1
1432 || n = state.pagecount - coverB
1433 || (n - coverA) mod columns = columns - 1)
1434 then accu
1435 else
1436 let accu =
1437 if vy + h > y
1438 then
1439 let pagey = max 0 (y - vy) in
1440 let pagedispy = if pagey > 0 then 0 else vy - y in
1441 let pagedispx, pagex =
1442 let pdx =
1443 if n = coverA - 1 || n = state.pagecount - coverB
1444 then state.x + (wadjsb state.winw - w) / 2
1445 else dx + xoff + state.x
1447 if pdx < 0
1448 then 0, -pdx
1449 else pdx, 0
1451 let pagevw =
1452 let vw = wadjsb state.winw - pagedispx in
1453 let pw = w - pagex in
1454 min vw pw
1456 let pagevh = min (h - pagey) (sh - pagedispy) in
1457 if pagevw > 0 && pagevh > 0
1458 then
1459 let e =
1460 { pageno = n
1461 ; pagedimno = pdimno
1462 ; pagew = w
1463 ; pageh = h
1464 ; pagex = pagex
1465 ; pagey = pagey
1466 ; pagevw = pagevw
1467 ; pagevh = pagevh
1468 ; pagedispx = pagedispx
1469 ; pagedispy = pagedispy
1470 ; pagecol = 0
1473 e :: accu
1474 else
1475 accu
1476 else
1477 accu
1479 fold accu (n+1)
1481 if Array.length b = 0
1482 then []
1483 else List.rev (fold [] (page_of_y y))
1486 let layoutS (columns, b) y sh =
1487 let sh = sh - hscrollh () in
1488 let rec fold accu n =
1489 if n = Array.length b
1490 then accu
1491 else
1492 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1493 if (vy - y) > sh
1494 then accu
1495 else
1496 let accu =
1497 if vy + pageh > y
1498 then
1499 let x = xoff + state.x in
1500 let pagey = max 0 (y - vy) in
1501 let pagedispy = if pagey > 0 then 0 else vy - y in
1502 let pagedispx, pagex =
1503 if px = 0
1504 then (
1505 if x < 0
1506 then 0, -x
1507 else x, 0
1509 else (
1510 let px = px - x in
1511 if px < 0
1512 then -px, 0
1513 else 0, px
1516 let pagecolw = pagew/columns in
1517 let pagedispx =
1518 if pagecolw < state.winw
1519 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
1520 else pagedispx
1522 let pagevw =
1523 let vw = wadjsb state.winw - pagedispx in
1524 let pw = pagew - pagex in
1525 min vw pw
1527 let pagevw = min pagevw pagecolw in
1528 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1529 if pagevw > 0 && pagevh > 0
1530 then
1531 let e =
1532 { pageno = n/columns
1533 ; pagedimno = pdimno
1534 ; pagew = pagew
1535 ; pageh = pageh
1536 ; pagex = pagex
1537 ; pagey = pagey
1538 ; pagevw = pagevw
1539 ; pagevh = pagevh
1540 ; pagedispx = pagedispx
1541 ; pagedispy = pagedispy
1542 ; pagecol = n mod columns
1545 e :: accu
1546 else
1547 accu
1548 else
1549 accu
1551 fold accu (n+1)
1553 List.rev (fold [] 0)
1556 let layout y sh =
1557 if nogeomcmds state.geomcmds
1558 then
1559 match conf.columns with
1560 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1561 | Cmulti c -> layoutN c y sh
1562 | Csplit s -> layoutS s y sh
1563 else []
1566 let clamp incr =
1567 let y = state.y + incr in
1568 let y = max 0 y in
1569 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1573 let itertiles l f =
1574 let tilex = l.pagex mod conf.tilew in
1575 let tiley = l.pagey mod conf.tileh in
1577 let col = l.pagex / conf.tilew in
1578 let row = l.pagey / conf.tileh in
1580 let rec rowloop row y0 dispy h =
1581 if h = 0
1582 then ()
1583 else (
1584 let dh = conf.tileh - y0 in
1585 let dh = min h dh in
1586 let rec colloop col x0 dispx w =
1587 if w = 0
1588 then ()
1589 else (
1590 let dw = conf.tilew - x0 in
1591 let dw = min w dw in
1593 f col row dispx dispy x0 y0 dw dh;
1594 colloop (col+1) 0 (dispx+dw) (w-dw)
1597 colloop col tilex l.pagedispx l.pagevw;
1598 rowloop (row+1) 0 (dispy+dh) (h-dh)
1601 if l.pagevw > 0 && l.pagevh > 0
1602 then rowloop row tiley l.pagedispy l.pagevh;
1605 let gettileopaque l col row =
1606 let key =
1607 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1609 try Some (Hashtbl.find state.tilemap key)
1610 with Not_found -> None
1613 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1614 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1615 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1618 let filledrect x0 y0 x1 y1 =
1619 GlArray.disable `texture_coord;
1620 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
1621 GlArray.vertex `two state.vraw;
1622 GlArray.draw_arrays `triangle_strip 0 4;
1623 GlArray.enable `texture_coord;
1626 let linerect x0 y0 x1 y1 =
1627 GlArray.disable `texture_coord;
1628 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
1629 GlArray.vertex `two state.vraw;
1630 GlArray.draw_arrays `line_loop 0 4;
1631 GlArray.enable `texture_coord;
1634 let drawtiles l color =
1635 GlDraw.color color;
1636 begintiles ();
1637 let f col row x y tilex tiley w h =
1638 match gettileopaque l col row with
1639 | Some (opaque, _, t) ->
1640 let params = x, y, w, h, tilex, tiley in
1641 if conf.invert
1642 then GlTex.env (`mode `blend);
1643 drawtile params opaque;
1644 if conf.invert
1645 then GlTex.env (`mode `modulate);
1646 if conf.debug
1647 then (
1648 endtiles ();
1649 let s = Printf.sprintf
1650 "%d[%d,%d] %f sec"
1651 l.pageno col row t
1653 let w = measurestr fstate.fontsize s in
1654 GlDraw.color (0.0, 0.0, 0.0);
1655 filledrect (float (x-2))
1656 (float (y-2))
1657 (float (x+2) +. w)
1658 (float (y + fstate.fontsize + 2));
1659 GlDraw.color (1.0, 1.0, 1.0);
1660 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1661 begintiles ();
1664 | None ->
1665 endtiles ();
1666 let w =
1667 let lw = wadjsb state.winw - x in
1668 min lw w
1669 and h =
1670 let lh = state.winh - y in
1671 min lh h
1673 if conf.invert
1674 then GlTex.env (`mode `blend);
1675 begin match state.texid with
1676 | Some id ->
1677 Gl.enable `texture_2d;
1678 GlTex.bind_texture `texture_2d id;
1679 let x0 = float x
1680 and y0 = float y
1681 and x1 = float (x+w)
1682 and y1 = float (y+h) in
1684 let tw = float w /. 16.0
1685 and th = float h /. 16.0 in
1686 let tx0 = float tilex /. 16.0
1687 and ty0 = float tiley /. 16.0 in
1688 let tx1 = tx0 +. tw
1689 and ty1 = ty0 +. th in
1690 Raw.sets_float state.vraw ~pos:0
1691 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
1692 Raw.sets_float state.traw ~pos:0
1693 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
1694 GlArray.vertex `two state.vraw;
1695 GlArray.tex_coord `two state.traw;
1696 GlArray.draw_arrays `triangle_strip 0 4;
1697 Gl.disable `texture_2d;
1699 | None ->
1700 GlDraw.color (1.0, 1.0, 1.0);
1701 filledrect (float x) (float y) (float (x+w)) (float (y+h));
1702 end;
1703 if conf.invert
1704 then GlTex.env (`mode `modulate);
1705 if w > 128 && h > fstate.fontsize + 10
1706 then (
1707 let c = if conf.invert then 1.0 else 0.0 in
1708 GlDraw.color (c, c, c);
1709 let c, r =
1710 if conf.verbose
1711 then (col*conf.tilew, row*conf.tileh)
1712 else col, row
1714 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1716 GlDraw.color color;
1717 begintiles ();
1719 itertiles l f;
1720 endtiles ();
1723 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1725 let tilevisible1 l x y =
1726 let ax0 = l.pagex
1727 and ax1 = l.pagex + l.pagevw
1728 and ay0 = l.pagey
1729 and ay1 = l.pagey + l.pagevh in
1731 let bx0 = x
1732 and by0 = y in
1733 let bx1 = min (bx0 + conf.tilew) l.pagew
1734 and by1 = min (by0 + conf.tileh) l.pageh in
1736 let rx0 = max ax0 bx0
1737 and ry0 = max ay0 by0
1738 and rx1 = min ax1 bx1
1739 and ry1 = min ay1 by1 in
1741 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1742 nonemptyintersection
1745 let tilevisible layout n x y =
1746 let rec findpageinlayout m = function
1747 | l :: rest when l.pageno = n ->
1748 tilevisible1 l x y || (
1749 match conf.columns with
1750 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1751 | _ -> false
1753 | _ :: rest -> findpageinlayout 0 rest
1754 | [] -> false
1756 findpageinlayout 0 layout;
1759 let tileready l x y =
1760 tilevisible1 l x y &&
1761 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1764 let tilepage n p layout =
1765 let rec loop = function
1766 | l :: rest ->
1767 if l.pageno = n
1768 then
1769 let f col row _ _ _ _ _ _ =
1770 if state.currently = Idle
1771 then
1772 match gettileopaque l col row with
1773 | Some _ -> ()
1774 | None ->
1775 let x = col*conf.tilew
1776 and y = row*conf.tileh in
1777 let w =
1778 let w = l.pagew - x in
1779 min w conf.tilew
1781 let h =
1782 let h = l.pageh - y in
1783 min h conf.tileh
1785 let pbo =
1786 if conf.usepbo
1787 then getpbo w h conf.colorspace
1788 else ~< "0"
1790 wcmd "tile %s %d %d %d %d %s"
1791 (~> p) x y w h (~> pbo);
1792 state.currently <-
1793 Tiling (
1794 l, p, conf.colorspace, conf.angle,
1795 state.gen, col, row, conf.tilew, conf.tileh
1798 itertiles l f;
1799 else
1800 loop rest
1802 | [] -> ()
1804 if nogeomcmds state.geomcmds
1805 then loop layout;
1808 let preloadlayout y =
1809 let y = if y < state.winh then 0 else y - state.winh in
1810 let h = state.winh*3 in
1811 layout y h;
1814 let load pages =
1815 let rec loop pages =
1816 if state.currently != Idle
1817 then ()
1818 else
1819 match pages with
1820 | l :: rest ->
1821 begin match getopaque l.pageno with
1822 | None ->
1823 wcmd "page %d %d" l.pageno l.pagedimno;
1824 state.currently <- Loading (l, state.gen);
1825 | Some opaque ->
1826 tilepage l.pageno opaque pages;
1827 loop rest
1828 end;
1829 | _ -> ()
1831 if nogeomcmds state.geomcmds
1832 then loop pages
1835 let preload pages =
1836 load pages;
1837 if conf.preload && state.currently = Idle
1838 then load (preloadlayout state.y);
1841 let layoutready layout =
1842 let rec fold all ls =
1843 all && match ls with
1844 | l :: rest ->
1845 let seen = ref false in
1846 let allvisible = ref true in
1847 let foo col row _ _ _ _ _ _ =
1848 seen := true;
1849 allvisible := !allvisible &&
1850 begin match gettileopaque l col row with
1851 | Some _ -> true
1852 | None -> false
1855 itertiles l foo;
1856 fold (!seen && !allvisible) rest
1857 | [] -> true
1859 let alltilesvisible = fold true layout in
1860 alltilesvisible;
1863 let gotoy y =
1864 let y = bound y 0 state.maxy in
1865 let y, layout, proceed =
1866 match conf.maxwait with
1867 | Some time when state.ghyll == noghyll ->
1868 begin match state.throttle with
1869 | None ->
1870 let layout = layout y state.winh in
1871 let ready = layoutready layout in
1872 if not ready
1873 then (
1874 load layout;
1875 state.throttle <- Some (layout, y, now ());
1877 else G.postRedisplay "gotoy showall (None)";
1878 y, layout, ready
1879 | Some (_, _, started) ->
1880 let dt = now () -. started in
1881 if dt > time
1882 then (
1883 state.throttle <- None;
1884 let layout = layout y state.winh in
1885 load layout;
1886 G.postRedisplay "maxwait";
1887 y, layout, true
1889 else -1, [], false
1892 | _ ->
1893 let layout = layout y state.winh in
1894 if not !wtmode || layoutready layout
1895 then G.postRedisplay "gotoy ready";
1896 y, layout, true
1898 if proceed
1899 then (
1900 state.y <- y;
1901 state.layout <- layout;
1902 begin match state.mode with
1903 | LinkNav (Ltexact (pageno, linkno)) ->
1904 let rec loop = function
1905 | [] ->
1906 state.mode <- LinkNav (Ltgendir 0)
1907 | l :: _ when l.pageno = pageno ->
1908 begin match getopaque pageno with
1909 | None ->
1910 state.mode <- LinkNav (Ltgendir 0)
1911 | Some opaque ->
1912 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1913 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1914 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1915 then state.mode <- LinkNav (Ltgendir 0)
1917 | _ :: rest -> loop rest
1919 loop layout
1920 | _ -> ()
1921 end;
1922 begin match state.mode with
1923 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1924 if not (pagevisible layout pageno)
1925 then (
1926 match state.layout with
1927 | [] -> ()
1928 | l :: _ ->
1929 state.mode <- Birdseye (
1930 conf, leftx, l.pageno, hooverpageno, anchor
1933 | LinkNav (Ltgendir dir as lt) ->
1934 let linknav =
1935 let rec loop = function
1936 | [] -> lt
1937 | l :: rest ->
1938 match getopaque l.pageno with
1939 | None -> loop rest
1940 | Some opaque ->
1941 let link =
1942 let ld =
1943 if dir = 0
1944 then LDfirstvisible (l.pagex, l.pagey, dir)
1945 else (
1946 if dir > 0 then LDfirst else LDlast
1949 findlink opaque ld
1951 match link with
1952 | Lnotfound -> loop rest
1953 | Lfound n ->
1954 showlinktype (getlink opaque n);
1955 Ltexact (l.pageno, n)
1957 loop state.layout
1959 state.mode <- LinkNav linknav
1960 | _ -> ()
1961 end;
1962 preload layout;
1964 state.ghyll <- noghyll;
1965 if conf.updatecurs
1966 then (
1967 let mx, my = state.mpos in
1968 updateunder mx my;
1972 let conttiling pageno opaque =
1973 tilepage pageno opaque
1974 (if conf.preload then preloadlayout state.y else state.layout)
1977 let gotoy_and_clear_text y =
1978 if not conf.verbose then state.text <- "";
1979 gotoy y;
1982 let getanchor1 l =
1983 let top =
1984 let coloff = l.pagecol * l.pageh in
1985 float (l.pagey + coloff) /. float l.pageh
1987 let dtop =
1988 if l.pagedispy = 0
1989 then
1991 else (
1992 if conf.presentation
1993 then float l.pagedispy /. float (calcips l.pageh)
1994 else float l.pagedispy /. float conf.interpagespace
1997 (l.pageno, top, dtop)
2000 let getanchor () =
2001 match state.layout with
2002 | l :: _ -> getanchor1 l
2003 | [] ->
2004 let n = page_of_y state.y in
2005 if n = -1
2006 then state.anchor
2007 else
2008 let y, h = getpageyh n in
2009 let dy = y - state.y in
2010 let dtop =
2011 if conf.presentation
2012 then
2013 let ips = calcips h in
2014 float (dy + ips) /. float ips
2015 else
2016 float dy /. float conf.interpagespace
2018 (n, 0.0, dtop)
2021 let getanchory (n, top, dtop) =
2022 let y, h = getpageyh n in
2023 if conf.presentation
2024 then
2025 let ips = calcips h in
2026 y + truncate (top*.float h -. dtop*.float ips) + ips;
2027 else
2028 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
2031 let gotoanchor anchor =
2032 gotoy (getanchory anchor);
2035 let addnav () =
2036 cbput state.hists.nav (getanchor ());
2039 let getnav dir =
2040 let anchor = cbgetc state.hists.nav dir in
2041 getanchory anchor;
2044 let gotoghyll1 single y =
2045 let scroll f n a b =
2046 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
2047 let snake f a b =
2048 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
2049 if f < a
2050 then s (float f /. float a)
2051 else (
2052 if f > b
2053 then 1.0 -. s ((float (f-b) /. float (n-b)))
2054 else 1.0
2057 snake f a b
2058 and summa n a b =
2059 let ins = float a *. 0.5
2060 and outs = float (n-b) *. 0.5 in
2061 let ones = b - a in
2062 ins +. outs +. float ones
2064 let rec set nab y sy =
2065 let (_N, _A, _B), y =
2066 if single
2067 then
2068 let scl = if y > sy then 3 else -3 in
2069 (5,1,4), y+conf.scrollstep*scl
2070 else nab,y in
2071 let sum = summa _N _A _B in
2072 let dy = float (y - sy) in
2073 state.ghyll <- (
2074 let rec gf n y1 o =
2075 if n >= _N
2076 then state.ghyll <- noghyll
2077 else
2078 let go n =
2079 let s = scroll n _N _A _B in
2080 let y1 = y1 +. ((s *. dy) /. sum) in
2081 gotoy_and_clear_text (truncate y1);
2082 state.ghyll <- gf (n+1) y1;
2084 match o with
2085 | None -> go n
2086 | Some y' when single -> set nab y' state.y
2087 | Some y' -> set (_N/2, 1, 1) y' state.y
2089 gf 0 (float state.y)
2092 match conf.ghyllscroll with
2093 | Some nab when not conf.presentation ->
2094 if state.ghyll == noghyll
2095 then set nab y state.y
2096 else state.ghyll (Some y)
2097 | _ ->
2098 gotoy_and_clear_text y
2101 let gotoghyll = gotoghyll1 false;;
2103 let gotopage n top =
2104 let y, h = getpageyh n in
2105 let y = y + (truncate (top *. float h)) in
2106 gotoghyll y
2109 let gotopage1 n top =
2110 let y = getpagey n in
2111 let y = y + top in
2112 gotoghyll y
2115 let invalidate s f =
2116 state.layout <- [];
2117 state.pdims <- [];
2118 state.rects <- [];
2119 state.rects1 <- [];
2120 match state.geomcmds with
2121 | ps, [] when emptystr ps ->
2122 f ();
2123 state.geomcmds <- s, [];
2125 | ps, [] ->
2126 state.geomcmds <- ps, [s, f];
2128 | ps, (s', _) :: rest when s' = s ->
2129 state.geomcmds <- ps, ((s, f) :: rest);
2131 | ps, cmds ->
2132 state.geomcmds <- ps, ((s, f) :: cmds);
2135 let flushpages () =
2136 Hashtbl.iter (fun _ opaque ->
2137 wcmd "freepage %s" (~> opaque);
2138 ) state.pagemap;
2139 Hashtbl.clear state.pagemap;
2142 let flushtiles () =
2143 if not (Queue.is_empty state.tilelru)
2144 then (
2145 Queue.iter (fun (k, p, s) ->
2146 wcmd "freetile %s" (~> p);
2147 state.memused <- state.memused - s;
2148 Hashtbl.remove state.tilemap k;
2149 ) state.tilelru;
2150 state.uioh#infochanged Memused;
2151 Queue.clear state.tilelru;
2153 load state.layout;
2156 let stateh h =
2157 let h = truncate (float h*.conf.zoom) in
2158 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2159 h - d
2162 let opendoc path password =
2163 state.path <- path;
2164 state.password <- password;
2165 state.gen <- state.gen + 1;
2166 state.docinfo <- [];
2168 flushpages ();
2169 setaalevel conf.aalevel;
2170 let titlepath =
2171 if emptystr state.origin
2172 then path
2173 else state.origin
2175 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2176 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
2177 invalidate "reqlayout"
2178 (fun () ->
2179 wcmd "reqlayout %d %d %d %s\000"
2180 conf.angle (FMTE.to_int conf.fitmodel)
2181 (stateh state.winh) state.nameddest
2185 let reload () =
2186 state.anchor <- getanchor ();
2187 opendoc state.path state.password;
2190 let scalecolor c =
2191 let c = c *. conf.colorscale in
2192 (c, c, c);
2195 let scalecolor2 (r, g, b) =
2196 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2199 let docolumns = function
2200 | Csingle _ ->
2201 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2202 let rec loop pageno pdimno pdim y ph pdims =
2203 if pageno = state.pagecount
2204 then ()
2205 else
2206 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2207 match pdims with
2208 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2209 pdimno+1, pdim, rest
2210 | _ ->
2211 pdimno, pdim, pdims
2213 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2214 let y = y +
2215 (if conf.presentation
2216 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2217 else (if pageno = 0 then 0 else conf.interpagespace)
2220 a.(pageno) <- (pdimno, x, y, pdim);
2221 loop (pageno+1) pdimno pdim (y + h) h pdims
2223 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2224 conf.columns <- Csingle a;
2226 | Cmulti ((columns, coverA, coverB), _) ->
2227 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2228 let rec loop pageno pdimno pdim x y rowh pdims =
2229 let rec fixrow m = if m = pageno then () else
2230 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2231 if h < rowh
2232 then (
2233 let y = y + (rowh - h) / 2 in
2234 a.(m) <- (pdimno, x, y, pdim);
2236 fixrow (m+1)
2238 if pageno = state.pagecount
2239 then fixrow (((pageno - 1) / columns) * columns)
2240 else
2241 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2242 match pdims with
2243 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2244 pdimno+1, pdim, rest
2245 | _ ->
2246 pdimno, pdim, pdims
2248 let x, y, rowh' =
2249 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2250 then (
2251 let x = (wadjsb state.winw - w) / 2 in
2252 let ips =
2253 if conf.presentation then calcips h else conf.interpagespace in
2254 x, y + ips + rowh, h
2256 else (
2257 if (pageno - coverA) mod columns = 0
2258 then (
2259 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2260 let y =
2261 if conf.presentation
2262 then
2263 let ips = calcips h in
2264 y + (if pageno = 0 then 0 else calcips rowh + ips)
2265 else
2266 y + (if pageno = 0 then 0 else conf.interpagespace)
2268 x, y + rowh, h
2270 else x, y, max rowh h
2273 let y =
2274 if pageno > 1 && (pageno - coverA) mod columns = 0
2275 then (
2276 let y =
2277 if pageno = columns && conf.presentation
2278 then (
2279 let ips = calcips rowh in
2280 for i = 0 to pred columns
2282 let (pdimno, x, y, pdim) = a.(i) in
2283 a.(i) <- (pdimno, x, y+ips, pdim)
2284 done;
2285 y+ips;
2287 else y
2289 fixrow (pageno - columns);
2292 else y
2294 a.(pageno) <- (pdimno, x, y, pdim);
2295 let x = x + w + xoff*2 + conf.interpagespace in
2296 loop (pageno+1) pdimno pdim x y rowh' pdims
2298 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2299 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2301 | Csplit (c, _) ->
2302 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2303 let rec loop pageno pdimno pdim y pdims =
2304 if pageno = state.pagecount
2305 then ()
2306 else
2307 let pdimno, ((_, w, h, _) as pdim), pdims =
2308 match pdims with
2309 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2310 pdimno+1, pdim, rest
2311 | _ ->
2312 pdimno, pdim, pdims
2314 let cw = w / c in
2315 let rec loop1 n x y =
2316 if n = c then y else (
2317 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2318 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2321 let y = loop1 0 0 y in
2322 loop (pageno+1) pdimno pdim y pdims
2324 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2325 conf.columns <- Csplit (c, a);
2328 let represent () =
2329 docolumns conf.columns;
2330 state.maxy <- calcheight ();
2331 if state.reprf == noreprf
2332 then (
2333 match state.mode with
2334 | Birdseye (_, _, pageno, _, _) ->
2335 let y, h = getpageyh pageno in
2336 let top = (state.winh - h) / 2 in
2337 gotoy (max 0 (y - top))
2338 | _ -> gotoanchor state.anchor
2340 else (
2341 state.reprf ();
2342 state.reprf <- noreprf;
2346 let reshape w h =
2347 GlDraw.viewport 0 0 w h;
2348 let firsttime = state.geomcmds == firstgeomcmds in
2349 if not firsttime && nogeomcmds state.geomcmds
2350 then state.anchor <- getanchor ();
2352 state.winw <- w;
2353 let w = wadjsb (truncate (float w *. conf.zoom)) in
2354 let w = max w 2 in
2355 state.winh <- h;
2356 setfontsize fstate.fontsize;
2357 GlMat.mode `modelview;
2358 GlMat.load_identity ();
2360 GlMat.mode `projection;
2361 GlMat.load_identity ();
2362 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2363 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2364 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2366 let relx =
2367 if conf.zoom <= 1.0
2368 then 0.0
2369 else float state.x /. float state.w
2371 invalidate "geometry"
2372 (fun () ->
2373 state.w <- w;
2374 if not firsttime
2375 then state.x <- truncate (relx *. float w);
2376 let w =
2377 match conf.columns with
2378 | Csingle _ -> w
2379 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2380 | Csplit (c, _) -> w * c
2382 wcmd "geometry %d %d %d"
2383 w (stateh h) (FMTE.to_int conf.fitmodel)
2387 let enttext () =
2388 let len = String.length state.text in
2389 let drawstring s =
2390 let hscrollh =
2391 match state.mode with
2392 | Textentry _ | View | LinkNav _ ->
2393 let h, _, _ = state.uioh#scrollpw in
2395 | _ -> 0
2397 let rect x w =
2398 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
2399 (x+.w) (float (state.winh - hscrollh))
2402 let w = float (wadjsb state.winw - 1) in
2403 if state.progress >= 0.0 && state.progress < 1.0
2404 then (
2405 GlDraw.color (0.3, 0.3, 0.3);
2406 let w1 = w *. state.progress in
2407 rect 0.0 w1;
2408 GlDraw.color (0.0, 0.0, 0.0);
2409 rect w1 (w-.w1)
2411 else (
2412 GlDraw.color (0.0, 0.0, 0.0);
2413 rect 0.0 w;
2416 GlDraw.color (1.0, 1.0, 1.0);
2417 drawstring fstate.fontsize
2418 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2420 let s =
2421 match state.mode with
2422 | Textentry ((prefix, text, _, _, _, _), _) ->
2423 let s =
2424 if len > 0
2425 then
2426 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2427 else
2428 Printf.sprintf "%s%s_" prefix text
2432 | _ -> state.text
2434 let s =
2435 if state.newerrmsgs
2436 then (
2437 if not (istextentry state.mode) && state.uioh#eformsgs
2438 then
2439 let s1 = "(press 'e' to review error messasges)" in
2440 if nonemptystr s then s ^ " " ^ s1 else s1
2441 else s
2443 else s
2445 if nonemptystr s
2446 then drawstring s
2449 let gctiles () =
2450 let len = Queue.length state.tilelru in
2451 let layout = lazy (
2452 match state.throttle with
2453 | None ->
2454 if conf.preload
2455 then preloadlayout state.y
2456 else state.layout
2457 | Some (layout, _, _) ->
2458 layout
2459 ) in
2460 let rec loop qpos =
2461 if state.memused <= conf.memlimit
2462 then ()
2463 else (
2464 if qpos < len
2465 then
2466 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2467 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2468 let (_, pw, ph, _) = getpagedim n in
2470 gen = state.gen
2471 && colorspace = conf.colorspace
2472 && angle = conf.angle
2473 && pagew = pw
2474 && pageh = ph
2475 && (
2476 let x = col*conf.tilew
2477 and y = row*conf.tileh in
2478 tilevisible (Lazy.force_val layout) n x y
2480 then Queue.push lruitem state.tilelru
2481 else (
2482 freepbo p;
2483 wcmd "freetile %s" (~> p);
2484 state.memused <- state.memused - s;
2485 state.uioh#infochanged Memused;
2486 Hashtbl.remove state.tilemap k;
2488 loop (qpos+1)
2491 loop 0
2494 let logcurrently = function
2495 | Idle -> dolog "Idle"
2496 | Loading (l, gen) ->
2497 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2498 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2499 dolog
2500 "Tiling %d[%d,%d] page=%s cs=%s angle"
2501 l.pageno col row (~> pageopaque)
2502 (CSTE.to_string colorspace)
2504 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2505 angle gen conf.angle state.gen
2506 tilew tileh
2507 conf.tilew conf.tileh
2509 | Outlining _ ->
2510 dolog "outlining"
2513 let splitatspace =
2514 let r = Str.regexp " " in
2515 fun s -> Str.bounded_split r s 2;
2518 let onpagerect pageno f =
2519 let b =
2520 match conf.columns with
2521 | Cmulti (_, b) -> b
2522 | Csingle b -> b
2523 | Csplit (_, b) -> b
2525 if pageno >= 0 && pageno < Array.length b
2526 then
2527 let (_, _, _, (w, h, _, _)) = b.(pageno) in
2528 f w h
2531 let gotopagexy1 pageno x y =
2532 let _,w1,h1,leftx = getpagedim pageno in
2533 let top = y /. (float h1) in
2534 let left = x /. (float w1) in
2535 let py, w, h = getpageywh pageno in
2536 let wh = state.winh - hscrollh () in
2537 let x = left *. (float w) in
2538 let x = leftx + state.x + truncate x in
2539 let sx =
2540 if x < 0 || x >= wadjsb state.winw
2541 then state.x - x
2542 else state.x
2544 let pdy = truncate (top *. float h) in
2545 let y' = py + pdy in
2546 let dy = y' - state.y in
2547 let sy =
2548 if x != state.x || not (dy > 0 && dy < wh)
2549 then (
2550 if conf.presentation
2551 then
2552 if abs (py - y') > wh
2553 then y'
2554 else py
2555 else y';
2557 else state.y
2559 if state.x != sx || state.y != sy
2560 then (
2561 let x, y =
2562 if !wtmode
2563 then (
2564 let ww = wadjsb state.winw in
2565 let qx = sx / ww
2566 and qy = pdy / wh in
2567 let x = qx * ww
2568 and y = py + qy * wh in
2569 let x = if -x + ww > w1 then -(w1-ww) else x
2570 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2571 let y =
2572 if conf.presentation
2573 then
2574 if abs (py - y') > wh
2575 then y'
2576 else py
2577 else y';
2579 (x, y)
2581 else (sx, sy)
2583 state.x <- x;
2584 gotoy_and_clear_text y;
2586 else gotoy_and_clear_text state.y;
2589 let gotopagexy pageno x y =
2590 match state.mode with
2591 | Birdseye _ -> gotopage pageno 0.0
2592 | _ -> gotopagexy1 pageno x y
2595 let act cmds =
2596 (* dolog "%S" cmds; *)
2597 let cl = splitatspace cmds in
2598 let scan s fmt f =
2599 try Scanf.sscanf s fmt f
2600 with exn ->
2601 dolog "error processing '%S': %s" cmds (exntos exn);
2602 exit 1
2604 let addoutline outline =
2605 match state.currently with
2606 | Outlining outlines ->
2607 state.currently <- Outlining (outline :: outlines)
2608 | Idle -> state.currently <- Outlining [outline]
2609 | currently ->
2610 dolog "invalid outlining state";
2611 logcurrently currently
2613 match cl with
2614 | "clear" :: [] ->
2615 state.uioh#infochanged Pdim;
2616 state.pdims <- [];
2618 | "clearrects" :: [] ->
2619 state.rects <- state.rects1;
2620 G.postRedisplay "clearrects";
2622 | "continue" :: args :: [] ->
2623 let n = scan args "%u" (fun n -> n) in
2624 state.pagecount <- n;
2625 begin match state.currently with
2626 | Outlining l ->
2627 state.currently <- Idle;
2628 state.outlines <- Array.of_list (List.rev l)
2629 | _ -> ()
2630 end;
2632 let cur, cmds = state.geomcmds in
2633 if emptystr cur
2634 then failwith "umpossible";
2636 begin match List.rev cmds with
2637 | [] ->
2638 state.geomcmds <- "", [];
2639 state.throttle <- None;
2640 represent ();
2641 | (s, f) :: rest ->
2642 f ();
2643 state.geomcmds <- s, List.rev rest;
2644 end;
2645 if conf.maxwait = None && not !wtmode
2646 then G.postRedisplay "continue";
2648 | "title" :: args :: [] ->
2649 Wsi.settitle args
2651 | "msg" :: args :: [] ->
2652 showtext ' ' args
2654 | "vmsg" :: args :: [] ->
2655 if conf.verbose
2656 then showtext ' ' args
2658 | "emsg" :: args :: [] ->
2659 Buffer.add_string state.errmsgs args;
2660 state.newerrmsgs <- true;
2661 G.postRedisplay "error message"
2663 | "progress" :: args :: [] ->
2664 let progress, text =
2665 scan args "%f %n"
2666 (fun f pos ->
2667 f, String.sub args pos (String.length args - pos))
2669 state.text <- text;
2670 state.progress <- progress;
2671 G.postRedisplay "progress"
2673 | "firstmatch" :: args :: [] ->
2674 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2675 scan args "%u %d %f %f %f %f %f %f %f %f"
2676 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2677 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2679 let y = (getpagey pageno) + truncate y0 in
2680 addnav ();
2681 gotoy y;
2682 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2684 | "match" :: args :: [] ->
2685 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2686 scan args "%u %d %f %f %f %f %f %f %f %f"
2687 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2688 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2690 state.rects1 <-
2691 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2693 | "page" :: args :: [] ->
2694 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
2695 let pageopaque = ~< pageopaques in
2696 begin match state.currently with
2697 | Loading (l, gen) ->
2698 vlog "page %d took %f sec" l.pageno t;
2699 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2700 begin match state.throttle with
2701 | None ->
2702 let preloadedpages =
2703 if conf.preload
2704 then preloadlayout state.y
2705 else state.layout
2707 let evict () =
2708 let set =
2709 List.fold_left (fun s l -> IntSet.add l.pageno s)
2710 IntSet.empty preloadedpages
2712 let evictedpages =
2713 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2714 if not (IntSet.mem pageno set)
2715 then (
2716 wcmd "freepage %s" (~> opaque);
2717 key :: accu
2719 else accu
2720 ) state.pagemap []
2722 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2724 evict ();
2725 state.currently <- Idle;
2726 if gen = state.gen
2727 then (
2728 tilepage l.pageno pageopaque state.layout;
2729 load state.layout;
2730 load preloadedpages;
2731 if pagevisible state.layout l.pageno
2732 && layoutready state.layout
2733 then G.postRedisplay "page";
2736 | Some (layout, _, _) ->
2737 state.currently <- Idle;
2738 tilepage l.pageno pageopaque layout;
2739 load state.layout
2740 end;
2742 | _ ->
2743 dolog "Inconsistent loading state";
2744 logcurrently state.currently;
2745 exit 1
2748 | "tile" :: args :: [] ->
2749 let (x, y, opaques, size, t) =
2750 scan args "%u %u %s %u %f"
2751 (fun x y p size t -> (x, y, p, size, t))
2753 let opaque = ~< opaques in
2754 begin match state.currently with
2755 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2756 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2758 unmappbo opaque;
2759 if tilew != conf.tilew || tileh != conf.tileh
2760 then (
2761 wcmd "freetile %s" (~> opaque);
2762 state.currently <- Idle;
2763 load state.layout;
2765 else (
2766 puttileopaque l col row gen cs angle opaque size t;
2767 state.memused <- state.memused + size;
2768 state.uioh#infochanged Memused;
2769 gctiles ();
2770 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2771 opaque, size) state.tilelru;
2773 let layout =
2774 match state.throttle with
2775 | None -> state.layout
2776 | Some (layout, _, _) -> layout
2779 state.currently <- Idle;
2780 if gen = state.gen
2781 && conf.colorspace = cs
2782 && conf.angle = angle
2783 && tilevisible layout l.pageno x y
2784 then conttiling l.pageno pageopaque;
2786 begin match state.throttle with
2787 | None ->
2788 preload state.layout;
2789 if gen = state.gen
2790 && conf.colorspace = cs
2791 && conf.angle = angle
2792 && tilevisible state.layout l.pageno x y
2793 && (not !wtmode || layoutready state.layout)
2794 then G.postRedisplay "tile nothrottle";
2796 | Some (layout, y, _) ->
2797 let ready = layoutready layout in
2798 if ready
2799 then (
2800 state.y <- y;
2801 state.layout <- layout;
2802 state.throttle <- None;
2803 G.postRedisplay "throttle";
2805 else load layout;
2806 end;
2809 | _ ->
2810 dolog "Inconsistent tiling state";
2811 logcurrently state.currently;
2812 exit 1
2815 | "pdim" :: args :: [] ->
2816 let (n, w, h, _) as pdim =
2817 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2819 let pdim =
2820 match conf.fitmodel, conf.columns with
2821 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2822 | _ -> pdim
2824 state.uioh#infochanged Pdim;
2825 state.pdims <- pdim :: state.pdims
2827 | "o" :: args :: [] ->
2828 let (l, n, t, h, pos) =
2829 scan args "%u %u %d %u %n"
2830 (fun l n t h pos -> l, n, t, h, pos)
2832 let s = String.sub args pos (String.length args - pos) in
2833 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
2835 | "ou" :: args :: [] ->
2836 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
2837 let s = String.sub args pos len in
2838 let pos2 = pos + len + 1 in
2839 let uri = String.sub args pos2 (String.length args - pos2) in
2840 addoutline (s, l, Ouri uri)
2842 | "on" :: args :: [] ->
2843 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
2844 let s = String.sub args pos (String.length args - pos) in
2845 addoutline (s, l, Onone)
2847 | "a" :: args :: [] ->
2848 let (n, l, t) =
2849 scan args "%u %d %d" (fun n l t -> n, l, t)
2851 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2853 | "info" :: args :: [] ->
2854 state.docinfo <- (1, args) :: state.docinfo
2856 | "infoend" :: [] ->
2857 state.uioh#infochanged Docinfo;
2858 state.docinfo <- List.rev state.docinfo
2860 | _ ->
2861 error "unknown cmd `%S'" cmds
2864 let onhist cb =
2865 let rc = cb.rc in
2866 let action = function
2867 | HCprev -> cbget cb ~-1
2868 | HCnext -> cbget cb 1
2869 | HCfirst -> cbget cb ~-(cb.rc)
2870 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2871 and cancel () = cb.rc <- rc
2872 in (action, cancel)
2875 let search pattern forward =
2876 match conf.columns with
2877 | Csplit _ ->
2878 showtext '!' "searching does not work properly in split columns mode"
2879 | _ ->
2880 if nonemptystr pattern
2881 then
2882 let pn, py =
2883 match state.layout with
2884 | [] -> 0, 0
2885 | l :: _ ->
2886 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2888 wcmd "search %d %d %d %d,%s\000"
2889 (btod conf.icase) pn py (btod forward) pattern;
2892 let intentry text key =
2893 let c =
2894 if key >= 32 && key < 127
2895 then Char.chr key
2896 else '\000'
2898 match c with
2899 | '0' .. '9' ->
2900 let text = addchar text c in
2901 TEcont text
2903 | _ ->
2904 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2905 TEcont text
2908 let linknentry text key =
2909 let c =
2910 if key >= 32 && key < 127
2911 then Char.chr key
2912 else '\000'
2914 match c with
2915 | 'a' .. 'z' ->
2916 let text = addchar text c in
2917 TEcont text
2919 | _ ->
2920 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2921 TEcont text
2924 let linkndone f s =
2925 if nonemptystr s
2926 then (
2927 let n =
2928 let l = String.length s in
2929 let rec loop pos n = if pos = l then n else
2930 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2931 loop (pos+1) (n*26 + m)
2932 in loop 0 0
2934 let rec loop n = function
2935 | [] -> ()
2936 | l :: rest ->
2937 match getopaque l.pageno with
2938 | None -> loop n rest
2939 | Some opaque ->
2940 let m = getlinkcount opaque in
2941 if n < m
2942 then (
2943 let under = getlink opaque n in
2944 f under
2946 else loop (n-m) rest
2948 loop n state.layout;
2952 let textentry text key =
2953 if key land 0xff00 = 0xff00
2954 then TEcont text
2955 else TEcont (text ^ toutf8 key)
2958 let reqlayout angle fitmodel =
2959 match state.throttle with
2960 | None ->
2961 if nogeomcmds state.geomcmds
2962 then state.anchor <- getanchor ();
2963 conf.angle <- angle mod 360;
2964 if conf.angle != 0
2965 then (
2966 match state.mode with
2967 | LinkNav _ -> state.mode <- View
2968 | _ -> ()
2970 conf.fitmodel <- fitmodel;
2971 invalidate "reqlayout"
2972 (fun () ->
2973 wcmd "reqlayout %d %d %d"
2974 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2976 | _ -> ()
2979 let settrim trimmargins trimfuzz =
2980 if nogeomcmds state.geomcmds
2981 then state.anchor <- getanchor ();
2982 conf.trimmargins <- trimmargins;
2983 conf.trimfuzz <- trimfuzz;
2984 let x0, y0, x1, y1 = trimfuzz in
2985 invalidate "settrim"
2986 (fun () ->
2987 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2988 flushpages ();
2991 let setzoom zoom =
2992 match state.throttle with
2993 | None ->
2994 let zoom = max 0.0001 zoom in
2995 if zoom <> conf.zoom
2996 then (
2997 state.prevzoom <- (conf.zoom, state.x);
2998 conf.zoom <- zoom;
2999 reshape state.winw state.winh;
3000 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
3003 | Some (layout, y, started) ->
3004 let time =
3005 match conf.maxwait with
3006 | None -> 0.0
3007 | Some t -> t
3009 let dt = now () -. started in
3010 if dt > time
3011 then (
3012 state.y <- y;
3013 load layout;
3017 let setcolumns mode columns coverA coverB =
3018 state.prevcolumns <- Some (conf.columns, conf.zoom);
3019 if columns < 0
3020 then (
3021 if isbirdseye mode
3022 then showtext '!' "split mode doesn't work in bird's eye"
3023 else (
3024 conf.columns <- Csplit (-columns, [||]);
3025 state.x <- 0;
3026 conf.zoom <- 1.0;
3029 else (
3030 if columns < 2
3031 then (
3032 conf.columns <- Csingle [||];
3033 state.x <- 0;
3034 setzoom 1.0;
3036 else (
3037 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
3038 conf.zoom <- 1.0;
3041 reshape state.winw state.winh;
3044 let resetmstate () =
3045 state.mstate <- Mnone;
3046 Wsi.setcursor Wsi.CURSOR_INHERIT;
3049 let enterbirdseye () =
3050 let zoom = float conf.thumbw /. float state.winw in
3051 let birdseyepageno =
3052 let cy = state.winh / 2 in
3053 let fold = function
3054 | [] -> 0
3055 | l :: rest ->
3056 let rec fold best = function
3057 | [] -> best.pageno
3058 | l :: rest ->
3059 let d = cy - (l.pagedispy + l.pagevh/2)
3060 and dbest = cy - (best.pagedispy + best.pagevh/2) in
3061 if abs d < abs dbest
3062 then fold l rest
3063 else best.pageno
3064 in fold l rest
3066 fold state.layout
3068 state.mode <- Birdseye (
3069 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
3071 resetmstate ();
3072 conf.zoom <- zoom;
3073 conf.presentation <- false;
3074 conf.interpagespace <- 10;
3075 conf.hlinks <- false;
3076 conf.fitmodel <- FitProportional;
3077 state.x <- 0;
3078 conf.maxwait <- None;
3079 conf.columns <- (
3080 match conf.beyecolumns with
3081 | Some c ->
3082 conf.zoom <- 1.0;
3083 Cmulti ((c, 0, 0), [||])
3084 | None -> Csingle [||]
3086 if conf.verbose
3087 then
3088 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
3089 (100.0*.zoom)
3090 else
3091 state.text <- ""
3093 reshape state.winw state.winh;
3096 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
3097 state.mode <- View;
3098 conf.zoom <- c.zoom;
3099 conf.presentation <- c.presentation;
3100 conf.interpagespace <- c.interpagespace;
3101 conf.maxwait <- c.maxwait;
3102 conf.hlinks <- c.hlinks;
3103 conf.fitmodel <- c.fitmodel;
3104 conf.beyecolumns <- (
3105 match conf.columns with
3106 | Cmulti ((c, _, _), _) -> Some c
3107 | Csingle _ -> None
3108 | Csplit _ -> failwith "leaving bird's eye split mode"
3110 conf.columns <- (
3111 match c.columns with
3112 | Cmulti (c, _) -> Cmulti (c, [||])
3113 | Csingle _ -> Csingle [||]
3114 | Csplit (c, _) -> Csplit (c, [||])
3116 if conf.verbose
3117 then
3118 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
3119 (100.0*.conf.zoom)
3121 reshape state.winw state.winh;
3122 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
3123 state.x <- leftx;
3126 let togglebirdseye () =
3127 match state.mode with
3128 | Birdseye vals -> leavebirdseye vals true
3129 | View -> enterbirdseye ()
3130 | _ -> ()
3133 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3134 let pageno = max 0 (pageno - incr) in
3135 let rec loop = function
3136 | [] -> gotopage1 pageno 0
3137 | l :: _ when l.pageno = pageno ->
3138 if l.pagedispy >= 0 && l.pagey = 0
3139 then G.postRedisplay "upbirdseye"
3140 else gotopage1 pageno 0
3141 | _ :: rest -> loop rest
3143 loop state.layout;
3144 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
3147 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3148 let pageno = min (state.pagecount - 1) (pageno + incr) in
3149 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3150 let rec loop = function
3151 | [] ->
3152 let y, h = getpageyh pageno in
3153 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
3154 gotoy (clamp dy)
3155 | l :: _ when l.pageno = pageno ->
3156 if l.pagevh != l.pageh
3157 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
3158 else G.postRedisplay "downbirdseye"
3159 | _ :: rest -> loop rest
3161 loop state.layout
3164 let boundastep h step =
3165 if step < 0
3166 then bound step ~-h 0
3167 else bound step 0 h
3170 let optentry mode _ key =
3171 let btos b = if b then "on" else "off" in
3172 if key >= 32 && key < 127
3173 then
3174 let c = Char.chr key in
3175 match c with
3176 | 's' ->
3177 let ondone s =
3178 try conf.scrollstep <- int_of_string s with exc ->
3179 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3181 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3183 | 'A' ->
3184 let ondone s =
3186 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
3187 if state.autoscroll <> None
3188 then state.autoscroll <- Some conf.autoscrollstep
3189 with exc ->
3190 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3192 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3194 | 'C' ->
3195 let ondone s =
3197 let n, a, b = multicolumns_of_string s in
3198 setcolumns mode n a b;
3199 with exc ->
3200 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3202 TEswitch ("columns: ", "", None, textentry, ondone, true)
3204 | 'Z' ->
3205 let ondone s =
3207 let zoom = float (int_of_string s) /. 100.0 in
3208 setzoom zoom
3209 with exc ->
3210 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3212 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3214 | 't' ->
3215 let ondone s =
3217 conf.thumbw <- bound (int_of_string s) 2 4096;
3218 state.text <-
3219 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3220 begin match mode with
3221 | Birdseye beye ->
3222 leavebirdseye beye false;
3223 enterbirdseye ();
3224 | _ -> ();
3226 with exc ->
3227 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3229 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3231 | 'R' ->
3232 let ondone s =
3233 match try
3234 Some (int_of_string s)
3235 with exc ->
3236 state.text <- Printf.sprintf "bad integer `%s': %s"
3237 s (exntos exc);
3238 None
3239 with
3240 | Some angle -> reqlayout angle conf.fitmodel
3241 | None -> ()
3243 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3245 | 'i' ->
3246 conf.icase <- not conf.icase;
3247 TEdone ("case insensitive search " ^ (btos conf.icase))
3249 | 'p' ->
3250 conf.preload <- not conf.preload;
3251 gotoy state.y;
3252 TEdone ("preload " ^ (btos conf.preload))
3254 | 'v' ->
3255 conf.verbose <- not conf.verbose;
3256 TEdone ("verbose " ^ (btos conf.verbose))
3258 | 'd' ->
3259 conf.debug <- not conf.debug;
3260 TEdone ("debug " ^ (btos conf.debug))
3262 | 'h' ->
3263 conf.maxhfit <- not conf.maxhfit;
3264 state.maxy <- calcheight ();
3265 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3267 | 'c' ->
3268 conf.crophack <- not conf.crophack;
3269 TEdone ("crophack " ^ btos conf.crophack)
3271 | 'a' ->
3272 let s =
3273 match conf.maxwait with
3274 | None ->
3275 conf.maxwait <- Some infinity;
3276 "always wait for page to complete"
3277 | Some _ ->
3278 conf.maxwait <- None;
3279 "show placeholder if page is not ready"
3281 TEdone s
3283 | 'f' ->
3284 conf.underinfo <- not conf.underinfo;
3285 TEdone ("underinfo " ^ btos conf.underinfo)
3287 | 'P' ->
3288 conf.savebmarks <- not conf.savebmarks;
3289 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3291 | 'S' ->
3292 let ondone s =
3294 let pageno, py =
3295 match state.layout with
3296 | [] -> 0, 0
3297 | l :: _ ->
3298 l.pageno, l.pagey
3300 conf.interpagespace <- int_of_string s;
3301 docolumns conf.columns;
3302 state.maxy <- calcheight ();
3303 let y = getpagey pageno in
3304 gotoy (y + py)
3305 with exc ->
3306 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3308 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3310 | 'l' ->
3311 let fm =
3312 match conf.fitmodel with
3313 | FitProportional -> FitWidth
3314 | _ -> FitProportional
3316 reqlayout conf.angle fm;
3317 TEdone ("proportional display " ^ btos (fm == FitProportional))
3319 | 'T' ->
3320 settrim (not conf.trimmargins) conf.trimfuzz;
3321 TEdone ("trim margins " ^ btos conf.trimmargins)
3323 | 'I' ->
3324 conf.invert <- not conf.invert;
3325 TEdone ("invert colors " ^ btos conf.invert)
3327 | 'x' ->
3328 let ondone s =
3329 cbput state.hists.sel s;
3330 conf.selcmd <- s;
3332 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3333 textentry, ondone, true)
3335 | 'M' ->
3336 if conf.pax == None
3337 then conf.pax <- Some (ref (0.0, 0, 0))
3338 else conf.pax <- None;
3339 TEdone ("PAX " ^ btos (conf.pax != None))
3341 | _ ->
3342 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3343 TEstop
3344 else
3345 TEcont state.text
3348 class type lvsource = object
3349 method getitemcount : int
3350 method getitem : int -> (string * int)
3351 method hasaction : int -> bool
3352 method exit :
3353 uioh:uioh ->
3354 cancel:bool ->
3355 active:int ->
3356 first:int ->
3357 pan:int ->
3358 uioh option
3359 method getactive : int
3360 method getfirst : int
3361 method getpan : int
3362 method getminfo : (int * int) array
3363 end;;
3365 class virtual lvsourcebase = object
3366 val mutable m_active = 0
3367 val mutable m_first = 0
3368 val mutable m_pan = 0
3369 method getactive = m_active
3370 method getfirst = m_first
3371 method getpan = m_pan
3372 method getminfo : (int * int) array = [||]
3373 end;;
3375 let withoutlastutf8 s =
3376 let len = String.length s in
3377 if len = 0
3378 then s
3379 else
3380 let rec find pos =
3381 if pos = 0
3382 then pos
3383 else
3384 let b = Char.code s.[pos] in
3385 if b land 0b11000000 = 0b11000000
3386 then pos
3387 else find (pos-1)
3389 let first =
3390 if Char.code s.[len-1] land 0x80 = 0
3391 then len-1
3392 else find (len-1)
3394 String.sub s 0 first;
3397 let textentrykeyboard
3398 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3399 let key =
3400 if key >= 0xffb0 && key <= 0xffb9
3401 then key - 0xffb0 + 48 else key
3403 let enttext te =
3404 state.mode <- Textentry (te, onleave);
3405 state.text <- "";
3406 enttext ();
3407 G.postRedisplay "textentrykeyboard enttext";
3409 let histaction cmd =
3410 match opthist with
3411 | None -> ()
3412 | Some (action, _) ->
3413 state.mode <- Textentry (
3414 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3416 G.postRedisplay "textentry histaction"
3418 match key with
3419 | 0xff08 -> (* backspace *)
3420 if emptystr text && cancelonempty
3421 then (
3422 onleave Cancel;
3423 G.postRedisplay "textentrykeyboard after cancel";
3425 else
3426 let s = withoutlastutf8 text in
3427 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3429 | 0xff0d | 0xff8d -> (* (kp) enter *)
3430 ondone text;
3431 onleave Confirm;
3432 G.postRedisplay "textentrykeyboard after confirm"
3434 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3435 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3436 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3437 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3439 | 0xff1b -> (* escape*)
3440 if emptystr text
3441 then (
3442 begin match opthist with
3443 | None -> ()
3444 | Some (_, onhistcancel) -> onhistcancel ()
3445 end;
3446 onleave Cancel;
3447 state.text <- "";
3448 G.postRedisplay "textentrykeyboard after cancel2"
3450 else (
3451 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3454 | 0xff9f | 0xffff -> () (* delete *)
3456 | _ when key != 0
3457 && key land 0xff00 != 0xff00 (* keyboard *)
3458 && key land 0xfe00 != 0xfe00 (* xkb *)
3459 && key land 0xfd00 != 0xfd00 (* 3270 *)
3461 begin match onkey text key with
3462 | TEdone text ->
3463 ondone text;
3464 onleave Confirm;
3465 G.postRedisplay "textentrykeyboard after confirm2";
3467 | TEcont text ->
3468 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3470 | TEstop ->
3471 onleave Cancel;
3472 G.postRedisplay "textentrykeyboard after cancel3"
3474 | TEswitch te ->
3475 state.mode <- Textentry (te, onleave);
3476 G.postRedisplay "textentrykeyboard switch";
3477 end;
3479 | _ ->
3480 vlog "unhandled key %s" (Wsi.keyname key)
3483 let firstof first active =
3484 if first > active || abs (first - active) > fstate.maxrows - 1
3485 then max 0 (active - (fstate.maxrows/2))
3486 else first
3489 let calcfirst first active =
3490 if active > first
3491 then
3492 let rows = active - first in
3493 if rows > fstate.maxrows then active - fstate.maxrows else first
3494 else active
3497 let scrollph y maxy =
3498 let sh = float (maxy + state.winh) /. float state.winh in
3499 let sh = float state.winh /. sh in
3500 let sh = max sh (float conf.scrollh) in
3502 let percent = float y /. float maxy in
3503 let position = (float state.winh -. sh) *. percent in
3505 let position =
3506 if position +. sh > float state.winh
3507 then float state.winh -. sh
3508 else position
3510 position, sh;
3513 let coe s = (s :> uioh);;
3515 class listview ~(source:lvsource) ~trusted ~modehash =
3516 object (self)
3517 val m_pan = source#getpan
3518 val m_first = source#getfirst
3519 val m_active = source#getactive
3520 val m_qsearch = ""
3521 val m_prev_uioh = state.uioh
3523 method private elemunder y =
3524 if y < 0
3525 then None
3526 else
3527 let n = y / (fstate.fontsize+1) in
3528 if m_first + n < source#getitemcount
3529 then (
3530 if source#hasaction (m_first + n)
3531 then Some (m_first + n)
3532 else None
3534 else None
3536 method display =
3537 Gl.enable `blend;
3538 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3539 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3540 filledrect 0. 0. (float state.winw) (float state.winh);
3541 GlDraw.color (1., 1., 1.);
3542 Gl.enable `texture_2d;
3543 let fs = fstate.fontsize in
3544 let nfs = fs + 1 in
3545 let ww = fstate.wwidth in
3546 let tabw = 30.0*.ww in
3547 let itemcount = source#getitemcount in
3548 let minfo = source#getminfo in
3549 let rec loop row =
3550 if (row - m_first) > fstate.maxrows
3551 then ()
3552 else (
3553 if row >= 0 && row < itemcount
3554 then (
3555 let (s, level) = source#getitem row in
3556 let y = (row - m_first) * nfs in
3557 let x = 5.0 +. float (level + m_pan) *. ww in
3558 if row = m_active
3559 then (
3560 Gl.disable `texture_2d;
3561 let alpha = if source#hasaction row then 0.9 else 0.3 in
3562 GlDraw.color (1., 1., 1.) ~alpha;
3563 linerect 1. (float (y + 1))
3564 (float (state.winw - conf.scrollbw - 1)) (float (y + fs + 3));
3565 Gl.enable `texture_2d;
3566 GlDraw.color (1., 1., 1.);
3568 let drawtabularstring s =
3569 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3570 if trusted
3571 then
3572 let tabpos = try String.index s '\t' with Not_found -> -1 in
3573 if tabpos > 0
3574 then
3575 let len = String.length s - tabpos - 1 in
3576 let s1 = String.sub s 0 tabpos
3577 and s2 = String.sub s (tabpos + 1) len in
3578 let nx = drawstr x s1 in
3579 let sw = nx -. x in
3580 let x = x +. (max tabw sw) in
3581 drawstr x s2
3582 else
3583 drawstr x s
3584 else
3585 drawstr x s
3587 let _ = drawtabularstring s in
3588 loop (row+1)
3592 loop m_first;
3593 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
3594 let rec loop row =
3595 if (row - m_first) > fstate.maxrows
3596 then ()
3597 else (
3598 if row >= 0 && row < itemcount
3599 then (
3600 let (s, level) = source#getitem row in
3601 let y = (row - m_first) * nfs in
3602 let x = 5.0 +. float (level + m_pan) *. ww in
3603 let (first, last) = minfo.(row) in
3604 let prefix = String.sub s 0 first in
3605 let suffix = String.sub s first (last - first) in
3606 let w1 = measurestr fstate.fontsize prefix in
3607 let w2 = measurestr fstate.fontsize suffix in
3608 let x0 = x +. w1
3609 and y0 = (float (y+2)) in
3610 let x1 = x0 +. w2
3611 and y1 = (float (y+fs+3)) in
3612 filledrect x0 y0 x1 y1;
3613 loop (row+1)
3617 Gl.disable `texture_2d;
3618 if Array.length minfo > 0 then loop m_first;
3619 Gl.disable `blend;
3621 method updownlevel incr =
3622 let len = source#getitemcount in
3623 let curlevel =
3624 if m_active >= 0 && m_active < len
3625 then snd (source#getitem m_active)
3626 else -1
3628 let rec flow i =
3629 if i = len then i-1 else if i = -1 then 0 else
3630 let _, l = source#getitem i in
3631 if l != curlevel then i else flow (i+incr)
3633 let active = flow m_active in
3634 let first = calcfirst m_first active in
3635 G.postRedisplay "outline updownlevel";
3636 {< m_active = active; m_first = first >}
3638 method private key1 key mask =
3639 let set1 active first qsearch =
3640 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3642 let search active pattern incr =
3643 let active = if active = -1 then m_first else active in
3644 let dosearch re =
3645 let rec loop n =
3646 if n >= 0 && n < source#getitemcount
3647 then (
3648 let s, _ = source#getitem n in
3650 (try ignore (Str.search_forward re s 0); true
3651 with Not_found -> false)
3652 then Some n
3653 else loop (n + incr)
3655 else None
3657 loop active
3660 let re = Str.regexp_case_fold pattern in
3661 dosearch re
3662 with Failure s ->
3663 state.text <- s;
3664 None
3666 let itemcount = source#getitemcount in
3667 let find start incr =
3668 let rec find i =
3669 if i = -1 || i = itemcount
3670 then -1
3671 else (
3672 if source#hasaction i
3673 then i
3674 else find (i + incr)
3677 find start
3679 let set active first =
3680 let first = bound first 0 (itemcount - fstate.maxrows) in
3681 state.text <- "";
3682 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3684 let navigate incr =
3685 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3686 let active, first =
3687 let incr1 = if incr > 0 then 1 else -1 in
3688 if isvisible m_first m_active
3689 then
3690 let next =
3691 let next = m_active + incr in
3692 let next =
3693 if next < 0 || next >= itemcount
3694 then -1
3695 else find next incr1
3697 if abs (m_active - next) > fstate.maxrows
3698 then -1
3699 else next
3701 if next = -1
3702 then
3703 let first = m_first + incr in
3704 let first = bound first 0 (itemcount - fstate.maxrows) in
3705 let next =
3706 let next = m_active + incr in
3707 let next = bound next 0 (itemcount - 1) in
3708 find next ~-incr1
3710 let active =
3711 if next = -1
3712 then m_active
3713 else (
3714 if isvisible first next
3715 then next
3716 else m_active
3719 active, first
3720 else
3721 let first = min next m_first in
3722 let first =
3723 if abs (next - first) > fstate.maxrows
3724 then first + incr
3725 else first
3727 next, first
3728 else
3729 let first = m_first + incr in
3730 let first = bound first 0 (itemcount - 1) in
3731 let active =
3732 let next = m_active + incr in
3733 let next = bound next 0 (itemcount - 1) in
3734 let next = find next incr1 in
3735 let active =
3736 if next = -1 || abs (m_active - first) > fstate.maxrows
3737 then (
3738 let active = if m_active = -1 then next else m_active in
3739 active
3741 else next
3743 if isvisible first active
3744 then active
3745 else -1
3747 active, first
3749 G.postRedisplay "listview navigate";
3750 set active first;
3752 match key with
3753 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3754 let incr = if key = 0x72 then -1 else 1 in
3755 let active, first =
3756 match search (m_active + incr) m_qsearch incr with
3757 | None ->
3758 state.text <- m_qsearch ^ " [not found]";
3759 m_active, m_first
3760 | Some active ->
3761 state.text <- m_qsearch;
3762 active, firstof m_first active
3764 G.postRedisplay "listview ctrl-r/s";
3765 set1 active first m_qsearch;
3767 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3768 if m_active >= 0 && m_active < source#getitemcount
3769 then (
3770 let s, _ = source#getitem m_active in
3771 selstring s;
3773 coe self
3775 | 0xff08 -> (* backspace *)
3776 if emptystr m_qsearch
3777 then coe self
3778 else (
3779 let qsearch = withoutlastutf8 m_qsearch in
3780 if emptystr qsearch
3781 then (
3782 state.text <- "";
3783 G.postRedisplay "listview empty qsearch";
3784 set1 m_active m_first "";
3786 else
3787 let active, first =
3788 match search m_active qsearch ~-1 with
3789 | None ->
3790 state.text <- qsearch ^ " [not found]";
3791 m_active, m_first
3792 | Some active ->
3793 state.text <- qsearch;
3794 active, firstof m_first active
3796 G.postRedisplay "listview backspace qsearch";
3797 set1 active first qsearch
3800 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3801 let pattern = m_qsearch ^ toutf8 key in
3802 let active, first =
3803 match search m_active pattern 1 with
3804 | None ->
3805 state.text <- pattern ^ " [not found]";
3806 m_active, m_first
3807 | Some active ->
3808 state.text <- pattern;
3809 active, firstof m_first active
3811 G.postRedisplay "listview qsearch add";
3812 set1 active first pattern;
3814 | 0xff1b -> (* escape *)
3815 state.text <- "";
3816 if emptystr m_qsearch
3817 then (
3818 G.postRedisplay "list view escape";
3819 begin
3820 match
3821 source#exit (coe self) true m_active m_first m_pan
3822 with
3823 | None -> m_prev_uioh
3824 | Some uioh -> uioh
3827 else (
3828 G.postRedisplay "list view kill qsearch";
3829 coe {< m_qsearch = "" >}
3832 | 0xff0d | 0xff8d -> (* (kp) enter *)
3833 state.text <- "";
3834 let self = {< m_qsearch = "" >} in
3835 let opt =
3836 G.postRedisplay "listview enter";
3837 if m_active >= 0 && m_active < source#getitemcount
3838 then (
3839 source#exit (coe self) false m_active m_first m_pan;
3841 else (
3842 source#exit (coe self) true m_active m_first m_pan;
3845 begin match opt with
3846 | None -> m_prev_uioh
3847 | Some uioh -> uioh
3850 | 0xff9f | 0xffff -> (* (kp) delete *)
3851 coe self
3853 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3854 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3855 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3856 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3858 | 0xff53 | 0xff98 -> (* (kp) right *)
3859 state.text <- "";
3860 G.postRedisplay "listview right";
3861 coe {< m_pan = m_pan - 1 >}
3863 | 0xff51 | 0xff96 -> (* (kp) left *)
3864 state.text <- "";
3865 G.postRedisplay "listview left";
3866 coe {< m_pan = m_pan + 1 >}
3868 | 0xff50 | 0xff95 -> (* (kp) home *)
3869 let active = find 0 1 in
3870 G.postRedisplay "listview home";
3871 set active 0;
3873 | 0xff57 | 0xff9c -> (* (kp) end *)
3874 let first = max 0 (itemcount - fstate.maxrows) in
3875 let active = find (itemcount - 1) ~-1 in
3876 G.postRedisplay "listview end";
3877 set active first;
3879 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3880 coe self
3882 | _ ->
3883 dolog "listview unknown key %#x" key; coe self
3885 method key key mask =
3886 match state.mode with
3887 | Textentry te -> textentrykeyboard key mask te; coe self
3888 | _ -> self#key1 key mask
3890 method button button down x y _ =
3891 let opt =
3892 match button with
3893 | 1 when x > state.winw - conf.scrollbw ->
3894 G.postRedisplay "listview scroll";
3895 if down
3896 then
3897 let _, position, sh = self#scrollph in
3898 if y > truncate position && y < truncate (position +. sh)
3899 then (
3900 state.mstate <- Mscrolly;
3901 Some (coe self)
3903 else
3904 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3905 let first = truncate (s *. float source#getitemcount) in
3906 let first = min source#getitemcount first in
3907 Some (coe {< m_first = first; m_active = first >})
3908 else (
3909 state.mstate <- Mnone;
3910 Some (coe self);
3912 | 1 when not down ->
3913 begin match self#elemunder y with
3914 | Some n ->
3915 G.postRedisplay "listview click";
3916 source#exit (coe {< m_active = n >}) false n m_first m_pan
3917 | _ ->
3918 Some (coe self)
3920 | n when (n == 4 || n == 5) && not down ->
3921 let len = source#getitemcount in
3922 let first =
3923 if n = 5 && m_first + fstate.maxrows >= len
3924 then
3925 m_first
3926 else
3927 let first = m_first + (if n == 4 then -1 else 1) in
3928 bound first 0 (len - 1)
3930 G.postRedisplay "listview wheel";
3931 Some (coe {< m_first = first >})
3932 | n when (n = 6 || n = 7) && not down ->
3933 let inc = if n = 7 then -1 else 1 in
3934 G.postRedisplay "listview hwheel";
3935 Some (coe {< m_pan = m_pan + inc >})
3936 | _ ->
3937 Some (coe self)
3939 match opt with
3940 | None -> m_prev_uioh
3941 | Some uioh -> uioh
3943 method multiclick _ x y = self#button 1 true x y
3945 method motion _ y =
3946 match state.mstate with
3947 | Mscrolly ->
3948 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3949 let first = truncate (s *. float source#getitemcount) in
3950 let first = min source#getitemcount first in
3951 G.postRedisplay "listview motion";
3952 coe {< m_first = first; m_active = first >}
3953 | _ -> coe self
3955 method pmotion x y =
3956 if x < state.winw - conf.scrollbw
3957 then
3958 let n =
3959 match self#elemunder y with
3960 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3961 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3963 let o =
3964 if n != m_active
3965 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3966 else self
3968 coe o
3969 else (
3970 Wsi.setcursor Wsi.CURSOR_INHERIT;
3971 coe self
3974 method infochanged _ = ()
3976 method scrollpw = (0, 0.0, 0.0)
3977 method scrollph =
3978 let nfs = fstate.fontsize + 1 in
3979 let y = m_first * nfs in
3980 let itemcount = source#getitemcount in
3981 let maxi = max 0 (itemcount - fstate.maxrows) in
3982 let maxy = maxi * nfs in
3983 let p, h = scrollph y maxy in
3984 conf.scrollbw, p, h
3986 method modehash = modehash
3987 method eformsgs = false
3988 end;;
3990 class outlinelistview ~source =
3991 let settext autonarrow s =
3992 if autonarrow
3993 then
3994 let ss = source#statestr in
3995 state.text <-
3996 if emptystr ss
3997 then "[" ^ s ^ "]"
3998 else "{" ^ ss ^ "} [" ^ s ^ "]"
3999 else state.text <- s
4001 object (self)
4002 inherit listview
4003 ~source:(source :> lvsource)
4004 ~trusted:false
4005 ~modehash:(findkeyhash conf "outline")
4006 as super
4008 val m_autonarrow = false
4010 method display = super#display
4012 method key key mask =
4013 let maxrows =
4014 if emptystr state.text
4015 then fstate.maxrows
4016 else fstate.maxrows - 2
4018 let calcfirst first active =
4019 if active > first
4020 then
4021 let rows = active - first in
4022 if rows > maxrows then active - maxrows else first
4023 else active
4025 let navigate incr =
4026 let active = m_active + incr in
4027 let active = bound active 0 (source#getitemcount - 1) in
4028 let first = calcfirst m_first active in
4029 G.postRedisplay "outline navigate";
4030 coe {< m_active = active; m_first = first >}
4032 let navscroll first =
4033 let active =
4034 let dist = m_active - first in
4035 if dist < 0
4036 then first
4037 else (
4038 if dist < maxrows
4039 then m_active
4040 else first + maxrows
4043 G.postRedisplay "outline navscroll";
4044 coe {< m_first = first; m_active = active >}
4046 let ctrl = Wsi.withctrl mask in
4047 match key with
4048 | 97 when ctrl -> (* ctrl-a *)
4049 let text =
4050 if m_autonarrow
4051 then (source#denarrow; "")
4052 else (
4053 let pattern = source#renarrow in
4054 if nonemptystr m_qsearch
4055 then (source#narrow m_qsearch; m_qsearch)
4056 else pattern
4059 settext (not m_autonarrow) text;
4060 G.postRedisplay "toggle auto narrowing";
4061 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
4063 | 47 when emptystr m_qsearch && not m_autonarrow -> (* / *)
4064 settext true "";
4065 G.postRedisplay "toggle auto narrowing";
4066 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
4068 | 110 when ctrl -> (* ctrl-n *)
4069 source#narrow m_qsearch;
4070 if not m_autonarrow
4071 then source#add_narrow_pattern m_qsearch;
4072 G.postRedisplay "outline ctrl-n";
4073 coe {< m_first = 0; m_active = 0 >}
4075 | 83 when ctrl -> (* ctrl-S *)
4076 let active = source#calcactive (getanchor ()) in
4077 let first = firstof m_first active in
4078 G.postRedisplay "outline ctrl-s";
4079 coe {< m_first = first; m_active = active >}
4081 | 117 when ctrl -> (* ctrl-u *)
4082 G.postRedisplay "outline ctrl-u";
4083 if m_autonarrow && nonemptystr m_qsearch
4084 then (
4085 ignore (source#renarrow);
4086 settext m_autonarrow "";
4087 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
4089 else (
4090 source#del_narrow_pattern;
4091 let pattern = source#renarrow in
4092 let text =
4093 if emptystr pattern then "" else "Narrowed to " ^ pattern
4095 settext m_autonarrow text;
4096 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
4099 | 108 when ctrl -> (* ctrl-l *)
4100 let first = max 0 (m_active - (fstate.maxrows / 2)) in
4101 G.postRedisplay "outline ctrl-l";
4102 coe {< m_first = first >}
4104 | 0xff09 when m_autonarrow -> (* tab *)
4105 if nonemptystr m_qsearch
4106 then (
4107 G.postRedisplay "outline list view tab";
4108 source#add_narrow_pattern m_qsearch;
4109 settext true "";
4110 coe {< m_qsearch = "" >}
4112 else coe self
4114 | 0xff1b when m_autonarrow -> (* escape *)
4115 if nonemptystr m_qsearch
4116 then source#add_narrow_pattern m_qsearch;
4117 super#key key mask
4119 | 0xff0d | 0xff8d when m_autonarrow -> (* (kp) enter *)
4120 if nonemptystr m_qsearch
4121 then source#add_narrow_pattern m_qsearch;
4122 super#key key mask
4124 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
4125 let pattern = m_qsearch ^ toutf8 key in
4126 G.postRedisplay "outlinelistview autonarrow add";
4127 source#narrow pattern;
4128 settext true pattern;
4129 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4131 | key when m_autonarrow && key = 0xff08 -> (* backspace *)
4132 if emptystr m_qsearch
4133 then coe self
4134 else
4135 let pattern = withoutlastutf8 m_qsearch in
4136 G.postRedisplay "outlinelistview autonarrow backspace";
4137 ignore (source#renarrow);
4138 source#narrow pattern;
4139 settext true pattern;
4140 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4142 | 0xff9f | 0xffff -> (* (kp) delete *)
4143 source#remove m_active;
4144 G.postRedisplay "outline delete";
4145 let active = max 0 (m_active-1) in
4146 coe {< m_first = firstof m_first active;
4147 m_active = active >}
4149 | 0xff52 | 0xff97 when ctrl -> (* ctrl-(kp) up *)
4150 navscroll (max 0 (m_first - 1))
4152 | 0xff54 | 0xff99 when ctrl -> (* ctrl-(kp) down *)
4153 navscroll (min (source#getitemcount - 1) (m_first + 1))
4155 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
4156 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
4157 | 0xff55 | 0xff9a -> (* (kp) prior *)
4158 navigate ~-(fstate.maxrows)
4159 | 0xff56 | 0xff9b -> (* (kp) next *)
4160 navigate fstate.maxrows
4162 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
4163 let o =
4164 if ctrl
4165 then (
4166 G.postRedisplay "outline ctrl right";
4167 {< m_pan = m_pan + 1 >}
4169 else self#updownlevel 1
4171 coe o
4173 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
4174 let o =
4175 if ctrl
4176 then (
4177 G.postRedisplay "outline ctrl left";
4178 {< m_pan = m_pan - 1 >}
4180 else self#updownlevel ~-1
4182 coe o
4184 | 0xff50 | 0xff95 -> (* (kp) home *)
4185 G.postRedisplay "outline home";
4186 coe {< m_first = 0; m_active = 0 >}
4188 | 0xff57 | 0xff9c -> (* (kp) end *)
4189 let active = source#getitemcount - 1 in
4190 let first = max 0 (active - fstate.maxrows) in
4191 G.postRedisplay "outline end";
4192 coe {< m_active = active; m_first = first >}
4194 | _ -> super#key key mask
4197 let gotounder under =
4198 let getpath filename =
4199 let path =
4200 if nonemptystr filename
4201 then
4202 if Filename.is_relative filename
4203 then
4204 let dir = Filename.dirname state.path in
4205 let dir =
4206 if Filename.is_implicit dir
4207 then Filename.concat (Sys.getcwd ()) dir
4208 else dir
4210 Filename.concat dir filename
4211 else filename
4212 else ""
4214 if Sys.file_exists path
4215 then path
4216 else ""
4218 match under with
4219 | Ulinkgoto (pageno, top) ->
4220 if pageno >= 0
4221 then (
4222 addnav ();
4223 gotopage1 pageno top;
4226 | Ulinkuri s ->
4227 gotouri s
4229 | Uremote (filename, pageno) ->
4230 let path = getpath filename in
4231 if nonemptystr path
4232 then (
4233 if conf.riani
4234 then
4235 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4236 try popen command []
4237 with exn ->
4238 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
4239 flush stderr;
4240 else
4241 let anchor = getanchor () in
4242 let ranchor = state.path, state.password, anchor, state.origin in
4243 state.origin <- "";
4244 state.anchor <- (pageno, 0.0, 0.0);
4245 state.ranchors <- ranchor :: state.ranchors;
4246 opendoc path "";
4248 else showtext '!' ("Could not find " ^ filename)
4250 | Uremotedest (filename, destname) ->
4251 let path = getpath filename in
4252 if nonemptystr path
4253 then (
4254 if conf.riani
4255 then
4256 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4257 try popen command []
4258 with exn ->
4259 Printf.eprintf
4260 "failed to execute `%s': %s\n" command (exntos exn);
4261 flush stderr;
4262 else
4263 let anchor = getanchor () in
4264 let ranchor = state.path, state.password, anchor, state.origin in
4265 state.origin <- "";
4266 state.nameddest <- destname;
4267 state.ranchors <- ranchor :: state.ranchors;
4268 opendoc path "";
4270 else showtext '!' ("Could not find " ^ filename)
4272 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4275 let gotooutline (_, _, kind) =
4276 match kind with
4277 | Onone -> ()
4278 | Oanchor anchor ->
4279 let (pageno, y, _) = anchor in
4280 let y = getanchory
4281 (if conf.presentation then (pageno, y, 1.0) else anchor)
4283 addnav ();
4284 gotoghyll y
4285 | Ouri uri -> gotounder (Ulinkuri uri)
4286 | Olaunch cmd -> gotounder (Ulaunch cmd)
4287 | Oremote remote -> gotounder (Uremote remote)
4288 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4291 let outlinesource usebookmarks =
4292 let empty = [||] in
4293 (object (self)
4294 inherit lvsourcebase
4295 val mutable m_items = empty
4296 val mutable m_minfo = empty
4297 val mutable m_orig_items = empty
4298 val mutable m_orig_minfo = empty
4299 val mutable m_narrow_patterns = []
4300 val mutable m_hadremovals = false
4301 val mutable m_gen = -1
4303 method getitemcount =
4304 Array.length m_items + (if m_hadremovals then 1 else 0)
4306 method getitem n =
4307 if n == Array.length m_items && m_hadremovals
4308 then
4309 ("[Confirm removal]", 0)
4310 else
4311 let s, n, _ = m_items.(n) in
4312 (s, n)
4314 method exit ~uioh ~cancel ~active ~first ~pan =
4315 ignore (uioh, first);
4316 let confrimremoval = m_hadremovals && active = Array.length m_items in
4317 let items, minfo =
4318 if m_narrow_patterns = []
4319 then m_orig_items, m_orig_minfo
4320 else m_items, m_minfo
4322 if not cancel
4323 then (
4324 if not confrimremoval
4325 then (
4326 gotooutline m_items.(active);
4327 m_items <- items;
4328 m_minfo <- minfo;
4330 else (
4331 state.bookmarks <- Array.to_list m_items;
4332 m_orig_items <- m_items;
4333 m_orig_minfo <- m_minfo;
4336 else (
4337 m_items <- items;
4338 m_minfo <- minfo;
4340 m_pan <- pan;
4341 None
4343 method hasaction _ = true
4345 method greetmsg =
4346 if Array.length m_items != Array.length m_orig_items
4347 then
4348 let s =
4349 match m_narrow_patterns with
4350 | one :: [] -> one
4351 | many -> String.concat "\xe2\x80\xa6" (List.rev many)
4353 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4354 else ""
4356 method statestr =
4357 match m_narrow_patterns with
4358 | [] -> ""
4359 | one :: [] -> one
4360 | head :: _ -> "\xe2\x80\xa6" ^ head
4362 method narrow pattern =
4363 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4364 match reopt with
4365 | None -> ()
4366 | Some re ->
4367 let rec loop accu minfo n =
4368 if n = -1
4369 then (
4370 m_items <- Array.of_list accu;
4371 m_minfo <- Array.of_list minfo;
4373 else
4374 let (s, _, _) as o = m_items.(n) in
4375 let accu, minfo =
4376 let first =
4377 try Str.search_forward re s 0
4378 with Not_found -> -1
4380 if first >= 0
4381 then o :: accu, (first, Str.match_end ()) :: minfo
4382 else accu, minfo
4384 loop accu minfo (n-1)
4386 loop [] [] (Array.length m_items - 1)
4388 method getminfo = m_minfo
4390 method denarrow =
4391 m_orig_items <- (
4392 if usebookmarks
4393 then Array.of_list state.bookmarks
4394 else state.outlines
4396 m_minfo <- m_orig_minfo;
4397 m_items <- m_orig_items
4399 method remove m =
4400 if usebookmarks
4401 then
4402 if m >= 0 && m < Array.length m_items
4403 then (
4404 m_hadremovals <- true;
4405 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4406 let n = if n >= m then n+1 else n in
4407 m_items.(n)
4411 method add_narrow_pattern pattern =
4412 m_narrow_patterns <- pattern :: m_narrow_patterns
4414 method del_narrow_pattern =
4415 match m_narrow_patterns with
4416 | _ :: rest -> m_narrow_patterns <- rest
4417 | [] -> ()
4419 method renarrow =
4420 self#denarrow;
4421 match m_narrow_patterns with
4422 | pattern :: [] -> self#narrow pattern; pattern
4423 | list ->
4424 List.fold_left (fun accu pattern ->
4425 self#narrow pattern;
4426 pattern ^ "\xe2\x80\xa6" ^ accu) "" list
4428 method calcactive anchor =
4429 let rely = getanchory anchor in
4430 let rec loop n best bestd =
4431 if n = Array.length m_items
4432 then best
4433 else
4434 let _, _, kind = m_items.(n) in
4435 match kind with
4436 | Oanchor anchor ->
4437 let orely = getanchory anchor in
4438 let d = abs (orely - rely) in
4439 if d < bestd
4440 then loop (n+1) n d
4441 else loop (n+1) best bestd
4442 | Onone | Oremote _ | Olaunch _ | Oremotedest _ | Ouri _ ->
4443 loop (n+1) best bestd
4445 loop 0 ~-1 max_int
4447 method reset anchor items =
4448 m_hadremovals <- false;
4449 if state.gen != m_gen
4450 then (
4451 m_orig_items <- items;
4452 m_items <- items;
4453 m_narrow_patterns <- [];
4454 m_minfo <- empty;
4455 m_orig_minfo <- empty;
4456 m_gen <- state.gen;
4458 else (
4459 if items != m_orig_items
4460 then (
4461 m_orig_items <- items;
4462 if m_narrow_patterns == []
4463 then m_items <- items;
4466 let active = self#calcactive anchor in
4467 m_active <- active;
4468 m_first <- firstof m_first active
4469 end)
4472 let enterselector usebookmarks =
4473 resetmstate ();
4474 let source = outlinesource usebookmarks in
4475 fun errmsg ->
4476 let outlines =
4477 if usebookmarks
4478 then Array.of_list state.bookmarks
4479 else state.outlines
4481 if Array.length outlines = 0
4482 then (
4483 showtext ' ' errmsg;
4485 else (
4486 state.text <- source#greetmsg;
4487 Wsi.setcursor Wsi.CURSOR_INHERIT;
4488 let anchor = getanchor () in
4489 source#reset anchor outlines;
4490 state.uioh <- coe (new outlinelistview ~source);
4491 G.postRedisplay "enter selector";
4495 let enteroutlinemode =
4496 let f = enterselector false in
4497 fun ()-> f "Document has no outline";
4500 let enterbookmarkmode =
4501 let f = enterselector true in
4502 fun () -> f "Document has no bookmarks (yet)";
4505 let color_of_string s =
4506 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4507 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4511 let color_to_string (r, g, b) =
4512 let r = truncate (r *. 256.0)
4513 and g = truncate (g *. 256.0)
4514 and b = truncate (b *. 256.0) in
4515 Printf.sprintf "%d/%d/%d" r g b
4518 let irect_of_string s =
4519 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4522 let irect_to_string (x0,y0,x1,y1) =
4523 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4526 let makecheckers () =
4527 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4528 following to say:
4529 converted by Issac Trotts. July 25, 2002 *)
4530 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4531 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4532 let id = GlTex.gen_texture () in
4533 GlTex.bind_texture `texture_2d id;
4534 GlPix.store (`unpack_alignment 1);
4535 GlTex.image2d image;
4536 List.iter (GlTex.parameter ~target:`texture_2d)
4537 [ `mag_filter `nearest; `min_filter `nearest ];
4541 let setcheckers enabled =
4542 match state.texid with
4543 | None ->
4544 if enabled then state.texid <- Some (makecheckers ())
4546 | Some texid ->
4547 if not enabled
4548 then (
4549 GlTex.delete_texture texid;
4550 state.texid <- None;
4554 let int_of_string_with_suffix s =
4555 let l = String.length s in
4556 let s1, shift =
4557 if l > 1
4558 then
4559 let suffix = Char.lowercase s.[l-1] in
4560 match suffix with
4561 | 'k' -> String.sub s 0 (l-1), 10
4562 | 'm' -> String.sub s 0 (l-1), 20
4563 | 'g' -> String.sub s 0 (l-1), 30
4564 | _ -> s, 0
4565 else s, 0
4567 let n = int_of_string s1 in
4568 let m = n lsl shift in
4569 if m < 0 || m < n
4570 then raise (Failure "value too large")
4571 else m
4574 let string_with_suffix_of_int n =
4575 if n = 0
4576 then "0"
4577 else
4578 let units = [(30, "G"); (20, "M"); (10, "K")] in
4579 let prettyint n =
4580 let rec loop s n =
4581 let h = n mod 1000 in
4582 let n = n / 1000 in
4583 if n = 0
4584 then string_of_int h ^ s
4585 else (
4586 let s = Printf.sprintf "_%03d%s" h s in
4587 loop s n
4590 loop "" n
4592 let rec find = function
4593 | [] -> prettyint n
4594 | (shift, suffix) :: rest ->
4595 if (n land ((1 lsl shift) - 1)) = 0
4596 then prettyint (n lsr shift) ^ suffix
4597 else find rest
4599 find units
4602 let defghyllscroll = (40,8,32);;
4603 let fastghyllscroll = (5,1,2);;
4604 let neatghyllscroll = (10,1,9);;
4605 let ghyllscroll_of_string s =
4606 match s with
4607 | "default" -> Some defghyllscroll
4608 | "fast" -> Some (5,1,2)
4609 | "neat" -> Some (10,1,9)
4610 | "" | "none" -> None
4611 | _ ->
4612 let (n,a,b) as nab =
4613 Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b) in
4614 if n <= a || n <= b || a >= b
4615 then error "invalid ghyll N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)"
4616 n a b;
4617 Some nab
4620 let ghyllscroll_to_string ((n, a, b) as nab) =
4621 (**) if nab = defghyllscroll then "default"
4622 else if nab = fastghyllscroll then "fast"
4623 else if nab = neatghyllscroll then "neat"
4624 else Printf.sprintf "%d,%d,%d" n a b;
4627 let describe_location () =
4628 let fn = page_of_y state.y in
4629 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4630 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4631 let percent =
4632 if maxy <= 0
4633 then 100.
4634 else (100. *. (float state.y /. float maxy))
4636 if fn = ln
4637 then
4638 Printf.sprintf "page %d of %d [%.2f%%]"
4639 (fn+1) state.pagecount percent
4640 else
4641 Printf.sprintf
4642 "pages %d-%d of %d [%.2f%%]"
4643 (fn+1) (ln+1) state.pagecount percent
4646 let setpresentationmode v =
4647 let n = page_of_y state.y in
4648 state.anchor <- (n, 0.0, 1.0);
4649 conf.presentation <- v;
4650 if conf.fitmodel = FitPage
4651 then reqlayout conf.angle conf.fitmodel;
4652 represent ();
4655 let enterinfomode =
4656 let btos b = if b then "\xe2\x88\x9a" else "" in
4657 let showextended = ref false in
4658 let leave mode = function
4659 | Confirm -> state.mode <- mode
4660 | Cancel -> state.mode <- mode in
4661 let src =
4662 (object
4663 val mutable m_first_time = true
4664 val mutable m_l = []
4665 val mutable m_a = [||]
4666 val mutable m_prev_uioh = nouioh
4667 val mutable m_prev_mode = View
4669 inherit lvsourcebase
4671 method reset prev_mode prev_uioh =
4672 m_a <- Array.of_list (List.rev m_l);
4673 m_l <- [];
4674 m_prev_mode <- prev_mode;
4675 m_prev_uioh <- prev_uioh;
4676 if m_first_time
4677 then (
4678 let rec loop n =
4679 if n >= Array.length m_a
4680 then ()
4681 else
4682 match m_a.(n) with
4683 | _, _, _, Action _ -> m_active <- n
4684 | _ -> loop (n+1)
4686 loop 0;
4687 m_first_time <- false;
4690 method int name get set =
4691 m_l <-
4692 (name, `int get, 1, Action (
4693 fun u ->
4694 let ondone s =
4695 try set (int_of_string s)
4696 with exn ->
4697 state.text <- Printf.sprintf "bad integer `%s': %s"
4698 s (exntos exn)
4700 state.text <- "";
4701 let te = name ^ ": ", "", None, intentry, ondone, true in
4702 state.mode <- Textentry (te, leave m_prev_mode);
4704 )) :: m_l
4706 method int_with_suffix name get set =
4707 m_l <-
4708 (name, `intws get, 1, Action (
4709 fun u ->
4710 let ondone s =
4711 try set (int_of_string_with_suffix s)
4712 with exn ->
4713 state.text <- Printf.sprintf "bad integer `%s': %s"
4714 s (exntos exn)
4716 state.text <- "";
4717 let te =
4718 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4720 state.mode <- Textentry (te, leave m_prev_mode);
4722 )) :: m_l
4724 method bool ?(offset=1) ?(btos=btos) name get set =
4725 m_l <-
4726 (name, `bool (btos, get), offset, Action (
4727 fun u ->
4728 let v = get () in
4729 set (not v);
4731 )) :: m_l
4733 method color name get set =
4734 m_l <-
4735 (name, `color get, 1, Action (
4736 fun u ->
4737 let invalid = (nan, nan, nan) in
4738 let ondone s =
4739 let c =
4740 try color_of_string s
4741 with exn ->
4742 state.text <- Printf.sprintf "bad color `%s': %s"
4743 s (exntos exn);
4744 invalid
4746 if c <> invalid
4747 then set c;
4749 let te = name ^ ": ", "", None, textentry, ondone, true in
4750 state.text <- color_to_string (get ());
4751 state.mode <- Textentry (te, leave m_prev_mode);
4753 )) :: m_l
4755 method string name get set =
4756 m_l <-
4757 (name, `string get, 1, Action (
4758 fun u ->
4759 let ondone s = set s in
4760 let te = name ^ ": ", "", None, textentry, ondone, true in
4761 state.mode <- Textentry (te, leave m_prev_mode);
4763 )) :: m_l
4765 method colorspace name get set =
4766 m_l <-
4767 (name, `string get, 1, Action (
4768 fun _ ->
4769 let source =
4770 (object
4771 inherit lvsourcebase
4773 initializer
4774 m_active <- CSTE.to_int conf.colorspace;
4775 m_first <- 0;
4777 method getitemcount =
4778 Array.length CSTE.names
4779 method getitem n =
4780 (CSTE.names.(n), 0)
4781 method exit ~uioh ~cancel ~active ~first ~pan =
4782 ignore (uioh, first, pan);
4783 if not cancel then set active;
4784 None
4785 method hasaction _ = true
4786 end)
4788 state.text <- "";
4789 let modehash = findkeyhash conf "info" in
4790 coe (new listview ~source ~trusted:true ~modehash)
4791 )) :: m_l
4793 method paxmark name get set =
4794 m_l <-
4795 (name, `string get, 1, Action (
4796 fun _ ->
4797 let source =
4798 (object
4799 inherit lvsourcebase
4801 initializer
4802 m_active <- MTE.to_int conf.paxmark;
4803 m_first <- 0;
4805 method getitemcount = Array.length MTE.names
4806 method getitem n = (MTE.names.(n), 0)
4807 method exit ~uioh ~cancel ~active ~first ~pan =
4808 ignore (uioh, first, pan);
4809 if not cancel then set active;
4810 None
4811 method hasaction _ = true
4812 end)
4814 state.text <- "";
4815 let modehash = findkeyhash conf "info" in
4816 coe (new listview ~source ~trusted:true ~modehash)
4817 )) :: m_l
4819 method fitmodel name get set =
4820 m_l <-
4821 (name, `string get, 1, Action (
4822 fun _ ->
4823 let source =
4824 (object
4825 inherit lvsourcebase
4827 initializer
4828 m_active <- FMTE.to_int conf.fitmodel;
4829 m_first <- 0;
4831 method getitemcount = Array.length FMTE.names
4832 method getitem n = (FMTE.names.(n), 0)
4833 method exit ~uioh ~cancel ~active ~first ~pan =
4834 ignore (uioh, first, pan);
4835 if not cancel then set active;
4836 None
4837 method hasaction _ = true
4838 end)
4840 state.text <- "";
4841 let modehash = findkeyhash conf "info" in
4842 coe (new listview ~source ~trusted:true ~modehash)
4843 )) :: m_l
4845 method caption s offset =
4846 m_l <- (s, `empty, offset, Noaction) :: m_l
4848 method caption2 s f offset =
4849 m_l <- (s, `string f, offset, Noaction) :: m_l
4851 method getitemcount = Array.length m_a
4853 method getitem n =
4854 let tostr = function
4855 | `int f -> string_of_int (f ())
4856 | `intws f -> string_with_suffix_of_int (f ())
4857 | `string f -> f ()
4858 | `color f -> color_to_string (f ())
4859 | `bool (btos, f) -> btos (f ())
4860 | `empty -> ""
4862 let name, t, offset, _ = m_a.(n) in
4863 ((let s = tostr t in
4864 if nonemptystr s
4865 then Printf.sprintf "%s\t%s" name s
4866 else name),
4867 offset)
4869 method exit ~uioh ~cancel ~active ~first ~pan =
4870 let uiohopt =
4871 if not cancel
4872 then (
4873 let uioh =
4874 match m_a.(active) with
4875 | _, _, _, Action f -> f uioh
4876 | _ -> uioh
4878 Some uioh
4880 else None
4882 m_active <- active;
4883 m_first <- first;
4884 m_pan <- pan;
4885 uiohopt
4887 method hasaction n =
4888 match m_a.(n) with
4889 | _, _, _, Action _ -> true
4890 | _ -> false
4891 end)
4893 let rec fillsrc prevmode prevuioh =
4894 let sep () = src#caption "" 0 in
4895 let colorp name get set =
4896 src#string name
4897 (fun () -> color_to_string (get ()))
4898 (fun v ->
4900 let c = color_of_string v in
4901 set c
4902 with exn ->
4903 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4906 let oldmode = state.mode in
4907 let birdseye = isbirdseye state.mode in
4909 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4911 src#bool "presentation mode"
4912 (fun () -> conf.presentation)
4913 (fun v -> setpresentationmode v);
4915 src#bool "ignore case in searches"
4916 (fun () -> conf.icase)
4917 (fun v -> conf.icase <- v);
4919 src#bool "preload"
4920 (fun () -> conf.preload)
4921 (fun v -> conf.preload <- v);
4923 src#bool "highlight links"
4924 (fun () -> conf.hlinks)
4925 (fun v -> conf.hlinks <- v);
4927 src#bool "under info"
4928 (fun () -> conf.underinfo)
4929 (fun v -> conf.underinfo <- v);
4931 src#bool "persistent bookmarks"
4932 (fun () -> conf.savebmarks)
4933 (fun v -> conf.savebmarks <- v);
4935 src#fitmodel "fit model"
4936 (fun () -> FMTE.to_string conf.fitmodel)
4937 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4939 src#bool "trim margins"
4940 (fun () -> conf.trimmargins)
4941 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4943 src#bool "persistent location"
4944 (fun () -> conf.jumpback)
4945 (fun v -> conf.jumpback <- v);
4947 sep ();
4948 src#int "inter-page space"
4949 (fun () -> conf.interpagespace)
4950 (fun n ->
4951 conf.interpagespace <- n;
4952 docolumns conf.columns;
4953 let pageno, py =
4954 match state.layout with
4955 | [] -> 0, 0
4956 | l :: _ ->
4957 l.pageno, l.pagey
4959 state.maxy <- calcheight ();
4960 let y = getpagey pageno in
4961 gotoy (y + py)
4964 src#int "page bias"
4965 (fun () -> conf.pagebias)
4966 (fun v -> conf.pagebias <- v);
4968 src#int "scroll step"
4969 (fun () -> conf.scrollstep)
4970 (fun n -> conf.scrollstep <- n);
4972 src#int "horizontal scroll step"
4973 (fun () -> conf.hscrollstep)
4974 (fun v -> conf.hscrollstep <- v);
4976 src#int "auto scroll step"
4977 (fun () ->
4978 match state.autoscroll with
4979 | Some step -> step
4980 | _ -> conf.autoscrollstep)
4981 (fun n ->
4982 let n = boundastep state.winh n in
4983 if state.autoscroll <> None
4984 then state.autoscroll <- Some n;
4985 conf.autoscrollstep <- n);
4987 src#int "zoom"
4988 (fun () -> truncate (conf.zoom *. 100.))
4989 (fun v -> setzoom ((float v) /. 100.));
4991 src#int "rotation"
4992 (fun () -> conf.angle)
4993 (fun v -> reqlayout v conf.fitmodel);
4995 src#int "scroll bar width"
4996 (fun () -> conf.scrollbw)
4997 (fun v ->
4998 conf.scrollbw <- v;
4999 reshape state.winw state.winh;
5002 src#int "scroll handle height"
5003 (fun () -> conf.scrollh)
5004 (fun v -> conf.scrollh <- v;);
5006 src#int "thumbnail width"
5007 (fun () -> conf.thumbw)
5008 (fun v ->
5009 conf.thumbw <- min 4096 v;
5010 match oldmode with
5011 | Birdseye beye ->
5012 leavebirdseye beye false;
5013 enterbirdseye ()
5014 | _ -> ()
5017 let mode = state.mode in
5018 src#string "columns"
5019 (fun () ->
5020 match conf.columns with
5021 | Csingle _ -> "1"
5022 | Cmulti (multi, _) -> multicolumns_to_string multi
5023 | Csplit (count, _) -> "-" ^ string_of_int count
5025 (fun v ->
5026 let n, a, b = multicolumns_of_string v in
5027 setcolumns mode n a b);
5029 sep ();
5030 src#caption "Pixmap cache" 0;
5031 src#int_with_suffix "size (advisory)"
5032 (fun () -> conf.memlimit)
5033 (fun v -> conf.memlimit <- v);
5035 src#caption2 "used"
5036 (fun () -> Printf.sprintf "%s bytes, %d tiles"
5037 (string_with_suffix_of_int state.memused)
5038 (Hashtbl.length state.tilemap)) 1;
5040 sep ();
5041 src#caption "Layout" 0;
5042 src#caption2 "Dimension"
5043 (fun () ->
5044 Printf.sprintf "%dx%d (virtual %dx%d)"
5045 state.winw state.winh
5046 state.w state.maxy)
5048 if conf.debug
5049 then
5050 src#caption2 "Position" (fun () ->
5051 Printf.sprintf "%dx%d" state.x state.y
5053 else
5054 src#caption2 "Position" (fun () -> describe_location ()) 1
5057 sep ();
5058 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
5059 "Save these parameters as global defaults at exit"
5060 (fun () -> conf.bedefault)
5061 (fun v -> conf.bedefault <- v)
5064 sep ();
5065 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
5066 src#bool ~offset:0 ~btos "Extended parameters"
5067 (fun () -> !showextended)
5068 (fun v -> showextended := v; fillsrc prevmode prevuioh);
5069 if !showextended
5070 then (
5071 src#bool "checkers"
5072 (fun () -> conf.checkers)
5073 (fun v -> conf.checkers <- v; setcheckers v);
5074 src#bool "update cursor"
5075 (fun () -> conf.updatecurs)
5076 (fun v -> conf.updatecurs <- v);
5077 src#bool "verbose"
5078 (fun () -> conf.verbose)
5079 (fun v -> conf.verbose <- v);
5080 src#bool "invert colors"
5081 (fun () -> conf.invert)
5082 (fun v -> conf.invert <- v);
5083 src#bool "max fit"
5084 (fun () -> conf.maxhfit)
5085 (fun v -> conf.maxhfit <- v);
5086 src#bool "redirect stderr"
5087 (fun () -> conf.redirectstderr)
5088 (fun v -> conf.redirectstderr <- v; redirectstderr ());
5089 src#bool "pax mode"
5090 (fun () -> conf.pax != None)
5091 (fun v ->
5092 if v
5093 then conf.pax <- Some (ref (now (), 0, 0))
5094 else conf.pax <- None);
5095 src#string "uri launcher"
5096 (fun () -> conf.urilauncher)
5097 (fun v -> conf.urilauncher <- v);
5098 src#string "path launcher"
5099 (fun () -> conf.pathlauncher)
5100 (fun v -> conf.pathlauncher <- v);
5101 src#string "tile size"
5102 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
5103 (fun v ->
5105 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
5106 conf.tilew <- max 64 w;
5107 conf.tileh <- max 64 h;
5108 flushtiles ();
5109 with exn ->
5110 state.text <- Printf.sprintf "bad tile size `%s': %s"
5111 v (exntos exn)
5113 src#int "texture count"
5114 (fun () -> conf.texcount)
5115 (fun v ->
5116 if realloctexts v
5117 then conf.texcount <- v
5118 else showtext '!' " Failed to set texture count please retry later"
5120 src#int "slice height"
5121 (fun () -> conf.sliceheight)
5122 (fun v ->
5123 conf.sliceheight <- v;
5124 wcmd "sliceh %d" conf.sliceheight;
5126 src#int "anti-aliasing level"
5127 (fun () -> conf.aalevel)
5128 (fun v ->
5129 conf.aalevel <- bound v 0 8;
5130 state.anchor <- getanchor ();
5131 opendoc state.path state.password;
5133 src#string "page scroll scaling factor"
5134 (fun () -> string_of_float conf.pgscale)
5135 (fun v ->
5137 let s = float_of_string v in
5138 conf.pgscale <- s
5139 with exn ->
5140 state.text <- Printf.sprintf
5141 "bad page scroll scaling factor `%s': %s" v (exntos exn)
5144 src#int "ui font size"
5145 (fun () -> fstate.fontsize)
5146 (fun v -> setfontsize (bound v 5 100));
5147 src#int "hint font size"
5148 (fun () -> conf.hfsize)
5149 (fun v -> conf.hfsize <- bound v 5 100);
5150 colorp "background color"
5151 (fun () -> conf.bgcolor)
5152 (fun v -> conf.bgcolor <- v);
5153 src#bool "crop hack"
5154 (fun () -> conf.crophack)
5155 (fun v -> conf.crophack <- v);
5156 src#string "trim fuzz"
5157 (fun () -> irect_to_string conf.trimfuzz)
5158 (fun v ->
5160 conf.trimfuzz <- irect_of_string v;
5161 if conf.trimmargins
5162 then settrim true conf.trimfuzz;
5163 with exn ->
5164 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
5166 src#string "throttle"
5167 (fun () ->
5168 match conf.maxwait with
5169 | None -> "show place holder if page is not ready"
5170 | Some time ->
5171 if time = infinity
5172 then "wait for page to fully render"
5173 else
5174 "wait " ^ string_of_float time
5175 ^ " seconds before showing placeholder"
5177 (fun v ->
5179 let f = float_of_string v in
5180 if f <= 0.0
5181 then conf.maxwait <- None
5182 else conf.maxwait <- Some f
5183 with exn ->
5184 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
5186 src#string "ghyll scroll"
5187 (fun () ->
5188 match conf.ghyllscroll with
5189 | None -> ""
5190 | Some nab -> ghyllscroll_to_string nab
5192 (fun v ->
5193 try conf.ghyllscroll <- ghyllscroll_of_string v
5194 with exn ->
5195 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
5197 src#string "selection command"
5198 (fun () -> conf.selcmd)
5199 (fun v -> conf.selcmd <- v);
5200 src#string "synctex command"
5201 (fun () -> conf.stcmd)
5202 (fun v -> conf.stcmd <- v);
5203 src#string "pax command"
5204 (fun () -> conf.paxcmd)
5205 (fun v -> conf.paxcmd <- v);
5206 src#colorspace "color space"
5207 (fun () -> CSTE.to_string conf.colorspace)
5208 (fun v ->
5209 conf.colorspace <- CSTE.of_int v;
5210 wcmd "cs %d" v;
5211 load state.layout;
5213 src#paxmark "pax mark method"
5214 (fun () -> MTE.to_string conf.paxmark)
5215 (fun v -> conf.paxmark <- MTE.of_int v);
5216 if pbousable ()
5217 then
5218 src#bool "use PBO"
5219 (fun () -> conf.usepbo)
5220 (fun v -> conf.usepbo <- v);
5221 src#bool "mouse wheel scrolls pages"
5222 (fun () -> conf.wheelbypage)
5223 (fun v -> conf.wheelbypage <- v);
5224 src#bool "open remote links in a new instance"
5225 (fun () -> conf.riani)
5226 (fun v -> conf.riani <- v);
5229 sep ();
5230 src#caption "Document" 0;
5231 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
5232 src#caption2 "Pages"
5233 (fun () -> string_of_int state.pagecount) 1;
5234 src#caption2 "Dimensions"
5235 (fun () -> string_of_int (List.length state.pdims)) 1;
5236 if conf.trimmargins
5237 then (
5238 sep ();
5239 src#caption "Trimmed margins" 0;
5240 src#caption2 "Dimensions"
5241 (fun () -> string_of_int (List.length state.pdims)) 1;
5244 sep ();
5245 src#caption "OpenGL" 0;
5246 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
5247 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
5249 sep ();
5250 src#caption "Location" 0;
5251 if nonemptystr state.origin
5252 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
5253 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
5255 src#reset prevmode prevuioh;
5257 fun () ->
5258 state.text <- "";
5259 resetmstate ();
5260 let prevmode = state.mode
5261 and prevuioh = state.uioh in
5262 fillsrc prevmode prevuioh;
5263 let source = (src :> lvsource) in
5264 let modehash = findkeyhash conf "info" in
5265 state.uioh <- coe (object (self)
5266 inherit listview ~source ~trusted:true ~modehash as super
5267 val mutable m_prevmemused = 0
5268 method infochanged = function
5269 | Memused ->
5270 if m_prevmemused != state.memused
5271 then (
5272 m_prevmemused <- state.memused;
5273 G.postRedisplay "memusedchanged";
5275 | Pdim -> G.postRedisplay "pdimchanged"
5276 | Docinfo -> fillsrc prevmode prevuioh
5278 method key key mask =
5279 if not (Wsi.withctrl mask)
5280 then
5281 match key with
5282 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
5283 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
5284 | _ -> super#key key mask
5285 else super#key key mask
5286 end);
5287 G.postRedisplay "info";
5290 let enterhelpmode =
5291 let source =
5292 (object
5293 inherit lvsourcebase
5294 method getitemcount = Array.length state.help
5295 method getitem n =
5296 let s, l, _ = state.help.(n) in
5297 (s, l)
5299 method exit ~uioh ~cancel ~active ~first ~pan =
5300 let optuioh =
5301 if not cancel
5302 then (
5303 match state.help.(active) with
5304 | _, _, Action f -> Some (f uioh)
5305 | _ -> Some (uioh)
5307 else None
5309 m_active <- active;
5310 m_first <- first;
5311 m_pan <- pan;
5312 optuioh
5314 method hasaction n =
5315 match state.help.(n) with
5316 | _, _, Action _ -> true
5317 | _ -> false
5319 initializer
5320 m_active <- -1
5321 end)
5322 in fun () ->
5323 let modehash = findkeyhash conf "help" in
5324 resetmstate ();
5325 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
5326 G.postRedisplay "help";
5329 let entermsgsmode =
5330 let msgsource =
5331 let re = Str.regexp "[\r\n]" in
5332 (object
5333 inherit lvsourcebase
5334 val mutable m_items = [||]
5336 method getitemcount = 1 + Array.length m_items
5338 method getitem n =
5339 if n = 0
5340 then "[Clear]", 0
5341 else m_items.(n-1), 0
5343 method exit ~uioh ~cancel ~active ~first ~pan =
5344 ignore uioh;
5345 if not cancel
5346 then (
5347 if active = 0
5348 then Buffer.clear state.errmsgs;
5350 m_active <- active;
5351 m_first <- first;
5352 m_pan <- pan;
5353 None
5355 method hasaction n =
5356 n = 0
5358 method reset =
5359 state.newerrmsgs <- false;
5360 let l = Str.split re (Buffer.contents state.errmsgs) in
5361 m_items <- Array.of_list l
5363 initializer
5364 m_active <- 0
5365 end)
5366 in fun () ->
5367 state.text <- "";
5368 resetmstate ();
5369 msgsource#reset;
5370 let source = (msgsource :> lvsource) in
5371 let modehash = findkeyhash conf "listview" in
5372 state.uioh <- coe (object
5373 inherit listview ~source ~trusted:false ~modehash as super
5374 method display =
5375 if state.newerrmsgs
5376 then msgsource#reset;
5377 super#display
5378 end);
5379 G.postRedisplay "msgs";
5382 let quickbookmark ?title () =
5383 match state.layout with
5384 | [] -> ()
5385 | l :: _ ->
5386 let title =
5387 match title with
5388 | None ->
5389 let sec = Unix.gettimeofday () in
5390 let tm = Unix.localtime sec in
5391 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
5392 (l.pageno+1)
5393 tm.Unix.tm_mday
5394 tm.Unix.tm_mon
5395 (tm.Unix.tm_year + 1900)
5396 tm.Unix.tm_hour
5397 tm.Unix.tm_min
5398 | Some title -> title
5400 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5403 let setautoscrollspeed step goingdown =
5404 let incr = max 1 ((abs step) / 2) in
5405 let incr = if goingdown then incr else -incr in
5406 let astep = boundastep state.winh (step + incr) in
5407 state.autoscroll <- Some astep;
5410 let canpan () =
5411 match conf.columns with
5412 | Csplit _ -> true
5413 | _ -> state.x != 0 || conf.zoom > 1.0
5416 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5418 let existsinrow pageno (columns, coverA, coverB) p =
5419 let last = ((pageno - coverA) mod columns) + columns in
5420 let rec any = function
5421 | [] -> false
5422 | l :: rest ->
5423 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5424 then p l
5425 else (
5426 if not (p l)
5427 then (if l.pageno = last then false else any rest)
5428 else true
5431 any state.layout
5434 let nextpage () =
5435 match state.layout with
5436 | [] ->
5437 let pageno = page_of_y state.y in
5438 gotoghyll (getpagey (pageno+1))
5439 | l :: rest ->
5440 match conf.columns with
5441 | Csingle _ ->
5442 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5443 then
5444 let y = clamp (pgscale state.winh) in
5445 gotoghyll y
5446 else
5447 let pageno = min (l.pageno+1) (state.pagecount-1) in
5448 gotoghyll (getpagey pageno)
5449 | Cmulti ((c, _, _) as cl, _) ->
5450 if conf.presentation
5451 && (existsinrow l.pageno cl
5452 (fun l -> l.pageh > l.pagey + l.pagevh))
5453 then
5454 let y = clamp (pgscale state.winh) in
5455 gotoghyll y
5456 else
5457 let pageno = min (l.pageno+c) (state.pagecount-1) in
5458 gotoghyll (getpagey pageno)
5459 | Csplit (n, _) ->
5460 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5461 then
5462 let pagey, pageh = getpageyh l.pageno in
5463 let pagey = pagey + pageh * l.pagecol in
5464 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5465 gotoghyll (pagey + pageh + ips)
5468 let prevpage () =
5469 match state.layout with
5470 | [] ->
5471 let pageno = page_of_y state.y in
5472 gotoghyll (getpagey (pageno-1))
5473 | l :: _ ->
5474 match conf.columns with
5475 | Csingle _ ->
5476 if conf.presentation && l.pagey != 0
5477 then
5478 gotoghyll (clamp (pgscale ~-(state.winh)))
5479 else
5480 let pageno = max 0 (l.pageno-1) in
5481 gotoghyll (getpagey pageno)
5482 | Cmulti ((c, _, coverB) as cl, _) ->
5483 if conf.presentation &&
5484 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5485 then
5486 gotoghyll (clamp (pgscale ~-(state.winh)))
5487 else
5488 let decr =
5489 if l.pageno = state.pagecount - coverB
5490 then 1
5491 else c
5493 let pageno = max 0 (l.pageno-decr) in
5494 gotoghyll (getpagey pageno)
5495 | Csplit (n, _) ->
5496 let y =
5497 if l.pagecol = 0
5498 then
5499 if l.pageno = 0
5500 then l.pagey
5501 else
5502 let pageno = max 0 (l.pageno-1) in
5503 let pagey, pageh = getpageyh pageno in
5504 pagey + (n-1)*pageh
5505 else
5506 let pagey, pageh = getpageyh l.pageno in
5507 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5509 gotoghyll y
5512 let viewkeyboard key mask =
5513 let enttext te =
5514 let mode = state.mode in
5515 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5516 state.text <- "";
5517 enttext ();
5518 G.postRedisplay "view:enttext"
5520 let ctrl = Wsi.withctrl mask in
5521 let key =
5522 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5524 match key with
5525 | 81 -> (* Q *)
5526 exit 0
5528 | 0xff63 -> (* insert *)
5529 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5530 then (
5531 state.mode <- LinkNav (Ltgendir 0);
5532 gotoy state.y;
5534 else showtext '!' "Keyboard link navigation does not work under rotation"
5536 | 0xff1b | 113 -> (* escape / q *)
5537 begin match state.mstate with
5538 | Mzoomrect _ ->
5539 resetmstate ();
5540 G.postRedisplay "kill zoom rect";
5541 | _ ->
5542 begin match state.mode with
5543 | LinkNav _ ->
5544 state.mode <- View;
5545 G.postRedisplay "esc leave linknav"
5546 | _ ->
5547 match state.ranchors with
5548 | [] -> raise Quit
5549 | (path, password, anchor, origin) :: rest ->
5550 state.ranchors <- rest;
5551 state.anchor <- anchor;
5552 state.origin <- origin;
5553 state.nameddest <- "";
5554 opendoc path password
5555 end;
5556 end;
5558 | 0xff08 -> (* backspace *)
5559 gotoghyll (getnav ~-1)
5561 | 111 -> (* o *)
5562 enteroutlinemode ()
5564 | 117 -> (* u *)
5565 state.rects <- [];
5566 state.text <- "";
5567 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
5568 G.postRedisplay "dehighlight";
5570 | 47 | 63 -> (* / ? *)
5571 let ondone isforw s =
5572 cbput state.hists.pat s;
5573 state.searchpattern <- s;
5574 search s isforw
5576 let s = String.create 1 in
5577 s.[0] <- Char.chr key;
5578 enttext (s, "", Some (onhist state.hists.pat),
5579 textentry, ondone (key = 47), true)
5581 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5582 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5583 setzoom (conf.zoom +. incr)
5585 | 43 | 0xffab -> (* + *)
5586 let ondone s =
5587 let n =
5588 try int_of_string s with exc ->
5589 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5590 max_int
5592 if n != max_int
5593 then (
5594 conf.pagebias <- n;
5595 state.text <- "page bias is now " ^ string_of_int n;
5598 enttext ("page bias: ", "", None, intentry, ondone, true)
5600 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5601 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5602 setzoom (max 0.01 (conf.zoom -. decr))
5604 | 45 | 0xffad -> (* - *)
5605 let ondone msg = state.text <- msg in
5606 enttext (
5607 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5608 optentry state.mode, ondone, true
5611 | 48 when ctrl -> (* ctrl-0 *)
5612 if conf.zoom = 1.0
5613 then (
5614 state.x <- 0;
5615 gotoy state.y
5617 else setzoom 1.0
5619 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5620 let cols =
5621 match conf.columns with
5622 | Csingle _ | Cmulti _ -> 1
5623 | Csplit (n, _) -> n
5625 let h = state.winh -
5626 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5628 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5629 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5630 then setzoom zoom
5632 | 51 when ctrl -> (* ctrl-3 *)
5633 let fm =
5634 match conf.fitmodel with
5635 | FitWidth -> FitProportional
5636 | FitProportional -> FitPage
5637 | FitPage -> FitWidth
5639 state.text <- "fit model: " ^ FMTE.to_string fm;
5640 reqlayout conf.angle fm
5642 | 0xffc6 -> (* f9 *)
5643 togglebirdseye ()
5645 | 57 when ctrl -> (* ctrl-9 *)
5646 togglebirdseye ()
5648 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5649 when not ctrl -> (* 0..9 *)
5650 let ondone s =
5651 let n =
5652 try int_of_string s with exc ->
5653 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5656 if n >= 0
5657 then (
5658 addnav ();
5659 cbput state.hists.pag (string_of_int n);
5660 gotopage1 (n + conf.pagebias - 1) 0;
5663 let pageentry text key =
5664 match Char.unsafe_chr key with
5665 | 'g' -> TEdone text
5666 | _ -> intentry text key
5668 let text = "x" in text.[0] <- Char.chr key;
5669 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5671 | 98 -> (* b *)
5672 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5673 reshape state.winw state.winh;
5675 | 66 -> (* B *)
5676 state.bzoom <- not state.bzoom;
5677 state.rects <- [];
5678 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5680 | 108 -> (* l *)
5681 conf.hlinks <- not conf.hlinks;
5682 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5683 G.postRedisplay "toggle highlightlinks";
5685 | 70 -> (* F *)
5686 state.glinks <- true;
5687 let mode = state.mode in
5688 state.mode <- Textentry (
5689 (":", "", None, linknentry, linkndone gotounder, false),
5690 (fun _ ->
5691 state.glinks <- false;
5692 state.mode <- mode)
5694 state.text <- "";
5695 G.postRedisplay "view:linkent(F)"
5697 | 121 -> (* y *)
5698 state.glinks <- true;
5699 let mode = state.mode in
5700 state.mode <- Textentry (
5702 ":", "", None, linknentry, linkndone (fun under ->
5703 selstring (undertext under);
5704 ), false
5706 fun _ ->
5707 state.glinks <- false;
5708 state.mode <- mode
5710 state.text <- "";
5711 G.postRedisplay "view:linkent"
5713 | 97 -> (* a *)
5714 begin match state.autoscroll with
5715 | Some step ->
5716 conf.autoscrollstep <- step;
5717 state.autoscroll <- None
5718 | None ->
5719 if conf.autoscrollstep = 0
5720 then state.autoscroll <- Some 1
5721 else state.autoscroll <- Some conf.autoscrollstep
5724 | 112 when ctrl -> (* ctrl-p *)
5725 launchpath ()
5727 | 80 -> (* P *)
5728 setpresentationmode (not conf.presentation);
5729 showtext ' ' ("presentation mode " ^
5730 if conf.presentation then "on" else "off");
5732 | 102 -> (* f *)
5733 if List.mem Wsi.Fullscreen state.winstate
5734 then Wsi.reshape conf.cwinw conf.cwinh
5735 else Wsi.fullscreen ()
5737 | 112 | 78 -> (* p|N *)
5738 search state.searchpattern false
5740 | 110 | 0xffc0 -> (* n|F3 *)
5741 search state.searchpattern true
5743 | 116 -> (* t *)
5744 begin match state.layout with
5745 | [] -> ()
5746 | l :: _ ->
5747 gotoghyll (getpagey l.pageno)
5750 | 32 -> (* space *)
5751 nextpage ()
5753 | 0xff9f | 0xffff -> (* delete *)
5754 prevpage ()
5756 | 61 -> (* = *)
5757 showtext ' ' (describe_location ());
5759 | 119 -> (* w *)
5760 begin match state.layout with
5761 | [] -> ()
5762 | l :: _ ->
5763 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5764 G.postRedisplay "w"
5767 | 39 -> (* ' *)
5768 enterbookmarkmode ()
5770 | 104 | 0xffbe -> (* h|F1 *)
5771 enterhelpmode ()
5773 | 105 -> (* i *)
5774 enterinfomode ()
5776 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5777 entermsgsmode ()
5779 | 109 -> (* m *)
5780 let ondone s =
5781 match state.layout with
5782 | l :: _ ->
5783 if nonemptystr s
5784 then
5785 state.bookmarks <-
5786 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5787 | _ -> ()
5789 enttext ("bookmark: ", "", None, textentry, ondone, true)
5791 | 126 -> (* ~ *)
5792 quickbookmark ();
5793 showtext ' ' "Quick bookmark added";
5795 | 122 -> (* z *)
5796 begin match state.layout with
5797 | l :: _ ->
5798 let rect = getpdimrect l.pagedimno in
5799 let w, h =
5800 if conf.crophack
5801 then
5802 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5803 truncate (1.2 *. (rect.(3) -. rect.(0))))
5804 else
5805 (truncate (rect.(1) -. rect.(0)),
5806 truncate (rect.(3) -. rect.(0)))
5808 let w = truncate ((float w)*.conf.zoom)
5809 and h = truncate ((float h)*.conf.zoom) in
5810 if w != 0 && h != 0
5811 then (
5812 state.anchor <- getanchor ();
5813 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5815 G.postRedisplay "z";
5817 | [] -> ()
5820 | 120 -> (* x *)
5821 state.roam ()
5822 | 60 | 62 -> (* < > *)
5823 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5825 | 91 | 93 -> (* [ ] *)
5826 conf.colorscale <-
5827 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5829 G.postRedisplay "brightness";
5831 | 99 when state.mode = View -> (* [alt-]c *)
5832 if Wsi.withalt mask
5833 then (
5834 if conf.zoom > 1.0
5835 then
5836 let m = (wadjsb state.winw - state.w) / 2 in
5837 state.x <- m;
5838 gotoy_and_clear_text state.y
5840 else
5841 let (c, a, b), z =
5842 match state.prevcolumns with
5843 | None -> (1, 0, 0), 1.0
5844 | Some (columns, z) ->
5845 let cab =
5846 match columns with
5847 | Csplit (c, _) -> -c, 0, 0
5848 | Cmulti ((c, a, b), _) -> c, a, b
5849 | Csingle _ -> 1, 0, 0
5851 cab, z
5853 setcolumns View c a b;
5854 setzoom z
5856 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
5857 -> (* ctrl-shift- (kp) [up|down] *)
5858 let zoom, x = state.prevzoom in
5859 setzoom zoom;
5860 state.x <- x;
5862 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5863 begin match state.autoscroll with
5864 | None ->
5865 begin match state.mode with
5866 | Birdseye beye -> upbirdseye 1 beye
5867 | _ ->
5868 if ctrl
5869 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5870 else (
5871 if not (Wsi.withshift mask) && conf.presentation
5872 then prevpage ()
5873 else gotoghyll1 true (clamp (-conf.scrollstep))
5876 | Some n ->
5877 setautoscrollspeed n false
5880 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5881 begin match state.autoscroll with
5882 | None ->
5883 begin match state.mode with
5884 | Birdseye beye -> downbirdseye 1 beye
5885 | _ ->
5886 if ctrl
5887 then gotoy_and_clear_text (clamp (state.winh/2))
5888 else (
5889 if not (Wsi.withshift mask) && conf.presentation
5890 then nextpage ()
5891 else gotoghyll1 true (clamp (conf.scrollstep))
5894 | Some n ->
5895 setautoscrollspeed n true
5898 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5899 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5900 if canpan ()
5901 then
5902 let dx =
5903 if ctrl
5904 then state.winw / 2
5905 else conf.hscrollstep
5907 let dx = if key = 0xff51 || key = 0xff96 then dx else -dx in
5908 state.x <- panbound (state.x + dx);
5909 gotoy_and_clear_text state.y
5910 else (
5911 state.text <- "";
5912 G.postRedisplay "left/right"
5915 | 0xff55 | 0xff9a -> (* (kp) prior *)
5916 let y =
5917 if ctrl
5918 then
5919 match state.layout with
5920 | [] -> state.y
5921 | l :: _ -> state.y - l.pagey
5922 else
5923 clamp (pgscale (-state.winh))
5925 gotoghyll y
5927 | 0xff56 | 0xff9b -> (* (kp) next *)
5928 let y =
5929 if ctrl
5930 then
5931 match List.rev state.layout with
5932 | [] -> state.y
5933 | l :: _ -> getpagey l.pageno
5934 else
5935 clamp (pgscale state.winh)
5937 gotoghyll y
5939 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5940 gotoghyll 0
5941 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5942 gotoghyll (clamp state.maxy)
5944 | 0xff53 | 0xff98
5945 when Wsi.withalt mask -> (* alt-(kp) right *)
5946 gotoghyll (getnav 1)
5947 | 0xff51 | 0xff96
5948 when Wsi.withalt mask -> (* alt-(kp) left *)
5949 gotoghyll (getnav ~-1)
5951 | 114 -> (* r *)
5952 reload ()
5954 | 118 when conf.debug -> (* v *)
5955 state.rects <- [];
5956 List.iter (fun l ->
5957 match getopaque l.pageno with
5958 | None -> ()
5959 | Some opaque ->
5960 let x0, y0, x1, y1 = pagebbox opaque in
5961 let a,b = float x0, float y0 in
5962 let c,d = float x1, float y0 in
5963 let e,f = float x1, float y1 in
5964 let h,j = float x0, float y1 in
5965 let rect = (a,b,c,d,e,f,h,j) in
5966 debugrect rect;
5967 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5968 ) state.layout;
5969 G.postRedisplay "v";
5971 | 124 -> (* | *)
5972 let mode = state.mode in
5973 let cmd = ref "" in
5974 let onleave = function
5975 | Cancel -> state.mode <- mode
5976 | Confirm ->
5977 List.iter (fun l ->
5978 match getopaque l.pageno with
5979 | Some opaque -> pipesel opaque !cmd
5980 | None -> ()) state.layout;
5981 state.mode <- mode
5983 let ondone s =
5984 cbput state.hists.sel s;
5985 cmd := s
5987 let te =
5988 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5990 G.postRedisplay "|";
5991 state.mode <- Textentry (te, onleave);
5993 | _ ->
5994 vlog "huh? %s" (Wsi.keyname key)
5997 let linknavkeyboard key mask linknav =
5998 let getpage pageno =
5999 let rec loop = function
6000 | [] -> None
6001 | l :: _ when l.pageno = pageno -> Some l
6002 | _ :: rest -> loop rest
6003 in loop state.layout
6005 let doexact (pageno, n) =
6006 match getopaque pageno, getpage pageno with
6007 | Some opaque, Some l ->
6008 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
6009 then
6010 let under = getlink opaque n in
6011 G.postRedisplay "link gotounder";
6012 gotounder under;
6013 state.mode <- View;
6014 else
6015 let opt, dir =
6016 match key with
6017 | 0xff50 -> (* home *)
6018 Some (findlink opaque LDfirst), -1
6020 | 0xff57 -> (* end *)
6021 Some (findlink opaque LDlast), 1
6023 | 0xff51 -> (* left *)
6024 Some (findlink opaque (LDleft n)), -1
6026 | 0xff53 -> (* right *)
6027 Some (findlink opaque (LDright n)), 1
6029 | 0xff52 -> (* up *)
6030 Some (findlink opaque (LDup n)), -1
6032 | 0xff54 -> (* down *)
6033 Some (findlink opaque (LDdown n)), 1
6035 | _ -> None, 0
6037 let pwl l dir =
6038 begin match findpwl l.pageno dir with
6039 | Pwlnotfound -> ()
6040 | Pwl pageno ->
6041 let notfound dir =
6042 state.mode <- LinkNav (Ltgendir dir);
6043 let y, h = getpageyh pageno in
6044 let y =
6045 if dir < 0
6046 then y + h - state.winh
6047 else y
6049 gotoy y
6051 begin match getopaque pageno, getpage pageno with
6052 | Some opaque, Some _ ->
6053 let link =
6054 let ld = if dir > 0 then LDfirst else LDlast in
6055 findlink opaque ld
6057 begin match link with
6058 | Lfound m ->
6059 showlinktype (getlink opaque m);
6060 state.mode <- LinkNav (Ltexact (pageno, m));
6061 G.postRedisplay "linknav jpage";
6062 | _ -> notfound dir
6063 end;
6064 | _ -> notfound dir
6065 end;
6066 end;
6068 begin match opt with
6069 | Some Lnotfound -> pwl l dir;
6070 | Some (Lfound m) ->
6071 if m = n
6072 then pwl l dir
6073 else (
6074 let _, y0, _, y1 = getlinkrect opaque m in
6075 if y0 < l.pagey
6076 then gotopage1 l.pageno y0
6077 else (
6078 let d = fstate.fontsize + 1 in
6079 if y1 - l.pagey > l.pagevh - d
6080 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
6081 else G.postRedisplay "linknav";
6083 showlinktype (getlink opaque m);
6084 state.mode <- LinkNav (Ltexact (l.pageno, m));
6087 | None -> viewkeyboard key mask
6088 end;
6089 | _ -> viewkeyboard key mask
6091 if key = 0xff63
6092 then (
6093 state.mode <- View;
6094 G.postRedisplay "leave linknav"
6096 else
6097 match linknav with
6098 | Ltgendir _ -> viewkeyboard key mask
6099 | Ltexact exact -> doexact exact
6102 let keyboard key mask =
6103 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
6104 then wcmd "interrupt"
6105 else state.uioh <- state.uioh#key key mask
6108 let birdseyekeyboard key mask
6109 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
6110 let incr =
6111 match conf.columns with
6112 | Csingle _ -> 1
6113 | Cmulti ((c, _, _), _) -> c
6114 | Csplit _ -> failwith "bird's eye split mode"
6116 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
6117 match key with
6118 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
6119 let y, h = getpageyh pageno in
6120 let top = (state.winh - h) / 2 in
6121 gotoy (max 0 (y - top))
6122 | 0xff0d (* enter *)
6123 | 0xff8d -> leavebirdseye beye false (* kp enter *)
6124 | 0xff1b -> leavebirdseye beye true (* escape *)
6125 | 0xff52 -> upbirdseye incr beye (* up *)
6126 | 0xff54 -> downbirdseye incr beye (* down *)
6127 | 0xff51 -> upbirdseye 1 beye (* left *)
6128 | 0xff53 -> downbirdseye 1 beye (* right *)
6130 | 0xff55 -> (* prior *)
6131 begin match state.layout with
6132 | l :: _ ->
6133 if l.pagey != 0
6134 then (
6135 state.mode <- Birdseye (
6136 oconf, leftx, l.pageno, hooverpageno, anchor
6138 gotopage1 l.pageno 0;
6140 else (
6141 let layout = layout (state.y-state.winh) (pgh state.layout) in
6142 match layout with
6143 | [] -> gotoy (clamp (-state.winh))
6144 | l :: _ ->
6145 state.mode <- Birdseye (
6146 oconf, leftx, l.pageno, hooverpageno, anchor
6148 gotopage1 l.pageno 0
6151 | [] -> gotoy (clamp (-state.winh))
6152 end;
6154 | 0xff56 -> (* next *)
6155 begin match List.rev state.layout with
6156 | l :: _ ->
6157 let layout = layout (state.y + (pgh state.layout)) state.winh in
6158 begin match layout with
6159 | [] ->
6160 let incr = l.pageh - l.pagevh in
6161 if incr = 0
6162 then (
6163 state.mode <-
6164 Birdseye (
6165 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
6167 G.postRedisplay "birdseye pagedown";
6169 else gotoy (clamp (incr + conf.interpagespace*2));
6171 | l :: _ ->
6172 state.mode <-
6173 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
6174 gotopage1 l.pageno 0;
6177 | [] -> gotoy (clamp state.winh)
6178 end;
6180 | 0xff50 -> (* home *)
6181 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
6182 gotopage1 0 0
6184 | 0xff57 -> (* end *)
6185 let pageno = state.pagecount - 1 in
6186 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
6187 if not (pagevisible state.layout pageno)
6188 then
6189 let h =
6190 match List.rev state.pdims with
6191 | [] -> state.winh
6192 | (_, _, h, _) :: _ -> h
6194 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
6195 else G.postRedisplay "birdseye end";
6196 | _ -> viewkeyboard key mask
6199 let drawpage l =
6200 let color =
6201 match state.mode with
6202 | Textentry _ -> scalecolor 0.4
6203 | LinkNav _
6204 | View -> scalecolor 1.0
6205 | Birdseye (_, _, pageno, hooverpageno, _) ->
6206 if l.pageno = hooverpageno
6207 then scalecolor 0.9
6208 else (
6209 if l.pageno = pageno
6210 then scalecolor 1.0
6211 else scalecolor 0.8
6214 drawtiles l color;
6217 let postdrawpage l linkindexbase =
6218 match getopaque l.pageno with
6219 | Some opaque ->
6220 if tileready l l.pagex l.pagey
6221 then
6222 let x = l.pagedispx - l.pagex
6223 and y = l.pagedispy - l.pagey in
6224 let hlmask =
6225 match conf.columns with
6226 | Csingle _ | Cmulti _ ->
6227 (if conf.hlinks then 1 else 0)
6228 + (if state.glinks
6229 && not (isbirdseye state.mode) then 2 else 0)
6230 | _ -> 0
6232 let s =
6233 match state.mode with
6234 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
6235 | _ -> ""
6237 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
6238 else 0
6239 | _ -> 0
6242 let scrollindicator () =
6243 let sbw, ph, sh = state.uioh#scrollph in
6244 let sbh, pw, sw = state.uioh#scrollpw in
6246 GlDraw.color (0.64, 0.64, 0.64);
6247 filledrect
6248 (float (state.winw - sbw)) 0.
6249 (float state.winw) (float state.winh)
6251 filledrect
6252 0. (float (state.winh - sbh))
6253 (float (wadjsb state.winw - 1)) (float state.winh)
6255 GlDraw.color (0.0, 0.0, 0.0);
6257 filledrect
6258 (float (state.winw - sbw)) ph
6259 (float state.winw) (ph +. sh)
6261 filledrect
6262 pw (float (state.winh - sbh))
6263 (pw +. sw) (float state.winh)
6267 let showsel () =
6268 match state.mstate with
6269 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
6272 | Msel ((x0, y0), (x1, y1)) ->
6273 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
6274 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< "", -1, 0, 0) in
6275 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< "", -1, 0, 0) in
6276 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
6279 let showrects = function [] -> () | rects ->
6280 Gl.enable `blend;
6281 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
6282 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6283 List.iter
6284 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
6285 List.iter (fun l ->
6286 if l.pageno = pageno
6287 then (
6288 let dx = float (l.pagedispx - l.pagex) in
6289 let dy = float (l.pagedispy - l.pagey) in
6290 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
6291 Raw.sets_float state.vraw ~pos:0
6292 [| x0+.dx; y0+.dy;
6293 x1+.dx; y1+.dy;
6294 x3+.dx; y3+.dy;
6295 x2+.dx; y2+.dy |];
6296 GlArray.vertex `two state.vraw;
6297 GlArray.draw_arrays `triangle_strip 0 4;
6299 ) state.layout
6300 ) rects
6302 Gl.disable `blend;
6305 let display () =
6306 GlClear.color (scalecolor2 conf.bgcolor);
6307 GlClear.clear [`color];
6308 List.iter drawpage state.layout;
6309 let rects =
6310 match state.mode with
6311 | LinkNav (Ltexact (pageno, linkno)) ->
6312 begin match getopaque pageno with
6313 | Some opaque ->
6314 let x0, y0, x1, y1 = getlinkrect opaque linkno in
6315 (pageno, 5, (
6316 float x0, float y0,
6317 float x1, float y0,
6318 float x1, float y1,
6319 float x0, float y1)
6320 ) :: state.rects
6321 | None -> state.rects
6323 | _ -> state.rects
6325 showrects rects;
6326 let rec postloop linkindexbase = function
6327 | l :: rest ->
6328 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
6329 postloop linkindexbase rest
6330 | [] -> ()
6332 showsel ();
6333 postloop 0 state.layout;
6334 state.uioh#display;
6335 begin match state.mstate with
6336 | Mzoomrect ((x0, y0), (x1, y1)) ->
6337 Gl.enable `blend;
6338 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
6339 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6340 filledrect (float x0) (float y0) (float x1) (float y1);
6341 Gl.disable `blend;
6342 | _ -> ()
6343 end;
6344 enttext ();
6345 scrollindicator ();
6346 Wsi.swapb ();
6349 let zoomrect x y x1 y1 =
6350 let x0 = min x x1
6351 and x1 = max x x1
6352 and y0 = min y y1 in
6353 gotoy (state.y + y0);
6354 state.anchor <- getanchor ();
6355 let zoom = (float state.w) /. float (x1 - x0) in
6356 let margin =
6357 match conf.fitmodel, conf.columns with
6358 | FitPage, Csplit _ ->
6359 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
6361 | _, _ ->
6362 let adjw = wadjsb state.winw in
6363 if state.w < adjw
6364 then (adjw - state.w) / 2
6365 else 0
6367 state.x <- (state.x + margin) - x0;
6368 setzoom zoom;
6369 resetmstate ();
6372 let zoomblock x y =
6373 let g opaque l px py =
6374 match rectofblock opaque px py with
6375 | Some a ->
6376 let x0 = a.(0) -. 20. in
6377 let x1 = a.(1) +. 20. in
6378 let y0 = a.(2) -. 20. in
6379 let zoom = (float state.w) /. (x1 -. x0) in
6380 let pagey = getpagey l.pageno in
6381 gotoy_and_clear_text (pagey + truncate y0);
6382 state.anchor <- getanchor ();
6383 let margin = (state.w - l.pagew)/2 in
6384 state.x <- -truncate x0 - margin;
6385 setzoom zoom;
6386 None
6387 | None -> None
6389 match conf.columns with
6390 | Csplit _ ->
6391 showtext '!' "block zooming does not work properly in split columns mode"
6392 | _ -> onppundermouse g x y ()
6395 let scrollx x =
6396 let winw = wadjsb state.winw - 1 in
6397 let s = float x /. float winw in
6398 let destx = truncate (float (state.w + winw) *. s) in
6399 state.x <- winw - destx;
6400 gotoy_and_clear_text state.y;
6401 state.mstate <- Mscrollx;
6404 let scrolly y =
6405 let s = float y /. float state.winh in
6406 let desty = truncate (float (state.maxy - state.winh) *. s) in
6407 gotoy_and_clear_text desty;
6408 state.mstate <- Mscrolly;
6411 let viewmulticlick clicks x y mask =
6412 let g opaque l px py =
6413 let mark =
6414 match clicks with
6415 | 2 -> Mark_word
6416 | 3 -> Mark_line
6417 | 4 -> Mark_block
6418 | _ -> Mark_page
6420 if markunder opaque px py mark
6421 then (
6422 Some (fun () ->
6423 let dopipe cmd =
6424 match getopaque l.pageno with
6425 | None -> ()
6426 | Some opaque -> pipesel opaque cmd
6428 state.roam <- (fun () -> dopipe conf.paxcmd);
6429 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
6432 else None
6434 G.postRedisplay "viewmulticlick";
6435 onppundermouse g x y (fun () -> showtext '!' "Nothing to select") ();
6438 let canselect () =
6439 match conf.columns with
6440 | Csplit _ -> false
6441 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
6444 let viewmouse button down x y mask =
6445 match button with
6446 | n when (n == 4 || n == 5) && not down ->
6447 if Wsi.withctrl mask
6448 then (
6449 match state.mstate with
6450 | Mzoom (oldn, i) ->
6451 if oldn = n
6452 then (
6453 if i = 2
6454 then
6455 let incr =
6456 match n with
6457 | 5 ->
6458 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6459 | _ ->
6460 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6462 let zoom = conf.zoom -. incr in
6463 setzoom zoom;
6464 state.mstate <- Mzoom (n, 0);
6465 else
6466 state.mstate <- Mzoom (n, i+1);
6468 else state.mstate <- Mzoom (n, 0)
6470 | _ -> state.mstate <- Mzoom (n, 0)
6472 else (
6473 match state.autoscroll with
6474 | Some step -> setautoscrollspeed step (n=4)
6475 | None ->
6476 if conf.wheelbypage || conf.presentation
6477 then (
6478 if n = 4
6479 then prevpage ()
6480 else nextpage ()
6482 else
6483 let incr =
6484 if n = 4
6485 then -conf.scrollstep
6486 else conf.scrollstep
6488 let incr = incr * 2 in
6489 let y = clamp incr in
6490 gotoy_and_clear_text y
6493 | n when (n = 6 || n = 7) && not down && canpan () ->
6494 state.x <-
6495 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6496 gotoy_and_clear_text state.y
6498 | 1 when Wsi.withshift mask ->
6499 state.mstate <- Mnone;
6500 if not down
6501 then (
6502 match unproject x y with
6503 | Some (pageno, ux, uy) ->
6504 let cmd = Printf.sprintf
6505 "%s %s %d %d %d"
6506 conf.stcmd state.path pageno ux uy
6508 popen cmd []
6509 | None -> ()
6512 | 1 when Wsi.withctrl mask ->
6513 if down
6514 then (
6515 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6516 state.mstate <- Mpan (x, y)
6518 else
6519 state.mstate <- Mnone
6521 | 3 ->
6522 if down
6523 then (
6524 Wsi.setcursor Wsi.CURSOR_CYCLE;
6525 let p = (x, y) in
6526 state.mstate <- Mzoomrect (p, p)
6528 else (
6529 match state.mstate with
6530 | Mzoomrect ((x0, y0), _) ->
6531 if abs (x-x0) > 10 && abs (y - y0) > 10
6532 then zoomrect x0 y0 x y
6533 else (
6534 resetmstate ();
6535 G.postRedisplay "kill accidental zoom rect";
6537 | _ ->
6538 resetmstate ()
6541 | 1 when x > state.winw - vscrollw () ->
6542 if down
6543 then
6544 let _, position, sh = state.uioh#scrollph in
6545 if y > truncate position && y < truncate (position +. sh)
6546 then state.mstate <- Mscrolly
6547 else scrolly y
6548 else
6549 state.mstate <- Mnone
6551 | 1 when y > state.winh - hscrollh () ->
6552 if down
6553 then
6554 let _, position, sw = state.uioh#scrollpw in
6555 if x > truncate position && x < truncate (position +. sw)
6556 then state.mstate <- Mscrollx
6557 else scrollx x
6558 else
6559 state.mstate <- Mnone
6561 | 1 when state.bzoom -> if not down then zoomblock x y
6563 | 1 ->
6564 let dest = if down then getunder x y else Unone in
6565 begin match dest with
6566 | Ulinkgoto _
6567 | Ulinkuri _
6568 | Uremote _ | Uremotedest _
6569 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6570 gotounder dest
6572 | Unone when down ->
6573 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6574 state.mstate <- Mpan (x, y);
6576 | Unone | Utext _ ->
6577 if down
6578 then (
6579 if canselect ()
6580 then (
6581 state.mstate <- Msel ((x, y), (x, y));
6582 G.postRedisplay "mouse select";
6585 else (
6586 match state.mstate with
6587 | Mnone -> ()
6589 | Mzoom _ | Mscrollx | Mscrolly ->
6590 state.mstate <- Mnone
6592 | Mzoomrect ((x0, y0), _) ->
6593 zoomrect x0 y0 x y
6595 | Mpan _ ->
6596 Wsi.setcursor Wsi.CURSOR_INHERIT;
6597 state.mstate <- Mnone
6599 | Msel ((x0, y0), (x1, y1)) ->
6600 let rec loop = function
6601 | [] -> ()
6602 | l :: rest ->
6603 let inside =
6604 let a0 = l.pagedispy in
6605 let a1 = a0 + l.pagevh in
6606 let b0 = l.pagedispx in
6607 let b1 = b0 + l.pagevw in
6608 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6609 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6611 if inside
6612 then
6613 match getopaque l.pageno with
6614 | Some opaque ->
6615 let dosel cmd () =
6616 match Ne.res Unix.pipe with
6617 | Ne.Exn exn ->
6618 showtext '!'
6619 (Printf.sprintf
6620 "can not create sel pipe: %s"
6621 (exntos exn));
6622 | Ne.Res (r, w) ->
6623 let clo what fd =
6624 Ne.clo fd (fun msg ->
6625 dolog "%s close failed: %s" what msg)
6627 let popened =
6628 try popen cmd [r, 0; w, -1]; true
6629 with exn ->
6630 dolog "can not execute %S: %s"
6631 cmd (exntos exn);
6632 false
6634 if popened
6635 then (
6636 copysel w opaque;
6637 G.postRedisplay "copysel";
6639 else clo "Msel pipe/w" w;
6640 clo "Msel pipe/r" r;
6642 dosel conf.selcmd ();
6643 state.roam <- dosel conf.paxcmd;
6644 | None -> ()
6645 else loop rest
6647 loop state.layout;
6648 resetmstate ();
6652 | _ -> ()
6655 let birdseyemouse button down x y mask
6656 (conf, leftx, _, hooverpageno, anchor) =
6657 match button with
6658 | 1 when down ->
6659 let rec loop = function
6660 | [] -> ()
6661 | l :: rest ->
6662 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6663 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6664 then (
6665 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6667 else loop rest
6669 loop state.layout
6670 | 3 -> ()
6671 | _ -> viewmouse button down x y mask
6674 let uioh = object
6675 method display = ()
6677 method key key mask =
6678 begin match state.mode with
6679 | Textentry textentry -> textentrykeyboard key mask textentry
6680 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6681 | View -> viewkeyboard key mask
6682 | LinkNav linknav -> linknavkeyboard key mask linknav
6683 end;
6684 state.uioh
6686 method button button bstate x y mask =
6687 begin match state.mode with
6688 | LinkNav _
6689 | View -> viewmouse button bstate x y mask
6690 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6691 | Textentry _ -> ()
6692 end;
6693 state.uioh
6695 method multiclick clicks x y mask =
6696 begin match state.mode with
6697 | LinkNav _
6698 | View -> viewmulticlick clicks x y mask
6699 | Birdseye _
6700 | Textentry _ -> ()
6701 end;
6702 state.uioh
6704 method motion x y =
6705 begin match state.mode with
6706 | Textentry _ -> ()
6707 | View | Birdseye _ | LinkNav _ ->
6708 match state.mstate with
6709 | Mzoom _ | Mnone -> ()
6711 | Mpan (x0, y0) ->
6712 let dx = x - x0
6713 and dy = y0 - y in
6714 state.mstate <- Mpan (x, y);
6715 if canpan ()
6716 then state.x <- panbound (state.x + dx);
6717 let y = clamp dy in
6718 gotoy_and_clear_text y
6720 | Msel (a, _) ->
6721 state.mstate <- Msel (a, (x, y));
6722 G.postRedisplay "motion select";
6724 | Mscrolly ->
6725 let y = min state.winh (max 0 y) in
6726 scrolly y
6728 | Mscrollx ->
6729 let x = min state.winw (max 0 x) in
6730 scrollx x
6732 | Mzoomrect (p0, _) ->
6733 state.mstate <- Mzoomrect (p0, (x, y));
6734 G.postRedisplay "motion zoomrect";
6735 end;
6736 state.uioh
6738 method pmotion x y =
6739 begin match state.mode with
6740 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6741 let rec loop = function
6742 | [] ->
6743 if hooverpageno != -1
6744 then (
6745 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6746 G.postRedisplay "pmotion birdseye no hoover";
6748 | l :: rest ->
6749 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6750 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6751 then (
6752 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6753 G.postRedisplay "pmotion birdseye hoover";
6755 else loop rest
6757 loop state.layout
6759 | Textentry _ -> ()
6761 | LinkNav _
6762 | View ->
6763 match state.mstate with
6764 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6766 | Mnone ->
6767 updateunder x y;
6768 if canselect ()
6769 then
6770 match conf.pax with
6771 | None -> ()
6772 | Some r ->
6773 let past, _, _ = !r in
6774 let now = now () in
6775 let delta = now -. past in
6776 if delta > 0.01
6777 then paxunder x y
6778 else r := (now, x, y)
6779 end;
6780 state.uioh
6782 method infochanged _ = ()
6784 method scrollph =
6785 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6786 let p, h =
6787 if maxy = 0
6788 then 0.0, float state.winh
6789 else scrollph state.y maxy
6791 vscrollw (), p, h
6793 method scrollpw =
6794 let winw = wadjsb state.winw in
6795 let fwinw = float winw in
6796 let sw =
6797 let sw = fwinw /. float state.w in
6798 let sw = fwinw *. sw in
6799 max sw (float conf.scrollh)
6801 let position =
6802 let maxx = state.w + winw in
6803 let x = winw - state.x in
6804 let percent = float x /. float maxx in
6805 (fwinw -. sw) *. percent
6807 hscrollh (), position, sw
6809 method modehash =
6810 let modename =
6811 match state.mode with
6812 | LinkNav _ -> "links"
6813 | Textentry _ -> "textentry"
6814 | Birdseye _ -> "birdseye"
6815 | View -> "view"
6817 findkeyhash conf modename
6819 method eformsgs = true
6820 end;;
6822 module Config =
6823 struct
6824 open Parser
6826 let fontpath = ref "";;
6828 module KeyMap =
6829 Map.Make (struct type t = (int * int) let compare = compare end);;
6831 let unent s =
6832 let l = String.length s in
6833 let b = Buffer.create l in
6834 unent b s 0 l;
6835 Buffer.contents b;
6838 let home =
6839 try Sys.getenv "HOME"
6840 with exn ->
6841 prerr_endline
6842 ("Can not determine home directory location: " ^ exntos exn);
6846 let modifier_of_string = function
6847 | "alt" -> Wsi.altmask
6848 | "shift" -> Wsi.shiftmask
6849 | "ctrl" | "control" -> Wsi.ctrlmask
6850 | "meta" -> Wsi.metamask
6851 | _ -> 0
6854 let key_of_string =
6855 let r = Str.regexp "-" in
6856 fun s ->
6857 let elems = Str.full_split r s in
6858 let f n k m =
6859 let g s =
6860 let m1 = modifier_of_string s in
6861 if m1 = 0
6862 then (Wsi.namekey s, m)
6863 else (k, m lor m1)
6864 in function
6865 | Str.Delim s when n land 1 = 0 -> g s
6866 | Str.Text s -> g s
6867 | Str.Delim _ -> (k, m)
6869 let rec loop n k m = function
6870 | [] -> (k, m)
6871 | x :: xs ->
6872 let k, m = f n k m x in
6873 loop (n+1) k m xs
6875 loop 0 0 0 elems
6878 let keys_of_string =
6879 let r = Str.regexp "[ \t]" in
6880 fun s ->
6881 let elems = Str.split r s in
6882 List.map key_of_string elems
6885 let copykeyhashes c =
6886 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6889 let config_of c attrs =
6890 let apply c k v =
6892 match k with
6893 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6894 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6895 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6896 | "preload" -> { c with preload = bool_of_string v }
6897 | "page-bias" -> { c with pagebias = int_of_string v }
6898 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6899 | "horizontal-scroll-step" ->
6900 { c with hscrollstep = max (int_of_string v) 1 }
6901 | "auto-scroll-step" ->
6902 { c with autoscrollstep = max 0 (int_of_string v) }
6903 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6904 | "crop-hack" -> { c with crophack = bool_of_string v }
6905 | "throttle" ->
6906 let mw =
6907 match String.lowercase v with
6908 | "true" -> Some infinity
6909 | "false" -> None
6910 | f -> Some (float_of_string f)
6912 { c with maxwait = mw}
6913 | "highlight-links" -> { c with hlinks = bool_of_string v }
6914 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6915 | "vertical-margin" ->
6916 { c with interpagespace = max 0 (int_of_string v) }
6917 | "zoom" ->
6918 let zoom = float_of_string v /. 100. in
6919 let zoom = max zoom 0.0 in
6920 { c with zoom = zoom }
6921 | "presentation" -> { c with presentation = bool_of_string v }
6922 | "rotation-angle" -> { c with angle = int_of_string v }
6923 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6924 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6925 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6926 | "proportional-display" ->
6927 let fm =
6928 if bool_of_string v
6929 then FitProportional
6930 else FitWidth
6932 { c with fitmodel = fm }
6933 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6934 | "pixmap-cache-size" ->
6935 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6936 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6937 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6938 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6939 | "persistent-location" -> { c with jumpback = bool_of_string v }
6940 | "background-color" -> { c with bgcolor = color_of_string v }
6941 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6942 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6943 | "mupdf-store-size" ->
6944 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6945 | "checkers" -> { c with checkers = bool_of_string v }
6946 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6947 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6948 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6949 | "uri-launcher" -> { c with urilauncher = unent v }
6950 | "path-launcher" -> { c with pathlauncher = unent v }
6951 | "color-space" -> { c with colorspace = CSTE.of_string v }
6952 | "invert-colors" -> { c with invert = bool_of_string v }
6953 | "brightness" -> { c with colorscale = float_of_string v }
6954 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6955 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
6956 | "columns" ->
6957 let (n, _, _) as nab = multicolumns_of_string v in
6958 if n < 0
6959 then { c with columns = Csplit (-n, [||]) }
6960 else { c with columns = Cmulti (nab, [||]) }
6961 | "birds-eye-columns" ->
6962 { c with beyecolumns = Some (max (int_of_string v) 2) }
6963 | "selection-command" -> { c with selcmd = unent v }
6964 | "synctex-command" -> { c with stcmd = unent v }
6965 | "pax-command" -> { c with paxcmd = unent v }
6966 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6967 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6968 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6969 | "use-pbo" -> { c with usepbo = bool_of_string v }
6970 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6971 | "horizontal-scrollbar-visible" ->
6972 let b =
6973 if bool_of_string v
6974 then c.scrollb lor scrollbhv
6975 else c.scrollb land (lnot scrollbhv)
6977 { c with scrollb = b }
6978 | "vertical-scrollbar-visible" ->
6979 let b =
6980 if bool_of_string v
6981 then c.scrollb lor scrollbvv
6982 else c.scrollb land (lnot scrollbvv)
6984 { c with scrollb = b }
6985 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6986 | "point-and-x" ->
6987 { c with pax =
6988 if bool_of_string v
6989 then Some (ref (0.0, 0, 0))
6990 else None }
6991 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
6992 | _ -> c
6993 with exn ->
6994 prerr_endline ("Error processing attribute (`" ^
6995 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6998 let rec fold c = function
6999 | [] -> c
7000 | (k, v) :: rest ->
7001 let c = apply c k v in
7002 fold c rest
7004 fold { c with keyhashes = copykeyhashes c } attrs;
7007 let fromstring f pos n v d =
7008 try f v
7009 with exn ->
7010 dolog "Error processing attribute (%S=%S) at %d\n%s"
7011 n v pos (exntos exn)
7016 let bookmark_of attrs =
7017 let rec fold title page rely visy = function
7018 | ("title", v) :: rest -> fold v page rely visy rest
7019 | ("page", v) :: rest -> fold title v rely visy rest
7020 | ("rely", v) :: rest -> fold title page v visy rest
7021 | ("visy", v) :: rest -> fold title page rely v rest
7022 | _ :: rest -> fold title page rely visy rest
7023 | [] -> title, page, rely, visy
7025 fold "invalid" "0" "0" "0" attrs
7028 let doc_of attrs =
7029 let rec fold path page rely pan visy = function
7030 | ("path", v) :: rest -> fold v page rely pan visy rest
7031 | ("page", v) :: rest -> fold path v rely pan visy rest
7032 | ("rely", v) :: rest -> fold path page v pan visy rest
7033 | ("pan", v) :: rest -> fold path page rely v visy rest
7034 | ("visy", v) :: rest -> fold path page rely pan v rest
7035 | _ :: rest -> fold path page rely pan visy rest
7036 | [] -> path, page, rely, pan, visy
7038 fold "" "0" "0" "0" "0" attrs
7041 let map_of attrs =
7042 let rec fold rs ls = function
7043 | ("out", v) :: rest -> fold v ls rest
7044 | ("in", v) :: rest -> fold rs v rest
7045 | _ :: rest -> fold ls rs rest
7046 | [] -> ls, rs
7048 fold "" "" attrs
7051 let setconf dst src =
7052 dst.scrollbw <- src.scrollbw;
7053 dst.scrollh <- src.scrollh;
7054 dst.icase <- src.icase;
7055 dst.preload <- src.preload;
7056 dst.pagebias <- src.pagebias;
7057 dst.verbose <- src.verbose;
7058 dst.scrollstep <- src.scrollstep;
7059 dst.maxhfit <- src.maxhfit;
7060 dst.crophack <- src.crophack;
7061 dst.autoscrollstep <- src.autoscrollstep;
7062 dst.maxwait <- src.maxwait;
7063 dst.hlinks <- src.hlinks;
7064 dst.underinfo <- src.underinfo;
7065 dst.interpagespace <- src.interpagespace;
7066 dst.zoom <- src.zoom;
7067 dst.presentation <- src.presentation;
7068 dst.angle <- src.angle;
7069 dst.cwinw <- src.cwinw;
7070 dst.cwinh <- src.cwinh;
7071 dst.savebmarks <- src.savebmarks;
7072 dst.memlimit <- src.memlimit;
7073 dst.fitmodel <- src.fitmodel;
7074 dst.texcount <- src.texcount;
7075 dst.sliceheight <- src.sliceheight;
7076 dst.thumbw <- src.thumbw;
7077 dst.jumpback <- src.jumpback;
7078 dst.bgcolor <- src.bgcolor;
7079 dst.tilew <- src.tilew;
7080 dst.tileh <- src.tileh;
7081 dst.mustoresize <- src.mustoresize;
7082 dst.checkers <- src.checkers;
7083 dst.aalevel <- src.aalevel;
7084 dst.trimmargins <- src.trimmargins;
7085 dst.trimfuzz <- src.trimfuzz;
7086 dst.urilauncher <- src.urilauncher;
7087 dst.colorspace <- src.colorspace;
7088 dst.invert <- src.invert;
7089 dst.colorscale <- src.colorscale;
7090 dst.redirectstderr <- src.redirectstderr;
7091 dst.ghyllscroll <- src.ghyllscroll;
7092 dst.columns <- src.columns;
7093 dst.beyecolumns <- src.beyecolumns;
7094 dst.selcmd <- src.selcmd;
7095 dst.updatecurs <- src.updatecurs;
7096 dst.pathlauncher <- src.pathlauncher;
7097 dst.keyhashes <- copykeyhashes src;
7098 dst.hfsize <- src.hfsize;
7099 dst.hscrollstep <- src.hscrollstep;
7100 dst.pgscale <- src.pgscale;
7101 dst.usepbo <- src.usepbo;
7102 dst.wheelbypage <- src.wheelbypage;
7103 dst.stcmd <- src.stcmd;
7104 dst.paxcmd <- src.paxcmd;
7105 dst.scrollb <- src.scrollb;
7106 dst.riani <- src.riani;
7107 dst.paxmark <- src.paxmark;
7108 dst.pax <-
7109 if src.pax = None
7110 then None
7111 else Some ((ref (0.0, 0, 0)));
7114 let get s =
7115 let h = Hashtbl.create 10 in
7116 let dc = { defconf with angle = defconf.angle } in
7117 let rec toplevel v t spos _ =
7118 match t with
7119 | Vdata | Vcdata | Vend -> v
7120 | Vopen ("llppconfig", _, closed) ->
7121 if closed
7122 then v
7123 else { v with f = llppconfig }
7124 | Vopen _ ->
7125 error "unexpected subelement at top level" s spos
7126 | Vclose _ -> error "unexpected close at top level" s spos
7128 and llppconfig v t spos _ =
7129 match t with
7130 | Vdata | Vcdata -> v
7131 | Vend -> error "unexpected end of input in llppconfig" s spos
7132 | Vopen ("defaults", attrs, closed) ->
7133 let c = config_of dc attrs in
7134 setconf dc c;
7135 if closed
7136 then v
7137 else { v with f = defaults }
7139 | Vopen ("ui-font", attrs, closed) ->
7140 let rec getsize size = function
7141 | [] -> size
7142 | ("size", v) :: rest ->
7143 let size =
7144 fromstring int_of_string spos "size" v fstate.fontsize in
7145 getsize size rest
7146 | l -> getsize size l
7148 fstate.fontsize <- getsize fstate.fontsize attrs;
7149 if closed
7150 then v
7151 else { v with f = uifont (Buffer.create 10) }
7153 | Vopen ("doc", attrs, closed) ->
7154 let pathent, spage, srely, span, svisy = doc_of attrs in
7155 let path = unent pathent
7156 and pageno = fromstring int_of_string spos "page" spage 0
7157 and rely = fromstring float_of_string spos "rely" srely 0.0
7158 and pan = fromstring int_of_string spos "pan" span 0
7159 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7160 let c = config_of dc attrs in
7161 let anchor = (pageno, rely, visy) in
7162 if closed
7163 then (Hashtbl.add h path (c, [], pan, anchor); v)
7164 else { v with f = doc path pan anchor c [] }
7166 | Vopen _ ->
7167 error "unexpected subelement in llppconfig" s spos
7169 | Vclose "llppconfig" -> { v with f = toplevel }
7170 | Vclose _ -> error "unexpected close in llppconfig" s spos
7172 and defaults v t spos _ =
7173 match t with
7174 | Vdata | Vcdata -> v
7175 | Vend -> error "unexpected end of input in defaults" s spos
7176 | Vopen ("keymap", attrs, closed) ->
7177 let modename =
7178 try List.assoc "mode" attrs
7179 with Not_found -> "global" in
7180 if closed
7181 then v
7182 else
7183 let ret keymap =
7184 let h = findkeyhash dc modename in
7185 KeyMap.iter (Hashtbl.replace h) keymap;
7186 defaults
7188 { v with f = pkeymap ret KeyMap.empty }
7190 | Vopen (_, _, _) ->
7191 error "unexpected subelement in defaults" s spos
7193 | Vclose "defaults" ->
7194 { v with f = llppconfig }
7196 | Vclose _ -> error "unexpected close in defaults" s spos
7198 and uifont b v t spos epos =
7199 match t with
7200 | Vdata | Vcdata ->
7201 Buffer.add_substring b s spos (epos - spos);
7203 | Vopen (_, _, _) ->
7204 error "unexpected subelement in ui-font" s spos
7205 | Vclose "ui-font" ->
7206 if emptystr !fontpath
7207 then fontpath := Buffer.contents b;
7208 { v with f = llppconfig }
7209 | Vclose _ -> error "unexpected close in ui-font" s spos
7210 | Vend -> error "unexpected end of input in ui-font" s spos
7212 and doc path pan anchor c bookmarks v t spos _ =
7213 match t with
7214 | Vdata | Vcdata -> v
7215 | Vend -> error "unexpected end of input in doc" s spos
7216 | Vopen ("bookmarks", _, closed) ->
7217 if closed
7218 then v
7219 else { v with f = pbookmarks path pan anchor c bookmarks }
7221 | Vopen ("keymap", attrs, closed) ->
7222 let modename =
7223 try List.assoc "mode" attrs
7224 with Not_found -> "global"
7226 if closed
7227 then v
7228 else
7229 let ret keymap =
7230 let h = findkeyhash c modename in
7231 KeyMap.iter (Hashtbl.replace h) keymap;
7232 doc path pan anchor c bookmarks
7234 { v with f = pkeymap ret KeyMap.empty }
7236 | Vopen (_, _, _) ->
7237 error "unexpected subelement in doc" s spos
7239 | Vclose "doc" ->
7240 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
7241 { v with f = llppconfig }
7243 | Vclose _ -> error "unexpected close in doc" s spos
7245 and pkeymap ret keymap v t spos _ =
7246 match t with
7247 | Vdata | Vcdata -> v
7248 | Vend -> error "unexpected end of input in keymap" s spos
7249 | Vopen ("map", attrs, closed) ->
7250 let r, l = map_of attrs in
7251 let kss = fromstring keys_of_string spos "in" r [] in
7252 let lss = fromstring keys_of_string spos "out" l [] in
7253 let keymap =
7254 match kss with
7255 | [] -> keymap
7256 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
7257 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
7259 if closed
7260 then { v with f = pkeymap ret keymap }
7261 else
7262 let f () = v in
7263 { v with f = skip "map" f }
7265 | Vopen _ ->
7266 error "unexpected subelement in keymap" s spos
7268 | Vclose "keymap" ->
7269 { v with f = ret keymap }
7271 | Vclose _ -> error "unexpected close in keymap" s spos
7273 and pbookmarks path pan anchor c bookmarks v t spos _ =
7274 match t with
7275 | Vdata | Vcdata -> v
7276 | Vend -> error "unexpected end of input in bookmarks" s spos
7277 | Vopen ("item", attrs, closed) ->
7278 let titleent, spage, srely, svisy = bookmark_of attrs in
7279 let page = fromstring int_of_string spos "page" spage 0
7280 and rely = fromstring float_of_string spos "rely" srely 0.0
7281 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7282 let bookmarks =
7283 (unent titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
7285 if closed
7286 then { v with f = pbookmarks path pan anchor c bookmarks }
7287 else
7288 let f () = v in
7289 { v with f = skip "item" f }
7291 | Vopen _ ->
7292 error "unexpected subelement in bookmarks" s spos
7294 | Vclose "bookmarks" ->
7295 { v with f = doc path pan anchor c bookmarks }
7297 | Vclose _ -> error "unexpected close in bookmarks" s spos
7299 and skip tag f v t spos _ =
7300 match t with
7301 | Vdata | Vcdata -> v
7302 | Vend ->
7303 error ("unexpected end of input in skipped " ^ tag) s spos
7304 | Vopen (tag', _, closed) ->
7305 if closed
7306 then v
7307 else
7308 let f' () = { v with f = skip tag f } in
7309 { v with f = skip tag' f' }
7310 | Vclose ctag ->
7311 if tag = ctag
7312 then f ()
7313 else error ("unexpected close in skipped " ^ tag) s spos
7316 parse { f = toplevel; accu = () } s;
7317 h, dc;
7320 let do_load f ic =
7322 let len = in_channel_length ic in
7323 let s = String.create len in
7324 really_input ic s 0 len;
7325 f s;
7326 with
7327 | Parse_error (msg, s, pos) ->
7328 let subs = subs s pos in
7329 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
7331 | exn ->
7332 failwith ("config load error: " ^ exntos exn)
7335 let defconfpath =
7336 let dir =
7338 let dir = Filename.concat home ".config" in
7339 if Sys.is_directory dir then dir else home
7340 with _ -> home
7342 Filename.concat dir "llpp.conf"
7345 let confpath = ref defconfpath;;
7347 let load1 f =
7348 if Sys.file_exists !confpath
7349 then
7350 match
7351 (try Some (open_in_bin !confpath)
7352 with exn ->
7353 prerr_endline
7354 ("Error opening configuration file `" ^ !confpath ^ "': " ^
7355 exntos exn);
7356 None
7358 with
7359 | Some ic ->
7360 let success =
7362 f (do_load get ic)
7363 with exn ->
7364 prerr_endline
7365 ("Error loading configuration from `" ^ !confpath ^ "': " ^
7366 exntos exn);
7367 false
7369 close_in ic;
7370 success
7372 | None -> false
7373 else
7374 f (Hashtbl.create 0, defconf)
7377 let load () =
7378 let f (h, dc) =
7379 let pc, pb, px, pa =
7381 let key =
7382 if emptystr state.origin
7383 then state.path
7384 else state.origin
7386 Hashtbl.find h (Filename.basename key)
7387 with Not_found -> dc, [], 0, emptyanchor
7389 setconf defconf dc;
7390 setconf conf pc;
7391 state.bookmarks <- pb;
7392 state.x <- px;
7393 if conf.jumpback
7394 then state.anchor <- pa;
7395 cbput state.hists.nav pa;
7396 true
7398 load1 f
7401 let add_attrs bb always dc c =
7402 let ob s a b =
7403 if always || a != b
7404 then Printf.bprintf bb "\n %s='%b'" s a
7405 and op s a b =
7406 if always || a <> b
7407 then Printf.bprintf bb "\n %s='%b'" s (a != None)
7408 and oi s a b =
7409 if always || a != b
7410 then Printf.bprintf bb "\n %s='%d'" s a
7411 and oI s a b =
7412 if always || a != b
7413 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
7414 and oz s a b =
7415 if always || a <> b
7416 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
7417 and oF s a b =
7418 if always || a <> b
7419 then Printf.bprintf bb "\n %s='%f'" s a
7420 and oc s a b =
7421 if always || a <> b
7422 then
7423 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
7424 and oC s a b =
7425 if always || a <> b
7426 then
7427 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
7428 and oR s a b =
7429 if always || a <> b
7430 then
7431 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
7432 and os s a b =
7433 if always || a <> b
7434 then
7435 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7436 and og s a b =
7437 if always || a <> b
7438 then
7439 match a with
7440 | Some (_N, _A, _B) ->
7441 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7442 | None ->
7443 match b with
7444 | None -> ()
7445 | _ ->
7446 Printf.bprintf bb "\n %s='none'" s
7447 and oW s a b =
7448 if always || a <> b
7449 then
7450 let v =
7451 match a with
7452 | None -> "false"
7453 | Some f ->
7454 if f = infinity
7455 then "true"
7456 else string_of_float f
7458 Printf.bprintf bb "\n %s='%s'" s v
7459 and oco s a b =
7460 if always || a <> b
7461 then
7462 match a with
7463 | Cmulti ((n, a, b), _) when n > 1 ->
7464 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7465 | Csplit (n, _) when n > 1 ->
7466 Printf.bprintf bb "\n %s='%d'" s ~-n
7467 | _ -> ()
7468 and obeco s a b =
7469 if always || a <> b
7470 then
7471 match a with
7472 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7473 | _ -> ()
7474 and oFm s a b =
7475 if always || a <> b
7476 then
7477 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7478 and oSv s a b m =
7479 if always || a <> b
7480 then
7481 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7482 and oPm s a b =
7483 if always || a <> b
7484 then
7485 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
7487 oi "width" c.cwinw dc.cwinw;
7488 oi "height" c.cwinh dc.cwinh;
7489 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7490 oi "scroll-handle-height" c.scrollh dc.scrollh;
7491 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7492 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7493 ob "case-insensitive-search" c.icase dc.icase;
7494 ob "preload" c.preload dc.preload;
7495 oi "page-bias" c.pagebias dc.pagebias;
7496 oi "scroll-step" c.scrollstep dc.scrollstep;
7497 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7498 ob "max-height-fit" c.maxhfit dc.maxhfit;
7499 ob "crop-hack" c.crophack dc.crophack;
7500 oW "throttle" c.maxwait dc.maxwait;
7501 ob "highlight-links" c.hlinks dc.hlinks;
7502 ob "under-cursor-info" c.underinfo dc.underinfo;
7503 oi "vertical-margin" c.interpagespace dc.interpagespace;
7504 oz "zoom" c.zoom dc.zoom;
7505 ob "presentation" c.presentation dc.presentation;
7506 oi "rotation-angle" c.angle dc.angle;
7507 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7508 oFm "fit-model" c.fitmodel dc.fitmodel;
7509 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7510 oi "tex-count" c.texcount dc.texcount;
7511 oi "slice-height" c.sliceheight dc.sliceheight;
7512 oi "thumbnail-width" c.thumbw dc.thumbw;
7513 ob "persistent-location" c.jumpback dc.jumpback;
7514 oc "background-color" c.bgcolor dc.bgcolor;
7515 oi "tile-width" c.tilew dc.tilew;
7516 oi "tile-height" c.tileh dc.tileh;
7517 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7518 ob "checkers" c.checkers dc.checkers;
7519 oi "aalevel" c.aalevel dc.aalevel;
7520 ob "trim-margins" c.trimmargins dc.trimmargins;
7521 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7522 os "uri-launcher" c.urilauncher dc.urilauncher;
7523 os "path-launcher" c.pathlauncher dc.pathlauncher;
7524 oC "color-space" c.colorspace dc.colorspace;
7525 ob "invert-colors" c.invert dc.invert;
7526 oF "brightness" c.colorscale dc.colorscale;
7527 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7528 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7529 oco "columns" c.columns dc.columns;
7530 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7531 os "selection-command" c.selcmd dc.selcmd;
7532 os "synctex-command" c.stcmd dc.stcmd;
7533 os "pax-command" c.paxcmd dc.paxcmd;
7534 ob "update-cursor" c.updatecurs dc.updatecurs;
7535 oi "hint-font-size" c.hfsize dc.hfsize;
7536 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7537 oF "page-scroll-scale" c.pgscale dc.pgscale;
7538 ob "use-pbo" c.usepbo dc.usepbo;
7539 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7540 ob "remote-in-a-new-instance" c.riani dc.riani;
7541 op "point-and-x" c.pax dc.pax;
7542 oPm "point-and-x-mark" c.paxmark dc.paxmark;
7545 let keymapsbuf always dc c =
7546 let bb = Buffer.create 16 in
7547 let rec loop = function
7548 | [] -> ()
7549 | (modename, h) :: rest ->
7550 let dh = findkeyhash dc modename in
7551 if always || h <> dh
7552 then (
7553 if Hashtbl.length h > 0
7554 then (
7555 if Buffer.length bb > 0
7556 then Buffer.add_char bb '\n';
7557 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7558 Hashtbl.iter (fun i o ->
7559 let isdifferent = always ||
7561 let dO = Hashtbl.find dh i in
7562 dO <> o
7563 with Not_found -> true
7565 if isdifferent
7566 then
7567 let addkm (k, m) =
7568 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7569 if Wsi.withalt m then Buffer.add_string bb "alt-";
7570 if Wsi.withshift m then Buffer.add_string bb "shift-";
7571 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7572 Buffer.add_string bb (Wsi.keyname k);
7574 let addkms l =
7575 let rec loop = function
7576 | [] -> ()
7577 | km :: [] -> addkm km
7578 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7580 loop l
7582 Buffer.add_string bb "<map in='";
7583 addkm i;
7584 match o with
7585 | KMinsrt km ->
7586 Buffer.add_string bb "' out='";
7587 addkm km;
7588 Buffer.add_string bb "'/>\n"
7590 | KMinsrl kms ->
7591 Buffer.add_string bb "' out='";
7592 addkms kms;
7593 Buffer.add_string bb "'/>\n"
7595 | KMmulti (ins, kms) ->
7596 Buffer.add_char bb ' ';
7597 addkms ins;
7598 Buffer.add_string bb "' out='";
7599 addkms kms;
7600 Buffer.add_string bb "'/>\n"
7601 ) h;
7602 Buffer.add_string bb "</keymap>";
7605 loop rest
7607 loop c.keyhashes;
7611 let save () =
7612 let uifontsize = fstate.fontsize in
7613 let bb = Buffer.create 32768 in
7614 let relx = float state.x /. float state.winw in
7615 let w, h, x =
7616 let cx w = truncate (relx *. float w) in
7617 List.fold_left
7618 (fun (w, h, x) ws ->
7619 match ws with
7620 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7621 | Wsi.MaxVert -> (w, conf.cwinh, x)
7622 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7624 (state.winw, state.winh, state.x) state.winstate
7626 conf.cwinw <- w;
7627 conf.cwinh <- h;
7628 let f (h, dc) =
7629 let dc = if conf.bedefault then conf else dc in
7630 Buffer.add_string bb "<llppconfig>\n";
7632 if nonemptystr !fontpath
7633 then
7634 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7635 uifontsize
7636 !fontpath
7637 else (
7638 if uifontsize <> 14
7639 then
7640 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7643 Buffer.add_string bb "<defaults";
7644 add_attrs bb true dc dc;
7645 let kb = keymapsbuf true dc dc in
7646 if Buffer.length kb > 0
7647 then (
7648 Buffer.add_string bb ">\n";
7649 Buffer.add_buffer bb kb;
7650 Buffer.add_string bb "\n</defaults>\n";
7652 else Buffer.add_string bb "/>\n";
7654 let adddoc path pan anchor c bookmarks =
7655 if bookmarks == [] && c = dc && anchor = emptyanchor
7656 then ()
7657 else (
7658 Printf.bprintf bb "<doc path='%s'"
7659 (enent path 0 (String.length path));
7661 if anchor <> emptyanchor
7662 then (
7663 let n, rely, visy = anchor in
7664 Printf.bprintf bb " page='%d'" n;
7665 if rely > 1e-6
7666 then
7667 Printf.bprintf bb " rely='%f'" rely
7669 if abs_float visy > 1e-6
7670 then
7671 Printf.bprintf bb " visy='%f'" visy
7675 if pan != 0
7676 then Printf.bprintf bb " pan='%d'" pan;
7678 add_attrs bb false dc c;
7679 let kb = keymapsbuf false dc c in
7681 begin match bookmarks with
7682 | [] ->
7683 if Buffer.length kb > 0
7684 then (
7685 Buffer.add_string bb ">\n";
7686 Buffer.add_buffer bb kb;
7687 Buffer.add_string bb "\n</doc>\n";
7689 else Buffer.add_string bb "/>\n"
7690 | _ ->
7691 Buffer.add_string bb ">\n<bookmarks>\n";
7692 List.iter (fun (title, _, kind) ->
7693 begin match kind with
7694 | Oanchor (page, rely, visy) ->
7695 Printf.bprintf bb
7696 "<item title='%s' page='%d'"
7697 (enent title 0 (String.length title))
7698 page
7700 if rely > 1e-6
7701 then
7702 Printf.bprintf bb " rely='%f'" rely
7704 if abs_float visy > 1e-6
7705 then
7706 Printf.bprintf bb " visy='%f'" visy
7708 | Onone | Ouri _ | Oremote _ | Oremotedest _ | Olaunch _ ->
7709 failwith "unexpected link in bookmarks"
7710 end;
7711 Buffer.add_string bb "/>\n";
7712 ) bookmarks;
7713 Buffer.add_string bb "</bookmarks>";
7714 if Buffer.length kb > 0
7715 then (
7716 Buffer.add_string bb "\n";
7717 Buffer.add_buffer bb kb;
7719 Buffer.add_string bb "\n</doc>\n";
7720 end;
7724 let pan, conf =
7725 match state.mode with
7726 | Birdseye (c, pan, _, _, _) ->
7727 let beyecolumns =
7728 match conf.columns with
7729 | Cmulti ((c, _, _), _) -> Some c
7730 | Csingle _ -> None
7731 | Csplit _ -> None
7732 and columns =
7733 match c.columns with
7734 | Cmulti (c, _) -> Cmulti (c, [||])
7735 | Csingle _ -> Csingle [||]
7736 | Csplit _ -> failwith "quit from bird's eye while split"
7738 pan, { c with beyecolumns = beyecolumns; columns = columns }
7739 | _ -> x, conf
7741 let basename = Filename.basename
7742 (if emptystr state.origin then state.path else state.origin)
7744 adddoc basename pan (getanchor ())
7745 (let autoscrollstep =
7746 match state.autoscroll with
7747 | Some step -> step
7748 | None -> conf.autoscrollstep
7749 in begin match state.mode with
7750 | Birdseye beye -> leavebirdseye beye true;
7751 | _ -> ()
7752 end;
7753 { conf with autoscrollstep = autoscrollstep })
7754 (if conf.savebmarks then state.bookmarks else []);
7756 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7757 if basename <> path
7758 then adddoc path x anchor c bookmarks
7759 ) h;
7760 Buffer.add_string bb "</llppconfig>\n";
7761 true;
7763 if load1 f && Buffer.length bb > 0
7764 then
7766 let tmp = !confpath ^ ".tmp" in
7767 let oc = open_out_bin tmp in
7768 Buffer.output_buffer oc bb;
7769 close_out oc;
7770 Unix.rename tmp !confpath;
7771 with exn ->
7772 prerr_endline
7773 ("error while saving configuration: " ^ exntos exn)
7775 end;;
7777 let adderrmsg src msg =
7778 Buffer.add_string state.errmsgs msg;
7779 state.newerrmsgs <- true;
7780 G.postRedisplay src
7783 let adderrfmt src fmt =
7784 Format.kprintf (fun s -> adderrmsg src s) fmt;
7787 let ract cmds =
7788 let cl = splitatspace cmds in
7789 let scan s fmt f =
7790 try Scanf.sscanf s fmt f
7791 with exn ->
7792 adderrfmt "remote exec"
7793 "error processing '%S': %s\n" cmds (exntos exn)
7795 match cl with
7796 | "reload" :: [] -> reload ()
7797 | "goto" :: args :: [] ->
7798 scan args "%u %f %f"
7799 (fun pageno x y ->
7800 let cmd, _ = state.geomcmds in
7801 if emptystr cmd
7802 then gotopagexy pageno x y
7803 else
7804 let f prevf () =
7805 gotopagexy pageno x y;
7806 prevf ()
7808 state.reprf <- f state.reprf
7810 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7811 | "gotor" :: args :: [] ->
7812 scan args "%S %u"
7813 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
7814 | "gotord" :: args :: [] ->
7815 scan args "%S %S"
7816 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
7817 | "rect" :: args :: [] ->
7818 scan args "%u %u %f %f %f %f"
7819 (fun pageno color x0 y0 x1 y1 ->
7820 onpagerect pageno (fun w h ->
7821 let _,w1,h1,_ = getpagedim pageno in
7822 let sw = float w1 /. float w
7823 and sh = float h1 /. float h in
7824 let x0s = x0 *. sw
7825 and x1s = x1 *. sw
7826 and y0s = y0 *. sh
7827 and y1s = y1 *. sh in
7828 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7829 debugrect rect;
7830 state.rects <- (pageno, color, rect) :: state.rects;
7831 G.postRedisplay "rect";
7834 | "activatewin" :: [] -> Wsi.activatewin ()
7835 | "quit" :: [] -> raise Quit
7836 | _ ->
7837 adderrfmt "remote command"
7838 "error processing remote command: %S\n" cmds;
7841 let remote =
7842 let scratch = String.create 80 in
7843 let buf = Buffer.create 80 in
7844 fun fd ->
7845 let rec tempfr () =
7846 try Some (Unix.read fd scratch 0 80)
7847 with
7848 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7849 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7850 | exn -> raise exn
7852 match tempfr () with
7853 | None -> Some fd
7854 | Some n ->
7855 if n = 0
7856 then (
7857 Unix.close fd;
7858 if Buffer.length buf > 0
7859 then (
7860 let s = Buffer.contents buf in
7861 Buffer.clear buf;
7862 ract s;
7864 None
7866 else
7867 let rec eat ppos =
7868 let nlpos =
7870 let pos = String.index_from scratch ppos '\n' in
7871 if pos >= n then -1 else pos
7872 with Not_found -> -1
7874 if nlpos >= 0
7875 then (
7876 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7877 let s = Buffer.contents buf in
7878 Buffer.clear buf;
7879 ract s;
7880 eat (nlpos+1);
7882 else (
7883 Buffer.add_substring buf scratch ppos (n-ppos);
7884 Some fd
7886 in eat 0
7889 let remoteopen path =
7890 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7891 with exn ->
7892 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7893 None
7896 let () =
7897 let trimcachepath = ref "" in
7898 let rcmdpath = ref "" in
7899 let pageno = ref None in
7900 selfexec := Sys.executable_name;
7901 Arg.parse
7902 (Arg.align
7903 [("-p", Arg.String (fun s -> state.password <- s),
7904 "<password> Set password");
7906 ("-f", Arg.String
7907 (fun s ->
7908 Config.fontpath := s;
7909 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7911 "<path> Set path to the user interface font");
7913 ("-c", Arg.String
7914 (fun s ->
7915 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7916 Config.confpath := s),
7917 "<path> Set path to the configuration file");
7919 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
7920 "<page-number> Jump to page");
7922 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7923 "<path> Set path to the trim cache file");
7925 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7926 "<named-destination> Set named destination");
7928 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7929 ("-cxack", Arg.Set cxack, " Cut corners");
7931 ("-remote", Arg.String (fun s -> rcmdpath := s),
7932 "<path> Set path to the remote commands source");
7934 ("-origin", Arg.String (fun s -> state.origin <- s),
7935 "<original-path> Set original path");
7937 ("-v", Arg.Unit (fun () ->
7938 Printf.printf
7939 "%s\nconfiguration path: %s\n"
7940 (version ())
7941 Config.defconfpath
7943 exit 0), " Print version and exit");
7946 (fun s -> state.path <- s)
7947 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7949 if !wtmode
7950 then selfexec := !selfexec ^ " -wtmode";
7952 if emptystr state.path
7953 then (prerr_endline "file name missing"; exit 1);
7955 if not (Config.load ())
7956 then prerr_endline "failed to load configuration";
7957 begin match !pageno with
7958 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
7959 | None -> ()
7960 end;
7962 let wsfd, winw, winh = Wsi.init (object (self)
7963 val mutable m_hack = false
7964 val mutable m_clicks = 0
7965 val mutable m_click_x = 0
7966 val mutable m_click_y = 0
7967 val mutable m_lastclicktime = infinity
7969 method private cleanup =
7970 state.roam <- noroam;
7971 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
7972 method expose = if not m_hack then G.postRedisplay "expose"
7973 method visible = G.postRedisplay "visible"
7974 method display = m_hack <- false; display ()
7975 method reshape w h =
7976 self#cleanup;
7977 m_hack <- w < state.winw && h < state.winh;
7978 reshape w h
7979 method mouse b d x y m =
7980 if d && canselect ()
7981 then (
7982 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
7983 m_click_x <- x;
7984 m_click_y <- y;
7985 if b = 1
7986 then (
7987 let t = now () in
7988 if abs x - m_click_x > 10
7989 || abs y - m_click_y > 10
7990 || abs_float (t -. m_lastclicktime) > 0.3
7991 then m_clicks <- 0;
7992 m_clicks <- m_clicks + 1;
7993 m_lastclicktime <- t;
7994 if m_clicks = 1
7995 then (
7996 self#cleanup;
7997 G.postRedisplay "cleanup";
7998 state.uioh <- state.uioh#button b d x y m;
8000 else state.uioh <- state.uioh#multiclick m_clicks x y m
8002 else (
8003 self#cleanup;
8004 m_clicks <- 0;
8005 m_lastclicktime <- infinity;
8006 state.uioh <- state.uioh#button b d x y m
8009 else (
8010 state.uioh <- state.uioh#button b d x y m
8012 method motion x y =
8013 state.mpos <- (x, y);
8014 state.uioh <- state.uioh#motion x y
8015 method pmotion x y =
8016 state.mpos <- (x, y);
8017 state.uioh <- state.uioh#pmotion x y
8018 method key k m =
8019 let mascm = m land (
8020 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
8021 ) in
8022 let keyboard k m =
8023 let x = state.x and y = state.y in
8024 keyboard k m;
8025 if x != state.x || y != state.y then self#cleanup
8027 match state.keystate with
8028 | KSnone ->
8029 let km = k, mascm in
8030 begin
8031 match
8032 let modehash = state.uioh#modehash in
8033 try Hashtbl.find modehash km
8034 with Not_found ->
8035 try Hashtbl.find (findkeyhash conf "global") km
8036 with Not_found -> KMinsrt (k, m)
8037 with
8038 | KMinsrt (k, m) -> keyboard k m
8039 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
8040 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
8042 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
8043 List.iter (fun (k, m) -> keyboard k m) insrt;
8044 state.keystate <- KSnone
8045 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
8046 state.keystate <- KSinto (keys, insrt)
8047 | _ ->
8048 state.keystate <- KSnone
8050 method enter x y =
8051 state.mpos <- (x, y);
8052 state.uioh <- state.uioh#pmotion x y
8053 method leave = state.mpos <- (-1, -1)
8054 method winstate wsl = state.winstate <- wsl; m_hack <- false
8055 method quit = raise Quit
8056 end) conf.cwinw conf.cwinh (platform = Posx) in
8058 state.wsfd <- wsfd;
8060 if not (
8061 List.exists GlMisc.check_extension
8062 [ "GL_ARB_texture_rectangle"
8063 ; "GL_EXT_texture_recangle"
8064 ; "GL_NV_texture_rectangle" ]
8066 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
8068 if (
8069 let r = GlMisc.get_string `renderer in
8070 let p = "Mesa DRI Intel(" in
8071 let l = String.length p in
8072 String.length r > l && String.sub r 0 l = p
8074 then (
8075 defconf.sliceheight <- 1024;
8076 defconf.texcount <- 32;
8077 defconf.usepbo <- true;
8080 let cr, sw =
8081 match Ne.res Unix.pipe with
8082 | Ne.Exn exn ->
8083 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
8084 exit 1
8085 | Ne.Res rw -> rw
8086 and sr, cw =
8087 match Ne.res Unix.pipe with
8088 | Ne.Exn exn ->
8089 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
8090 exit 1
8091 | Ne.Res rw -> rw
8094 cloexec cr;
8095 cloexec sw;
8096 cloexec sr;
8097 cloexec cw;
8099 setcheckers conf.checkers;
8100 redirectstderr ();
8101 if conf.redirectstderr
8102 then
8103 at_exit (fun () ->
8104 let s = Buffer.contents state.errmsgs ^
8105 (match state.errfd with
8106 | Some fd ->
8107 let s = String.create (80*24) in
8108 let n =
8110 let r, _, _ = Unix.select [fd] [] [] 0.0 in
8111 if List.mem fd r
8112 then Unix.read fd s 0 (String.length s)
8113 else 0
8114 with _ -> 0
8116 if n = 0
8117 then ""
8118 else String.sub s 0 n
8119 | None -> ""
8122 try ignore (Unix.write state.stderr s 0 (String.length s))
8123 with exn -> print_endline (exntos exn)
8127 init (cr, cw) (
8128 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
8129 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
8130 !Config.fontpath, !trimcachepath,
8131 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
8133 List.iter GlArray.enable [`texture_coord; `vertex];
8134 state.sr <- sr;
8135 state.sw <- sw;
8136 state.text <- "Opening " ^ (mbtoutf8 state.path);
8137 reshape winw winh;
8138 opendoc state.path state.password;
8139 state.uioh <- uioh;
8140 display ();
8141 Wsi.mapwin ();
8142 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
8143 let optrfd =
8144 ref (
8145 if nonemptystr !rcmdpath
8146 then remoteopen !rcmdpath
8147 else None
8151 let rec loop deadline =
8152 let r =
8153 match state.errfd with
8154 | None -> [state.sr; state.wsfd]
8155 | Some fd -> [state.sr; state.wsfd; fd]
8157 let r =
8158 match !optrfd with
8159 | None -> r
8160 | Some fd -> fd :: r
8162 if state.redisplay
8163 then (
8164 state.redisplay <- false;
8165 display ();
8167 let timeout =
8168 let now = now () in
8169 if deadline > now
8170 then (
8171 if deadline = infinity
8172 then ~-.1.0
8173 else max 0.0 (deadline -. now)
8175 else 0.0
8177 let r, _, _ =
8178 try Unix.select r [] [] timeout
8179 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
8181 begin match r with
8182 | [] ->
8183 state.ghyll None;
8184 let newdeadline =
8185 if state.ghyll == noghyll
8186 then
8187 match state.autoscroll with
8188 | Some step when step != 0 ->
8189 let y = state.y + step in
8190 let y =
8191 if y < 0
8192 then state.maxy
8193 else if y >= state.maxy then 0 else y
8195 gotoy y;
8196 if state.mode = View
8197 then state.text <- "";
8198 deadline +. 0.01
8199 | _ -> infinity
8200 else deadline +. 0.01
8202 loop newdeadline
8204 | l ->
8205 let rec checkfds = function
8206 | [] -> ()
8207 | fd :: rest when fd = state.sr ->
8208 let cmd = readcmd state.sr in
8209 act cmd;
8210 checkfds rest
8212 | fd :: rest when fd = state.wsfd ->
8213 Wsi.readresp fd;
8214 checkfds rest
8216 | fd :: rest when Some fd = !optrfd ->
8217 begin match remote fd with
8218 | None -> optrfd := remoteopen !rcmdpath;
8219 | opt -> optrfd := opt
8220 end;
8221 checkfds rest
8223 | fd :: rest ->
8224 let s = String.create 80 in
8225 let n = tempfailureretry (Unix.read fd s 0) 80 in
8226 if conf.redirectstderr
8227 then (
8228 Buffer.add_substring state.errmsgs s 0 n;
8229 state.newerrmsgs <- true;
8230 state.redisplay <- true;
8232 else (
8233 prerr_string (String.sub s 0 n);
8234 flush stderr;
8236 checkfds rest
8238 checkfds l;
8239 let newdeadline =
8240 let deadline1 =
8241 if deadline = infinity
8242 then now () +. 0.01
8243 else deadline
8245 match state.autoscroll with
8246 | Some step when step != 0 -> deadline1
8247 | _ -> if state.ghyll == noghyll then infinity else deadline1
8249 loop newdeadline
8250 end;
8253 loop infinity;
8254 with Quit ->
8255 Config.save ();