Add a verb
[llpp.git] / main.ml
blob69ae304b30a14f7ed110355f50cae466e096a479
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" 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 gotoghyll 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 (_N, _A, _B) y sy =
2065 let sum = summa _N _A _B in
2066 let dy = float (y - sy) in
2067 state.ghyll <- (
2068 let rec gf n y1 o =
2069 if n >= _N
2070 then state.ghyll <- noghyll
2071 else
2072 let go n =
2073 let s = scroll n _N _A _B in
2074 let y1 = y1 +. ((s *. dy) /. sum) in
2075 gotoy_and_clear_text (truncate y1);
2076 state.ghyll <- gf (n+1) y1;
2078 match o with
2079 | None -> go n
2080 | Some y' -> set (_N/2, 1, 1) y' state.y
2082 gf 0 (float state.y)
2085 match conf.ghyllscroll with
2086 | Some nab when not conf.presentation ->
2087 if state.ghyll == noghyll
2088 then set nab y state.y
2089 else state.ghyll (Some y)
2090 | _ ->
2091 gotoy_and_clear_text y
2094 let gotopage n top =
2095 let y, h = getpageyh n in
2096 let y = y + (truncate (top *. float h)) in
2097 gotoghyll y
2100 let gotopage1 n top =
2101 let y = getpagey n in
2102 let y = y + top in
2103 gotoghyll y
2106 let invalidate s f =
2107 state.layout <- [];
2108 state.pdims <- [];
2109 state.rects <- [];
2110 state.rects1 <- [];
2111 match state.geomcmds with
2112 | ps, [] when emptystr ps ->
2113 f ();
2114 state.geomcmds <- s, [];
2116 | ps, [] ->
2117 state.geomcmds <- ps, [s, f];
2119 | ps, (s', _) :: rest when s' = s ->
2120 state.geomcmds <- ps, ((s, f) :: rest);
2122 | ps, cmds ->
2123 state.geomcmds <- ps, ((s, f) :: cmds);
2126 let flushpages () =
2127 Hashtbl.iter (fun _ opaque ->
2128 wcmd "freepage %s" (~> opaque);
2129 ) state.pagemap;
2130 Hashtbl.clear state.pagemap;
2133 let flushtiles () =
2134 if not (Queue.is_empty state.tilelru)
2135 then (
2136 Queue.iter (fun (k, p, s) ->
2137 wcmd "freetile %s" (~> p);
2138 state.memused <- state.memused - s;
2139 Hashtbl.remove state.tilemap k;
2140 ) state.tilelru;
2141 state.uioh#infochanged Memused;
2142 Queue.clear state.tilelru;
2144 load state.layout;
2147 let stateh h =
2148 let h = truncate (float h*.conf.zoom) in
2149 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2150 h - d
2153 let opendoc path password =
2154 state.path <- path;
2155 state.password <- password;
2156 state.gen <- state.gen + 1;
2157 state.docinfo <- [];
2159 flushpages ();
2160 setaalevel conf.aalevel;
2161 let titlepath =
2162 if emptystr state.origin
2163 then path
2164 else state.origin
2166 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2167 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
2168 invalidate "reqlayout"
2169 (fun () ->
2170 wcmd "reqlayout %d %d %d %s\000"
2171 conf.angle (FMTE.to_int conf.fitmodel)
2172 (stateh state.winh) state.nameddest
2176 let reload () =
2177 state.anchor <- getanchor ();
2178 opendoc state.path state.password;
2181 let scalecolor c =
2182 let c = c *. conf.colorscale in
2183 (c, c, c);
2186 let scalecolor2 (r, g, b) =
2187 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2190 let docolumns = function
2191 | Csingle _ ->
2192 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2193 let rec loop pageno pdimno pdim y ph pdims =
2194 if pageno = state.pagecount
2195 then ()
2196 else
2197 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2198 match pdims with
2199 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2200 pdimno+1, pdim, rest
2201 | _ ->
2202 pdimno, pdim, pdims
2204 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2205 let y = y +
2206 (if conf.presentation
2207 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2208 else (if pageno = 0 then 0 else conf.interpagespace)
2211 a.(pageno) <- (pdimno, x, y, pdim);
2212 loop (pageno+1) pdimno pdim (y + h) h pdims
2214 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2215 conf.columns <- Csingle a;
2217 | Cmulti ((columns, coverA, coverB), _) ->
2218 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2219 let rec loop pageno pdimno pdim x y rowh pdims =
2220 let rec fixrow m = if m = pageno then () else
2221 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2222 if h < rowh
2223 then (
2224 let y = y + (rowh - h) / 2 in
2225 a.(m) <- (pdimno, x, y, pdim);
2227 fixrow (m+1)
2229 if pageno = state.pagecount
2230 then fixrow (((pageno - 1) / columns) * columns)
2231 else
2232 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2233 match pdims with
2234 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2235 pdimno+1, pdim, rest
2236 | _ ->
2237 pdimno, pdim, pdims
2239 let x, y, rowh' =
2240 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2241 then (
2242 let x = (wadjsb state.winw - w) / 2 in
2243 let ips =
2244 if conf.presentation then calcips h else conf.interpagespace in
2245 x, y + ips + rowh, h
2247 else (
2248 if (pageno - coverA) mod columns = 0
2249 then (
2250 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2251 let y =
2252 if conf.presentation
2253 then
2254 let ips = calcips h in
2255 y + (if pageno = 0 then 0 else calcips rowh + ips)
2256 else
2257 y + (if pageno = 0 then 0 else conf.interpagespace)
2259 x, y + rowh, h
2261 else x, y, max rowh h
2264 let y =
2265 if pageno > 1 && (pageno - coverA) mod columns = 0
2266 then (
2267 let y =
2268 if pageno = columns && conf.presentation
2269 then (
2270 let ips = calcips rowh in
2271 for i = 0 to pred columns
2273 let (pdimno, x, y, pdim) = a.(i) in
2274 a.(i) <- (pdimno, x, y+ips, pdim)
2275 done;
2276 y+ips;
2278 else y
2280 fixrow (pageno - columns);
2283 else y
2285 a.(pageno) <- (pdimno, x, y, pdim);
2286 let x = x + w + xoff*2 + conf.interpagespace in
2287 loop (pageno+1) pdimno pdim x y rowh' pdims
2289 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2290 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2292 | Csplit (c, _) ->
2293 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2294 let rec loop pageno pdimno pdim y pdims =
2295 if pageno = state.pagecount
2296 then ()
2297 else
2298 let pdimno, ((_, w, h, _) as pdim), pdims =
2299 match pdims with
2300 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2301 pdimno+1, pdim, rest
2302 | _ ->
2303 pdimno, pdim, pdims
2305 let cw = w / c in
2306 let rec loop1 n x y =
2307 if n = c then y else (
2308 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2309 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2312 let y = loop1 0 0 y in
2313 loop (pageno+1) pdimno pdim y pdims
2315 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2316 conf.columns <- Csplit (c, a);
2319 let represent () =
2320 docolumns conf.columns;
2321 state.maxy <- calcheight ();
2322 if state.reprf == noreprf
2323 then (
2324 match state.mode with
2325 | Birdseye (_, _, pageno, _, _) ->
2326 let y, h = getpageyh pageno in
2327 let top = (state.winh - h) / 2 in
2328 gotoy (max 0 (y - top))
2329 | _ -> gotoanchor state.anchor
2331 else (
2332 state.reprf ();
2333 state.reprf <- noreprf;
2337 let reshape w h =
2338 GlDraw.viewport 0 0 w h;
2339 let firsttime = state.geomcmds == firstgeomcmds in
2340 if not firsttime && nogeomcmds state.geomcmds
2341 then state.anchor <- getanchor ();
2343 state.winw <- w;
2344 let w = wadjsb (truncate (float w *. conf.zoom)) in
2345 let w = max w 2 in
2346 state.winh <- h;
2347 setfontsize fstate.fontsize;
2348 GlMat.mode `modelview;
2349 GlMat.load_identity ();
2351 GlMat.mode `projection;
2352 GlMat.load_identity ();
2353 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2354 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2355 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2357 let relx =
2358 if conf.zoom <= 1.0
2359 then 0.0
2360 else float state.x /. float state.w
2362 invalidate "geometry"
2363 (fun () ->
2364 state.w <- w;
2365 if not firsttime
2366 then state.x <- truncate (relx *. float w);
2367 let w =
2368 match conf.columns with
2369 | Csingle _ -> w
2370 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2371 | Csplit (c, _) -> w * c
2373 wcmd "geometry %d %d %d"
2374 w (stateh h) (FMTE.to_int conf.fitmodel)
2378 let enttext () =
2379 let len = String.length state.text in
2380 let drawstring s =
2381 let hscrollh =
2382 match state.mode with
2383 | Textentry _ | View | LinkNav _ ->
2384 let h, _, _ = state.uioh#scrollpw in
2386 | _ -> 0
2388 let rect x w =
2389 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
2390 (x+.w) (float (state.winh - hscrollh))
2393 let w = float (wadjsb state.winw - 1) in
2394 if state.progress >= 0.0 && state.progress < 1.0
2395 then (
2396 GlDraw.color (0.3, 0.3, 0.3);
2397 let w1 = w *. state.progress in
2398 rect 0.0 w1;
2399 GlDraw.color (0.0, 0.0, 0.0);
2400 rect w1 (w-.w1)
2402 else (
2403 GlDraw.color (0.0, 0.0, 0.0);
2404 rect 0.0 w;
2407 GlDraw.color (1.0, 1.0, 1.0);
2408 drawstring fstate.fontsize
2409 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2411 let s =
2412 match state.mode with
2413 | Textentry ((prefix, text, _, _, _, _), _) ->
2414 let s =
2415 if len > 0
2416 then
2417 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2418 else
2419 Printf.sprintf "%s%s_" prefix text
2423 | _ -> state.text
2425 let s =
2426 if state.newerrmsgs
2427 then (
2428 if not (istextentry state.mode) && state.uioh#eformsgs
2429 then
2430 let s1 = "(press 'e' to review error messasges)" in
2431 if nonemptystr s then s ^ " " ^ s1 else s1
2432 else s
2434 else s
2436 if nonemptystr s
2437 then drawstring s
2440 let gctiles () =
2441 let len = Queue.length state.tilelru in
2442 let layout = lazy (
2443 match state.throttle with
2444 | None ->
2445 if conf.preload
2446 then preloadlayout state.y
2447 else state.layout
2448 | Some (layout, _, _) ->
2449 layout
2450 ) in
2451 let rec loop qpos =
2452 if state.memused <= conf.memlimit
2453 then ()
2454 else (
2455 if qpos < len
2456 then
2457 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2458 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2459 let (_, pw, ph, _) = getpagedim n in
2461 gen = state.gen
2462 && colorspace = conf.colorspace
2463 && angle = conf.angle
2464 && pagew = pw
2465 && pageh = ph
2466 && (
2467 let x = col*conf.tilew
2468 and y = row*conf.tileh in
2469 tilevisible (Lazy.force_val layout) n x y
2471 then Queue.push lruitem state.tilelru
2472 else (
2473 freepbo p;
2474 wcmd "freetile %s" (~> p);
2475 state.memused <- state.memused - s;
2476 state.uioh#infochanged Memused;
2477 Hashtbl.remove state.tilemap k;
2479 loop (qpos+1)
2482 loop 0
2485 let logcurrently = function
2486 | Idle -> dolog "Idle"
2487 | Loading (l, gen) ->
2488 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2489 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2490 dolog
2491 "Tiling %d[%d,%d] page=%s cs=%s angle"
2492 l.pageno col row (~> pageopaque)
2493 (CSTE.to_string colorspace)
2495 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2496 angle gen conf.angle state.gen
2497 tilew tileh
2498 conf.tilew conf.tileh
2500 | Outlining _ ->
2501 dolog "outlining"
2504 let splitatspace =
2505 let r = Str.regexp " " in
2506 fun s -> Str.bounded_split r s 2;
2509 let onpagerect pageno f =
2510 let b =
2511 match conf.columns with
2512 | Cmulti (_, b) -> b
2513 | Csingle b -> b
2514 | Csplit (_, b) -> b
2516 if pageno >= 0 && pageno < Array.length b
2517 then
2518 let (_, _, _, (w, h, _, _)) = b.(pageno) in
2519 f w h
2522 let gotopagexy1 pageno x y =
2523 let _,w1,h1,leftx = getpagedim pageno in
2524 let top = y /. (float h1) in
2525 let left = x /. (float w1) in
2526 let py, w, h = getpageywh pageno in
2527 let wh = state.winh - hscrollh () in
2528 let x = left *. (float w) in
2529 let x = leftx + state.x + truncate x in
2530 let sx =
2531 if x < 0 || x >= wadjsb state.winw
2532 then state.x - x
2533 else state.x
2535 let pdy = truncate (top *. float h) in
2536 let y' = py + pdy in
2537 let dy = y' - state.y in
2538 let sy =
2539 if x != state.x || not (dy > 0 && dy < wh)
2540 then (
2541 if conf.presentation
2542 then
2543 if abs (py - y') > wh
2544 then y'
2545 else py
2546 else y';
2548 else state.y
2550 if state.x != sx || state.y != sy
2551 then (
2552 let x, y =
2553 if !wtmode
2554 then (
2555 let ww = wadjsb state.winw in
2556 let qx = sx / ww
2557 and qy = pdy / wh in
2558 let x = qx * ww
2559 and y = py + qy * wh in
2560 let x = if -x + ww > w1 then -(w1-ww) else x
2561 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2562 let y =
2563 if conf.presentation
2564 then
2565 if abs (py - y') > wh
2566 then y'
2567 else py
2568 else y';
2570 (x, y)
2572 else (sx, sy)
2574 state.x <- x;
2575 gotoy_and_clear_text y;
2577 else gotoy_and_clear_text state.y;
2580 let gotopagexy pageno x y =
2581 match state.mode with
2582 | Birdseye _ -> gotopage pageno 0.0
2583 | _ -> gotopagexy1 pageno x y
2586 let act cmds =
2587 (* dolog "%S" cmds; *)
2588 let cl = splitatspace cmds in
2589 let scan s fmt f =
2590 try Scanf.sscanf s fmt f
2591 with exn ->
2592 dolog "error processing '%S': %s" cmds (exntos exn);
2593 exit 1
2595 let addoutline outline =
2596 match state.currently with
2597 | Outlining outlines ->
2598 state.currently <- Outlining (outline :: outlines)
2599 | Idle -> state.currently <- Outlining [outline]
2600 | currently ->
2601 dolog "invalid outlining state";
2602 logcurrently currently
2604 match cl with
2605 | "clear" :: [] ->
2606 state.uioh#infochanged Pdim;
2607 state.pdims <- [];
2609 | "clearrects" :: [] ->
2610 state.rects <- state.rects1;
2611 G.postRedisplay "clearrects";
2613 | "continue" :: args :: [] ->
2614 let n = scan args "%u" (fun n -> n) in
2615 state.pagecount <- n;
2616 begin match state.currently with
2617 | Outlining l ->
2618 state.currently <- Idle;
2619 state.outlines <- Array.of_list (List.rev l)
2620 | _ -> ()
2621 end;
2623 let cur, cmds = state.geomcmds in
2624 if emptystr cur
2625 then failwith "umpossible";
2627 begin match List.rev cmds with
2628 | [] ->
2629 state.geomcmds <- "", [];
2630 state.throttle <- None;
2631 represent ();
2632 | (s, f) :: rest ->
2633 f ();
2634 state.geomcmds <- s, List.rev rest;
2635 end;
2636 if conf.maxwait = None && not !wtmode
2637 then G.postRedisplay "continue";
2639 | "title" :: args :: [] ->
2640 Wsi.settitle args
2642 | "msg" :: args :: [] ->
2643 showtext ' ' args
2645 | "vmsg" :: args :: [] ->
2646 if conf.verbose
2647 then showtext ' ' args
2649 | "emsg" :: args :: [] ->
2650 Buffer.add_string state.errmsgs args;
2651 state.newerrmsgs <- true;
2652 G.postRedisplay "error message"
2654 | "progress" :: args :: [] ->
2655 let progress, text =
2656 scan args "%f %n"
2657 (fun f pos ->
2658 f, String.sub args pos (String.length args - pos))
2660 state.text <- text;
2661 state.progress <- progress;
2662 G.postRedisplay "progress"
2664 | "firstmatch" :: args :: [] ->
2665 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2666 scan args "%u %d %f %f %f %f %f %f %f %f"
2667 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2668 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2670 let y = (getpagey pageno) + truncate y0 in
2671 addnav ();
2672 gotoy y;
2673 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2675 | "match" :: args :: [] ->
2676 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2677 scan args "%u %d %f %f %f %f %f %f %f %f"
2678 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2679 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2681 state.rects1 <-
2682 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2684 | "page" :: args :: [] ->
2685 let pageopaques, t = scan args "%s %f" (fun p t -> p, t) in
2686 let pageopaque = ~< pageopaques in
2687 begin match state.currently with
2688 | Loading (l, gen) ->
2689 vlog "page %d took %f sec" l.pageno t;
2690 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2691 begin match state.throttle with
2692 | None ->
2693 let preloadedpages =
2694 if conf.preload
2695 then preloadlayout state.y
2696 else state.layout
2698 let evict () =
2699 let set =
2700 List.fold_left (fun s l -> IntSet.add l.pageno s)
2701 IntSet.empty preloadedpages
2703 let evictedpages =
2704 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2705 if not (IntSet.mem pageno set)
2706 then (
2707 wcmd "freepage %s" (~> opaque);
2708 key :: accu
2710 else accu
2711 ) state.pagemap []
2713 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2715 evict ();
2716 state.currently <- Idle;
2717 if gen = state.gen
2718 then (
2719 tilepage l.pageno pageopaque state.layout;
2720 load state.layout;
2721 load preloadedpages;
2722 if pagevisible state.layout l.pageno
2723 && layoutready state.layout
2724 then G.postRedisplay "page";
2727 | Some (layout, _, _) ->
2728 state.currently <- Idle;
2729 tilepage l.pageno pageopaque layout;
2730 load state.layout
2731 end;
2733 | _ ->
2734 dolog "Inconsistent loading state";
2735 logcurrently state.currently;
2736 exit 1
2739 | "tile" :: args :: [] ->
2740 let (x, y, opaques, size, t) =
2741 scan args "%u %u %s %u %f"
2742 (fun x y p size t -> (x, y, p, size, t))
2744 let opaque = ~< opaques in
2745 begin match state.currently with
2746 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2747 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2749 unmappbo opaque;
2750 if tilew != conf.tilew || tileh != conf.tileh
2751 then (
2752 wcmd "freetile %s" (~> opaque);
2753 state.currently <- Idle;
2754 load state.layout;
2756 else (
2757 puttileopaque l col row gen cs angle opaque size t;
2758 state.memused <- state.memused + size;
2759 state.uioh#infochanged Memused;
2760 gctiles ();
2761 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2762 opaque, size) state.tilelru;
2764 let layout =
2765 match state.throttle with
2766 | None -> state.layout
2767 | Some (layout, _, _) -> layout
2770 state.currently <- Idle;
2771 if gen = state.gen
2772 && conf.colorspace = cs
2773 && conf.angle = angle
2774 && tilevisible layout l.pageno x y
2775 then conttiling l.pageno pageopaque;
2777 begin match state.throttle with
2778 | None ->
2779 preload state.layout;
2780 if gen = state.gen
2781 && conf.colorspace = cs
2782 && conf.angle = angle
2783 && tilevisible state.layout l.pageno x y
2784 && (not !wtmode || layoutready state.layout)
2785 then G.postRedisplay "tile nothrottle";
2787 | Some (layout, y, _) ->
2788 let ready = layoutready layout in
2789 if ready
2790 then (
2791 state.y <- y;
2792 state.layout <- layout;
2793 state.throttle <- None;
2794 G.postRedisplay "throttle";
2796 else load layout;
2797 end;
2800 | _ ->
2801 dolog "Inconsistent tiling state";
2802 logcurrently state.currently;
2803 exit 1
2806 | "pdim" :: args :: [] ->
2807 let (n, w, h, _) as pdim =
2808 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2810 let pdim =
2811 match conf.fitmodel, conf.columns with
2812 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2813 | _ -> pdim
2815 state.uioh#infochanged Pdim;
2816 state.pdims <- pdim :: state.pdims
2818 | "o" :: args :: [] ->
2819 let (l, n, t, h, pos) =
2820 scan args "%u %u %d %u %n"
2821 (fun l n t h pos -> l, n, t, h, pos)
2823 let s = String.sub args pos (String.length args - pos) in
2824 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
2826 | "ou" :: args :: [] ->
2827 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
2828 let s = String.sub args pos len in
2829 let pos2 = pos + len + 1 in
2830 let uri = String.sub args pos2 (String.length args - pos2) in
2831 addoutline (s, l, Ouri uri)
2833 | "on" :: args :: [] ->
2834 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
2835 let s = String.sub args pos (String.length args - pos) in
2836 addoutline (s, l, Onone)
2838 | "a" :: args :: [] ->
2839 let (n, l, t) =
2840 scan args "%u %d %d" (fun n l t -> n, l, t)
2842 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2844 | "info" :: args :: [] ->
2845 state.docinfo <- (1, args) :: state.docinfo
2847 | "infoend" :: [] ->
2848 state.uioh#infochanged Docinfo;
2849 state.docinfo <- List.rev state.docinfo
2851 | _ ->
2852 error "unknown cmd `%S'" cmds
2855 let onhist cb =
2856 let rc = cb.rc in
2857 let action = function
2858 | HCprev -> cbget cb ~-1
2859 | HCnext -> cbget cb 1
2860 | HCfirst -> cbget cb ~-(cb.rc)
2861 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2862 and cancel () = cb.rc <- rc
2863 in (action, cancel)
2866 let search pattern forward =
2867 match conf.columns with
2868 | Csplit _ ->
2869 showtext '!' "searching does not work properly in split columns mode"
2870 | _ ->
2871 if nonemptystr pattern
2872 then
2873 let pn, py =
2874 match state.layout with
2875 | [] -> 0, 0
2876 | l :: _ ->
2877 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2879 wcmd "search %d %d %d %d,%s\000"
2880 (btod conf.icase) pn py (btod forward) pattern;
2883 let intentry text key =
2884 let c =
2885 if key >= 32 && key < 127
2886 then Char.chr key
2887 else '\000'
2889 match c with
2890 | '0' .. '9' ->
2891 let text = addchar text c in
2892 TEcont text
2894 | _ ->
2895 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2896 TEcont text
2899 let linknentry text key =
2900 let c =
2901 if key >= 32 && key < 127
2902 then Char.chr key
2903 else '\000'
2905 match c with
2906 | 'a' .. 'z' ->
2907 let text = addchar text c in
2908 TEcont text
2910 | _ ->
2911 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2912 TEcont text
2915 let linkndone f s =
2916 if nonemptystr s
2917 then (
2918 let n =
2919 let l = String.length s in
2920 let rec loop pos n = if pos = l then n else
2921 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2922 loop (pos+1) (n*26 + m)
2923 in loop 0 0
2925 let rec loop n = function
2926 | [] -> ()
2927 | l :: rest ->
2928 match getopaque l.pageno with
2929 | None -> loop n rest
2930 | Some opaque ->
2931 let m = getlinkcount opaque in
2932 if n < m
2933 then (
2934 let under = getlink opaque n in
2935 f under
2937 else loop (n-m) rest
2939 loop n state.layout;
2943 let textentry text key =
2944 if key land 0xff00 = 0xff00
2945 then TEcont text
2946 else TEcont (text ^ toutf8 key)
2949 let reqlayout angle fitmodel =
2950 match state.throttle with
2951 | None ->
2952 if nogeomcmds state.geomcmds
2953 then state.anchor <- getanchor ();
2954 conf.angle <- angle mod 360;
2955 if conf.angle != 0
2956 then (
2957 match state.mode with
2958 | LinkNav _ -> state.mode <- View
2959 | _ -> ()
2961 conf.fitmodel <- fitmodel;
2962 invalidate "reqlayout"
2963 (fun () ->
2964 wcmd "reqlayout %d %d %d"
2965 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2967 | _ -> ()
2970 let settrim trimmargins trimfuzz =
2971 if nogeomcmds state.geomcmds
2972 then state.anchor <- getanchor ();
2973 conf.trimmargins <- trimmargins;
2974 conf.trimfuzz <- trimfuzz;
2975 let x0, y0, x1, y1 = trimfuzz in
2976 invalidate "settrim"
2977 (fun () ->
2978 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2979 flushpages ();
2982 let setzoom zoom =
2983 match state.throttle with
2984 | None ->
2985 let zoom = max 0.0001 zoom in
2986 if zoom <> conf.zoom
2987 then (
2988 state.prevzoom <- (conf.zoom, state.x);
2989 conf.zoom <- zoom;
2990 reshape state.winw state.winh;
2991 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2994 | Some (layout, y, started) ->
2995 let time =
2996 match conf.maxwait with
2997 | None -> 0.0
2998 | Some t -> t
3000 let dt = now () -. started in
3001 if dt > time
3002 then (
3003 state.y <- y;
3004 load layout;
3008 let setcolumns mode columns coverA coverB =
3009 state.prevcolumns <- Some (conf.columns, conf.zoom);
3010 if columns < 0
3011 then (
3012 if isbirdseye mode
3013 then showtext '!' "split mode doesn't work in bird's eye"
3014 else (
3015 conf.columns <- Csplit (-columns, [||]);
3016 state.x <- 0;
3017 conf.zoom <- 1.0;
3020 else (
3021 if columns < 2
3022 then (
3023 conf.columns <- Csingle [||];
3024 state.x <- 0;
3025 setzoom 1.0;
3027 else (
3028 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
3029 conf.zoom <- 1.0;
3032 reshape state.winw state.winh;
3035 let resetmstate () =
3036 state.mstate <- Mnone;
3037 Wsi.setcursor Wsi.CURSOR_INHERIT;
3040 let enterbirdseye () =
3041 let zoom = float conf.thumbw /. float state.winw in
3042 let birdseyepageno =
3043 let cy = state.winh / 2 in
3044 let fold = function
3045 | [] -> 0
3046 | l :: rest ->
3047 let rec fold best = function
3048 | [] -> best.pageno
3049 | l :: rest ->
3050 let d = cy - (l.pagedispy + l.pagevh/2)
3051 and dbest = cy - (best.pagedispy + best.pagevh/2) in
3052 if abs d < abs dbest
3053 then fold l rest
3054 else best.pageno
3055 in fold l rest
3057 fold state.layout
3059 state.mode <- Birdseye (
3060 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
3062 resetmstate ();
3063 conf.zoom <- zoom;
3064 conf.presentation <- false;
3065 conf.interpagespace <- 10;
3066 conf.hlinks <- false;
3067 conf.fitmodel <- FitProportional;
3068 state.x <- 0;
3069 conf.maxwait <- None;
3070 conf.columns <- (
3071 match conf.beyecolumns with
3072 | Some c ->
3073 conf.zoom <- 1.0;
3074 Cmulti ((c, 0, 0), [||])
3075 | None -> Csingle [||]
3077 if conf.verbose
3078 then
3079 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
3080 (100.0*.zoom)
3081 else
3082 state.text <- ""
3084 reshape state.winw state.winh;
3087 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
3088 state.mode <- View;
3089 conf.zoom <- c.zoom;
3090 conf.presentation <- c.presentation;
3091 conf.interpagespace <- c.interpagespace;
3092 conf.maxwait <- c.maxwait;
3093 conf.hlinks <- c.hlinks;
3094 conf.fitmodel <- c.fitmodel;
3095 conf.beyecolumns <- (
3096 match conf.columns with
3097 | Cmulti ((c, _, _), _) -> Some c
3098 | Csingle _ -> None
3099 | Csplit _ -> failwith "leaving bird's eye split mode"
3101 conf.columns <- (
3102 match c.columns with
3103 | Cmulti (c, _) -> Cmulti (c, [||])
3104 | Csingle _ -> Csingle [||]
3105 | Csplit (c, _) -> Csplit (c, [||])
3107 if conf.verbose
3108 then
3109 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
3110 (100.0*.conf.zoom)
3112 reshape state.winw state.winh;
3113 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
3114 state.x <- leftx;
3117 let togglebirdseye () =
3118 match state.mode with
3119 | Birdseye vals -> leavebirdseye vals true
3120 | View -> enterbirdseye ()
3121 | _ -> ()
3124 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3125 let pageno = max 0 (pageno - incr) in
3126 let rec loop = function
3127 | [] -> gotopage1 pageno 0
3128 | l :: _ when l.pageno = pageno ->
3129 if l.pagedispy >= 0 && l.pagey = 0
3130 then G.postRedisplay "upbirdseye"
3131 else gotopage1 pageno 0
3132 | _ :: rest -> loop rest
3134 loop state.layout;
3135 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
3138 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3139 let pageno = min (state.pagecount - 1) (pageno + incr) in
3140 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3141 let rec loop = function
3142 | [] ->
3143 let y, h = getpageyh pageno in
3144 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
3145 gotoy (clamp dy)
3146 | l :: _ when l.pageno = pageno ->
3147 if l.pagevh != l.pageh
3148 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
3149 else G.postRedisplay "downbirdseye"
3150 | _ :: rest -> loop rest
3152 loop state.layout
3155 let boundastep h step =
3156 if step < 0
3157 then bound step ~-h 0
3158 else bound step 0 h
3161 let optentry mode _ key =
3162 let btos b = if b then "on" else "off" in
3163 if key >= 32 && key < 127
3164 then
3165 let c = Char.chr key in
3166 match c with
3167 | 's' ->
3168 let ondone s =
3169 try conf.scrollstep <- int_of_string s with exc ->
3170 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3172 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3174 | 'A' ->
3175 let ondone s =
3177 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
3178 if state.autoscroll <> None
3179 then state.autoscroll <- Some conf.autoscrollstep
3180 with exc ->
3181 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3183 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3185 | 'C' ->
3186 let ondone s =
3188 let n, a, b = multicolumns_of_string s in
3189 setcolumns mode n a b;
3190 with exc ->
3191 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3193 TEswitch ("columns: ", "", None, textentry, ondone, true)
3195 | 'Z' ->
3196 let ondone s =
3198 let zoom = float (int_of_string s) /. 100.0 in
3199 setzoom zoom
3200 with exc ->
3201 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3203 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3205 | 't' ->
3206 let ondone s =
3208 conf.thumbw <- bound (int_of_string s) 2 4096;
3209 state.text <-
3210 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3211 begin match mode with
3212 | Birdseye beye ->
3213 leavebirdseye beye false;
3214 enterbirdseye ();
3215 | _ -> ();
3217 with exc ->
3218 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3220 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3222 | 'R' ->
3223 let ondone s =
3224 match try
3225 Some (int_of_string s)
3226 with exc ->
3227 state.text <- Printf.sprintf "bad integer `%s': %s"
3228 s (exntos exc);
3229 None
3230 with
3231 | Some angle -> reqlayout angle conf.fitmodel
3232 | None -> ()
3234 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3236 | 'i' ->
3237 conf.icase <- not conf.icase;
3238 TEdone ("case insensitive search " ^ (btos conf.icase))
3240 | 'p' ->
3241 conf.preload <- not conf.preload;
3242 gotoy state.y;
3243 TEdone ("preload " ^ (btos conf.preload))
3245 | 'v' ->
3246 conf.verbose <- not conf.verbose;
3247 TEdone ("verbose " ^ (btos conf.verbose))
3249 | 'd' ->
3250 conf.debug <- not conf.debug;
3251 TEdone ("debug " ^ (btos conf.debug))
3253 | 'h' ->
3254 conf.maxhfit <- not conf.maxhfit;
3255 state.maxy <- calcheight ();
3256 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3258 | 'c' ->
3259 conf.crophack <- not conf.crophack;
3260 TEdone ("crophack " ^ btos conf.crophack)
3262 | 'a' ->
3263 let s =
3264 match conf.maxwait with
3265 | None ->
3266 conf.maxwait <- Some infinity;
3267 "always wait for page to complete"
3268 | Some _ ->
3269 conf.maxwait <- None;
3270 "show placeholder if page is not ready"
3272 TEdone s
3274 | 'f' ->
3275 conf.underinfo <- not conf.underinfo;
3276 TEdone ("underinfo " ^ btos conf.underinfo)
3278 | 'P' ->
3279 conf.savebmarks <- not conf.savebmarks;
3280 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3282 | 'S' ->
3283 let ondone s =
3285 let pageno, py =
3286 match state.layout with
3287 | [] -> 0, 0
3288 | l :: _ ->
3289 l.pageno, l.pagey
3291 conf.interpagespace <- int_of_string s;
3292 docolumns conf.columns;
3293 state.maxy <- calcheight ();
3294 let y = getpagey pageno in
3295 gotoy (y + py)
3296 with exc ->
3297 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3299 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3301 | 'l' ->
3302 let fm =
3303 match conf.fitmodel with
3304 | FitProportional -> FitWidth
3305 | _ -> FitProportional
3307 reqlayout conf.angle fm;
3308 TEdone ("proportional display " ^ btos (fm == FitProportional))
3310 | 'T' ->
3311 settrim (not conf.trimmargins) conf.trimfuzz;
3312 TEdone ("trim margins " ^ btos conf.trimmargins)
3314 | 'I' ->
3315 conf.invert <- not conf.invert;
3316 TEdone ("invert colors " ^ btos conf.invert)
3318 | 'x' ->
3319 let ondone s =
3320 cbput state.hists.sel s;
3321 conf.selcmd <- s;
3323 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3324 textentry, ondone, true)
3326 | 'M' ->
3327 if conf.pax == None
3328 then conf.pax <- Some (ref (0.0, 0, 0))
3329 else conf.pax <- None;
3330 TEdone ("PAX " ^ btos (conf.pax != None))
3332 | _ ->
3333 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3334 TEstop
3335 else
3336 TEcont state.text
3339 class type lvsource = object
3340 method getitemcount : int
3341 method getitem : int -> (string * int)
3342 method hasaction : int -> bool
3343 method exit :
3344 uioh:uioh ->
3345 cancel:bool ->
3346 active:int ->
3347 first:int ->
3348 pan:int ->
3349 uioh option
3350 method getactive : int
3351 method getfirst : int
3352 method getpan : int
3353 method getminfo : (int * int) array
3354 end;;
3356 class virtual lvsourcebase = object
3357 val mutable m_active = 0
3358 val mutable m_first = 0
3359 val mutable m_pan = 0
3360 method getactive = m_active
3361 method getfirst = m_first
3362 method getpan = m_pan
3363 method getminfo : (int * int) array = [||]
3364 end;;
3366 let withoutlastutf8 s =
3367 let len = String.length s in
3368 if len = 0
3369 then s
3370 else
3371 let rec find pos =
3372 if pos = 0
3373 then pos
3374 else
3375 let b = Char.code s.[pos] in
3376 if b land 0b11000000 = 0b11000000
3377 then pos
3378 else find (pos-1)
3380 let first =
3381 if Char.code s.[len-1] land 0x80 = 0
3382 then len-1
3383 else find (len-1)
3385 String.sub s 0 first;
3388 let textentrykeyboard
3389 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3390 let key =
3391 if key >= 0xffb0 && key <= 0xffb9
3392 then key - 0xffb0 + 48 else key
3394 let enttext te =
3395 state.mode <- Textentry (te, onleave);
3396 state.text <- "";
3397 enttext ();
3398 G.postRedisplay "textentrykeyboard enttext";
3400 let histaction cmd =
3401 match opthist with
3402 | None -> ()
3403 | Some (action, _) ->
3404 state.mode <- Textentry (
3405 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3407 G.postRedisplay "textentry histaction"
3409 match key with
3410 | 0xff08 -> (* backspace *)
3411 if emptystr text && cancelonempty
3412 then (
3413 onleave Cancel;
3414 G.postRedisplay "textentrykeyboard after cancel";
3416 else
3417 let s = withoutlastutf8 text in
3418 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3420 | 0xff0d | 0xff8d -> (* (kp) enter *)
3421 ondone text;
3422 onleave Confirm;
3423 G.postRedisplay "textentrykeyboard after confirm"
3425 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3426 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3427 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3428 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3430 | 0xff1b -> (* escape*)
3431 if emptystr text
3432 then (
3433 begin match opthist with
3434 | None -> ()
3435 | Some (_, onhistcancel) -> onhistcancel ()
3436 end;
3437 onleave Cancel;
3438 state.text <- "";
3439 G.postRedisplay "textentrykeyboard after cancel2"
3441 else (
3442 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3445 | 0xff9f | 0xffff -> () (* delete *)
3447 | _ when key != 0
3448 && key land 0xff00 != 0xff00 (* keyboard *)
3449 && key land 0xfe00 != 0xfe00 (* xkb *)
3450 && key land 0xfd00 != 0xfd00 (* 3270 *)
3452 begin match onkey text key with
3453 | TEdone text ->
3454 ondone text;
3455 onleave Confirm;
3456 G.postRedisplay "textentrykeyboard after confirm2";
3458 | TEcont text ->
3459 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3461 | TEstop ->
3462 onleave Cancel;
3463 G.postRedisplay "textentrykeyboard after cancel3"
3465 | TEswitch te ->
3466 state.mode <- Textentry (te, onleave);
3467 G.postRedisplay "textentrykeyboard switch";
3468 end;
3470 | _ ->
3471 vlog "unhandled key %s" (Wsi.keyname key)
3474 let firstof first active =
3475 if first > active || abs (first - active) > fstate.maxrows - 1
3476 then max 0 (active - (fstate.maxrows/2))
3477 else first
3480 let calcfirst first active =
3481 if active > first
3482 then
3483 let rows = active - first in
3484 if rows > fstate.maxrows then active - fstate.maxrows else first
3485 else active
3488 let scrollph y maxy =
3489 let sh = float (maxy + state.winh) /. float state.winh in
3490 let sh = float state.winh /. sh in
3491 let sh = max sh (float conf.scrollh) in
3493 let percent = float y /. float maxy in
3494 let position = (float state.winh -. sh) *. percent in
3496 let position =
3497 if position +. sh > float state.winh
3498 then float state.winh -. sh
3499 else position
3501 position, sh;
3504 let coe s = (s :> uioh);;
3506 class listview ~(source:lvsource) ~trusted ~modehash =
3507 object (self)
3508 val m_pan = source#getpan
3509 val m_first = source#getfirst
3510 val m_active = source#getactive
3511 val m_qsearch = ""
3512 val m_prev_uioh = state.uioh
3514 method private elemunder y =
3515 let n = y / (fstate.fontsize+1) in
3516 if m_first + n < source#getitemcount
3517 then (
3518 if source#hasaction (m_first + n)
3519 then Some (m_first + n)
3520 else None
3522 else None
3524 method display =
3525 Gl.enable `blend;
3526 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3527 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3528 filledrect 0. 0. (float state.winw) (float state.winh);
3529 GlDraw.color (1., 1., 1.);
3530 Gl.enable `texture_2d;
3531 let fs = fstate.fontsize in
3532 let nfs = fs + 1 in
3533 let ww = fstate.wwidth in
3534 let tabw = 30.0*.ww in
3535 let itemcount = source#getitemcount in
3536 let minfo = source#getminfo in
3537 let rec loop row =
3538 if (row - m_first) > fstate.maxrows
3539 then ()
3540 else (
3541 if row >= 0 && row < itemcount
3542 then (
3543 let (s, level) = source#getitem row in
3544 let y = (row - m_first) * nfs in
3545 let x = 5.0 +. float (level + m_pan) *. ww in
3546 if row = m_active
3547 then (
3548 Gl.disable `texture_2d;
3549 let alpha = if source#hasaction row then 0.9 else 0.3 in
3550 GlDraw.color (1., 1., 1.) ~alpha;
3551 linerect 1. (float (y + 1))
3552 (float (state.winw - conf.scrollbw - 1)) (float (y + fs + 3));
3553 Gl.enable `texture_2d;
3554 GlDraw.color (1., 1., 1.);
3556 let drawtabularstring s =
3557 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3558 if trusted
3559 then
3560 let tabpos = try String.index s '\t' with Not_found -> -1 in
3561 if tabpos > 0
3562 then
3563 let len = String.length s - tabpos - 1 in
3564 let s1 = String.sub s 0 tabpos
3565 and s2 = String.sub s (tabpos + 1) len in
3566 let nx = drawstr x s1 in
3567 let sw = nx -. x in
3568 let x = x +. (max tabw sw) in
3569 drawstr x s2
3570 else
3571 drawstr x s
3572 else
3573 drawstr x s
3575 let _ = drawtabularstring s in
3576 loop (row+1)
3580 loop m_first;
3581 GlDraw.color (1.0, 1.0, 1.0) ~alpha:0.5;
3582 let rec loop row =
3583 if (row - m_first) > fstate.maxrows
3584 then ()
3585 else (
3586 if row >= 0 && row < itemcount
3587 then (
3588 let (s, level) = source#getitem row in
3589 let y = (row - m_first) * nfs in
3590 let x = 5.0 +. float (level + m_pan) *. ww in
3591 let (first, last) = minfo.(row) in
3592 let prefix = String.sub s 0 first in
3593 let suffix = String.sub s first (last - first) in
3594 let w1 = measurestr fstate.fontsize prefix in
3595 let w2 = measurestr fstate.fontsize suffix in
3596 let x0 = x +. w1
3597 and y0 = (float (y+2)) in
3598 let x1 = x0 +. w2
3599 and y1 = (float (y+fs+3)) in
3600 filledrect x0 y0 x1 y1;
3601 loop (row+1)
3605 Gl.disable `texture_2d;
3606 if Array.length minfo > 0 then loop m_first;
3607 Gl.disable `blend;
3609 method updownlevel incr =
3610 let len = source#getitemcount in
3611 let curlevel =
3612 if m_active >= 0 && m_active < len
3613 then snd (source#getitem m_active)
3614 else -1
3616 let rec flow i =
3617 if i = len then i-1 else if i = -1 then 0 else
3618 let _, l = source#getitem i in
3619 if l != curlevel then i else flow (i+incr)
3621 let active = flow m_active in
3622 let first = calcfirst m_first active in
3623 G.postRedisplay "outline updownlevel";
3624 {< m_active = active; m_first = first >}
3626 method private key1 key mask =
3627 let set1 active first qsearch =
3628 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3630 let search active pattern incr =
3631 let active = if active = -1 then m_first else active in
3632 let dosearch re =
3633 let rec loop n =
3634 if n >= 0 && n < source#getitemcount
3635 then (
3636 let s, _ = source#getitem n in
3638 (try ignore (Str.search_forward re s 0); true
3639 with Not_found -> false)
3640 then Some n
3641 else loop (n + incr)
3643 else None
3645 loop active
3648 let re = Str.regexp_case_fold pattern in
3649 dosearch re
3650 with Failure s ->
3651 state.text <- s;
3652 None
3654 let itemcount = source#getitemcount in
3655 let find start incr =
3656 let rec find i =
3657 if i = -1 || i = itemcount
3658 then -1
3659 else (
3660 if source#hasaction i
3661 then i
3662 else find (i + incr)
3665 find start
3667 let set active first =
3668 let first = bound first 0 (itemcount - fstate.maxrows) in
3669 state.text <- "";
3670 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3672 let navigate incr =
3673 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3674 let active, first =
3675 let incr1 = if incr > 0 then 1 else -1 in
3676 if isvisible m_first m_active
3677 then
3678 let next =
3679 let next = m_active + incr in
3680 let next =
3681 if next < 0 || next >= itemcount
3682 then -1
3683 else find next incr1
3685 if abs (m_active - next) > fstate.maxrows
3686 then -1
3687 else next
3689 if next = -1
3690 then
3691 let first = m_first + incr in
3692 let first = bound first 0 (itemcount - fstate.maxrows) in
3693 let next =
3694 let next = m_active + incr in
3695 let next = bound next 0 (itemcount - 1) in
3696 find next ~-incr1
3698 let active =
3699 if next = -1
3700 then m_active
3701 else (
3702 if isvisible first next
3703 then next
3704 else m_active
3707 active, first
3708 else
3709 let first = min next m_first in
3710 let first =
3711 if abs (next - first) > fstate.maxrows
3712 then first + incr
3713 else first
3715 next, first
3716 else
3717 let first = m_first + incr in
3718 let first = bound first 0 (itemcount - 1) in
3719 let active =
3720 let next = m_active + incr in
3721 let next = bound next 0 (itemcount - 1) in
3722 let next = find next incr1 in
3723 let active =
3724 if next = -1 || abs (m_active - first) > fstate.maxrows
3725 then (
3726 let active = if m_active = -1 then next else m_active in
3727 active
3729 else next
3731 if isvisible first active
3732 then active
3733 else -1
3735 active, first
3737 G.postRedisplay "listview navigate";
3738 set active first;
3740 match key with
3741 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3742 let incr = if key = 0x72 then -1 else 1 in
3743 let active, first =
3744 match search (m_active + incr) m_qsearch incr with
3745 | None ->
3746 state.text <- m_qsearch ^ " [not found]";
3747 m_active, m_first
3748 | Some active ->
3749 state.text <- m_qsearch;
3750 active, firstof m_first active
3752 G.postRedisplay "listview ctrl-r/s";
3753 set1 active first m_qsearch;
3755 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3756 if m_active >= 0 && m_active < source#getitemcount
3757 then (
3758 let s, _ = source#getitem m_active in
3759 selstring s;
3761 coe self
3763 | 0xff08 -> (* backspace *)
3764 if emptystr m_qsearch
3765 then coe self
3766 else (
3767 let qsearch = withoutlastutf8 m_qsearch in
3768 if emptystr qsearch
3769 then (
3770 state.text <- "";
3771 G.postRedisplay "listview empty qsearch";
3772 set1 m_active m_first "";
3774 else
3775 let active, first =
3776 match search m_active qsearch ~-1 with
3777 | None ->
3778 state.text <- qsearch ^ " [not found]";
3779 m_active, m_first
3780 | Some active ->
3781 state.text <- qsearch;
3782 active, firstof m_first active
3784 G.postRedisplay "listview backspace qsearch";
3785 set1 active first qsearch
3788 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3789 let pattern = m_qsearch ^ toutf8 key in
3790 let active, first =
3791 match search m_active pattern 1 with
3792 | None ->
3793 state.text <- pattern ^ " [not found]";
3794 m_active, m_first
3795 | Some active ->
3796 state.text <- pattern;
3797 active, firstof m_first active
3799 G.postRedisplay "listview qsearch add";
3800 set1 active first pattern;
3802 | 0xff1b -> (* escape *)
3803 state.text <- "";
3804 if emptystr m_qsearch
3805 then (
3806 G.postRedisplay "list view escape";
3807 begin
3808 match
3809 source#exit (coe self) true m_active m_first m_pan
3810 with
3811 | None -> m_prev_uioh
3812 | Some uioh -> uioh
3815 else (
3816 G.postRedisplay "list view kill qsearch";
3817 coe {< m_qsearch = "" >}
3820 | 0xff0d | 0xff8d -> (* (kp) enter *)
3821 state.text <- "";
3822 let self = {< m_qsearch = "" >} in
3823 let opt =
3824 G.postRedisplay "listview enter";
3825 if m_active >= 0 && m_active < source#getitemcount
3826 then (
3827 source#exit (coe self) false m_active m_first m_pan;
3829 else (
3830 source#exit (coe self) true m_active m_first m_pan;
3833 begin match opt with
3834 | None -> m_prev_uioh
3835 | Some uioh -> uioh
3838 | 0xff9f | 0xffff -> (* (kp) delete *)
3839 coe self
3841 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3842 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3843 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3844 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3846 | 0xff53 | 0xff98 -> (* (kp) right *)
3847 state.text <- "";
3848 G.postRedisplay "listview right";
3849 coe {< m_pan = m_pan - 1 >}
3851 | 0xff51 | 0xff96 -> (* (kp) left *)
3852 state.text <- "";
3853 G.postRedisplay "listview left";
3854 coe {< m_pan = m_pan + 1 >}
3856 | 0xff50 | 0xff95 -> (* (kp) home *)
3857 let active = find 0 1 in
3858 G.postRedisplay "listview home";
3859 set active 0;
3861 | 0xff57 | 0xff9c -> (* (kp) end *)
3862 let first = max 0 (itemcount - fstate.maxrows) in
3863 let active = find (itemcount - 1) ~-1 in
3864 G.postRedisplay "listview end";
3865 set active first;
3867 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3868 coe self
3870 | _ ->
3871 dolog "listview unknown key %#x" key; coe self
3873 method key key mask =
3874 match state.mode with
3875 | Textentry te -> textentrykeyboard key mask te; coe self
3876 | _ -> self#key1 key mask
3878 method button button down x y _ =
3879 let opt =
3880 match button with
3881 | 1 when x > state.winw - conf.scrollbw ->
3882 G.postRedisplay "listview scroll";
3883 if down
3884 then
3885 let _, position, sh = self#scrollph in
3886 if y > truncate position && y < truncate (position +. sh)
3887 then (
3888 state.mstate <- Mscrolly;
3889 Some (coe self)
3891 else
3892 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3893 let first = truncate (s *. float source#getitemcount) in
3894 let first = min source#getitemcount first in
3895 Some (coe {< m_first = first; m_active = first >})
3896 else (
3897 state.mstate <- Mnone;
3898 Some (coe self);
3900 | 1 when not down ->
3901 begin match self#elemunder y with
3902 | Some n ->
3903 G.postRedisplay "listview click";
3904 source#exit (coe {< m_active = n >}) false n m_first m_pan
3905 | _ ->
3906 Some (coe self)
3908 | n when (n == 4 || n == 5) && not down ->
3909 let len = source#getitemcount in
3910 let first =
3911 if n = 5 && m_first + fstate.maxrows >= len
3912 then
3913 m_first
3914 else
3915 let first = m_first + (if n == 4 then -1 else 1) in
3916 bound first 0 (len - 1)
3918 G.postRedisplay "listview wheel";
3919 Some (coe {< m_first = first >})
3920 | n when (n = 6 || n = 7) && not down ->
3921 let inc = if n = 7 then -1 else 1 in
3922 G.postRedisplay "listview hwheel";
3923 Some (coe {< m_pan = m_pan + inc >})
3924 | _ ->
3925 Some (coe self)
3927 match opt with
3928 | None -> m_prev_uioh
3929 | Some uioh -> uioh
3931 method multiclick _ x y = self#button 1 true x y
3933 method motion _ y =
3934 match state.mstate with
3935 | Mscrolly ->
3936 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3937 let first = truncate (s *. float source#getitemcount) in
3938 let first = min source#getitemcount first in
3939 G.postRedisplay "listview motion";
3940 coe {< m_first = first; m_active = first >}
3941 | _ -> coe self
3943 method pmotion x y =
3944 if x < state.winw - conf.scrollbw
3945 then
3946 let n =
3947 match self#elemunder y with
3948 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3949 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3951 let o =
3952 if n != m_active
3953 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3954 else self
3956 coe o
3957 else (
3958 Wsi.setcursor Wsi.CURSOR_INHERIT;
3959 coe self
3962 method infochanged _ = ()
3964 method scrollpw = (0, 0.0, 0.0)
3965 method scrollph =
3966 let nfs = fstate.fontsize + 1 in
3967 let y = m_first * nfs in
3968 let itemcount = source#getitemcount in
3969 let maxi = max 0 (itemcount - fstate.maxrows) in
3970 let maxy = maxi * nfs in
3971 let p, h = scrollph y maxy in
3972 conf.scrollbw, p, h
3974 method modehash = modehash
3975 method eformsgs = false
3976 end;;
3978 class outlinelistview ~source =
3979 let settext autonarrow s =
3980 if autonarrow
3981 then
3982 let ss = source#statestr in
3983 state.text <-
3984 if emptystr ss
3985 then "[" ^ s ^ "]"
3986 else "{" ^ ss ^ "} [" ^ s ^ "]"
3987 else state.text <- s
3989 object (self)
3990 inherit listview
3991 ~source:(source :> lvsource)
3992 ~trusted:false
3993 ~modehash:(findkeyhash conf "outline")
3994 as super
3996 val m_autonarrow = false
3998 method display = super#display
4000 method key key mask =
4001 let maxrows =
4002 if emptystr state.text
4003 then fstate.maxrows
4004 else fstate.maxrows - 2
4006 let calcfirst first active =
4007 if active > first
4008 then
4009 let rows = active - first in
4010 if rows > maxrows then active - maxrows else first
4011 else active
4013 let navigate incr =
4014 let active = m_active + incr in
4015 let active = bound active 0 (source#getitemcount - 1) in
4016 let first = calcfirst m_first active in
4017 G.postRedisplay "outline navigate";
4018 coe {< m_active = active; m_first = first >}
4020 let navscroll first =
4021 let active =
4022 let dist = m_active - first in
4023 if dist < 0
4024 then first
4025 else (
4026 if dist < maxrows
4027 then m_active
4028 else first + maxrows
4031 G.postRedisplay "outline navscroll";
4032 coe {< m_first = first; m_active = active >}
4034 let ctrl = Wsi.withctrl mask in
4035 match key with
4036 | 97 when ctrl -> (* ctrl-a *)
4037 let text =
4038 if m_autonarrow
4039 then (source#denarrow; "")
4040 else (
4041 let pattern = source#renarrow in
4042 if nonemptystr m_qsearch
4043 then (source#narrow m_qsearch; m_qsearch)
4044 else pattern
4047 settext (not m_autonarrow) text;
4048 G.postRedisplay "toggle auto narrowing";
4049 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
4051 | 47 when emptystr m_qsearch && not m_autonarrow -> (* / *)
4052 settext true "";
4053 G.postRedisplay "toggle auto narrowing";
4054 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
4056 | 110 when ctrl -> (* ctrl-n *)
4057 source#narrow m_qsearch;
4058 if not m_autonarrow
4059 then source#add_narrow_pattern m_qsearch;
4060 G.postRedisplay "outline ctrl-n";
4061 coe {< m_first = 0; m_active = 0 >}
4063 | 83 when ctrl -> (* ctrl-S *)
4064 let active = source#calcactive (getanchor ()) in
4065 let first = firstof m_first active in
4066 G.postRedisplay "outline ctrl-s";
4067 coe {< m_first = first; m_active = active >}
4069 | 117 when ctrl -> (* ctrl-u *)
4070 G.postRedisplay "outline ctrl-u";
4071 if m_autonarrow && nonemptystr m_qsearch
4072 then (
4073 ignore (source#renarrow);
4074 settext m_autonarrow "";
4075 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
4077 else (
4078 source#del_narrow_pattern;
4079 let pattern = source#renarrow in
4080 let text =
4081 if emptystr pattern then "" else "Narrowed to " ^ pattern
4083 settext m_autonarrow text;
4084 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
4087 | 108 when ctrl -> (* ctrl-l *)
4088 let first = max 0 (m_active - (fstate.maxrows / 2)) in
4089 G.postRedisplay "outline ctrl-l";
4090 coe {< m_first = first >}
4092 | 0xff09 when m_autonarrow -> (* tab *)
4093 if nonemptystr m_qsearch
4094 then (
4095 G.postRedisplay "outline list view tab";
4096 source#add_narrow_pattern m_qsearch;
4097 settext true "";
4098 coe {< m_qsearch = "" >}
4100 else coe self
4102 | 0xff1b when m_autonarrow -> (* escape *)
4103 if nonemptystr m_qsearch
4104 then source#add_narrow_pattern m_qsearch;
4105 super#key key mask
4107 | 0xff0d | 0xff8d when m_autonarrow -> (* (kp) enter *)
4108 if nonemptystr m_qsearch
4109 then source#add_narrow_pattern m_qsearch;
4110 super#key key mask
4112 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
4113 let pattern = m_qsearch ^ toutf8 key in
4114 G.postRedisplay "outlinelistview autonarrow add";
4115 source#narrow pattern;
4116 settext true pattern;
4117 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4119 | key when m_autonarrow && key = 0xff08 -> (* backspace *)
4120 if emptystr m_qsearch
4121 then coe self
4122 else
4123 let pattern = withoutlastutf8 m_qsearch in
4124 G.postRedisplay "outlinelistview autonarrow backspace";
4125 ignore (source#renarrow);
4126 source#narrow pattern;
4127 settext true pattern;
4128 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4130 | 0xff9f | 0xffff -> (* (kp) delete *)
4131 source#remove m_active;
4132 G.postRedisplay "outline delete";
4133 let active = max 0 (m_active-1) in
4134 coe {< m_first = firstof m_first active;
4135 m_active = active >}
4137 | 0xff52 | 0xff97 when ctrl -> (* ctrl-(kp) up *)
4138 navscroll (max 0 (m_first - 1))
4140 | 0xff54 | 0xff99 when ctrl -> (* ctrl-(kp) down *)
4141 navscroll (min (source#getitemcount - 1) (m_first + 1))
4143 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
4144 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
4145 | 0xff55 | 0xff9a -> (* (kp) prior *)
4146 navigate ~-(fstate.maxrows)
4147 | 0xff56 | 0xff9b -> (* (kp) next *)
4148 navigate fstate.maxrows
4150 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
4151 let o =
4152 if ctrl
4153 then (
4154 G.postRedisplay "outline ctrl right";
4155 {< m_pan = m_pan + 1 >}
4157 else self#updownlevel 1
4159 coe o
4161 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
4162 let o =
4163 if ctrl
4164 then (
4165 G.postRedisplay "outline ctrl left";
4166 {< m_pan = m_pan - 1 >}
4168 else self#updownlevel ~-1
4170 coe o
4172 | 0xff50 | 0xff95 -> (* (kp) home *)
4173 G.postRedisplay "outline home";
4174 coe {< m_first = 0; m_active = 0 >}
4176 | 0xff57 | 0xff9c -> (* (kp) end *)
4177 let active = source#getitemcount - 1 in
4178 let first = max 0 (active - fstate.maxrows) in
4179 G.postRedisplay "outline end";
4180 coe {< m_active = active; m_first = first >}
4182 | _ -> super#key key mask
4185 let gotounder under =
4186 let getpath filename =
4187 let path =
4188 if nonemptystr filename
4189 then
4190 if Filename.is_relative filename
4191 then
4192 let dir = Filename.dirname state.path in
4193 let dir =
4194 if Filename.is_implicit dir
4195 then Filename.concat (Sys.getcwd ()) dir
4196 else dir
4198 Filename.concat dir filename
4199 else filename
4200 else ""
4202 if Sys.file_exists path
4203 then path
4204 else ""
4206 match under with
4207 | Ulinkgoto (pageno, top) ->
4208 if pageno >= 0
4209 then (
4210 addnav ();
4211 gotopage1 pageno top;
4214 | Ulinkuri s ->
4215 gotouri s
4217 | Uremote (filename, pageno) ->
4218 let path = getpath filename in
4219 if nonemptystr path
4220 then (
4221 if conf.riani
4222 then
4223 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4224 try popen command []
4225 with exn ->
4226 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
4227 flush stderr;
4228 else
4229 let anchor = getanchor () in
4230 let ranchor = state.path, state.password, anchor, state.origin in
4231 state.origin <- "";
4232 state.anchor <- (pageno, 0.0, 0.0);
4233 state.ranchors <- ranchor :: state.ranchors;
4234 opendoc path "";
4236 else showtext '!' ("Could not find " ^ filename)
4238 | Uremotedest (filename, destname) ->
4239 let path = getpath filename in
4240 if nonemptystr path
4241 then (
4242 if conf.riani
4243 then
4244 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4245 try popen command []
4246 with exn ->
4247 Printf.eprintf
4248 "failed to execute `%s': %s\n" command (exntos exn);
4249 flush stderr;
4250 else
4251 let anchor = getanchor () in
4252 let ranchor = state.path, state.password, anchor, state.origin in
4253 state.origin <- "";
4254 state.nameddest <- destname;
4255 state.ranchors <- ranchor :: state.ranchors;
4256 opendoc path "";
4258 else showtext '!' ("Could not find " ^ filename)
4260 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4263 let gotooutline (_, _, kind) =
4264 match kind with
4265 | Onone -> ()
4266 | Oanchor anchor ->
4267 let (pageno, y, _) = anchor in
4268 let y = getanchory
4269 (if conf.presentation then (pageno, y, 1.0) else anchor)
4271 addnav ();
4272 gotoghyll y
4273 | Ouri uri -> gotounder (Ulinkuri uri)
4274 | Olaunch cmd -> gotounder (Ulaunch cmd)
4275 | Oremote remote -> gotounder (Uremote remote)
4276 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4279 let outlinesource usebookmarks =
4280 let empty = [||] in
4281 (object (self)
4282 inherit lvsourcebase
4283 val mutable m_items = empty
4284 val mutable m_minfo = empty
4285 val mutable m_orig_items = empty
4286 val mutable m_orig_minfo = empty
4287 val mutable m_narrow_patterns = []
4288 val mutable m_hadremovals = false
4289 val mutable m_gen = -1
4291 method getitemcount =
4292 Array.length m_items + (if m_hadremovals then 1 else 0)
4294 method getitem n =
4295 if n == Array.length m_items && m_hadremovals
4296 then
4297 ("[Confirm removal]", 0)
4298 else
4299 let s, n, _ = m_items.(n) in
4300 (s, n)
4302 method exit ~uioh ~cancel ~active ~first ~pan =
4303 ignore (uioh, first);
4304 let confrimremoval = m_hadremovals && active = Array.length m_items in
4305 let items, minfo =
4306 if m_narrow_patterns = []
4307 then m_orig_items, m_orig_minfo
4308 else m_items, m_minfo
4310 if not cancel
4311 then (
4312 if not confrimremoval
4313 then (
4314 gotooutline m_items.(active);
4315 m_items <- items;
4316 m_minfo <- minfo;
4318 else (
4319 state.bookmarks <- Array.to_list m_items;
4320 m_orig_items <- m_items;
4321 m_orig_minfo <- m_minfo;
4324 else (
4325 m_items <- items;
4326 m_minfo <- minfo;
4328 m_pan <- pan;
4329 None
4331 method hasaction _ = true
4333 method greetmsg =
4334 if Array.length m_items != Array.length m_orig_items
4335 then
4336 let s =
4337 match m_narrow_patterns with
4338 | one :: [] -> one
4339 | many -> String.concat "\xe2\x80\xa6" (List.rev many)
4341 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4342 else ""
4344 method statestr =
4345 match m_narrow_patterns with
4346 | [] -> ""
4347 | one :: [] -> one
4348 | head :: _ -> "\xe2\x80\xa6" ^ head
4350 method narrow pattern =
4351 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4352 match reopt with
4353 | None -> ()
4354 | Some re ->
4355 let rec loop accu minfo n =
4356 if n = -1
4357 then (
4358 m_items <- Array.of_list accu;
4359 m_minfo <- Array.of_list minfo;
4361 else
4362 let (s, _, _) as o = m_items.(n) in
4363 let accu, minfo =
4364 let first =
4365 try Str.search_forward re s 0
4366 with Not_found -> -1
4368 if first >= 0
4369 then o :: accu, (first, Str.match_end ()) :: minfo
4370 else accu, minfo
4372 loop accu minfo (n-1)
4374 loop [] [] (Array.length m_items - 1)
4376 method getminfo = m_minfo
4378 method denarrow =
4379 m_orig_items <- (
4380 if usebookmarks
4381 then Array.of_list state.bookmarks
4382 else state.outlines
4384 m_minfo <- m_orig_minfo;
4385 m_items <- m_orig_items
4387 method remove m =
4388 if usebookmarks
4389 then
4390 if m >= 0 && m < Array.length m_items
4391 then (
4392 m_hadremovals <- true;
4393 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4394 let n = if n >= m then n+1 else n in
4395 m_items.(n)
4399 method add_narrow_pattern pattern =
4400 m_narrow_patterns <- pattern :: m_narrow_patterns
4402 method del_narrow_pattern =
4403 match m_narrow_patterns with
4404 | _ :: rest -> m_narrow_patterns <- rest
4405 | [] -> ()
4407 method renarrow =
4408 self#denarrow;
4409 match m_narrow_patterns with
4410 | pattern :: [] -> self#narrow pattern; pattern
4411 | list ->
4412 List.fold_left (fun accu pattern ->
4413 self#narrow pattern;
4414 pattern ^ "\xe2\x80\xa6" ^ accu) "" list
4416 method calcactive anchor =
4417 let rely = getanchory anchor in
4418 let rec loop n best bestd =
4419 if n = Array.length m_items
4420 then best
4421 else
4422 let _, _, kind = m_items.(n) in
4423 match kind with
4424 | Oanchor anchor ->
4425 let orely = getanchory anchor in
4426 let d = abs (orely - rely) in
4427 if d < bestd
4428 then loop (n+1) n d
4429 else loop (n+1) best bestd
4430 | Onone | Oremote _ | Olaunch _ | Oremotedest _ | Ouri _ ->
4431 loop (n+1) best bestd
4433 loop 0 ~-1 max_int
4435 method reset anchor items =
4436 m_hadremovals <- false;
4437 if state.gen != m_gen
4438 then (
4439 m_orig_items <- items;
4440 m_items <- items;
4441 m_narrow_patterns <- [];
4442 m_minfo <- empty;
4443 m_orig_minfo <- empty;
4444 m_gen <- state.gen;
4446 else (
4447 if items != m_orig_items
4448 then (
4449 m_orig_items <- items;
4450 if m_narrow_patterns == []
4451 then m_items <- items;
4454 let active = self#calcactive anchor in
4455 m_active <- active;
4456 m_first <- firstof m_first active
4457 end)
4460 let enterselector usebookmarks =
4461 resetmstate ();
4462 let source = outlinesource usebookmarks in
4463 fun errmsg ->
4464 let outlines =
4465 if usebookmarks
4466 then Array.of_list state.bookmarks
4467 else state.outlines
4469 if Array.length outlines = 0
4470 then (
4471 showtext ' ' errmsg;
4473 else (
4474 state.text <- source#greetmsg;
4475 Wsi.setcursor Wsi.CURSOR_INHERIT;
4476 let anchor = getanchor () in
4477 source#reset anchor outlines;
4478 state.uioh <- coe (new outlinelistview ~source);
4479 G.postRedisplay "enter selector";
4483 let enteroutlinemode =
4484 let f = enterselector false in
4485 fun ()-> f "Document has no outline";
4488 let enterbookmarkmode =
4489 let f = enterselector true in
4490 fun () -> f "Document has no bookmarks (yet)";
4493 let color_of_string s =
4494 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4495 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4499 let color_to_string (r, g, b) =
4500 let r = truncate (r *. 256.0)
4501 and g = truncate (g *. 256.0)
4502 and b = truncate (b *. 256.0) in
4503 Printf.sprintf "%d/%d/%d" r g b
4506 let irect_of_string s =
4507 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4510 let irect_to_string (x0,y0,x1,y1) =
4511 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4514 let makecheckers () =
4515 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4516 following to say:
4517 converted by Issac Trotts. July 25, 2002 *)
4518 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4519 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4520 let id = GlTex.gen_texture () in
4521 GlTex.bind_texture `texture_2d id;
4522 GlPix.store (`unpack_alignment 1);
4523 GlTex.image2d image;
4524 List.iter (GlTex.parameter ~target:`texture_2d)
4525 [ `mag_filter `nearest; `min_filter `nearest ];
4529 let setcheckers enabled =
4530 match state.texid with
4531 | None ->
4532 if enabled then state.texid <- Some (makecheckers ())
4534 | Some texid ->
4535 if not enabled
4536 then (
4537 GlTex.delete_texture texid;
4538 state.texid <- None;
4542 let int_of_string_with_suffix s =
4543 let l = String.length s in
4544 let s1, shift =
4545 if l > 1
4546 then
4547 let suffix = Char.lowercase s.[l-1] in
4548 match suffix with
4549 | 'k' -> String.sub s 0 (l-1), 10
4550 | 'm' -> String.sub s 0 (l-1), 20
4551 | 'g' -> String.sub s 0 (l-1), 30
4552 | _ -> s, 0
4553 else s, 0
4555 let n = int_of_string s1 in
4556 let m = n lsl shift in
4557 if m < 0 || m < n
4558 then raise (Failure "value too large")
4559 else m
4562 let string_with_suffix_of_int n =
4563 if n = 0
4564 then "0"
4565 else
4566 let units = [(30, "G"); (20, "M"); (10, "K")] in
4567 let prettyint n =
4568 let rec loop s n =
4569 let h = n mod 1000 in
4570 let n = n / 1000 in
4571 if n = 0
4572 then string_of_int h ^ s
4573 else (
4574 let s = Printf.sprintf "_%03d%s" h s in
4575 loop s n
4578 loop "" n
4580 let rec find = function
4581 | [] -> prettyint n
4582 | (shift, suffix) :: rest ->
4583 if (n land ((1 lsl shift) - 1)) = 0
4584 then prettyint (n lsr shift) ^ suffix
4585 else find rest
4587 find units
4590 let defghyllscroll = (40, 8, 32);;
4591 let fastghyllscroll = (5,1,2);;
4592 let neatghyllscroll = (10,1,9);;
4593 let ghyllscroll_of_string s =
4594 match s with
4595 | "default" -> Some defghyllscroll
4596 | "fast" -> Some (5,1,2)
4597 | "neat" -> Some (10,1,9)
4598 | "" | "none" -> None
4599 | _ ->
4600 let (n,a,b) as nab =
4601 Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b) in
4602 if n <= a || n <= b || a >= b
4603 then error "invalid ghyll N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)"
4604 n a b;
4605 Some nab
4608 let ghyllscroll_to_string ((n, a, b) as nab) =
4609 (**) if nab = defghyllscroll then "default"
4610 else if nab = fastghyllscroll then "fast"
4611 else if nab = neatghyllscroll then "neat"
4612 else Printf.sprintf "%d,%d,%d" n a b;
4615 let describe_location () =
4616 let fn = page_of_y state.y in
4617 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4618 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4619 let percent =
4620 if maxy <= 0
4621 then 100.
4622 else (100. *. (float state.y /. float maxy))
4624 if fn = ln
4625 then
4626 Printf.sprintf "page %d of %d [%.2f%%]"
4627 (fn+1) state.pagecount percent
4628 else
4629 Printf.sprintf
4630 "pages %d-%d of %d [%.2f%%]"
4631 (fn+1) (ln+1) state.pagecount percent
4634 let setpresentationmode v =
4635 let n = page_of_y state.y in
4636 state.anchor <- (n, 0.0, 1.0);
4637 conf.presentation <- v;
4638 if conf.fitmodel = FitPage
4639 then reqlayout conf.angle conf.fitmodel;
4640 represent ();
4643 let enterinfomode =
4644 let btos b = if b then "\xe2\x88\x9a" else "" in
4645 let showextended = ref false in
4646 let leave mode = function
4647 | Confirm -> state.mode <- mode
4648 | Cancel -> state.mode <- mode in
4649 let src =
4650 (object
4651 val mutable m_first_time = true
4652 val mutable m_l = []
4653 val mutable m_a = [||]
4654 val mutable m_prev_uioh = nouioh
4655 val mutable m_prev_mode = View
4657 inherit lvsourcebase
4659 method reset prev_mode prev_uioh =
4660 m_a <- Array.of_list (List.rev m_l);
4661 m_l <- [];
4662 m_prev_mode <- prev_mode;
4663 m_prev_uioh <- prev_uioh;
4664 if m_first_time
4665 then (
4666 let rec loop n =
4667 if n >= Array.length m_a
4668 then ()
4669 else
4670 match m_a.(n) with
4671 | _, _, _, Action _ -> m_active <- n
4672 | _ -> loop (n+1)
4674 loop 0;
4675 m_first_time <- false;
4678 method int name get set =
4679 m_l <-
4680 (name, `int get, 1, Action (
4681 fun u ->
4682 let ondone s =
4683 try set (int_of_string s)
4684 with exn ->
4685 state.text <- Printf.sprintf "bad integer `%s': %s"
4686 s (exntos exn)
4688 state.text <- "";
4689 let te = name ^ ": ", "", None, intentry, ondone, true in
4690 state.mode <- Textentry (te, leave m_prev_mode);
4692 )) :: m_l
4694 method int_with_suffix name get set =
4695 m_l <-
4696 (name, `intws get, 1, Action (
4697 fun u ->
4698 let ondone s =
4699 try set (int_of_string_with_suffix s)
4700 with exn ->
4701 state.text <- Printf.sprintf "bad integer `%s': %s"
4702 s (exntos exn)
4704 state.text <- "";
4705 let te =
4706 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4708 state.mode <- Textentry (te, leave m_prev_mode);
4710 )) :: m_l
4712 method bool ?(offset=1) ?(btos=btos) name get set =
4713 m_l <-
4714 (name, `bool (btos, get), offset, Action (
4715 fun u ->
4716 let v = get () in
4717 set (not v);
4719 )) :: m_l
4721 method color name get set =
4722 m_l <-
4723 (name, `color get, 1, Action (
4724 fun u ->
4725 let invalid = (nan, nan, nan) in
4726 let ondone s =
4727 let c =
4728 try color_of_string s
4729 with exn ->
4730 state.text <- Printf.sprintf "bad color `%s': %s"
4731 s (exntos exn);
4732 invalid
4734 if c <> invalid
4735 then set c;
4737 let te = name ^ ": ", "", None, textentry, ondone, true in
4738 state.text <- color_to_string (get ());
4739 state.mode <- Textentry (te, leave m_prev_mode);
4741 )) :: m_l
4743 method string name get set =
4744 m_l <-
4745 (name, `string get, 1, Action (
4746 fun u ->
4747 let ondone s = set s in
4748 let te = name ^ ": ", "", None, textentry, ondone, true in
4749 state.mode <- Textentry (te, leave m_prev_mode);
4751 )) :: m_l
4753 method colorspace name get set =
4754 m_l <-
4755 (name, `string get, 1, Action (
4756 fun _ ->
4757 let source =
4758 (object
4759 inherit lvsourcebase
4761 initializer
4762 m_active <- CSTE.to_int conf.colorspace;
4763 m_first <- 0;
4765 method getitemcount =
4766 Array.length CSTE.names
4767 method getitem n =
4768 (CSTE.names.(n), 0)
4769 method exit ~uioh ~cancel ~active ~first ~pan =
4770 ignore (uioh, first, pan);
4771 if not cancel then set active;
4772 None
4773 method hasaction _ = true
4774 end)
4776 state.text <- "";
4777 let modehash = findkeyhash conf "info" in
4778 coe (new listview ~source ~trusted:true ~modehash)
4779 )) :: m_l
4781 method paxmark name get set =
4782 m_l <-
4783 (name, `string get, 1, Action (
4784 fun _ ->
4785 let source =
4786 (object
4787 inherit lvsourcebase
4789 initializer
4790 m_active <- MTE.to_int conf.paxmark;
4791 m_first <- 0;
4793 method getitemcount = Array.length MTE.names
4794 method getitem n = (MTE.names.(n), 0)
4795 method exit ~uioh ~cancel ~active ~first ~pan =
4796 ignore (uioh, first, pan);
4797 if not cancel then set active;
4798 None
4799 method hasaction _ = true
4800 end)
4802 state.text <- "";
4803 let modehash = findkeyhash conf "info" in
4804 coe (new listview ~source ~trusted:true ~modehash)
4805 )) :: m_l
4807 method fitmodel name get set =
4808 m_l <-
4809 (name, `string get, 1, Action (
4810 fun _ ->
4811 let source =
4812 (object
4813 inherit lvsourcebase
4815 initializer
4816 m_active <- FMTE.to_int conf.fitmodel;
4817 m_first <- 0;
4819 method getitemcount = Array.length FMTE.names
4820 method getitem n = (FMTE.names.(n), 0)
4821 method exit ~uioh ~cancel ~active ~first ~pan =
4822 ignore (uioh, first, pan);
4823 if not cancel then set active;
4824 None
4825 method hasaction _ = true
4826 end)
4828 state.text <- "";
4829 let modehash = findkeyhash conf "info" in
4830 coe (new listview ~source ~trusted:true ~modehash)
4831 )) :: m_l
4833 method caption s offset =
4834 m_l <- (s, `empty, offset, Noaction) :: m_l
4836 method caption2 s f offset =
4837 m_l <- (s, `string f, offset, Noaction) :: m_l
4839 method getitemcount = Array.length m_a
4841 method getitem n =
4842 let tostr = function
4843 | `int f -> string_of_int (f ())
4844 | `intws f -> string_with_suffix_of_int (f ())
4845 | `string f -> f ()
4846 | `color f -> color_to_string (f ())
4847 | `bool (btos, f) -> btos (f ())
4848 | `empty -> ""
4850 let name, t, offset, _ = m_a.(n) in
4851 ((let s = tostr t in
4852 if nonemptystr s
4853 then Printf.sprintf "%s\t%s" name s
4854 else name),
4855 offset)
4857 method exit ~uioh ~cancel ~active ~first ~pan =
4858 let uiohopt =
4859 if not cancel
4860 then (
4861 let uioh =
4862 match m_a.(active) with
4863 | _, _, _, Action f -> f uioh
4864 | _ -> uioh
4866 Some uioh
4868 else None
4870 m_active <- active;
4871 m_first <- first;
4872 m_pan <- pan;
4873 uiohopt
4875 method hasaction n =
4876 match m_a.(n) with
4877 | _, _, _, Action _ -> true
4878 | _ -> false
4879 end)
4881 let rec fillsrc prevmode prevuioh =
4882 let sep () = src#caption "" 0 in
4883 let colorp name get set =
4884 src#string name
4885 (fun () -> color_to_string (get ()))
4886 (fun v ->
4888 let c = color_of_string v in
4889 set c
4890 with exn ->
4891 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4894 let oldmode = state.mode in
4895 let birdseye = isbirdseye state.mode in
4897 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4899 src#bool "presentation mode"
4900 (fun () -> conf.presentation)
4901 (fun v -> setpresentationmode v);
4903 src#bool "ignore case in searches"
4904 (fun () -> conf.icase)
4905 (fun v -> conf.icase <- v);
4907 src#bool "preload"
4908 (fun () -> conf.preload)
4909 (fun v -> conf.preload <- v);
4911 src#bool "highlight links"
4912 (fun () -> conf.hlinks)
4913 (fun v -> conf.hlinks <- v);
4915 src#bool "under info"
4916 (fun () -> conf.underinfo)
4917 (fun v -> conf.underinfo <- v);
4919 src#bool "persistent bookmarks"
4920 (fun () -> conf.savebmarks)
4921 (fun v -> conf.savebmarks <- v);
4923 src#fitmodel "fit model"
4924 (fun () -> FMTE.to_string conf.fitmodel)
4925 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4927 src#bool "trim margins"
4928 (fun () -> conf.trimmargins)
4929 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4931 src#bool "persistent location"
4932 (fun () -> conf.jumpback)
4933 (fun v -> conf.jumpback <- v);
4935 sep ();
4936 src#int "inter-page space"
4937 (fun () -> conf.interpagespace)
4938 (fun n ->
4939 conf.interpagespace <- n;
4940 docolumns conf.columns;
4941 let pageno, py =
4942 match state.layout with
4943 | [] -> 0, 0
4944 | l :: _ ->
4945 l.pageno, l.pagey
4947 state.maxy <- calcheight ();
4948 let y = getpagey pageno in
4949 gotoy (y + py)
4952 src#int "page bias"
4953 (fun () -> conf.pagebias)
4954 (fun v -> conf.pagebias <- v);
4956 src#int "scroll step"
4957 (fun () -> conf.scrollstep)
4958 (fun n -> conf.scrollstep <- n);
4960 src#int "horizontal scroll step"
4961 (fun () -> conf.hscrollstep)
4962 (fun v -> conf.hscrollstep <- v);
4964 src#int "auto scroll step"
4965 (fun () ->
4966 match state.autoscroll with
4967 | Some step -> step
4968 | _ -> conf.autoscrollstep)
4969 (fun n ->
4970 let n = boundastep state.winh n in
4971 if state.autoscroll <> None
4972 then state.autoscroll <- Some n;
4973 conf.autoscrollstep <- n);
4975 src#int "zoom"
4976 (fun () -> truncate (conf.zoom *. 100.))
4977 (fun v -> setzoom ((float v) /. 100.));
4979 src#int "rotation"
4980 (fun () -> conf.angle)
4981 (fun v -> reqlayout v conf.fitmodel);
4983 src#int "scroll bar width"
4984 (fun () -> conf.scrollbw)
4985 (fun v ->
4986 conf.scrollbw <- v;
4987 reshape state.winw state.winh;
4990 src#int "scroll handle height"
4991 (fun () -> conf.scrollh)
4992 (fun v -> conf.scrollh <- v;);
4994 src#int "thumbnail width"
4995 (fun () -> conf.thumbw)
4996 (fun v ->
4997 conf.thumbw <- min 4096 v;
4998 match oldmode with
4999 | Birdseye beye ->
5000 leavebirdseye beye false;
5001 enterbirdseye ()
5002 | _ -> ()
5005 let mode = state.mode in
5006 src#string "columns"
5007 (fun () ->
5008 match conf.columns with
5009 | Csingle _ -> "1"
5010 | Cmulti (multi, _) -> multicolumns_to_string multi
5011 | Csplit (count, _) -> "-" ^ string_of_int count
5013 (fun v ->
5014 let n, a, b = multicolumns_of_string v in
5015 setcolumns mode n a b);
5017 sep ();
5018 src#caption "Pixmap cache" 0;
5019 src#int_with_suffix "size (advisory)"
5020 (fun () -> conf.memlimit)
5021 (fun v -> conf.memlimit <- v);
5023 src#caption2 "used"
5024 (fun () -> Printf.sprintf "%s bytes, %d tiles"
5025 (string_with_suffix_of_int state.memused)
5026 (Hashtbl.length state.tilemap)) 1;
5028 sep ();
5029 src#caption "Layout" 0;
5030 src#caption2 "Dimension"
5031 (fun () ->
5032 Printf.sprintf "%dx%d (virtual %dx%d)"
5033 state.winw state.winh
5034 state.w state.maxy)
5036 if conf.debug
5037 then
5038 src#caption2 "Position" (fun () ->
5039 Printf.sprintf "%dx%d" state.x state.y
5041 else
5042 src#caption2 "Position" (fun () -> describe_location ()) 1
5045 sep ();
5046 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
5047 "Save these parameters as global defaults at exit"
5048 (fun () -> conf.bedefault)
5049 (fun v -> conf.bedefault <- v)
5052 sep ();
5053 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
5054 src#bool ~offset:0 ~btos "Extended parameters"
5055 (fun () -> !showextended)
5056 (fun v -> showextended := v; fillsrc prevmode prevuioh);
5057 if !showextended
5058 then (
5059 src#bool "checkers"
5060 (fun () -> conf.checkers)
5061 (fun v -> conf.checkers <- v; setcheckers v);
5062 src#bool "update cursor"
5063 (fun () -> conf.updatecurs)
5064 (fun v -> conf.updatecurs <- v);
5065 src#bool "verbose"
5066 (fun () -> conf.verbose)
5067 (fun v -> conf.verbose <- v);
5068 src#bool "invert colors"
5069 (fun () -> conf.invert)
5070 (fun v -> conf.invert <- v);
5071 src#bool "max fit"
5072 (fun () -> conf.maxhfit)
5073 (fun v -> conf.maxhfit <- v);
5074 src#bool "redirect stderr"
5075 (fun () -> conf.redirectstderr)
5076 (fun v -> conf.redirectstderr <- v; redirectstderr ());
5077 src#bool "pax mode"
5078 (fun () -> conf.pax != None)
5079 (fun v ->
5080 if v
5081 then conf.pax <- Some (ref (now (), 0, 0))
5082 else conf.pax <- None);
5083 src#string "uri launcher"
5084 (fun () -> conf.urilauncher)
5085 (fun v -> conf.urilauncher <- v);
5086 src#string "path launcher"
5087 (fun () -> conf.pathlauncher)
5088 (fun v -> conf.pathlauncher <- v);
5089 src#string "tile size"
5090 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
5091 (fun v ->
5093 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
5094 conf.tilew <- max 64 w;
5095 conf.tileh <- max 64 h;
5096 flushtiles ();
5097 with exn ->
5098 state.text <- Printf.sprintf "bad tile size `%s': %s"
5099 v (exntos exn)
5101 src#int "texture count"
5102 (fun () -> conf.texcount)
5103 (fun v ->
5104 if realloctexts v
5105 then conf.texcount <- v
5106 else showtext '!' " Failed to set texture count please retry later"
5108 src#int "slice height"
5109 (fun () -> conf.sliceheight)
5110 (fun v ->
5111 conf.sliceheight <- v;
5112 wcmd "sliceh %d" conf.sliceheight;
5114 src#int "anti-aliasing level"
5115 (fun () -> conf.aalevel)
5116 (fun v ->
5117 conf.aalevel <- bound v 0 8;
5118 state.anchor <- getanchor ();
5119 opendoc state.path state.password;
5121 src#string "page scroll scaling factor"
5122 (fun () -> string_of_float conf.pgscale)
5123 (fun v ->
5125 let s = float_of_string v in
5126 conf.pgscale <- s
5127 with exn ->
5128 state.text <- Printf.sprintf
5129 "bad page scroll scaling factor `%s': %s" v (exntos exn)
5132 src#int "ui font size"
5133 (fun () -> fstate.fontsize)
5134 (fun v -> setfontsize (bound v 5 100));
5135 src#int "hint font size"
5136 (fun () -> conf.hfsize)
5137 (fun v -> conf.hfsize <- bound v 5 100);
5138 colorp "background color"
5139 (fun () -> conf.bgcolor)
5140 (fun v -> conf.bgcolor <- v);
5141 src#bool "crop hack"
5142 (fun () -> conf.crophack)
5143 (fun v -> conf.crophack <- v);
5144 src#string "trim fuzz"
5145 (fun () -> irect_to_string conf.trimfuzz)
5146 (fun v ->
5148 conf.trimfuzz <- irect_of_string v;
5149 if conf.trimmargins
5150 then settrim true conf.trimfuzz;
5151 with exn ->
5152 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
5154 src#string "throttle"
5155 (fun () ->
5156 match conf.maxwait with
5157 | None -> "show place holder if page is not ready"
5158 | Some time ->
5159 if time = infinity
5160 then "wait for page to fully render"
5161 else
5162 "wait " ^ string_of_float time
5163 ^ " seconds before showing placeholder"
5165 (fun v ->
5167 let f = float_of_string v in
5168 if f <= 0.0
5169 then conf.maxwait <- None
5170 else conf.maxwait <- Some f
5171 with exn ->
5172 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
5174 src#string "ghyll scroll"
5175 (fun () ->
5176 match conf.ghyllscroll with
5177 | None -> ""
5178 | Some nab -> ghyllscroll_to_string nab
5180 (fun v ->
5181 try conf.ghyllscroll <- ghyllscroll_of_string v
5182 with exn ->
5183 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
5185 src#string "selection command"
5186 (fun () -> conf.selcmd)
5187 (fun v -> conf.selcmd <- v);
5188 src#string "synctex command"
5189 (fun () -> conf.stcmd)
5190 (fun v -> conf.stcmd <- v);
5191 src#string "pax command"
5192 (fun () -> conf.paxcmd)
5193 (fun v -> conf.paxcmd <- v);
5194 src#colorspace "color space"
5195 (fun () -> CSTE.to_string conf.colorspace)
5196 (fun v ->
5197 conf.colorspace <- CSTE.of_int v;
5198 wcmd "cs %d" v;
5199 load state.layout;
5201 src#paxmark "pax mark method"
5202 (fun () -> MTE.to_string conf.paxmark)
5203 (fun v -> conf.paxmark <- MTE.of_int v);
5204 if pbousable ()
5205 then
5206 src#bool "use PBO"
5207 (fun () -> conf.usepbo)
5208 (fun v -> conf.usepbo <- v);
5209 src#bool "mouse wheel scrolls pages"
5210 (fun () -> conf.wheelbypage)
5211 (fun v -> conf.wheelbypage <- v);
5212 src#bool "open remote links in a new instance"
5213 (fun () -> conf.riani)
5214 (fun v -> conf.riani <- v);
5217 sep ();
5218 src#caption "Document" 0;
5219 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
5220 src#caption2 "Pages"
5221 (fun () -> string_of_int state.pagecount) 1;
5222 src#caption2 "Dimensions"
5223 (fun () -> string_of_int (List.length state.pdims)) 1;
5224 if conf.trimmargins
5225 then (
5226 sep ();
5227 src#caption "Trimmed margins" 0;
5228 src#caption2 "Dimensions"
5229 (fun () -> string_of_int (List.length state.pdims)) 1;
5232 sep ();
5233 src#caption "OpenGL" 0;
5234 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
5235 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
5237 sep ();
5238 src#caption "Location" 0;
5239 if nonemptystr state.origin
5240 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
5241 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
5243 src#reset prevmode prevuioh;
5245 fun () ->
5246 state.text <- "";
5247 resetmstate ();
5248 let prevmode = state.mode
5249 and prevuioh = state.uioh in
5250 fillsrc prevmode prevuioh;
5251 let source = (src :> lvsource) in
5252 let modehash = findkeyhash conf "info" in
5253 state.uioh <- coe (object (self)
5254 inherit listview ~source ~trusted:true ~modehash as super
5255 val mutable m_prevmemused = 0
5256 method infochanged = function
5257 | Memused ->
5258 if m_prevmemused != state.memused
5259 then (
5260 m_prevmemused <- state.memused;
5261 G.postRedisplay "memusedchanged";
5263 | Pdim -> G.postRedisplay "pdimchanged"
5264 | Docinfo -> fillsrc prevmode prevuioh
5266 method key key mask =
5267 if not (Wsi.withctrl mask)
5268 then
5269 match key with
5270 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
5271 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
5272 | _ -> super#key key mask
5273 else super#key key mask
5274 end);
5275 G.postRedisplay "info";
5278 let enterhelpmode =
5279 let source =
5280 (object
5281 inherit lvsourcebase
5282 method getitemcount = Array.length state.help
5283 method getitem n =
5284 let s, l, _ = state.help.(n) in
5285 (s, l)
5287 method exit ~uioh ~cancel ~active ~first ~pan =
5288 let optuioh =
5289 if not cancel
5290 then (
5291 match state.help.(active) with
5292 | _, _, Action f -> Some (f uioh)
5293 | _ -> Some (uioh)
5295 else None
5297 m_active <- active;
5298 m_first <- first;
5299 m_pan <- pan;
5300 optuioh
5302 method hasaction n =
5303 match state.help.(n) with
5304 | _, _, Action _ -> true
5305 | _ -> false
5307 initializer
5308 m_active <- -1
5309 end)
5310 in fun () ->
5311 let modehash = findkeyhash conf "help" in
5312 resetmstate ();
5313 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
5314 G.postRedisplay "help";
5317 let entermsgsmode =
5318 let msgsource =
5319 let re = Str.regexp "[\r\n]" in
5320 (object
5321 inherit lvsourcebase
5322 val mutable m_items = [||]
5324 method getitemcount = 1 + Array.length m_items
5326 method getitem n =
5327 if n = 0
5328 then "[Clear]", 0
5329 else m_items.(n-1), 0
5331 method exit ~uioh ~cancel ~active ~first ~pan =
5332 ignore uioh;
5333 if not cancel
5334 then (
5335 if active = 0
5336 then Buffer.clear state.errmsgs;
5338 m_active <- active;
5339 m_first <- first;
5340 m_pan <- pan;
5341 None
5343 method hasaction n =
5344 n = 0
5346 method reset =
5347 state.newerrmsgs <- false;
5348 let l = Str.split re (Buffer.contents state.errmsgs) in
5349 m_items <- Array.of_list l
5351 initializer
5352 m_active <- 0
5353 end)
5354 in fun () ->
5355 state.text <- "";
5356 resetmstate ();
5357 msgsource#reset;
5358 let source = (msgsource :> lvsource) in
5359 let modehash = findkeyhash conf "listview" in
5360 state.uioh <- coe (object
5361 inherit listview ~source ~trusted:false ~modehash as super
5362 method display =
5363 if state.newerrmsgs
5364 then msgsource#reset;
5365 super#display
5366 end);
5367 G.postRedisplay "msgs";
5370 let quickbookmark ?title () =
5371 match state.layout with
5372 | [] -> ()
5373 | l :: _ ->
5374 let title =
5375 match title with
5376 | None ->
5377 let sec = Unix.gettimeofday () in
5378 let tm = Unix.localtime sec in
5379 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
5380 (l.pageno+1)
5381 tm.Unix.tm_mday
5382 tm.Unix.tm_mon
5383 (tm.Unix.tm_year + 1900)
5384 tm.Unix.tm_hour
5385 tm.Unix.tm_min
5386 | Some title -> title
5388 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5391 let setautoscrollspeed step goingdown =
5392 let incr = max 1 ((abs step) / 2) in
5393 let incr = if goingdown then incr else -incr in
5394 let astep = boundastep state.winh (step + incr) in
5395 state.autoscroll <- Some astep;
5398 let canpan () =
5399 match conf.columns with
5400 | Csplit _ -> true
5401 | _ -> state.x != 0 || conf.zoom > 1.0
5404 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5406 let existsinrow pageno (columns, coverA, coverB) p =
5407 let last = ((pageno - coverA) mod columns) + columns in
5408 let rec any = function
5409 | [] -> false
5410 | l :: rest ->
5411 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5412 then p l
5413 else (
5414 if not (p l)
5415 then (if l.pageno = last then false else any rest)
5416 else true
5419 any state.layout
5422 let nextpage () =
5423 match state.layout with
5424 | [] ->
5425 let pageno = page_of_y state.y in
5426 gotoghyll (getpagey (pageno+1))
5427 | l :: rest ->
5428 match conf.columns with
5429 | Csingle _ ->
5430 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5431 then
5432 let y = clamp (pgscale state.winh) in
5433 gotoghyll y
5434 else
5435 let pageno = min (l.pageno+1) (state.pagecount-1) in
5436 gotoghyll (getpagey pageno)
5437 | Cmulti ((c, _, _) as cl, _) ->
5438 if conf.presentation
5439 && (existsinrow l.pageno cl
5440 (fun l -> l.pageh > l.pagey + l.pagevh))
5441 then
5442 let y = clamp (pgscale state.winh) in
5443 gotoghyll y
5444 else
5445 let pageno = min (l.pageno+c) (state.pagecount-1) in
5446 gotoghyll (getpagey pageno)
5447 | Csplit (n, _) ->
5448 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5449 then
5450 let pagey, pageh = getpageyh l.pageno in
5451 let pagey = pagey + pageh * l.pagecol in
5452 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5453 gotoghyll (pagey + pageh + ips)
5456 let prevpage () =
5457 match state.layout with
5458 | [] ->
5459 let pageno = page_of_y state.y in
5460 gotoghyll (getpagey (pageno-1))
5461 | l :: _ ->
5462 match conf.columns with
5463 | Csingle _ ->
5464 if conf.presentation && l.pagey != 0
5465 then
5466 gotoghyll (clamp (pgscale ~-(state.winh)))
5467 else
5468 let pageno = max 0 (l.pageno-1) in
5469 gotoghyll (getpagey pageno)
5470 | Cmulti ((c, _, coverB) as cl, _) ->
5471 if conf.presentation &&
5472 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5473 then
5474 gotoghyll (clamp (pgscale ~-(state.winh)))
5475 else
5476 let decr =
5477 if l.pageno = state.pagecount - coverB
5478 then 1
5479 else c
5481 let pageno = max 0 (l.pageno-decr) in
5482 gotoghyll (getpagey pageno)
5483 | Csplit (n, _) ->
5484 let y =
5485 if l.pagecol = 0
5486 then
5487 if l.pageno = 0
5488 then l.pagey
5489 else
5490 let pageno = max 0 (l.pageno-1) in
5491 let pagey, pageh = getpageyh pageno in
5492 pagey + (n-1)*pageh
5493 else
5494 let pagey, pageh = getpageyh l.pageno in
5495 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5497 gotoghyll y
5500 let viewkeyboard key mask =
5501 let enttext te =
5502 let mode = state.mode in
5503 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5504 state.text <- "";
5505 enttext ();
5506 G.postRedisplay "view:enttext"
5508 let ctrl = Wsi.withctrl mask in
5509 let key =
5510 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5512 match key with
5513 | 81 -> (* Q *)
5514 exit 0
5516 | 0xff63 -> (* insert *)
5517 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5518 then (
5519 state.mode <- LinkNav (Ltgendir 0);
5520 gotoy state.y;
5522 else showtext '!' "Keyboard link navigation does not work under rotation"
5524 | 0xff1b | 113 -> (* escape / q *)
5525 begin match state.mstate with
5526 | Mzoomrect _ ->
5527 resetmstate ();
5528 G.postRedisplay "kill zoom rect";
5529 | _ ->
5530 begin match state.mode with
5531 | LinkNav _ ->
5532 state.mode <- View;
5533 G.postRedisplay "esc leave linknav"
5534 | _ ->
5535 match state.ranchors with
5536 | [] -> raise Quit
5537 | (path, password, anchor, origin) :: rest ->
5538 state.ranchors <- rest;
5539 state.anchor <- anchor;
5540 state.origin <- origin;
5541 state.nameddest <- "";
5542 opendoc path password
5543 end;
5544 end;
5546 | 0xff08 -> (* backspace *)
5547 gotoghyll (getnav ~-1)
5549 | 111 -> (* o *)
5550 enteroutlinemode ()
5552 | 117 -> (* u *)
5553 state.rects <- [];
5554 state.text <- "";
5555 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
5556 G.postRedisplay "dehighlight";
5558 | 47 | 63 -> (* / ? *)
5559 let ondone isforw s =
5560 cbput state.hists.pat s;
5561 state.searchpattern <- s;
5562 search s isforw
5564 let s = String.create 1 in
5565 s.[0] <- Char.chr key;
5566 enttext (s, "", Some (onhist state.hists.pat),
5567 textentry, ondone (key = 47), true)
5569 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5570 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5571 setzoom (conf.zoom +. incr)
5573 | 43 | 0xffab -> (* + *)
5574 let ondone s =
5575 let n =
5576 try int_of_string s with exc ->
5577 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5578 max_int
5580 if n != max_int
5581 then (
5582 conf.pagebias <- n;
5583 state.text <- "page bias is now " ^ string_of_int n;
5586 enttext ("page bias: ", "", None, intentry, ondone, true)
5588 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5589 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5590 setzoom (max 0.01 (conf.zoom -. decr))
5592 | 45 | 0xffad -> (* - *)
5593 let ondone msg = state.text <- msg in
5594 enttext (
5595 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5596 optentry state.mode, ondone, true
5599 | 48 when ctrl -> (* ctrl-0 *)
5600 if conf.zoom = 1.0
5601 then (
5602 state.x <- 0;
5603 gotoy state.y
5605 else setzoom 1.0
5607 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5608 let cols =
5609 match conf.columns with
5610 | Csingle _ | Cmulti _ -> 1
5611 | Csplit (n, _) -> n
5613 let h = state.winh -
5614 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5616 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5617 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5618 then setzoom zoom
5620 | 51 when ctrl -> (* ctrl-3 *)
5621 let fm =
5622 match conf.fitmodel with
5623 | FitWidth -> FitProportional
5624 | FitProportional -> FitPage
5625 | FitPage -> FitWidth
5627 state.text <- "fit model: " ^ FMTE.to_string fm;
5628 reqlayout conf.angle fm
5630 | 0xffc6 -> (* f9 *)
5631 togglebirdseye ()
5633 | 57 when ctrl -> (* ctrl-9 *)
5634 togglebirdseye ()
5636 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5637 when not ctrl -> (* 0..9 *)
5638 let ondone s =
5639 let n =
5640 try int_of_string s with exc ->
5641 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5644 if n >= 0
5645 then (
5646 addnav ();
5647 cbput state.hists.pag (string_of_int n);
5648 gotopage1 (n + conf.pagebias - 1) 0;
5651 let pageentry text key =
5652 match Char.unsafe_chr key with
5653 | 'g' -> TEdone text
5654 | _ -> intentry text key
5656 let text = "x" in text.[0] <- Char.chr key;
5657 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5659 | 98 -> (* b *)
5660 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5661 reshape state.winw state.winh;
5663 | 66 -> (* B *)
5664 state.bzoom <- not state.bzoom;
5665 state.rects <- [];
5666 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5668 | 108 -> (* l *)
5669 conf.hlinks <- not conf.hlinks;
5670 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5671 G.postRedisplay "toggle highlightlinks";
5673 | 70 -> (* F *)
5674 state.glinks <- true;
5675 let mode = state.mode in
5676 state.mode <- Textentry (
5677 (":", "", None, linknentry, linkndone gotounder, false),
5678 (fun _ ->
5679 state.glinks <- false;
5680 state.mode <- mode)
5682 state.text <- "";
5683 G.postRedisplay "view:linkent(F)"
5685 | 121 -> (* y *)
5686 state.glinks <- true;
5687 let mode = state.mode in
5688 state.mode <- Textentry (
5690 ":", "", None, linknentry, linkndone (fun under ->
5691 selstring (undertext under);
5692 ), false
5694 fun _ ->
5695 state.glinks <- false;
5696 state.mode <- mode
5698 state.text <- "";
5699 G.postRedisplay "view:linkent"
5701 | 97 -> (* a *)
5702 begin match state.autoscroll with
5703 | Some step ->
5704 conf.autoscrollstep <- step;
5705 state.autoscroll <- None
5706 | None ->
5707 if conf.autoscrollstep = 0
5708 then state.autoscroll <- Some 1
5709 else state.autoscroll <- Some conf.autoscrollstep
5712 | 112 when ctrl -> (* ctrl-p *)
5713 launchpath ()
5715 | 80 -> (* P *)
5716 setpresentationmode (not conf.presentation);
5717 showtext ' ' ("presentation mode " ^
5718 if conf.presentation then "on" else "off");
5720 | 102 -> (* f *)
5721 if List.mem Wsi.Fullscreen state.winstate
5722 then Wsi.reshape conf.cwinw conf.cwinh
5723 else Wsi.fullscreen ()
5725 | 112 | 78 -> (* p|N *)
5726 search state.searchpattern false
5728 | 110 | 0xffc0 -> (* n|F3 *)
5729 search state.searchpattern true
5731 | 116 -> (* t *)
5732 begin match state.layout with
5733 | [] -> ()
5734 | l :: _ ->
5735 gotoghyll (getpagey l.pageno)
5738 | 32 -> (* space *)
5739 nextpage ()
5741 | 0xff9f | 0xffff -> (* delete *)
5742 prevpage ()
5744 | 61 -> (* = *)
5745 showtext ' ' (describe_location ());
5747 | 119 -> (* w *)
5748 begin match state.layout with
5749 | [] -> ()
5750 | l :: _ ->
5751 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5752 G.postRedisplay "w"
5755 | 39 -> (* ' *)
5756 enterbookmarkmode ()
5758 | 104 | 0xffbe -> (* h|F1 *)
5759 enterhelpmode ()
5761 | 105 -> (* i *)
5762 enterinfomode ()
5764 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5765 entermsgsmode ()
5767 | 109 -> (* m *)
5768 let ondone s =
5769 match state.layout with
5770 | l :: _ ->
5771 if nonemptystr s
5772 then
5773 state.bookmarks <-
5774 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5775 | _ -> ()
5777 enttext ("bookmark: ", "", None, textentry, ondone, true)
5779 | 126 -> (* ~ *)
5780 quickbookmark ();
5781 showtext ' ' "Quick bookmark added";
5783 | 122 -> (* z *)
5784 begin match state.layout with
5785 | l :: _ ->
5786 let rect = getpdimrect l.pagedimno in
5787 let w, h =
5788 if conf.crophack
5789 then
5790 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5791 truncate (1.2 *. (rect.(3) -. rect.(0))))
5792 else
5793 (truncate (rect.(1) -. rect.(0)),
5794 truncate (rect.(3) -. rect.(0)))
5796 let w = truncate ((float w)*.conf.zoom)
5797 and h = truncate ((float h)*.conf.zoom) in
5798 if w != 0 && h != 0
5799 then (
5800 state.anchor <- getanchor ();
5801 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5803 G.postRedisplay "z";
5805 | [] -> ()
5808 | 120 -> (* x *)
5809 state.roam ()
5810 | 60 | 62 -> (* < > *)
5811 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5813 | 91 | 93 -> (* [ ] *)
5814 conf.colorscale <-
5815 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5817 G.postRedisplay "brightness";
5819 | 99 when state.mode = View -> (* [alt-]c *)
5820 if Wsi.withalt mask
5821 then (
5822 if conf.zoom > 1.0
5823 then
5824 let m = (wadjsb state.winw - state.w) / 2 in
5825 state.x <- m;
5826 gotoy_and_clear_text state.y
5828 else
5829 let (c, a, b), z =
5830 match state.prevcolumns with
5831 | None -> (1, 0, 0), 1.0
5832 | Some (columns, z) ->
5833 let cab =
5834 match columns with
5835 | Csplit (c, _) -> -c, 0, 0
5836 | Cmulti ((c, a, b), _) -> c, a, b
5837 | Csingle _ -> 1, 0, 0
5839 cab, z
5841 setcolumns View c a b;
5842 setzoom z
5844 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
5845 -> (* ctrl-shift- (kp) [up|down] *)
5846 let zoom, x = state.prevzoom in
5847 setzoom zoom;
5848 state.x <- x;
5850 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5851 begin match state.autoscroll with
5852 | None ->
5853 begin match state.mode with
5854 | Birdseye beye -> upbirdseye 1 beye
5855 | _ ->
5856 if ctrl
5857 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5858 else (
5859 if not (Wsi.withshift mask) && conf.presentation
5860 then prevpage ()
5861 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5864 | Some n ->
5865 setautoscrollspeed n false
5868 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5869 begin match state.autoscroll with
5870 | None ->
5871 begin match state.mode with
5872 | Birdseye beye -> downbirdseye 1 beye
5873 | _ ->
5874 if ctrl
5875 then gotoy_and_clear_text (clamp (state.winh/2))
5876 else (
5877 if not (Wsi.withshift mask) && conf.presentation
5878 then nextpage ()
5879 else gotoy_and_clear_text (clamp conf.scrollstep)
5882 | Some n ->
5883 setautoscrollspeed n true
5886 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5887 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5888 if canpan ()
5889 then
5890 let dx =
5891 if ctrl
5892 then state.winw / 2
5893 else conf.hscrollstep
5895 let dx = if key = 0xff51 || key = 0xff96 then dx else -dx in
5896 state.x <- panbound (state.x + dx);
5897 gotoy_and_clear_text state.y
5898 else (
5899 state.text <- "";
5900 G.postRedisplay "left/right"
5903 | 0xff55 | 0xff9a -> (* (kp) prior *)
5904 let y =
5905 if ctrl
5906 then
5907 match state.layout with
5908 | [] -> state.y
5909 | l :: _ -> state.y - l.pagey
5910 else
5911 clamp (pgscale (-state.winh))
5913 gotoghyll y
5915 | 0xff56 | 0xff9b -> (* (kp) next *)
5916 let y =
5917 if ctrl
5918 then
5919 match List.rev state.layout with
5920 | [] -> state.y
5921 | l :: _ -> getpagey l.pageno
5922 else
5923 clamp (pgscale state.winh)
5925 gotoghyll y
5927 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5928 gotoghyll 0
5929 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5930 gotoghyll (clamp state.maxy)
5932 | 0xff53 | 0xff98
5933 when Wsi.withalt mask -> (* alt-(kp) right *)
5934 gotoghyll (getnav 1)
5935 | 0xff51 | 0xff96
5936 when Wsi.withalt mask -> (* alt-(kp) left *)
5937 gotoghyll (getnav ~-1)
5939 | 114 -> (* r *)
5940 reload ()
5942 | 118 when conf.debug -> (* v *)
5943 state.rects <- [];
5944 List.iter (fun l ->
5945 match getopaque l.pageno with
5946 | None -> ()
5947 | Some opaque ->
5948 let x0, y0, x1, y1 = pagebbox opaque in
5949 let a,b = float x0, float y0 in
5950 let c,d = float x1, float y0 in
5951 let e,f = float x1, float y1 in
5952 let h,j = float x0, float y1 in
5953 let rect = (a,b,c,d,e,f,h,j) in
5954 debugrect rect;
5955 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5956 ) state.layout;
5957 G.postRedisplay "v";
5959 | 124 -> (* | *)
5960 let mode = state.mode in
5961 let cmd = ref "" in
5962 let onleave = function
5963 | Cancel -> state.mode <- mode
5964 | Confirm ->
5965 List.iter (fun l ->
5966 match getopaque l.pageno with
5967 | Some opaque -> pipesel opaque !cmd
5968 | None -> ()) state.layout;
5969 state.mode <- mode
5971 let ondone s =
5972 cbput state.hists.sel s;
5973 cmd := s
5975 let te =
5976 "| ", !cmd, Some (onhist state.hists.sel), textentry, ondone, true
5978 G.postRedisplay "|";
5979 state.mode <- Textentry (te, onleave);
5981 | _ ->
5982 vlog "huh? %s" (Wsi.keyname key)
5985 let linknavkeyboard key mask linknav =
5986 let getpage pageno =
5987 let rec loop = function
5988 | [] -> None
5989 | l :: _ when l.pageno = pageno -> Some l
5990 | _ :: rest -> loop rest
5991 in loop state.layout
5993 let doexact (pageno, n) =
5994 match getopaque pageno, getpage pageno with
5995 | Some opaque, Some l ->
5996 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5997 then
5998 let under = getlink opaque n in
5999 G.postRedisplay "link gotounder";
6000 gotounder under;
6001 state.mode <- View;
6002 else
6003 let opt, dir =
6004 match key with
6005 | 0xff50 -> (* home *)
6006 Some (findlink opaque LDfirst), -1
6008 | 0xff57 -> (* end *)
6009 Some (findlink opaque LDlast), 1
6011 | 0xff51 -> (* left *)
6012 Some (findlink opaque (LDleft n)), -1
6014 | 0xff53 -> (* right *)
6015 Some (findlink opaque (LDright n)), 1
6017 | 0xff52 -> (* up *)
6018 Some (findlink opaque (LDup n)), -1
6020 | 0xff54 -> (* down *)
6021 Some (findlink opaque (LDdown n)), 1
6023 | _ -> None, 0
6025 let pwl l dir =
6026 begin match findpwl l.pageno dir with
6027 | Pwlnotfound -> ()
6028 | Pwl pageno ->
6029 let notfound dir =
6030 state.mode <- LinkNav (Ltgendir dir);
6031 let y, h = getpageyh pageno in
6032 let y =
6033 if dir < 0
6034 then y + h - state.winh
6035 else y
6037 gotoy y
6039 begin match getopaque pageno, getpage pageno with
6040 | Some opaque, Some _ ->
6041 let link =
6042 let ld = if dir > 0 then LDfirst else LDlast in
6043 findlink opaque ld
6045 begin match link with
6046 | Lfound m ->
6047 showlinktype (getlink opaque m);
6048 state.mode <- LinkNav (Ltexact (pageno, m));
6049 G.postRedisplay "linknav jpage";
6050 | _ -> notfound dir
6051 end;
6052 | _ -> notfound dir
6053 end;
6054 end;
6056 begin match opt with
6057 | Some Lnotfound -> pwl l dir;
6058 | Some (Lfound m) ->
6059 if m = n
6060 then pwl l dir
6061 else (
6062 let _, y0, _, y1 = getlinkrect opaque m in
6063 if y0 < l.pagey
6064 then gotopage1 l.pageno y0
6065 else (
6066 let d = fstate.fontsize + 1 in
6067 if y1 - l.pagey > l.pagevh - d
6068 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
6069 else G.postRedisplay "linknav";
6071 showlinktype (getlink opaque m);
6072 state.mode <- LinkNav (Ltexact (l.pageno, m));
6075 | None -> viewkeyboard key mask
6076 end;
6077 | _ -> viewkeyboard key mask
6079 if key = 0xff63
6080 then (
6081 state.mode <- View;
6082 G.postRedisplay "leave linknav"
6084 else
6085 match linknav with
6086 | Ltgendir _ -> viewkeyboard key mask
6087 | Ltexact exact -> doexact exact
6090 let keyboard key mask =
6091 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
6092 then wcmd "interrupt"
6093 else state.uioh <- state.uioh#key key mask
6096 let birdseyekeyboard key mask
6097 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
6098 let incr =
6099 match conf.columns with
6100 | Csingle _ -> 1
6101 | Cmulti ((c, _, _), _) -> c
6102 | Csplit _ -> failwith "bird's eye split mode"
6104 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
6105 match key with
6106 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
6107 let y, h = getpageyh pageno in
6108 let top = (state.winh - h) / 2 in
6109 gotoy (max 0 (y - top))
6110 | 0xff0d (* enter *)
6111 | 0xff8d -> leavebirdseye beye false (* kp enter *)
6112 | 0xff1b -> leavebirdseye beye true (* escape *)
6113 | 0xff52 -> upbirdseye incr beye (* up *)
6114 | 0xff54 -> downbirdseye incr beye (* down *)
6115 | 0xff51 -> upbirdseye 1 beye (* left *)
6116 | 0xff53 -> downbirdseye 1 beye (* right *)
6118 | 0xff55 -> (* prior *)
6119 begin match state.layout with
6120 | l :: _ ->
6121 if l.pagey != 0
6122 then (
6123 state.mode <- Birdseye (
6124 oconf, leftx, l.pageno, hooverpageno, anchor
6126 gotopage1 l.pageno 0;
6128 else (
6129 let layout = layout (state.y-state.winh) (pgh state.layout) in
6130 match layout with
6131 | [] -> gotoy (clamp (-state.winh))
6132 | l :: _ ->
6133 state.mode <- Birdseye (
6134 oconf, leftx, l.pageno, hooverpageno, anchor
6136 gotopage1 l.pageno 0
6139 | [] -> gotoy (clamp (-state.winh))
6140 end;
6142 | 0xff56 -> (* next *)
6143 begin match List.rev state.layout with
6144 | l :: _ ->
6145 let layout = layout (state.y + (pgh state.layout)) state.winh in
6146 begin match layout with
6147 | [] ->
6148 let incr = l.pageh - l.pagevh in
6149 if incr = 0
6150 then (
6151 state.mode <-
6152 Birdseye (
6153 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
6155 G.postRedisplay "birdseye pagedown";
6157 else gotoy (clamp (incr + conf.interpagespace*2));
6159 | l :: _ ->
6160 state.mode <-
6161 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
6162 gotopage1 l.pageno 0;
6165 | [] -> gotoy (clamp state.winh)
6166 end;
6168 | 0xff50 -> (* home *)
6169 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
6170 gotopage1 0 0
6172 | 0xff57 -> (* end *)
6173 let pageno = state.pagecount - 1 in
6174 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
6175 if not (pagevisible state.layout pageno)
6176 then
6177 let h =
6178 match List.rev state.pdims with
6179 | [] -> state.winh
6180 | (_, _, h, _) :: _ -> h
6182 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
6183 else G.postRedisplay "birdseye end";
6184 | _ -> viewkeyboard key mask
6187 let drawpage l =
6188 let color =
6189 match state.mode with
6190 | Textentry _ -> scalecolor 0.4
6191 | LinkNav _
6192 | View -> scalecolor 1.0
6193 | Birdseye (_, _, pageno, hooverpageno, _) ->
6194 if l.pageno = hooverpageno
6195 then scalecolor 0.9
6196 else (
6197 if l.pageno = pageno
6198 then scalecolor 1.0
6199 else scalecolor 0.8
6202 drawtiles l color;
6205 let postdrawpage l linkindexbase =
6206 match getopaque l.pageno with
6207 | Some opaque ->
6208 if tileready l l.pagex l.pagey
6209 then
6210 let x = l.pagedispx - l.pagex
6211 and y = l.pagedispy - l.pagey in
6212 let hlmask =
6213 match conf.columns with
6214 | Csingle _ | Cmulti _ ->
6215 (if conf.hlinks then 1 else 0)
6216 + (if state.glinks
6217 && not (isbirdseye state.mode) then 2 else 0)
6218 | _ -> 0
6220 let s =
6221 match state.mode with
6222 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
6223 | _ -> ""
6225 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
6226 else 0
6227 | _ -> 0
6230 let scrollindicator () =
6231 let sbw, ph, sh = state.uioh#scrollph in
6232 let sbh, pw, sw = state.uioh#scrollpw in
6234 GlDraw.color (0.64, 0.64, 0.64);
6235 filledrect
6236 (float (state.winw - sbw)) 0.
6237 (float state.winw) (float state.winh)
6239 filledrect
6240 0. (float (state.winh - sbh))
6241 (float (wadjsb state.winw - 1)) (float state.winh)
6243 GlDraw.color (0.0, 0.0, 0.0);
6245 filledrect
6246 (float (state.winw - sbw)) ph
6247 (float state.winw) (ph +. sh)
6249 filledrect
6250 pw (float (state.winh - sbh))
6251 (pw +. sw) (float state.winh)
6255 let showsel () =
6256 match state.mstate with
6257 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
6260 | Msel ((x0, y0), (x1, y1)) ->
6261 let identify opaque l px py = Some (opaque, l.pageno, px, py) in
6262 let o0,n0,px0,py0 = onppundermouse identify x0 y0 (~< "", -1, 0, 0) in
6263 let _o1,n1,px1,py1 = onppundermouse identify x1 y1 (~< "", -1, 0, 0) in
6264 if n0 != -1 && n0 = n1 then seltext o0 (px0, py0, px1, py1);
6267 let showrects = function [] -> () | rects ->
6268 Gl.enable `blend;
6269 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
6270 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6271 List.iter
6272 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
6273 List.iter (fun l ->
6274 if l.pageno = pageno
6275 then (
6276 let dx = float (l.pagedispx - l.pagex) in
6277 let dy = float (l.pagedispy - l.pagey) in
6278 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
6279 Raw.sets_float state.vraw ~pos:0
6280 [| x0+.dx; y0+.dy;
6281 x1+.dx; y1+.dy;
6282 x3+.dx; y3+.dy;
6283 x2+.dx; y2+.dy |];
6284 GlArray.vertex `two state.vraw;
6285 GlArray.draw_arrays `triangle_strip 0 4;
6287 ) state.layout
6288 ) rects
6290 Gl.disable `blend;
6293 let display () =
6294 GlClear.color (scalecolor2 conf.bgcolor);
6295 GlClear.clear [`color];
6296 List.iter drawpage state.layout;
6297 let rects =
6298 match state.mode with
6299 | LinkNav (Ltexact (pageno, linkno)) ->
6300 begin match getopaque pageno with
6301 | Some opaque ->
6302 let x0, y0, x1, y1 = getlinkrect opaque linkno in
6303 (pageno, 5, (
6304 float x0, float y0,
6305 float x1, float y0,
6306 float x1, float y1,
6307 float x0, float y1)
6308 ) :: state.rects
6309 | None -> state.rects
6311 | _ -> state.rects
6313 showrects rects;
6314 let rec postloop linkindexbase = function
6315 | l :: rest ->
6316 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
6317 postloop linkindexbase rest
6318 | [] -> ()
6320 showsel ();
6321 postloop 0 state.layout;
6322 state.uioh#display;
6323 begin match state.mstate with
6324 | Mzoomrect ((x0, y0), (x1, y1)) ->
6325 Gl.enable `blend;
6326 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
6327 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6328 filledrect (float x0) (float y0) (float x1) (float y1);
6329 Gl.disable `blend;
6330 | _ -> ()
6331 end;
6332 enttext ();
6333 scrollindicator ();
6334 Wsi.swapb ();
6337 let zoomrect x y x1 y1 =
6338 let x0 = min x x1
6339 and x1 = max x x1
6340 and y0 = min y y1 in
6341 gotoy (state.y + y0);
6342 state.anchor <- getanchor ();
6343 let zoom = (float state.w) /. float (x1 - x0) in
6344 let margin =
6345 match conf.fitmodel, conf.columns with
6346 | FitPage, Csplit _ ->
6347 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
6349 | _, _ ->
6350 let adjw = wadjsb state.winw in
6351 if state.w < adjw
6352 then (adjw - state.w) / 2
6353 else 0
6355 state.x <- (state.x + margin) - x0;
6356 setzoom zoom;
6357 resetmstate ();
6360 let zoomblock x y =
6361 let g opaque l px py =
6362 match rectofblock opaque px py with
6363 | Some a ->
6364 let x0 = a.(0) -. 20. in
6365 let x1 = a.(1) +. 20. in
6366 let y0 = a.(2) -. 20. in
6367 let zoom = (float state.w) /. (x1 -. x0) in
6368 let pagey = getpagey l.pageno in
6369 gotoy_and_clear_text (pagey + truncate y0);
6370 state.anchor <- getanchor ();
6371 let margin = (state.w - l.pagew)/2 in
6372 state.x <- -truncate x0 - margin;
6373 setzoom zoom;
6374 None
6375 | None -> None
6377 match conf.columns with
6378 | Csplit _ ->
6379 showtext '!' "block zooming does not work properly in split columns mode"
6380 | _ -> onppundermouse g x y ()
6383 let scrollx x =
6384 let winw = wadjsb state.winw - 1 in
6385 let s = float x /. float winw in
6386 let destx = truncate (float (state.w + winw) *. s) in
6387 state.x <- winw - destx;
6388 gotoy_and_clear_text state.y;
6389 state.mstate <- Mscrollx;
6392 let scrolly y =
6393 let s = float y /. float state.winh in
6394 let desty = truncate (float (state.maxy - state.winh) *. s) in
6395 gotoy_and_clear_text desty;
6396 state.mstate <- Mscrolly;
6399 let viewmulticlick clicks x y mask =
6400 let g opaque l px py =
6401 let mark =
6402 match clicks with
6403 | 2 -> Mark_word
6404 | 3 -> Mark_line
6405 | 4 -> Mark_block
6406 | _ -> Mark_page
6408 if markunder opaque px py mark
6409 then (
6410 Some (fun () ->
6411 let dopipe cmd =
6412 match getopaque l.pageno with
6413 | None -> ()
6414 | Some opaque -> pipesel opaque cmd
6416 state.roam <- (fun () -> dopipe conf.paxcmd);
6417 if not (Wsi.withctrl mask) then dopipe conf.selcmd;
6420 else None
6422 G.postRedisplay "viewmulticlick";
6423 onppundermouse g x y (fun () -> showtext '!' "Nothing to select") ();
6426 let canselect () =
6427 match conf.columns with
6428 | Csplit _ -> false
6429 | Csingle _ | Cmulti _ -> conf.angle mod 360 = 0
6432 let viewmouse button down x y mask =
6433 match button with
6434 | n when (n == 4 || n == 5) && not down ->
6435 if Wsi.withctrl mask
6436 then (
6437 match state.mstate with
6438 | Mzoom (oldn, i) ->
6439 if oldn = n
6440 then (
6441 if i = 2
6442 then
6443 let incr =
6444 match n with
6445 | 5 ->
6446 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6447 | _ ->
6448 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6450 let zoom = conf.zoom -. incr in
6451 setzoom zoom;
6452 state.mstate <- Mzoom (n, 0);
6453 else
6454 state.mstate <- Mzoom (n, i+1);
6456 else state.mstate <- Mzoom (n, 0)
6458 | _ -> state.mstate <- Mzoom (n, 0)
6460 else (
6461 match state.autoscroll with
6462 | Some step -> setautoscrollspeed step (n=4)
6463 | None ->
6464 if conf.wheelbypage || conf.presentation
6465 then (
6466 if n = 4
6467 then prevpage ()
6468 else nextpage ()
6470 else
6471 let incr =
6472 if n = 4
6473 then -conf.scrollstep
6474 else conf.scrollstep
6476 let incr = incr * 2 in
6477 let y = clamp incr in
6478 gotoy_and_clear_text y
6481 | n when (n = 6 || n = 7) && not down && canpan () ->
6482 state.x <-
6483 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6484 gotoy_and_clear_text state.y
6486 | 1 when Wsi.withshift mask ->
6487 state.mstate <- Mnone;
6488 if not down
6489 then (
6490 match unproject x y with
6491 | Some (pageno, ux, uy) ->
6492 let cmd = Printf.sprintf
6493 "%s %s %d %d %d"
6494 conf.stcmd state.path pageno ux uy
6496 popen cmd []
6497 | None -> ()
6500 | 1 when Wsi.withctrl mask ->
6501 if down
6502 then (
6503 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6504 state.mstate <- Mpan (x, y)
6506 else
6507 state.mstate <- Mnone
6509 | 3 ->
6510 if down
6511 then (
6512 Wsi.setcursor Wsi.CURSOR_CYCLE;
6513 let p = (x, y) in
6514 state.mstate <- Mzoomrect (p, p)
6516 else (
6517 match state.mstate with
6518 | Mzoomrect ((x0, y0), _) ->
6519 if abs (x-x0) > 10 && abs (y - y0) > 10
6520 then zoomrect x0 y0 x y
6521 else (
6522 resetmstate ();
6523 G.postRedisplay "kill accidental zoom rect";
6525 | _ ->
6526 resetmstate ()
6529 | 1 when x > state.winw - vscrollw () ->
6530 if down
6531 then
6532 let _, position, sh = state.uioh#scrollph in
6533 if y > truncate position && y < truncate (position +. sh)
6534 then state.mstate <- Mscrolly
6535 else scrolly y
6536 else
6537 state.mstate <- Mnone
6539 | 1 when y > state.winh - hscrollh () ->
6540 if down
6541 then
6542 let _, position, sw = state.uioh#scrollpw in
6543 if x > truncate position && x < truncate (position +. sw)
6544 then state.mstate <- Mscrollx
6545 else scrollx x
6546 else
6547 state.mstate <- Mnone
6549 | 1 when state.bzoom -> if not down then zoomblock x y
6551 | 1 ->
6552 let dest = if down then getunder x y else Unone in
6553 begin match dest with
6554 | Ulinkgoto _
6555 | Ulinkuri _
6556 | Uremote _ | Uremotedest _
6557 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6558 gotounder dest
6560 | Unone when down ->
6561 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6562 state.mstate <- Mpan (x, y);
6564 | Unone | Utext _ ->
6565 if down
6566 then (
6567 if canselect ()
6568 then (
6569 state.mstate <- Msel ((x, y), (x, y));
6570 G.postRedisplay "mouse select";
6573 else (
6574 match state.mstate with
6575 | Mnone -> ()
6577 | Mzoom _ | Mscrollx | Mscrolly ->
6578 state.mstate <- Mnone
6580 | Mzoomrect ((x0, y0), _) ->
6581 zoomrect x0 y0 x y
6583 | Mpan _ ->
6584 Wsi.setcursor Wsi.CURSOR_INHERIT;
6585 state.mstate <- Mnone
6587 | Msel ((x0, y0), (x1, y1)) ->
6588 let rec loop = function
6589 | [] -> ()
6590 | l :: rest ->
6591 let inside =
6592 let a0 = l.pagedispy in
6593 let a1 = a0 + l.pagevh in
6594 let b0 = l.pagedispx in
6595 let b1 = b0 + l.pagevw in
6596 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6597 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6599 if inside
6600 then
6601 match getopaque l.pageno with
6602 | Some opaque ->
6603 let dosel cmd () =
6604 match Ne.res Unix.pipe with
6605 | Ne.Exn exn ->
6606 showtext '!'
6607 (Printf.sprintf
6608 "can not create sel pipe: %s"
6609 (exntos exn));
6610 | Ne.Res (r, w) ->
6611 let clo what fd =
6612 Ne.clo fd (fun msg ->
6613 dolog "%s close failed: %s" what msg)
6615 let popened =
6616 try popen cmd [r, 0; w, -1]; true
6617 with exn ->
6618 dolog "can not execute %S: %s"
6619 cmd (exntos exn);
6620 false
6622 if popened
6623 then (
6624 copysel w opaque;
6625 G.postRedisplay "copysel";
6627 else clo "Msel pipe/w" w;
6628 clo "Msel pipe/r" r;
6630 dosel conf.selcmd ();
6631 state.roam <- dosel conf.paxcmd;
6632 | None -> ()
6633 else loop rest
6635 loop state.layout;
6636 resetmstate ();
6640 | _ -> ()
6643 let birdseyemouse button down x y mask
6644 (conf, leftx, _, hooverpageno, anchor) =
6645 match button with
6646 | 1 when down ->
6647 let rec loop = function
6648 | [] -> ()
6649 | l :: rest ->
6650 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6651 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6652 then (
6653 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6655 else loop rest
6657 loop state.layout
6658 | 3 -> ()
6659 | _ -> viewmouse button down x y mask
6662 let uioh = object
6663 method display = ()
6665 method key key mask =
6666 begin match state.mode with
6667 | Textentry textentry -> textentrykeyboard key mask textentry
6668 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6669 | View -> viewkeyboard key mask
6670 | LinkNav linknav -> linknavkeyboard key mask linknav
6671 end;
6672 state.uioh
6674 method button button bstate x y mask =
6675 begin match state.mode with
6676 | LinkNav _
6677 | View -> viewmouse button bstate x y mask
6678 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6679 | Textentry _ -> ()
6680 end;
6681 state.uioh
6683 method multiclick clicks x y mask =
6684 begin match state.mode with
6685 | LinkNav _
6686 | View -> viewmulticlick clicks x y mask
6687 | Birdseye _
6688 | Textentry _ -> ()
6689 end;
6690 state.uioh
6692 method motion x y =
6693 begin match state.mode with
6694 | Textentry _ -> ()
6695 | View | Birdseye _ | LinkNav _ ->
6696 match state.mstate with
6697 | Mzoom _ | Mnone -> ()
6699 | Mpan (x0, y0) ->
6700 let dx = x - x0
6701 and dy = y0 - y in
6702 state.mstate <- Mpan (x, y);
6703 if canpan ()
6704 then state.x <- panbound (state.x + dx);
6705 let y = clamp dy in
6706 gotoy_and_clear_text y
6708 | Msel (a, _) ->
6709 state.mstate <- Msel (a, (x, y));
6710 G.postRedisplay "motion select";
6712 | Mscrolly ->
6713 let y = min state.winh (max 0 y) in
6714 scrolly y
6716 | Mscrollx ->
6717 let x = min state.winw (max 0 x) in
6718 scrollx x
6720 | Mzoomrect (p0, _) ->
6721 state.mstate <- Mzoomrect (p0, (x, y));
6722 G.postRedisplay "motion zoomrect";
6723 end;
6724 state.uioh
6726 method pmotion x y =
6727 begin match state.mode with
6728 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6729 let rec loop = function
6730 | [] ->
6731 if hooverpageno != -1
6732 then (
6733 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6734 G.postRedisplay "pmotion birdseye no hoover";
6736 | l :: rest ->
6737 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6738 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6739 then (
6740 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6741 G.postRedisplay "pmotion birdseye hoover";
6743 else loop rest
6745 loop state.layout
6747 | Textentry _ -> ()
6749 | LinkNav _
6750 | View ->
6751 match state.mstate with
6752 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6754 | Mnone ->
6755 updateunder x y;
6756 if canselect ()
6757 then
6758 match conf.pax with
6759 | None -> ()
6760 | Some r ->
6761 let past, _, _ = !r in
6762 let now = now () in
6763 let delta = now -. past in
6764 if delta > 0.01
6765 then paxunder x y
6766 else r := (now, x, y)
6767 end;
6768 state.uioh
6770 method infochanged _ = ()
6772 method scrollph =
6773 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6774 let p, h =
6775 if maxy = 0
6776 then 0.0, float state.winh
6777 else scrollph state.y maxy
6779 vscrollw (), p, h
6781 method scrollpw =
6782 let winw = wadjsb state.winw in
6783 let fwinw = float winw in
6784 let sw =
6785 let sw = fwinw /. float state.w in
6786 let sw = fwinw *. sw in
6787 max sw (float conf.scrollh)
6789 let position =
6790 let maxx = state.w + winw in
6791 let x = winw - state.x in
6792 let percent = float x /. float maxx in
6793 (fwinw -. sw) *. percent
6795 hscrollh (), position, sw
6797 method modehash =
6798 let modename =
6799 match state.mode with
6800 | LinkNav _ -> "links"
6801 | Textentry _ -> "textentry"
6802 | Birdseye _ -> "birdseye"
6803 | View -> "view"
6805 findkeyhash conf modename
6807 method eformsgs = true
6808 end;;
6810 module Config =
6811 struct
6812 open Parser
6814 let fontpath = ref "";;
6816 module KeyMap =
6817 Map.Make (struct type t = (int * int) let compare = compare end);;
6819 let unent s =
6820 let l = String.length s in
6821 let b = Buffer.create l in
6822 unent b s 0 l;
6823 Buffer.contents b;
6826 let home =
6827 try Sys.getenv "HOME"
6828 with exn ->
6829 prerr_endline
6830 ("Can not determine home directory location: " ^ exntos exn);
6834 let modifier_of_string = function
6835 | "alt" -> Wsi.altmask
6836 | "shift" -> Wsi.shiftmask
6837 | "ctrl" | "control" -> Wsi.ctrlmask
6838 | "meta" -> Wsi.metamask
6839 | _ -> 0
6842 let key_of_string =
6843 let r = Str.regexp "-" in
6844 fun s ->
6845 let elems = Str.full_split r s in
6846 let f n k m =
6847 let g s =
6848 let m1 = modifier_of_string s in
6849 if m1 = 0
6850 then (Wsi.namekey s, m)
6851 else (k, m lor m1)
6852 in function
6853 | Str.Delim s when n land 1 = 0 -> g s
6854 | Str.Text s -> g s
6855 | Str.Delim _ -> (k, m)
6857 let rec loop n k m = function
6858 | [] -> (k, m)
6859 | x :: xs ->
6860 let k, m = f n k m x in
6861 loop (n+1) k m xs
6863 loop 0 0 0 elems
6866 let keys_of_string =
6867 let r = Str.regexp "[ \t]" in
6868 fun s ->
6869 let elems = Str.split r s in
6870 List.map key_of_string elems
6873 let copykeyhashes c =
6874 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6877 let config_of c attrs =
6878 let apply c k v =
6880 match k with
6881 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6882 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6883 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6884 | "preload" -> { c with preload = bool_of_string v }
6885 | "page-bias" -> { c with pagebias = int_of_string v }
6886 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6887 | "horizontal-scroll-step" ->
6888 { c with hscrollstep = max (int_of_string v) 1 }
6889 | "auto-scroll-step" ->
6890 { c with autoscrollstep = max 0 (int_of_string v) }
6891 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6892 | "crop-hack" -> { c with crophack = bool_of_string v }
6893 | "throttle" ->
6894 let mw =
6895 match String.lowercase v with
6896 | "true" -> Some infinity
6897 | "false" -> None
6898 | f -> Some (float_of_string f)
6900 { c with maxwait = mw}
6901 | "highlight-links" -> { c with hlinks = bool_of_string v }
6902 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6903 | "vertical-margin" ->
6904 { c with interpagespace = max 0 (int_of_string v) }
6905 | "zoom" ->
6906 let zoom = float_of_string v /. 100. in
6907 let zoom = max zoom 0.0 in
6908 { c with zoom = zoom }
6909 | "presentation" -> { c with presentation = bool_of_string v }
6910 | "rotation-angle" -> { c with angle = int_of_string v }
6911 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6912 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6913 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6914 | "proportional-display" ->
6915 let fm =
6916 if bool_of_string v
6917 then FitProportional
6918 else FitWidth
6920 { c with fitmodel = fm }
6921 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6922 | "pixmap-cache-size" ->
6923 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6924 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6925 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6926 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6927 | "persistent-location" -> { c with jumpback = bool_of_string v }
6928 | "background-color" -> { c with bgcolor = color_of_string v }
6929 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6930 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6931 | "mupdf-store-size" ->
6932 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6933 | "checkers" -> { c with checkers = bool_of_string v }
6934 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6935 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6936 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6937 | "uri-launcher" -> { c with urilauncher = unent v }
6938 | "path-launcher" -> { c with pathlauncher = unent v }
6939 | "color-space" -> { c with colorspace = CSTE.of_string v }
6940 | "invert-colors" -> { c with invert = bool_of_string v }
6941 | "brightness" -> { c with colorscale = float_of_string v }
6942 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6943 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
6944 | "columns" ->
6945 let (n, _, _) as nab = multicolumns_of_string v in
6946 if n < 0
6947 then { c with columns = Csplit (-n, [||]) }
6948 else { c with columns = Cmulti (nab, [||]) }
6949 | "birds-eye-columns" ->
6950 { c with beyecolumns = Some (max (int_of_string v) 2) }
6951 | "selection-command" -> { c with selcmd = unent v }
6952 | "synctex-command" -> { c with stcmd = unent v }
6953 | "pax-command" -> { c with paxcmd = unent v }
6954 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6955 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6956 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6957 | "use-pbo" -> { c with usepbo = bool_of_string v }
6958 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6959 | "horizontal-scrollbar-visible" ->
6960 let b =
6961 if bool_of_string v
6962 then c.scrollb lor scrollbhv
6963 else c.scrollb land (lnot scrollbhv)
6965 { c with scrollb = b }
6966 | "vertical-scrollbar-visible" ->
6967 let b =
6968 if bool_of_string v
6969 then c.scrollb lor scrollbvv
6970 else c.scrollb land (lnot scrollbvv)
6972 { c with scrollb = b }
6973 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6974 | "point-and-x" ->
6975 { c with pax =
6976 if bool_of_string v
6977 then Some (ref (0.0, 0, 0))
6978 else None }
6979 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
6980 | _ -> c
6981 with exn ->
6982 prerr_endline ("Error processing attribute (`" ^
6983 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6986 let rec fold c = function
6987 | [] -> c
6988 | (k, v) :: rest ->
6989 let c = apply c k v in
6990 fold c rest
6992 fold { c with keyhashes = copykeyhashes c } attrs;
6995 let fromstring f pos n v d =
6996 try f v
6997 with exn ->
6998 dolog "Error processing attribute (%S=%S) at %d\n%s"
6999 n v pos (exntos exn)
7004 let bookmark_of attrs =
7005 let rec fold title page rely visy = function
7006 | ("title", v) :: rest -> fold v page rely visy rest
7007 | ("page", v) :: rest -> fold title v rely visy rest
7008 | ("rely", v) :: rest -> fold title page v visy rest
7009 | ("visy", v) :: rest -> fold title page rely v rest
7010 | _ :: rest -> fold title page rely visy rest
7011 | [] -> title, page, rely, visy
7013 fold "invalid" "0" "0" "0" attrs
7016 let doc_of attrs =
7017 let rec fold path page rely pan visy = function
7018 | ("path", v) :: rest -> fold v page rely pan visy rest
7019 | ("page", v) :: rest -> fold path v rely pan visy rest
7020 | ("rely", v) :: rest -> fold path page v pan visy rest
7021 | ("pan", v) :: rest -> fold path page rely v visy rest
7022 | ("visy", v) :: rest -> fold path page rely pan v rest
7023 | _ :: rest -> fold path page rely pan visy rest
7024 | [] -> path, page, rely, pan, visy
7026 fold "" "0" "0" "0" "0" attrs
7029 let map_of attrs =
7030 let rec fold rs ls = function
7031 | ("out", v) :: rest -> fold v ls rest
7032 | ("in", v) :: rest -> fold rs v rest
7033 | _ :: rest -> fold ls rs rest
7034 | [] -> ls, rs
7036 fold "" "" attrs
7039 let setconf dst src =
7040 dst.scrollbw <- src.scrollbw;
7041 dst.scrollh <- src.scrollh;
7042 dst.icase <- src.icase;
7043 dst.preload <- src.preload;
7044 dst.pagebias <- src.pagebias;
7045 dst.verbose <- src.verbose;
7046 dst.scrollstep <- src.scrollstep;
7047 dst.maxhfit <- src.maxhfit;
7048 dst.crophack <- src.crophack;
7049 dst.autoscrollstep <- src.autoscrollstep;
7050 dst.maxwait <- src.maxwait;
7051 dst.hlinks <- src.hlinks;
7052 dst.underinfo <- src.underinfo;
7053 dst.interpagespace <- src.interpagespace;
7054 dst.zoom <- src.zoom;
7055 dst.presentation <- src.presentation;
7056 dst.angle <- src.angle;
7057 dst.cwinw <- src.cwinw;
7058 dst.cwinh <- src.cwinh;
7059 dst.savebmarks <- src.savebmarks;
7060 dst.memlimit <- src.memlimit;
7061 dst.fitmodel <- src.fitmodel;
7062 dst.texcount <- src.texcount;
7063 dst.sliceheight <- src.sliceheight;
7064 dst.thumbw <- src.thumbw;
7065 dst.jumpback <- src.jumpback;
7066 dst.bgcolor <- src.bgcolor;
7067 dst.tilew <- src.tilew;
7068 dst.tileh <- src.tileh;
7069 dst.mustoresize <- src.mustoresize;
7070 dst.checkers <- src.checkers;
7071 dst.aalevel <- src.aalevel;
7072 dst.trimmargins <- src.trimmargins;
7073 dst.trimfuzz <- src.trimfuzz;
7074 dst.urilauncher <- src.urilauncher;
7075 dst.colorspace <- src.colorspace;
7076 dst.invert <- src.invert;
7077 dst.colorscale <- src.colorscale;
7078 dst.redirectstderr <- src.redirectstderr;
7079 dst.ghyllscroll <- src.ghyllscroll;
7080 dst.columns <- src.columns;
7081 dst.beyecolumns <- src.beyecolumns;
7082 dst.selcmd <- src.selcmd;
7083 dst.updatecurs <- src.updatecurs;
7084 dst.pathlauncher <- src.pathlauncher;
7085 dst.keyhashes <- copykeyhashes src;
7086 dst.hfsize <- src.hfsize;
7087 dst.hscrollstep <- src.hscrollstep;
7088 dst.pgscale <- src.pgscale;
7089 dst.usepbo <- src.usepbo;
7090 dst.wheelbypage <- src.wheelbypage;
7091 dst.stcmd <- src.stcmd;
7092 dst.paxcmd <- src.paxcmd;
7093 dst.scrollb <- src.scrollb;
7094 dst.riani <- src.riani;
7095 dst.paxmark <- src.paxmark;
7096 dst.pax <-
7097 if src.pax = None
7098 then None
7099 else Some ((ref (0.0, 0, 0)));
7102 let get s =
7103 let h = Hashtbl.create 10 in
7104 let dc = { defconf with angle = defconf.angle } in
7105 let rec toplevel v t spos _ =
7106 match t with
7107 | Vdata | Vcdata | Vend -> v
7108 | Vopen ("llppconfig", _, closed) ->
7109 if closed
7110 then v
7111 else { v with f = llppconfig }
7112 | Vopen _ ->
7113 error "unexpected subelement at top level" s spos
7114 | Vclose _ -> error "unexpected close at top level" s spos
7116 and llppconfig v t spos _ =
7117 match t with
7118 | Vdata | Vcdata -> v
7119 | Vend -> error "unexpected end of input in llppconfig" s spos
7120 | Vopen ("defaults", attrs, closed) ->
7121 let c = config_of dc attrs in
7122 setconf dc c;
7123 if closed
7124 then v
7125 else { v with f = defaults }
7127 | Vopen ("ui-font", attrs, closed) ->
7128 let rec getsize size = function
7129 | [] -> size
7130 | ("size", v) :: rest ->
7131 let size =
7132 fromstring int_of_string spos "size" v fstate.fontsize in
7133 getsize size rest
7134 | l -> getsize size l
7136 fstate.fontsize <- getsize fstate.fontsize attrs;
7137 if closed
7138 then v
7139 else { v with f = uifont (Buffer.create 10) }
7141 | Vopen ("doc", attrs, closed) ->
7142 let pathent, spage, srely, span, svisy = doc_of attrs in
7143 let path = unent pathent
7144 and pageno = fromstring int_of_string spos "page" spage 0
7145 and rely = fromstring float_of_string spos "rely" srely 0.0
7146 and pan = fromstring int_of_string spos "pan" span 0
7147 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7148 let c = config_of dc attrs in
7149 let anchor = (pageno, rely, visy) in
7150 if closed
7151 then (Hashtbl.add h path (c, [], pan, anchor); v)
7152 else { v with f = doc path pan anchor c [] }
7154 | Vopen _ ->
7155 error "unexpected subelement in llppconfig" s spos
7157 | Vclose "llppconfig" -> { v with f = toplevel }
7158 | Vclose _ -> error "unexpected close in llppconfig" s spos
7160 and defaults v t spos _ =
7161 match t with
7162 | Vdata | Vcdata -> v
7163 | Vend -> error "unexpected end of input in defaults" s spos
7164 | Vopen ("keymap", attrs, closed) ->
7165 let modename =
7166 try List.assoc "mode" attrs
7167 with Not_found -> "global" in
7168 if closed
7169 then v
7170 else
7171 let ret keymap =
7172 let h = findkeyhash dc modename in
7173 KeyMap.iter (Hashtbl.replace h) keymap;
7174 defaults
7176 { v with f = pkeymap ret KeyMap.empty }
7178 | Vopen (_, _, _) ->
7179 error "unexpected subelement in defaults" s spos
7181 | Vclose "defaults" ->
7182 { v with f = llppconfig }
7184 | Vclose _ -> error "unexpected close in defaults" s spos
7186 and uifont b v t spos epos =
7187 match t with
7188 | Vdata | Vcdata ->
7189 Buffer.add_substring b s spos (epos - spos);
7191 | Vopen (_, _, _) ->
7192 error "unexpected subelement in ui-font" s spos
7193 | Vclose "ui-font" ->
7194 if emptystr !fontpath
7195 then fontpath := Buffer.contents b;
7196 { v with f = llppconfig }
7197 | Vclose _ -> error "unexpected close in ui-font" s spos
7198 | Vend -> error "unexpected end of input in ui-font" s spos
7200 and doc path pan anchor c bookmarks v t spos _ =
7201 match t with
7202 | Vdata | Vcdata -> v
7203 | Vend -> error "unexpected end of input in doc" s spos
7204 | Vopen ("bookmarks", _, closed) ->
7205 if closed
7206 then v
7207 else { v with f = pbookmarks path pan anchor c bookmarks }
7209 | Vopen ("keymap", attrs, closed) ->
7210 let modename =
7211 try List.assoc "mode" attrs
7212 with Not_found -> "global"
7214 if closed
7215 then v
7216 else
7217 let ret keymap =
7218 let h = findkeyhash c modename in
7219 KeyMap.iter (Hashtbl.replace h) keymap;
7220 doc path pan anchor c bookmarks
7222 { v with f = pkeymap ret KeyMap.empty }
7224 | Vopen (_, _, _) ->
7225 error "unexpected subelement in doc" s spos
7227 | Vclose "doc" ->
7228 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
7229 { v with f = llppconfig }
7231 | Vclose _ -> error "unexpected close in doc" s spos
7233 and pkeymap ret keymap v t spos _ =
7234 match t with
7235 | Vdata | Vcdata -> v
7236 | Vend -> error "unexpected end of input in keymap" s spos
7237 | Vopen ("map", attrs, closed) ->
7238 let r, l = map_of attrs in
7239 let kss = fromstring keys_of_string spos "in" r [] in
7240 let lss = fromstring keys_of_string spos "out" l [] in
7241 let keymap =
7242 match kss with
7243 | [] -> keymap
7244 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
7245 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
7247 if closed
7248 then { v with f = pkeymap ret keymap }
7249 else
7250 let f () = v in
7251 { v with f = skip "map" f }
7253 | Vopen _ ->
7254 error "unexpected subelement in keymap" s spos
7256 | Vclose "keymap" ->
7257 { v with f = ret keymap }
7259 | Vclose _ -> error "unexpected close in keymap" s spos
7261 and pbookmarks path pan anchor c bookmarks v t spos _ =
7262 match t with
7263 | Vdata | Vcdata -> v
7264 | Vend -> error "unexpected end of input in bookmarks" s spos
7265 | Vopen ("item", attrs, closed) ->
7266 let titleent, spage, srely, svisy = bookmark_of attrs in
7267 let page = fromstring int_of_string spos "page" spage 0
7268 and rely = fromstring float_of_string spos "rely" srely 0.0
7269 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7270 let bookmarks =
7271 (unent titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
7273 if closed
7274 then { v with f = pbookmarks path pan anchor c bookmarks }
7275 else
7276 let f () = v in
7277 { v with f = skip "item" f }
7279 | Vopen _ ->
7280 error "unexpected subelement in bookmarks" s spos
7282 | Vclose "bookmarks" ->
7283 { v with f = doc path pan anchor c bookmarks }
7285 | Vclose _ -> error "unexpected close in bookmarks" s spos
7287 and skip tag f v t spos _ =
7288 match t with
7289 | Vdata | Vcdata -> v
7290 | Vend ->
7291 error ("unexpected end of input in skipped " ^ tag) s spos
7292 | Vopen (tag', _, closed) ->
7293 if closed
7294 then v
7295 else
7296 let f' () = { v with f = skip tag f } in
7297 { v with f = skip tag' f' }
7298 | Vclose ctag ->
7299 if tag = ctag
7300 then f ()
7301 else error ("unexpected close in skipped " ^ tag) s spos
7304 parse { f = toplevel; accu = () } s;
7305 h, dc;
7308 let do_load f ic =
7310 let len = in_channel_length ic in
7311 let s = String.create len in
7312 really_input ic s 0 len;
7313 f s;
7314 with
7315 | Parse_error (msg, s, pos) ->
7316 let subs = subs s pos in
7317 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
7319 | exn ->
7320 failwith ("config load error: " ^ exntos exn)
7323 let defconfpath =
7324 let dir =
7326 let dir = Filename.concat home ".config" in
7327 if Sys.is_directory dir then dir else home
7328 with _ -> home
7330 Filename.concat dir "llpp.conf"
7333 let confpath = ref defconfpath;;
7335 let load1 f =
7336 if Sys.file_exists !confpath
7337 then
7338 match
7339 (try Some (open_in_bin !confpath)
7340 with exn ->
7341 prerr_endline
7342 ("Error opening configuration file `" ^ !confpath ^ "': " ^
7343 exntos exn);
7344 None
7346 with
7347 | Some ic ->
7348 let success =
7350 f (do_load get ic)
7351 with exn ->
7352 prerr_endline
7353 ("Error loading configuration from `" ^ !confpath ^ "': " ^
7354 exntos exn);
7355 false
7357 close_in ic;
7358 success
7360 | None -> false
7361 else
7362 f (Hashtbl.create 0, defconf)
7365 let load () =
7366 let f (h, dc) =
7367 let pc, pb, px, pa =
7369 let key =
7370 if emptystr state.origin
7371 then state.path
7372 else state.origin
7374 Hashtbl.find h (Filename.basename key)
7375 with Not_found -> dc, [], 0, emptyanchor
7377 setconf defconf dc;
7378 setconf conf pc;
7379 state.bookmarks <- pb;
7380 state.x <- px;
7381 if conf.jumpback
7382 then state.anchor <- pa;
7383 cbput state.hists.nav pa;
7384 true
7386 load1 f
7389 let add_attrs bb always dc c =
7390 let ob s a b =
7391 if always || a != b
7392 then Printf.bprintf bb "\n %s='%b'" s a
7393 and op s a b =
7394 if always || a <> b
7395 then Printf.bprintf bb "\n %s='%b'" s (a != None)
7396 and oi s a b =
7397 if always || a != b
7398 then Printf.bprintf bb "\n %s='%d'" s a
7399 and oI s a b =
7400 if always || a != b
7401 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
7402 and oz s a b =
7403 if always || a <> b
7404 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
7405 and oF s a b =
7406 if always || a <> b
7407 then Printf.bprintf bb "\n %s='%f'" s a
7408 and oc s a b =
7409 if always || a <> b
7410 then
7411 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
7412 and oC s a b =
7413 if always || a <> b
7414 then
7415 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
7416 and oR s a b =
7417 if always || a <> b
7418 then
7419 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
7420 and os s a b =
7421 if always || a <> b
7422 then
7423 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7424 and og s a b =
7425 if always || a <> b
7426 then
7427 match a with
7428 | Some (_N, _A, _B) ->
7429 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7430 | None ->
7431 match b with
7432 | None -> ()
7433 | _ ->
7434 Printf.bprintf bb "\n %s='none'" s
7435 and oW s a b =
7436 if always || a <> b
7437 then
7438 let v =
7439 match a with
7440 | None -> "false"
7441 | Some f ->
7442 if f = infinity
7443 then "true"
7444 else string_of_float f
7446 Printf.bprintf bb "\n %s='%s'" s v
7447 and oco s a b =
7448 if always || a <> b
7449 then
7450 match a with
7451 | Cmulti ((n, a, b), _) when n > 1 ->
7452 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7453 | Csplit (n, _) when n > 1 ->
7454 Printf.bprintf bb "\n %s='%d'" s ~-n
7455 | _ -> ()
7456 and obeco s a b =
7457 if always || a <> b
7458 then
7459 match a with
7460 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7461 | _ -> ()
7462 and oFm s a b =
7463 if always || a <> b
7464 then
7465 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7466 and oSv s a b m =
7467 if always || a <> b
7468 then
7469 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7470 and oPm s a b =
7471 if always || a <> b
7472 then
7473 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
7475 oi "width" c.cwinw dc.cwinw;
7476 oi "height" c.cwinh dc.cwinh;
7477 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7478 oi "scroll-handle-height" c.scrollh dc.scrollh;
7479 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7480 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7481 ob "case-insensitive-search" c.icase dc.icase;
7482 ob "preload" c.preload dc.preload;
7483 oi "page-bias" c.pagebias dc.pagebias;
7484 oi "scroll-step" c.scrollstep dc.scrollstep;
7485 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7486 ob "max-height-fit" c.maxhfit dc.maxhfit;
7487 ob "crop-hack" c.crophack dc.crophack;
7488 oW "throttle" c.maxwait dc.maxwait;
7489 ob "highlight-links" c.hlinks dc.hlinks;
7490 ob "under-cursor-info" c.underinfo dc.underinfo;
7491 oi "vertical-margin" c.interpagespace dc.interpagespace;
7492 oz "zoom" c.zoom dc.zoom;
7493 ob "presentation" c.presentation dc.presentation;
7494 oi "rotation-angle" c.angle dc.angle;
7495 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7496 oFm "fit-model" c.fitmodel dc.fitmodel;
7497 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7498 oi "tex-count" c.texcount dc.texcount;
7499 oi "slice-height" c.sliceheight dc.sliceheight;
7500 oi "thumbnail-width" c.thumbw dc.thumbw;
7501 ob "persistent-location" c.jumpback dc.jumpback;
7502 oc "background-color" c.bgcolor dc.bgcolor;
7503 oi "tile-width" c.tilew dc.tilew;
7504 oi "tile-height" c.tileh dc.tileh;
7505 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7506 ob "checkers" c.checkers dc.checkers;
7507 oi "aalevel" c.aalevel dc.aalevel;
7508 ob "trim-margins" c.trimmargins dc.trimmargins;
7509 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7510 os "uri-launcher" c.urilauncher dc.urilauncher;
7511 os "path-launcher" c.pathlauncher dc.pathlauncher;
7512 oC "color-space" c.colorspace dc.colorspace;
7513 ob "invert-colors" c.invert dc.invert;
7514 oF "brightness" c.colorscale dc.colorscale;
7515 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7516 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7517 oco "columns" c.columns dc.columns;
7518 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7519 os "selection-command" c.selcmd dc.selcmd;
7520 os "synctex-command" c.stcmd dc.stcmd;
7521 os "pax-command" c.paxcmd dc.paxcmd;
7522 ob "update-cursor" c.updatecurs dc.updatecurs;
7523 oi "hint-font-size" c.hfsize dc.hfsize;
7524 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7525 oF "page-scroll-scale" c.pgscale dc.pgscale;
7526 ob "use-pbo" c.usepbo dc.usepbo;
7527 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7528 ob "remote-in-a-new-instance" c.riani dc.riani;
7529 op "point-and-x" c.pax dc.pax;
7530 oPm "point-and-x-mark" c.paxmark dc.paxmark;
7533 let keymapsbuf always dc c =
7534 let bb = Buffer.create 16 in
7535 let rec loop = function
7536 | [] -> ()
7537 | (modename, h) :: rest ->
7538 let dh = findkeyhash dc modename in
7539 if always || h <> dh
7540 then (
7541 if Hashtbl.length h > 0
7542 then (
7543 if Buffer.length bb > 0
7544 then Buffer.add_char bb '\n';
7545 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7546 Hashtbl.iter (fun i o ->
7547 let isdifferent = always ||
7549 let dO = Hashtbl.find dh i in
7550 dO <> o
7551 with Not_found -> true
7553 if isdifferent
7554 then
7555 let addkm (k, m) =
7556 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7557 if Wsi.withalt m then Buffer.add_string bb "alt-";
7558 if Wsi.withshift m then Buffer.add_string bb "shift-";
7559 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7560 Buffer.add_string bb (Wsi.keyname k);
7562 let addkms l =
7563 let rec loop = function
7564 | [] -> ()
7565 | km :: [] -> addkm km
7566 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7568 loop l
7570 Buffer.add_string bb "<map in='";
7571 addkm i;
7572 match o with
7573 | KMinsrt km ->
7574 Buffer.add_string bb "' out='";
7575 addkm km;
7576 Buffer.add_string bb "'/>\n"
7578 | KMinsrl kms ->
7579 Buffer.add_string bb "' out='";
7580 addkms kms;
7581 Buffer.add_string bb "'/>\n"
7583 | KMmulti (ins, kms) ->
7584 Buffer.add_char bb ' ';
7585 addkms ins;
7586 Buffer.add_string bb "' out='";
7587 addkms kms;
7588 Buffer.add_string bb "'/>\n"
7589 ) h;
7590 Buffer.add_string bb "</keymap>";
7593 loop rest
7595 loop c.keyhashes;
7599 let save () =
7600 let uifontsize = fstate.fontsize in
7601 let bb = Buffer.create 32768 in
7602 let relx = float state.x /. float state.winw in
7603 let w, h, x =
7604 let cx w = truncate (relx *. float w) in
7605 List.fold_left
7606 (fun (w, h, x) ws ->
7607 match ws with
7608 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7609 | Wsi.MaxVert -> (w, conf.cwinh, x)
7610 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7612 (state.winw, state.winh, state.x) state.winstate
7614 conf.cwinw <- w;
7615 conf.cwinh <- h;
7616 let f (h, dc) =
7617 let dc = if conf.bedefault then conf else dc in
7618 Buffer.add_string bb "<llppconfig>\n";
7620 if nonemptystr !fontpath
7621 then
7622 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7623 uifontsize
7624 !fontpath
7625 else (
7626 if uifontsize <> 14
7627 then
7628 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7631 Buffer.add_string bb "<defaults";
7632 add_attrs bb true dc dc;
7633 let kb = keymapsbuf true dc dc in
7634 if Buffer.length kb > 0
7635 then (
7636 Buffer.add_string bb ">\n";
7637 Buffer.add_buffer bb kb;
7638 Buffer.add_string bb "\n</defaults>\n";
7640 else Buffer.add_string bb "/>\n";
7642 let adddoc path pan anchor c bookmarks =
7643 if bookmarks == [] && c = dc && anchor = emptyanchor
7644 then ()
7645 else (
7646 Printf.bprintf bb "<doc path='%s'"
7647 (enent path 0 (String.length path));
7649 if anchor <> emptyanchor
7650 then (
7651 let n, rely, visy = anchor in
7652 Printf.bprintf bb " page='%d'" n;
7653 if rely > 1e-6
7654 then
7655 Printf.bprintf bb " rely='%f'" rely
7657 if abs_float visy > 1e-6
7658 then
7659 Printf.bprintf bb " visy='%f'" visy
7663 if pan != 0
7664 then Printf.bprintf bb " pan='%d'" pan;
7666 add_attrs bb false dc c;
7667 let kb = keymapsbuf false dc c in
7669 begin match bookmarks with
7670 | [] ->
7671 if Buffer.length kb > 0
7672 then (
7673 Buffer.add_string bb ">\n";
7674 Buffer.add_buffer bb kb;
7675 Buffer.add_string bb "\n</doc>\n";
7677 else Buffer.add_string bb "/>\n"
7678 | _ ->
7679 Buffer.add_string bb ">\n<bookmarks>\n";
7680 List.iter (fun (title, _, kind) ->
7681 begin match kind with
7682 | Oanchor (page, rely, visy) ->
7683 Printf.bprintf bb
7684 "<item title='%s' page='%d'"
7685 (enent title 0 (String.length title))
7686 page
7688 if rely > 1e-6
7689 then
7690 Printf.bprintf bb " rely='%f'" rely
7692 if abs_float visy > 1e-6
7693 then
7694 Printf.bprintf bb " visy='%f'" visy
7696 | Onone | Ouri _ | Oremote _ | Oremotedest _ | Olaunch _ ->
7697 failwith "unexpected link in bookmarks"
7698 end;
7699 Buffer.add_string bb "/>\n";
7700 ) bookmarks;
7701 Buffer.add_string bb "</bookmarks>";
7702 if Buffer.length kb > 0
7703 then (
7704 Buffer.add_string bb "\n";
7705 Buffer.add_buffer bb kb;
7707 Buffer.add_string bb "\n</doc>\n";
7708 end;
7712 let pan, conf =
7713 match state.mode with
7714 | Birdseye (c, pan, _, _, _) ->
7715 let beyecolumns =
7716 match conf.columns with
7717 | Cmulti ((c, _, _), _) -> Some c
7718 | Csingle _ -> None
7719 | Csplit _ -> None
7720 and columns =
7721 match c.columns with
7722 | Cmulti (c, _) -> Cmulti (c, [||])
7723 | Csingle _ -> Csingle [||]
7724 | Csplit _ -> failwith "quit from bird's eye while split"
7726 pan, { c with beyecolumns = beyecolumns; columns = columns }
7727 | _ -> x, conf
7729 let basename = Filename.basename
7730 (if emptystr state.origin then state.path else state.origin)
7732 adddoc basename pan (getanchor ())
7733 (let autoscrollstep =
7734 match state.autoscroll with
7735 | Some step -> step
7736 | None -> conf.autoscrollstep
7737 in begin match state.mode with
7738 | Birdseye beye -> leavebirdseye beye true;
7739 | _ -> ()
7740 end;
7741 { conf with autoscrollstep = autoscrollstep })
7742 (if conf.savebmarks then state.bookmarks else []);
7744 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7745 if basename <> path
7746 then adddoc path x anchor c bookmarks
7747 ) h;
7748 Buffer.add_string bb "</llppconfig>\n";
7749 true;
7751 if load1 f && Buffer.length bb > 0
7752 then
7754 let tmp = !confpath ^ ".tmp" in
7755 let oc = open_out_bin tmp in
7756 Buffer.output_buffer oc bb;
7757 close_out oc;
7758 Unix.rename tmp !confpath;
7759 with exn ->
7760 prerr_endline
7761 ("error while saving configuration: " ^ exntos exn)
7763 end;;
7765 let adderrmsg src msg =
7766 Buffer.add_string state.errmsgs msg;
7767 state.newerrmsgs <- true;
7768 G.postRedisplay src
7771 let adderrfmt src fmt =
7772 Format.kprintf (fun s -> adderrmsg src s) fmt;
7775 let ract cmds =
7776 let cl = splitatspace cmds in
7777 let scan s fmt f =
7778 try Scanf.sscanf s fmt f
7779 with exn ->
7780 adderrfmt "remote exec"
7781 "error processing '%S': %s\n" cmds (exntos exn)
7783 match cl with
7784 | "reload" :: [] -> reload ()
7785 | "goto" :: args :: [] ->
7786 scan args "%u %f %f"
7787 (fun pageno x y ->
7788 let cmd, _ = state.geomcmds in
7789 if emptystr cmd
7790 then gotopagexy pageno x y
7791 else
7792 let f prevf () =
7793 gotopagexy pageno x y;
7794 prevf ()
7796 state.reprf <- f state.reprf
7798 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7799 | "gotor" :: args :: [] ->
7800 scan args "%S %u"
7801 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
7802 | "gotord" :: args :: [] ->
7803 scan args "%S %S"
7804 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
7805 | "rect" :: args :: [] ->
7806 scan args "%u %u %f %f %f %f"
7807 (fun pageno color x0 y0 x1 y1 ->
7808 onpagerect pageno (fun w h ->
7809 let _,w1,h1,_ = getpagedim pageno in
7810 let sw = float w1 /. float w
7811 and sh = float h1 /. float h in
7812 let x0s = x0 *. sw
7813 and x1s = x1 *. sw
7814 and y0s = y0 *. sh
7815 and y1s = y1 *. sh in
7816 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7817 debugrect rect;
7818 state.rects <- (pageno, color, rect) :: state.rects;
7819 G.postRedisplay "rect";
7822 | "activatewin" :: [] -> Wsi.activatewin ()
7823 | "quit" :: [] -> raise Quit
7824 | _ ->
7825 adderrfmt "remote command"
7826 "error processing remote command: %S\n" cmds;
7829 let remote =
7830 let scratch = String.create 80 in
7831 let buf = Buffer.create 80 in
7832 fun fd ->
7833 let rec tempfr () =
7834 try Some (Unix.read fd scratch 0 80)
7835 with
7836 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7837 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7838 | exn -> raise exn
7840 match tempfr () with
7841 | None -> Some fd
7842 | Some n ->
7843 if n = 0
7844 then (
7845 Unix.close fd;
7846 if Buffer.length buf > 0
7847 then (
7848 let s = Buffer.contents buf in
7849 Buffer.clear buf;
7850 ract s;
7852 None
7854 else
7855 let rec eat ppos =
7856 let nlpos =
7858 let pos = String.index_from scratch ppos '\n' in
7859 if pos >= n then -1 else pos
7860 with Not_found -> -1
7862 if nlpos >= 0
7863 then (
7864 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7865 let s = Buffer.contents buf in
7866 Buffer.clear buf;
7867 ract s;
7868 eat (nlpos+1);
7870 else (
7871 Buffer.add_substring buf scratch ppos (n-ppos);
7872 Some fd
7874 in eat 0
7877 let remoteopen path =
7878 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7879 with exn ->
7880 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7881 None
7884 let () =
7885 let trimcachepath = ref "" in
7886 let rcmdpath = ref "" in
7887 let pageno = ref None in
7888 selfexec := Sys.executable_name;
7889 Arg.parse
7890 (Arg.align
7891 [("-p", Arg.String (fun s -> state.password <- s),
7892 "<password> Set password");
7894 ("-f", Arg.String
7895 (fun s ->
7896 Config.fontpath := s;
7897 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7899 "<path> Set path to the user interface font");
7901 ("-c", Arg.String
7902 (fun s ->
7903 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7904 Config.confpath := s),
7905 "<path> Set path to the configuration file");
7907 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
7908 "<page-number> Jump to page");
7910 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7911 "<path> Set path to the trim cache file");
7913 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7914 "<named-destination> Set named destination");
7916 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7917 ("-cxack", Arg.Set cxack, " Cut corners");
7919 ("-remote", Arg.String (fun s -> rcmdpath := s),
7920 "<path> Set path to the remote commands source");
7922 ("-origin", Arg.String (fun s -> state.origin <- s),
7923 "<original-path> Set original path");
7925 ("-v", Arg.Unit (fun () ->
7926 Printf.printf
7927 "%s\nconfiguration path: %s\n"
7928 (version ())
7929 Config.defconfpath
7931 exit 0), " Print version and exit");
7934 (fun s -> state.path <- s)
7935 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7937 if !wtmode
7938 then selfexec := !selfexec ^ " -wtmode";
7940 if emptystr state.path
7941 then (prerr_endline "file name missing"; exit 1);
7943 if not (Config.load ())
7944 then prerr_endline "failed to load configuration";
7945 begin match !pageno with
7946 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
7947 | None -> ()
7948 end;
7950 let wsfd, winw, winh = Wsi.init (object (self)
7951 val mutable m_hack = false
7952 val mutable m_clicks = 0
7953 val mutable m_click_x = 0
7954 val mutable m_click_y = 0
7955 val mutable m_lastclicktime = infinity
7957 method private cleanup =
7958 state.roam <- noroam;
7959 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
7960 method expose = if not m_hack then G.postRedisplay "expose"
7961 method visible = G.postRedisplay "visible"
7962 method display = m_hack <- false; display ()
7963 method reshape w h =
7964 self#cleanup;
7965 m_hack <- w < state.winw && h < state.winh;
7966 reshape w h
7967 method mouse b d x y m =
7968 if d && canselect ()
7969 then (
7970 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
7971 m_click_x <- x;
7972 m_click_y <- y;
7973 if b = 1
7974 then (
7975 let t = now () in
7976 if abs x - m_click_x > 10
7977 || abs y - m_click_y > 10
7978 || abs_float (t -. m_lastclicktime) > 0.3
7979 then m_clicks <- 0;
7980 m_clicks <- m_clicks + 1;
7981 m_lastclicktime <- t;
7982 if m_clicks = 1
7983 then (
7984 self#cleanup;
7985 G.postRedisplay "cleanup";
7986 state.uioh <- state.uioh#button b d x y m;
7988 else state.uioh <- state.uioh#multiclick m_clicks x y m
7990 else (
7991 self#cleanup;
7992 m_clicks <- 0;
7993 m_lastclicktime <- infinity;
7994 state.uioh <- state.uioh#button b d x y m
7997 else (
7998 state.uioh <- state.uioh#button b d x y m
8000 method motion x y =
8001 state.mpos <- (x, y);
8002 state.uioh <- state.uioh#motion x y
8003 method pmotion x y =
8004 state.mpos <- (x, y);
8005 state.uioh <- state.uioh#pmotion x y
8006 method key k m =
8007 let mascm = m land (
8008 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
8009 ) in
8010 let keyboard k m =
8011 let x = state.x and y = state.y in
8012 keyboard k m;
8013 if x != state.x || y != state.y then self#cleanup
8015 match state.keystate with
8016 | KSnone ->
8017 let km = k, mascm in
8018 begin
8019 match
8020 let modehash = state.uioh#modehash in
8021 try Hashtbl.find modehash km
8022 with Not_found ->
8023 try Hashtbl.find (findkeyhash conf "global") km
8024 with Not_found -> KMinsrt (k, m)
8025 with
8026 | KMinsrt (k, m) -> keyboard k m
8027 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
8028 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
8030 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
8031 List.iter (fun (k, m) -> keyboard k m) insrt;
8032 state.keystate <- KSnone
8033 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
8034 state.keystate <- KSinto (keys, insrt)
8035 | _ ->
8036 state.keystate <- KSnone
8038 method enter x y =
8039 state.mpos <- (x, y);
8040 state.uioh <- state.uioh#pmotion x y
8041 method leave = state.mpos <- (-1, -1)
8042 method winstate wsl = state.winstate <- wsl; m_hack <- false
8043 method quit = raise Quit
8044 end) conf.cwinw conf.cwinh (platform = Posx) in
8046 state.wsfd <- wsfd;
8048 if not (
8049 List.exists GlMisc.check_extension
8050 [ "GL_ARB_texture_rectangle"
8051 ; "GL_EXT_texture_recangle"
8052 ; "GL_NV_texture_rectangle" ]
8054 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
8056 if (
8057 let r = GlMisc.get_string `renderer in
8058 let p = "Mesa DRI Intel(" in
8059 let l = String.length p in
8060 String.length r > l && String.sub r 0 l = p
8062 then (
8063 defconf.sliceheight <- 1024;
8064 defconf.texcount <- 32;
8065 defconf.usepbo <- true;
8068 let cr, sw =
8069 match Ne.res Unix.pipe with
8070 | Ne.Exn exn ->
8071 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
8072 exit 1
8073 | Ne.Res rw -> rw
8074 and sr, cw =
8075 match Ne.res Unix.pipe with
8076 | Ne.Exn exn ->
8077 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
8078 exit 1
8079 | Ne.Res rw -> rw
8082 cloexec cr;
8083 cloexec sw;
8084 cloexec sr;
8085 cloexec cw;
8087 setcheckers conf.checkers;
8088 redirectstderr ();
8089 if conf.redirectstderr
8090 then
8091 at_exit (fun () ->
8092 let s = Buffer.contents state.errmsgs ^
8093 (match state.errfd with
8094 | Some fd ->
8095 let s = String.create (80*24) in
8096 let n =
8098 let r, _, _ = Unix.select [fd] [] [] 0.0 in
8099 if List.mem fd r
8100 then Unix.read fd s 0 (String.length s)
8101 else 0
8102 with _ -> 0
8104 if n = 0
8105 then ""
8106 else String.sub s 0 n
8107 | None -> ""
8110 try ignore (Unix.write state.stderr s 0 (String.length s))
8111 with exn -> print_endline (exntos exn)
8115 init (cr, cw) (
8116 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
8117 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
8118 !Config.fontpath, !trimcachepath,
8119 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
8121 List.iter GlArray.enable [`texture_coord; `vertex];
8122 state.sr <- sr;
8123 state.sw <- sw;
8124 state.text <- "Opening " ^ (mbtoutf8 state.path);
8125 reshape winw winh;
8126 opendoc state.path state.password;
8127 state.uioh <- uioh;
8128 display ();
8129 Wsi.mapwin ();
8130 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
8131 let optrfd =
8132 ref (
8133 if nonemptystr !rcmdpath
8134 then remoteopen !rcmdpath
8135 else None
8139 let rec loop deadline =
8140 let r =
8141 match state.errfd with
8142 | None -> [state.sr; state.wsfd]
8143 | Some fd -> [state.sr; state.wsfd; fd]
8145 let r =
8146 match !optrfd with
8147 | None -> r
8148 | Some fd -> fd :: r
8150 if state.redisplay
8151 then (
8152 state.redisplay <- false;
8153 display ();
8155 let timeout =
8156 let now = now () in
8157 if deadline > now
8158 then (
8159 if deadline = infinity
8160 then ~-.1.0
8161 else max 0.0 (deadline -. now)
8163 else 0.0
8165 let r, _, _ =
8166 try Unix.select r [] [] timeout
8167 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
8169 begin match r with
8170 | [] ->
8171 state.ghyll None;
8172 let newdeadline =
8173 if state.ghyll == noghyll
8174 then
8175 match state.autoscroll with
8176 | Some step when step != 0 ->
8177 let y = state.y + step in
8178 let y =
8179 if y < 0
8180 then state.maxy
8181 else if y >= state.maxy then 0 else y
8183 gotoy y;
8184 if state.mode = View
8185 then state.text <- "";
8186 deadline +. 0.01
8187 | _ -> infinity
8188 else deadline +. 0.01
8190 loop newdeadline
8192 | l ->
8193 let rec checkfds = function
8194 | [] -> ()
8195 | fd :: rest when fd = state.sr ->
8196 let cmd = readcmd state.sr in
8197 act cmd;
8198 checkfds rest
8200 | fd :: rest when fd = state.wsfd ->
8201 Wsi.readresp fd;
8202 checkfds rest
8204 | fd :: rest when Some fd = !optrfd ->
8205 begin match remote fd with
8206 | None -> optrfd := remoteopen !rcmdpath;
8207 | opt -> optrfd := opt
8208 end;
8209 checkfds rest
8211 | fd :: rest ->
8212 let s = String.create 80 in
8213 let n = tempfailureretry (Unix.read fd s 0) 80 in
8214 if conf.redirectstderr
8215 then (
8216 Buffer.add_substring state.errmsgs s 0 n;
8217 state.newerrmsgs <- true;
8218 state.redisplay <- true;
8220 else (
8221 prerr_string (String.sub s 0 n);
8222 flush stderr;
8224 checkfds rest
8226 checkfds l;
8227 let newdeadline =
8228 let deadline1 =
8229 if deadline = infinity
8230 then now () +. 0.01
8231 else deadline
8233 match state.autoscroll with
8234 | Some step when step != 0 -> deadline1
8235 | _ -> if state.ghyll == noghyll then infinity else deadline1
8237 loop newdeadline
8238 end;
8241 loop infinity;
8242 with Quit ->
8243 Config.save ();