Support uri links in outline
[llpp.git] / main.ml
blob170acfbe2d62bdbf7a9c3bc3990ad0ff05907b1b
1 open Utils;;
3 exception Quit;;
5 type under =
6 | Unone
7 | Ulinkuri of string
8 | Ulinkgoto of (int * int)
9 | Utext of facename
10 | Uunexpected of string
11 | Ulaunch of launchcommand
12 | Unamed of destname
13 | Uremote of (filename * pageno)
14 | Uremotedest of (filename * destname)
15 and facename = string
16 and launchcommand = string
17 and filename = string
18 and pageno = int
19 and destname = string;;
21 type mark =
22 | Mark_page
23 | Mark_block
24 | Mark_line
25 | Mark_word
28 type params = (angle * fitmodel * trimparams
29 * texcount * sliceheight * memsize
30 * colorspace * fontpath * trimcachepath
31 * haspbo)
32 and width = int
33 and height = int
34 and leftx = int
35 and opaque = string
36 and recttype = int
37 and pixmapsize = int
38 and angle = int
39 and trimmargins = bool
40 and interpagespace = int
41 and texcount = int
42 and sliceheight = int
43 and gen = int
44 and top = float
45 and dtop = float
46 and fontpath = string
47 and trimcachepath = string
48 and memsize = int
49 and aalevel = int
50 and irect = (int * int * int * int)
51 and trimparams = (trimmargins * irect)
52 and colorspace = | Rgb | Bgr | Gray
53 and fitmodel = | FitWidth | FitProportional | FitPage
54 and haspbo = bool
55 and uri = string
56 and caption = string
59 type x = int
60 and y = int
61 and tilex = int
62 and tiley = int
63 and tileparams = (x * y * width * height * tilex * tiley)
66 type link =
67 | Lnotfound
68 | Lfound of int
69 and linkdir =
70 | LDfirst
71 | LDlast
72 | LDfirstvisible of (int * int * int)
73 | LDleft of int
74 | LDright of int
75 | LDdown of int
76 | LDup of int
79 type pagewithlinks =
80 | Pwlnotfound
81 | Pwl of int
84 type keymap =
85 | KMinsrt of key
86 | KMinsrl of key list
87 | KMmulti of key list * key list
88 and key = int * int
89 and keyhash = (key, keymap) Hashtbl.t
90 and keystate =
91 | KSnone
92 | KSinto of (key list * key list)
95 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
96 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
98 type pipe = (Unix.file_descr * Unix.file_descr);;
100 external init : pipe -> params -> unit = "ml_init";;
101 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
102 external copysel : Unix.file_descr -> opaque -> bool -> unit = "ml_copysel";;
103 external getpdimrect : int -> float array = "ml_getpdimrect";;
104 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
105 external markunder : string -> int -> int -> mark -> bool = "ml_markunder";;
106 external clearmark : string -> unit = "ml_clearmark";;
107 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
108 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
109 external measurestr : int -> string -> float = "ml_measure_string";;
110 external postprocess :
111 opaque -> int -> int -> int -> (int * string * int) -> int
112 = "ml_postprocess";;
113 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
114 external platform : unit -> platform = "ml_platform";;
115 external setaalevel : int -> unit = "ml_setaalevel";;
116 external realloctexts : int -> bool = "ml_realloctexts";;
117 external findlink : opaque -> linkdir -> link = "ml_findlink";;
118 external getlink : opaque -> int -> under = "ml_getlink";;
119 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
120 external getlinkcount : opaque -> int = "ml_getlinkcount";;
121 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
122 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
123 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
124 external freepbo : string -> unit = "ml_freepbo";;
125 external unmappbo : string -> unit = "ml_unmappbo";;
126 external pbousable : unit -> bool = "ml_pbo_usable";;
127 external unproject : opaque -> int -> int -> (int * int) option
128 = "ml_unproject";;
129 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
130 external rectofblock : opaque -> int -> int -> float array option
131 = "ml_rectofblock";;
132 external fz_version : unit -> string = "ml_fz_version";;
133 external begintiles : unit -> unit = "ml_begintiles";;
134 external endtiles : unit -> unit = "ml_endtiles";;
136 let platform_to_string = function
137 | Punknown -> "unknown"
138 | Plinux -> "Linux"
139 | Posx -> "OSX"
140 | Psun -> "Sun"
141 | Pfreebsd -> "FreeBSD"
142 | Pdragonflybsd -> "DragonflyBSD"
143 | Popenbsd -> "OpenBSD"
144 | Pnetbsd -> "NetBSD"
145 | Pcygwin -> "Cygwin"
148 let platform = platform ();;
150 let now = Unix.gettimeofday;;
152 let selfexec = ref "";;
154 let popen cmd fda =
155 if platform = Pcygwin
156 then (
157 let sh = "/bin/sh" in
158 let args = [|sh; "-c"; cmd|] in
159 let rec std si so se = function
160 | [] -> si, so, se
161 | (fd, 0) :: rest -> std fd so se rest
162 | (fd, -1) :: rest ->
163 Unix.set_close_on_exec fd;
164 std si so se rest
165 | (_, n) :: _ ->
166 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
168 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
169 ignore (Unix.create_process sh args si so se)
171 else popen cmd fda;
174 type mpos = int * int
175 and mstate =
176 | Msel of (mpos * mpos)
177 | Mpan of mpos
178 | Mscrolly | Mscrollx
179 | Mzoom of (int * int)
180 | Mzoomrect of (mpos * mpos)
181 | Mnone
184 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
185 and onkey = string -> int -> te
186 and ondone = string -> unit
187 and histcancel = unit -> unit
188 and onhist = ((histcmd -> string) * histcancel)
189 and histcmd = HCnext | HCprev | HCfirst | HClast
190 and cancelonempty = bool
191 and te =
192 | TEstop
193 | TEdone of string
194 | TEcont of string
195 | TEswitch of textentry
198 type 'a circbuf =
199 { store : 'a array
200 ; mutable rc : int
201 ; mutable wc : int
202 ; mutable len : int
206 let bound v minv maxv =
207 max minv (min maxv v);
210 let cbnew n v =
211 { store = Array.create n v
212 ; rc = 0
213 ; wc = 0
214 ; len = 0
218 let cbcap b = Array.length b.store;;
220 let cbput b v =
221 let cap = cbcap b in
222 b.store.(b.wc) <- v;
223 b.wc <- (b.wc + 1) mod cap;
224 b.rc <- b.wc;
225 b.len <- min (b.len + 1) cap;
228 let cbempty b = b.len = 0;;
230 let cbgetg b circular dir =
231 if cbempty b
232 then b.store.(0)
233 else
234 let rc = b.rc + dir in
235 let rc =
236 if circular
237 then (
238 if rc = -1
239 then b.len-1
240 else (
241 if rc >= b.len
242 then 0
243 else rc
246 else bound rc 0 (b.len-1)
248 b.rc <- rc;
249 b.store.(rc);
252 let cbget b = cbgetg b false;;
253 let cbgetc b = cbgetg b true;;
255 let drawstring size x y s =
256 Gl.enable `blend;
257 Gl.enable `texture_2d;
258 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
259 ignore (drawstr size x y s);
260 Gl.disable `blend;
261 Gl.disable `texture_2d;
264 let drawstring1 size x y s =
265 drawstr size x y s;
268 let drawstring2 size x y fmt =
269 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
272 type page =
273 { pageno : int
274 ; pagedimno : int
275 ; pagew : int
276 ; pageh : int
277 ; pagex : int
278 ; pagey : int
279 ; pagevw : int
280 ; pagevh : int
281 ; pagedispx : int
282 ; pagedispy : int
283 ; pagecol : int
287 let debugl l =
288 dolog "l %d dim=%d {" l.pageno l.pagedimno;
289 dolog " WxH %dx%d" l.pagew l.pageh;
290 dolog " vWxH %dx%d" l.pagevw l.pagevh;
291 dolog " pagex,y %d,%d" l.pagex l.pagey;
292 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
293 dolog " column %d" l.pagecol;
294 dolog "}";
297 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
298 dolog "rect {";
299 dolog " x0,y0=(% f, % f)" x0 y0;
300 dolog " x1,y1=(% f, % f)" x1 y1;
301 dolog " x2,y2=(% f, % f)" x2 y2;
302 dolog " x3,y3=(% f, % f)" x3 y3;
303 dolog "}";
306 type multicolumns = multicol * pagegeom
307 and singlecolumn = pagegeom
308 and splitcolumns = columncount * pagegeom
309 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
310 and multicol = columncount * covercount * covercount
311 and pdimno = int
312 and columncount = int
313 and covercount = int;;
315 type scrollb = int;;
316 let scrollbvv = 1;;
317 let scrollbhv = 2;;
319 type conf =
320 { mutable scrollbw : int
321 ; mutable scrollh : int
322 ; mutable scrollb : scrollb
323 ; mutable icase : bool
324 ; mutable preload : bool
325 ; mutable pagebias : int
326 ; mutable verbose : bool
327 ; mutable debug : bool
328 ; mutable scrollstep : int
329 ; mutable hscrollstep : int
330 ; mutable maxhfit : bool
331 ; mutable crophack : bool
332 ; mutable autoscrollstep : int
333 ; mutable maxwait : float option
334 ; mutable hlinks : bool
335 ; mutable underinfo : bool
336 ; mutable interpagespace : interpagespace
337 ; mutable zoom : float
338 ; mutable presentation : bool
339 ; mutable angle : angle
340 ; mutable cwinw : int
341 ; mutable cwinh : int
342 ; mutable savebmarks : bool
343 ; mutable fitmodel : fitmodel
344 ; mutable trimmargins : trimmargins
345 ; mutable trimfuzz : irect
346 ; mutable memlimit : memsize
347 ; mutable texcount : texcount
348 ; mutable sliceheight : sliceheight
349 ; mutable thumbw : width
350 ; mutable jumpback : bool
351 ; mutable bgcolor : (float * float * float)
352 ; mutable bedefault : bool
353 ; mutable tilew : int
354 ; mutable tileh : int
355 ; mutable mustoresize : memsize
356 ; mutable checkers : bool
357 ; mutable aalevel : int
358 ; mutable urilauncher : string
359 ; mutable pathlauncher : string
360 ; mutable colorspace : colorspace
361 ; mutable invert : bool
362 ; mutable colorscale : float
363 ; mutable redirectstderr : bool
364 ; mutable ghyllscroll : (int * int * int) option
365 ; mutable columns : columns
366 ; mutable beyecolumns : columncount option
367 ; mutable selcmd : string
368 ; mutable paxcmd : string
369 ; mutable updatecurs : bool
370 ; mutable keyhashes : (string * keyhash) list
371 ; mutable hfsize : int
372 ; mutable pgscale : float
373 ; mutable usepbo : bool
374 ; mutable wheelbypage : bool
375 ; mutable stcmd : string
376 ; mutable riani : bool
377 ; mutable pax : (float * int * int) ref option
378 ; mutable paxmark : mark
380 and columns =
381 | Csingle of singlecolumn
382 | Cmulti of multicolumns
383 | Csplit of splitcolumns
386 type anchor = pageno * top * dtop;;
388 type outlinekind =
389 | Oanchor of anchor
390 | Ouri of uri
391 | Olaunch of launchcommand
392 | Oremote of (filename * pageno)
393 | Oremotedest of (filename * destname)
394 and outline = (caption * outlinelevel * outlinekind)
395 and outlinelevel = int
398 type rect = float * float * float * float * float * float * float * float;;
400 type tile = opaque * pixmapsize * elapsed
401 and elapsed = float;;
402 type pagemapkey = pageno * gen;;
403 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
404 and row = int
405 and col = int;;
407 let emptyanchor = (0, 0.0, 0.0);;
409 type infochange = | Memused | Docinfo | Pdim;;
411 class type uioh = object
412 method display : unit
413 method key : int -> int -> uioh
414 method button : int -> bool -> int -> int -> int -> uioh
415 method motion : int -> int -> uioh
416 method pmotion : int -> int -> uioh
417 method infochanged : infochange -> unit
418 method scrollpw : (int * float * float)
419 method scrollph : (int * float * float)
420 method modehash : keyhash
421 method eformsgs : bool
422 end;;
424 type mode =
425 | Birdseye of (conf * leftx * pageno * pageno * anchor)
426 | Textentry of (textentry * onleave)
427 | View
428 | LinkNav of linktarget
429 and onleave = leavetextentrystatus -> unit
430 and leavetextentrystatus = | Cancel | Confirm
431 and helpitem = string * int * action
432 and action =
433 | Noaction
434 | Action of (uioh -> uioh)
435 and linktarget =
436 | Ltexact of (pageno * int)
437 | Ltgendir of int
440 let isbirdseye = function Birdseye _ -> true | _ -> false;;
441 let istextentry = function Textentry _ -> true | _ -> false;;
443 type currently =
444 | Idle
445 | Loading of (page * gen)
446 | Tiling of (
447 page * opaque * colorspace * angle * gen * col * row * width * height
449 | Outlining of outline list
452 let emptykeyhash = Hashtbl.create 0;;
453 let nouioh : uioh = object (self)
454 method display = ()
455 method key _ _ = self
456 method button _ _ _ _ _ = self
457 method motion _ _ = self
458 method pmotion _ _ = self
459 method infochanged _ = ()
460 method scrollpw = (0, nan, nan)
461 method scrollph = (0, nan, nan)
462 method modehash = emptykeyhash
463 method eformsgs = false
464 end;;
466 type state =
467 { mutable sr : Unix.file_descr
468 ; mutable sw : Unix.file_descr
469 ; mutable wsfd : Unix.file_descr
470 ; mutable errfd : Unix.file_descr option
471 ; mutable stderr : Unix.file_descr
472 ; mutable errmsgs : Buffer.t
473 ; mutable newerrmsgs : bool
474 ; mutable w : int
475 ; mutable x : int
476 ; mutable y : int
477 ; mutable anchor : anchor
478 ; mutable ranchors : (string * string * anchor * string) list
479 ; mutable maxy : int
480 ; mutable layout : page list
481 ; pagemap : (pagemapkey, opaque) Hashtbl.t
482 ; tilemap : (tilemapkey, tile) Hashtbl.t
483 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
484 ; mutable pdims : (pageno * width * height * leftx) list
485 ; mutable pagecount : int
486 ; mutable currently : currently
487 ; mutable mstate : mstate
488 ; mutable searchpattern : string
489 ; mutable rects : (pageno * recttype * rect) list
490 ; mutable rects1 : (pageno * recttype * rect) list
491 ; mutable text : string
492 ; mutable winstate : Wsi.winstate list
493 ; mutable mode : mode
494 ; mutable uioh : uioh
495 ; mutable outlines : outline array
496 ; mutable bookmarks : outline list
497 ; mutable path : string
498 ; mutable password : string
499 ; mutable nameddest : string
500 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
501 ; mutable memused : memsize
502 ; mutable gen : gen
503 ; mutable throttle : (page list * int * float) option
504 ; mutable autoscroll : int option
505 ; mutable ghyll : (int option -> unit)
506 ; mutable help : helpitem array
507 ; mutable docinfo : (int * string) list
508 ; mutable texid : GlTex.texture_id option
509 ; hists : hists
510 ; mutable prevzoom : (float * int)
511 ; mutable progress : float
512 ; mutable redisplay : bool
513 ; mutable mpos : mpos
514 ; mutable keystate : keystate
515 ; mutable glinks : bool
516 ; mutable prevcolumns : (columns * float) option
517 ; mutable winw : int
518 ; mutable winh : int
519 ; mutable reprf : (unit -> unit)
520 ; mutable origin : string
521 ; mutable roam : (unit -> unit)
522 ; mutable bzoom : bool
523 ; mutable traw : [`float] Raw.t
524 ; mutable vraw : [`float] Raw.t
526 and hists =
527 { pat : string circbuf
528 ; pag : string circbuf
529 ; nav : anchor circbuf
530 ; sel : string circbuf
534 let defconf =
535 { scrollbw = 7
536 ; scrollh = 12
537 ; scrollb = scrollbhv lor scrollbvv
538 ; icase = true
539 ; preload = true
540 ; pagebias = 0
541 ; verbose = false
542 ; debug = false
543 ; scrollstep = 24
544 ; hscrollstep = 24
545 ; maxhfit = true
546 ; crophack = false
547 ; autoscrollstep = 2
548 ; maxwait = None
549 ; hlinks = false
550 ; underinfo = false
551 ; interpagespace = 2
552 ; zoom = 1.0
553 ; presentation = false
554 ; angle = 0
555 ; cwinw = 900
556 ; cwinh = 900
557 ; savebmarks = true
558 ; fitmodel = FitProportional
559 ; trimmargins = false
560 ; trimfuzz = (0,0,0,0)
561 ; memlimit = 32 lsl 20
562 ; texcount = 256
563 ; sliceheight = 24
564 ; thumbw = 76
565 ; jumpback = true
566 ; bgcolor = (0.5, 0.5, 0.5)
567 ; bedefault = false
568 ; tilew = 2048
569 ; tileh = 2048
570 ; mustoresize = 256 lsl 20
571 ; checkers = true
572 ; aalevel = 8
573 ; urilauncher =
574 (match platform with
575 | Plinux | Pfreebsd | Pdragonflybsd
576 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
577 | Posx -> "open \"%s\""
578 | Pcygwin -> "cygstart \"%s\""
579 | Punknown -> "echo %s")
580 ; pathlauncher = "lp \"%s\""
581 ; selcmd =
582 (match platform with
583 | Plinux | Pfreebsd | Pdragonflybsd
584 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
585 | Posx -> "pbcopy"
586 | Pcygwin -> "wsel"
587 | Punknown -> "cat")
588 ; paxcmd = "cat"
589 ; colorspace = Rgb
590 ; invert = false
591 ; colorscale = 1.0
592 ; redirectstderr = false
593 ; ghyllscroll = None
594 ; columns = Csingle [||]
595 ; beyecolumns = None
596 ; updatecurs = false
597 ; hfsize = 12
598 ; pgscale = 1.0
599 ; usepbo = false
600 ; wheelbypage = false
601 ; stcmd = "echo SyncTex"
602 ; riani = false
603 ; pax = None
604 ; paxmark = Mark_word
605 ; keyhashes =
606 let mk n = (n, Hashtbl.create 1) in
607 [ mk "global"
608 ; mk "info"
609 ; mk "help"
610 ; mk "outline"
611 ; mk "listview"
612 ; mk "birdseye"
613 ; mk "textentry"
614 ; mk "links"
615 ; mk "view"
620 let wtmode = ref false;;
621 let cxack = ref false;;
623 let findkeyhash c name =
624 try List.assoc name c.keyhashes
625 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
628 let conf = { defconf with angle = defconf.angle };;
630 let pgscale h = truncate (float h *. conf.pgscale);;
632 type fontstate =
633 { mutable fontsize : int
634 ; mutable wwidth : float
635 ; mutable maxrows : int
639 let fstate =
640 { fontsize = 14
641 ; wwidth = nan
642 ; maxrows = -1
646 let geturl s =
647 let colonpos = try String.index s ':' with Not_found -> -1 in
648 let len = String.length s in
649 if colonpos >= 0 && colonpos + 3 < len
650 then (
651 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
652 then
653 let schemestartpos =
654 try String.rindex_from s colonpos ' '
655 with Not_found -> -1
657 let scheme =
658 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
660 match scheme with
661 | "http" | "ftp" | "mailto" ->
662 let epos =
663 try String.index_from s colonpos ' '
664 with Not_found -> len
666 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
667 | _ -> ""
668 else ""
670 else ""
673 let gotouri uri =
674 if emptystr conf.urilauncher
675 then print_endline uri
676 else (
677 let url = geturl uri in
678 if emptystr url
679 then Printf.eprintf "obtained empty url from uri %S" uri
680 else
681 let re = Str.regexp "%s" in
682 let command = Str.global_replace re url conf.urilauncher in
683 try popen command []
684 with exn ->
685 Printf.eprintf
686 "failed to execute `%s': %s\n" command (exntos exn);
687 flush stderr;
691 let version () =
692 Printf.sprintf "llpp version %s, fitz %s, ocaml %s (%s/%dbit)"
693 Help.version (fz_version ()) Sys.ocaml_version
694 (platform_to_string platform) Sys.word_size
697 let makehelp () =
698 let strings = version () :: "" :: Help.keys in
699 Array.of_list (
700 List.map (fun s ->
701 let url = geturl s in
702 if nonemptystr url
703 then (s, 0, Action (fun u -> gotouri url; u))
704 else (s, 0, Noaction)
705 ) strings);
708 let noghyll _ = ();;
709 let firstgeomcmds = "", [];;
710 let noreprf () = ();;
712 let state =
713 { sr = Unix.stdin
714 ; sw = Unix.stdin
715 ; wsfd = Unix.stdin
716 ; errfd = None
717 ; stderr = Unix.stderr
718 ; errmsgs = Buffer.create 0
719 ; newerrmsgs = false
720 ; x = 0
721 ; y = 0
722 ; w = 0
723 ; anchor = emptyanchor
724 ; ranchors = []
725 ; layout = []
726 ; maxy = max_int
727 ; tilelru = Queue.create ()
728 ; pagemap = Hashtbl.create 10
729 ; tilemap = Hashtbl.create 10
730 ; pdims = []
731 ; pagecount = 0
732 ; currently = Idle
733 ; mstate = Mnone
734 ; rects = []
735 ; rects1 = []
736 ; text = ""
737 ; mode = View
738 ; winstate = []
739 ; searchpattern = ""
740 ; outlines = [||]
741 ; bookmarks = []
742 ; path = ""
743 ; password = ""
744 ; nameddest = ""
745 ; geomcmds = firstgeomcmds
746 ; hists =
747 { nav = cbnew 10 emptyanchor
748 ; pat = cbnew 10 ""
749 ; pag = cbnew 10 ""
750 ; sel = cbnew 10 ""
752 ; memused = 0
753 ; gen = 0
754 ; throttle = None
755 ; autoscroll = None
756 ; ghyll = noghyll
757 ; help = makehelp ()
758 ; docinfo = []
759 ; texid = None
760 ; prevzoom = (1.0, 0)
761 ; progress = -1.0
762 ; uioh = nouioh
763 ; redisplay = true
764 ; mpos = (-1, -1)
765 ; keystate = KSnone
766 ; glinks = false
767 ; prevcolumns = None
768 ; winw = -1
769 ; winh = -1
770 ; reprf = noreprf
771 ; origin = ""
772 ; roam = (fun () -> ())
773 ; bzoom = false
774 ; traw = Raw.create_static `float 8
775 ; vraw = Raw.create_static `float 8
779 let hscrollh () =
780 if (conf.scrollb land scrollbhv = 0)
781 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
782 then 0
783 else conf.scrollbw
786 let vscrollw () =
787 if (conf.scrollb land scrollbvv = 0)
788 then 0
789 else conf.scrollbw
792 let wadjsb w = w - vscrollw ();;
794 let setfontsize n =
795 fstate.fontsize <- n;
796 fstate.wwidth <- measurestr fstate.fontsize "w";
797 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
800 let vlog fmt =
801 if conf.verbose
802 then
803 Printf.kprintf prerr_endline fmt
804 else
805 Printf.kprintf ignore fmt
808 let launchpath () =
809 if emptystr conf.pathlauncher
810 then print_endline state.path
811 else (
812 let re = Str.regexp "%s" in
813 let command = Str.global_replace re state.path conf.pathlauncher in
814 try popen command []
815 with exn ->
816 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
817 flush stderr;
821 module Ne = struct
822 type 'a t = | Res of 'a | Exn of exn;;
824 let pipe () =
825 try Res (Unix.pipe ())
826 with exn -> Exn exn
829 let clo fd f =
830 try tempfailureretry Unix.close fd
831 with exn -> f (exntos exn)
834 let dup fd =
835 try Res (tempfailureretry Unix.dup fd)
836 with exn -> Exn exn
839 let dup2 fd1 fd2 =
840 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
841 with exn -> Exn exn
843 end;;
845 let redirectstderr () =
846 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
847 if conf.redirectstderr
848 then
849 match Ne.pipe () with
850 | Ne.Exn exn ->
851 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
853 | Ne.Res (r, w) ->
854 begin match Ne.dup Unix.stderr with
855 | Ne.Exn exn ->
856 dolog "failed to dup stderr: %s" (exntos exn);
857 Ne.clo r (clofail "pipe/r");
858 Ne.clo w (clofail "pipe/w");
860 | Ne.Res dupstderr ->
861 begin match Ne.dup2 w Unix.stderr with
862 | Ne.Exn exn ->
863 dolog "failed to dup2 to stderr: %s" (exntos exn);
864 Ne.clo dupstderr (clofail "stderr duplicate");
865 Ne.clo r (clofail "redir pipe/r");
866 Ne.clo w (clofail "redir pipe/w");
868 | Ne.Res () ->
869 state.stderr <- dupstderr;
870 state.errfd <- Some r;
871 end;
873 else (
874 state.newerrmsgs <- false;
875 begin match state.errfd with
876 | Some fd ->
877 begin match Ne.dup2 state.stderr Unix.stderr with
878 | Ne.Exn exn ->
879 dolog "failed to dup2 original stderr: %s" (exntos exn)
880 | Ne.Res () ->
881 Ne.clo fd (clofail "dup of stderr");
882 state.errfd <- None;
883 end;
884 | None -> ()
885 end;
886 prerr_string (Buffer.contents state.errmsgs);
887 flush stderr;
888 Buffer.clear state.errmsgs;
892 module G =
893 struct
894 let postRedisplay who =
895 if conf.verbose
896 then prerr_endline ("redisplay for " ^ who);
897 state.redisplay <- true;
899 end;;
901 let getopaque pageno =
902 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
903 with Not_found -> None
906 let putopaque pageno opaque =
907 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
910 let pagetranslatepoint l x y =
911 let dy = y - l.pagedispy in
912 let y = dy + l.pagey in
913 let dx = x - l.pagedispx in
914 let x = dx + l.pagex in
915 (x, y);
918 let onppundermouse g x y d =
919 let rec f = function
920 | l :: rest ->
921 begin match getopaque l.pageno with
922 | Some opaque ->
923 let x0 = l.pagedispx in
924 let x1 = x0 + l.pagevw in
925 let y0 = l.pagedispy in
926 let y1 = y0 + l.pagevh in
927 if y >= y0 && y <= y1 && x >= x0 && x <= x1
928 then
929 let px, py = pagetranslatepoint l x y in
930 match g opaque l px py with
931 | Some res -> res
932 | None -> f rest
933 else f rest
934 | _ ->
935 f rest
937 | [] -> d
939 f state.layout
942 let getunder x y =
943 let g opaque l px py =
944 if state.bzoom
945 then (
946 match rectofblock opaque px py with
947 | Some a ->
948 let rect = (a.(0),a.(2),a.(1),a.(2),a.(1),a.(3),a.(0),a.(3)) in
949 state.rects <- [l.pageno, l.pageno mod 3, rect];
950 G.postRedisplay "getunder";
951 | None -> ()
953 match whatsunder opaque px py with
954 | Unone -> None
955 | under -> Some under
957 onppundermouse g x y Unone
960 let unproject x y =
961 let g opaque l x y =
962 match unproject opaque x y with
963 | Some (x, y) -> Some (Some (l.pageno, x, y))
964 | None -> None
966 onppundermouse g x y None;
969 let showtext c s =
970 state.text <- Printf.sprintf "%c%s" c s;
971 G.postRedisplay "showtext";
974 let paxunder x y =
975 let g opaque l px py =
976 if markunder opaque px py conf.paxmark
977 then (
978 Some (fun () ->
979 match getopaque l.pageno with
980 | None -> ()
981 | Some opaque ->
982 match Ne.pipe () with
983 | Ne.Exn exn ->
984 showtext '!'
985 (Printf.sprintf
986 "can not create mark pipe: %s"
987 (exntos exn));
988 | Ne.Res (r, w) ->
989 let doclose what fd =
990 Ne.clo fd (fun msg ->
991 dolog "%s close failed: %s" what msg)
994 popen conf.paxcmd [r, 0; w, -1];
995 copysel w opaque false;
996 doclose "pipe/r" r;
997 G.postRedisplay "paxunder";
998 with exn ->
999 dolog "can not execute %S: %s"
1000 conf.paxcmd (exntos exn);
1001 doclose "pipe/r" r;
1002 doclose "pipe/w" w;
1005 else None
1007 G.postRedisplay "paxunder";
1008 if conf.paxmark = Mark_page
1009 then
1010 List.iter (fun l ->
1011 match getopaque l.pageno with
1012 | None -> ()
1013 | Some opaque -> clearmark opaque) state.layout;
1014 state.roam <-
1015 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
1018 let selstring s =
1019 match Ne.pipe () with
1020 | Ne.Exn exn ->
1021 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
1022 | Ne.Res (r, w) ->
1023 let popened =
1024 try popen conf.selcmd [r, 0; w, -1]; true
1025 with exn ->
1026 showtext '!'
1027 (Printf.sprintf "failed to execute %s: %s"
1028 conf.selcmd (exntos exn));
1029 false
1031 let clo cap fd =
1032 Ne.clo fd (fun msg ->
1033 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
1036 if popened
1037 then (
1039 let l = String.length s in
1040 let n = tempfailureretry (Unix.write w s 0) l in
1041 if n != l
1042 then
1043 showtext '!'
1044 (Printf.sprintf
1045 "failed to write %d characters to sel pipe, wrote %d"
1048 with exn ->
1049 showtext '!'
1050 (Printf.sprintf "failed to write to sel pipe: %s"
1051 (exntos exn)
1054 else dolog "%s" s;
1055 clo "pipe/r" r;
1056 clo "pipe/w" w;
1059 let undertext = function
1060 | Unone -> "none"
1061 | Ulinkuri s -> s
1062 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
1063 | Utext s -> "font: " ^ s
1064 | Uunexpected s -> "unexpected: " ^ s
1065 | Ulaunch s -> "launch: " ^ s
1066 | Unamed s -> "named: " ^ s
1067 | Uremote (filename, pageno) ->
1068 Printf.sprintf "%s: page %d" filename (pageno+1)
1069 | Uremotedest (filename, destname) ->
1070 Printf.sprintf "%s: destination %S" filename destname
1073 let updateunder x y =
1074 match getunder x y with
1075 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
1076 | Ulinkuri uri ->
1077 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1078 Wsi.setcursor Wsi.CURSOR_INFO
1079 | Ulinkgoto (pageno, _) ->
1080 if conf.underinfo
1081 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
1082 Wsi.setcursor Wsi.CURSOR_INFO
1083 | Utext s ->
1084 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1085 Wsi.setcursor Wsi.CURSOR_TEXT
1086 | Uunexpected s ->
1087 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
1088 Wsi.setcursor Wsi.CURSOR_INHERIT
1089 | Ulaunch s ->
1090 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
1091 Wsi.setcursor Wsi.CURSOR_INHERIT
1092 | Unamed s ->
1093 if conf.underinfo then showtext 'n' ("amed: " ^ s);
1094 Wsi.setcursor Wsi.CURSOR_INHERIT
1095 | Uremote (filename, pageno) ->
1096 if conf.underinfo then showtext 'r'
1097 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
1098 Wsi.setcursor Wsi.CURSOR_INFO
1099 | Uremotedest (filename, destname) ->
1100 if conf.underinfo then showtext 'r'
1101 (Printf.sprintf "emote destination: %s (%S)" filename destname);
1102 Wsi.setcursor Wsi.CURSOR_INFO
1105 let showlinktype under =
1106 if conf.underinfo
1107 then
1108 match under with
1109 | Unone -> ()
1110 | under ->
1111 let s = undertext under in
1112 showtext ' ' s
1115 let addchar s c =
1116 let b = Buffer.create (String.length s + 1) in
1117 Buffer.add_string b s;
1118 Buffer.add_char b c;
1119 Buffer.contents b;
1122 module type TextEnumType =
1124 type t
1125 val name : string
1126 val names : string array
1127 end;;
1129 module TextEnumMake (Ten : TextEnumType) =
1130 struct
1131 let names = Ten.names;;
1132 let to_int (t : Ten.t) = Obj.magic t;;
1133 let to_string t = names.(to_int t);;
1134 let of_int n : Ten.t = Obj.magic n;;
1135 let of_string s =
1136 let rec find i =
1137 if i = Array.length names
1138 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
1139 else (
1140 if Ten.names.(i) = s
1141 then of_int i
1142 else find (i+1)
1144 in find 0;;
1145 end;;
1147 module CSTE = TextEnumMake (struct
1148 type t = colorspace;;
1149 let name = "colorspace";;
1150 let names = [|"rgb"; "bgr"; "gray"|];;
1151 end);;
1153 module MTE = TextEnumMake (struct
1154 type t = mark;;
1155 let name = "mark";;
1156 let names = [|"page"; "block"; "line"; "word"|];;
1157 end);;
1159 module FMTE = TextEnumMake (struct
1160 type t= fitmodel;;
1161 let name = "fitmodel";;
1162 let names = [|"width"; "proportional"; "page"|];;
1163 end);;
1165 let intentry_with_suffix text key =
1166 let c =
1167 if key >= 32 && key < 127
1168 then Char.chr key
1169 else '\000'
1171 match Char.lowercase c with
1172 | '0' .. '9' ->
1173 let text = addchar text c in
1174 TEcont text
1176 | 'k' | 'm' | 'g' ->
1177 let text = addchar text c in
1178 TEcont text
1180 | _ ->
1181 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1182 TEcont text
1185 let multicolumns_to_string (n, a, b) =
1186 if a = 0 && b = 0
1187 then Printf.sprintf "%d" n
1188 else Printf.sprintf "%d,%d,%d" n a b;
1191 let multicolumns_of_string s =
1193 (int_of_string s, 0, 0)
1194 with _ ->
1195 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1196 if a > 1 || b > 1
1197 then failwith "subtly broken"; (n, a, b)
1201 let readcmd fd =
1202 let s = "xxxx" in
1203 let n = tempfailureretry (Unix.read fd s 0) 4 in
1204 if n != 4 then error "incomplete read(len) = %d" n;
1205 let len = 0
1206 lor (Char.code s.[0] lsl 24)
1207 lor (Char.code s.[1] lsl 16)
1208 lor (Char.code s.[2] lsl 8)
1209 lor (Char.code s.[3] lsl 0)
1211 let s = String.create len in
1212 let n = tempfailureretry (Unix.read fd s 0) len in
1213 if n != len then error "incomplete read(data) %d vs %d" n len;
1217 let btod b = if b then 1 else 0;;
1219 let wcmd fmt =
1220 let b = Buffer.create 16 in
1221 Buffer.add_string b "llll";
1222 Printf.kbprintf
1223 (fun b ->
1224 let s = Buffer.contents b in
1225 let n = String.length s in
1226 let len = n - 4 in
1227 (* dolog "wcmd %S" (String.sub s 4 len); *)
1228 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1229 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1230 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1231 s.[3] <- Char.chr (len land 0xff);
1232 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1233 if n' != n then error "write failed %d vs %d" n' n;
1234 ) b fmt;
1237 let calcips h =
1238 let d = state.winh - h in
1239 max conf.interpagespace ((d + 1) / 2)
1242 let rowyh (c, coverA, coverB) b n =
1243 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1244 then
1245 let _, _, vy, (_, _, h, _) = b.(n) in
1246 (vy, h)
1247 else
1248 let n' = n - coverA in
1249 let d = n' mod c in
1250 let s = n - d in
1251 let e = min state.pagecount (s + c) in
1252 let rec find m miny maxh = if m = e then miny, maxh else
1253 let _, _, y, (_, _, h, _) = b.(m) in
1254 let miny = min miny y in
1255 let maxh = max maxh h in
1256 find (m+1) miny maxh
1257 in find s max_int 0
1260 let calcheight () =
1261 match conf.columns with
1262 | Cmulti ((_, _, _) as cl, b) ->
1263 if Array.length b > 0
1264 then
1265 let y, h = rowyh cl b (Array.length b - 1) in
1266 y + h + (if conf.presentation then calcips h else 0)
1267 else 0
1268 | Csingle b ->
1269 if Array.length b > 0
1270 then
1271 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1272 y + h + (if conf.presentation then calcips h else 0)
1273 else 0
1274 | Csplit (_, b) ->
1275 if Array.length b > 0
1276 then
1277 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1278 y + h
1279 else 0
1282 let getpageywh pageno =
1283 let pageno = bound pageno 0 (state.pagecount-1) in
1284 match conf.columns with
1285 | Csingle b ->
1286 if Array.length b = 0
1287 then 0, 0, 0
1288 else
1289 let (_, _, y, (_, w, h, _)) = b.(pageno) in
1290 let y =
1291 if conf.presentation
1292 then y - calcips h
1293 else y
1295 y, w, h
1296 | Cmulti (cl, b) ->
1297 if Array.length b = 0
1298 then 0, 0, 0
1299 else
1300 let y, h = rowyh cl b pageno in
1301 let (_, _, _, (_, w, _, _)) = b.(pageno) in
1302 let y =
1303 if conf.presentation
1304 then y - calcips h
1305 else y
1307 y, w, h
1308 | Csplit (c, b) ->
1309 if Array.length b = 0
1310 then 0, 0, 0
1311 else
1312 let n = pageno*c in
1313 let (_, _, y, (_, w, h, _)) = b.(n) in
1314 y, w / c, h
1317 let getpageyh pageno =
1318 let y,_,h = getpageywh pageno in
1319 y, h;
1322 let getpagedim pageno =
1323 let rec f ppdim l =
1324 match l with
1325 | (n, _, _, _) as pdim :: rest ->
1326 if n >= pageno
1327 then (if n = pageno then pdim else ppdim)
1328 else f pdim rest
1330 | [] -> ppdim
1332 f (-1, -1, -1, -1) state.pdims
1335 let getpagey pageno = fst (getpageyh pageno);;
1337 let nogeomcmds cmds =
1338 match cmds with
1339 | s, [] -> emptystr s
1340 | _ -> false
1343 let page_of_y y =
1344 let ((c, coverA, coverB) as cl), b =
1345 match conf.columns with
1346 | Csingle b -> (1, 0, 0), b
1347 | Cmulti (c, b) -> c, b
1348 | Csplit (_, b) -> (1, 0, 0), b
1350 if Array.length b = 0
1351 then -1
1352 else
1353 let rec bsearch nmin nmax =
1354 if nmin > nmax
1355 then bound nmin 0 (state.pagecount-1)
1356 else
1357 let n = (nmax + nmin) / 2 in
1358 let vy, h = rowyh cl b n in
1359 let y0, y1 =
1360 if conf.presentation
1361 then
1362 let ips = calcips h in
1363 let y0 = vy - ips in
1364 let y1 = vy + h + ips in
1365 y0, y1
1366 else (
1367 if n = 0
1368 then 0, vy + h + conf.interpagespace
1369 else
1370 let y0 = vy - conf.interpagespace in
1371 y0, y0 + h + conf.interpagespace
1374 if y >= y0 && y < y1
1375 then (
1376 if c = 1
1377 then n
1378 else (
1379 if n > coverA
1380 then
1381 if n < state.pagecount - coverB
1382 then ((n-coverA)/c)*c + coverA
1383 else n
1384 else n
1387 else (
1388 if y > y0
1389 then bsearch (n+1) nmax
1390 else bsearch nmin (n-1)
1393 bsearch 0 (state.pagecount-1);
1396 let layoutN ((columns, coverA, coverB), b) y sh =
1397 let sh = sh - (hscrollh ()) in
1398 let rec fold accu n =
1399 if n = Array.length b
1400 then accu
1401 else
1402 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1403 if (vy - y) > sh &&
1404 (n = coverA - 1
1405 || n = state.pagecount - coverB
1406 || (n - coverA) mod columns = columns - 1)
1407 then accu
1408 else
1409 let accu =
1410 if vy + h > y
1411 then
1412 let pagey = max 0 (y - vy) in
1413 let pagedispy = if pagey > 0 then 0 else vy - y in
1414 let pagedispx, pagex =
1415 let pdx =
1416 if n = coverA - 1 || n = state.pagecount - coverB
1417 then state.x + (wadjsb state.winw - w) / 2
1418 else dx + xoff + state.x
1420 if pdx < 0
1421 then 0, -pdx
1422 else pdx, 0
1424 let pagevw =
1425 let vw = wadjsb state.winw - pagedispx in
1426 let pw = w - pagex in
1427 min vw pw
1429 let pagevh = min (h - pagey) (sh - pagedispy) in
1430 if pagevw > 0 && pagevh > 0
1431 then
1432 let e =
1433 { pageno = n
1434 ; pagedimno = pdimno
1435 ; pagew = w
1436 ; pageh = h
1437 ; pagex = pagex
1438 ; pagey = pagey
1439 ; pagevw = pagevw
1440 ; pagevh = pagevh
1441 ; pagedispx = pagedispx
1442 ; pagedispy = pagedispy
1443 ; pagecol = 0
1446 e :: accu
1447 else
1448 accu
1449 else
1450 accu
1452 fold accu (n+1)
1454 if Array.length b = 0
1455 then []
1456 else List.rev (fold [] (page_of_y y))
1459 let layoutS (columns, b) y sh =
1460 let sh = sh - hscrollh () in
1461 let rec fold accu n =
1462 if n = Array.length b
1463 then accu
1464 else
1465 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1466 if (vy - y) > sh
1467 then accu
1468 else
1469 let accu =
1470 if vy + pageh > y
1471 then
1472 let x = xoff + state.x in
1473 let pagey = max 0 (y - vy) in
1474 let pagedispy = if pagey > 0 then 0 else vy - y in
1475 let pagedispx, pagex =
1476 if px = 0
1477 then (
1478 if x < 0
1479 then 0, -x
1480 else x, 0
1482 else (
1483 let px = px - x in
1484 if px < 0
1485 then -px, 0
1486 else 0, px
1489 let pagecolw = pagew/columns in
1490 let pagedispx =
1491 if pagecolw < state.winw
1492 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
1493 else pagedispx
1495 let pagevw =
1496 let vw = wadjsb state.winw - pagedispx in
1497 let pw = pagew - pagex in
1498 min vw pw
1500 let pagevw = min pagevw pagecolw in
1501 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1502 if pagevw > 0 && pagevh > 0
1503 then
1504 let e =
1505 { pageno = n/columns
1506 ; pagedimno = pdimno
1507 ; pagew = pagew
1508 ; pageh = pageh
1509 ; pagex = pagex
1510 ; pagey = pagey
1511 ; pagevw = pagevw
1512 ; pagevh = pagevh
1513 ; pagedispx = pagedispx
1514 ; pagedispy = pagedispy
1515 ; pagecol = n mod columns
1518 e :: accu
1519 else
1520 accu
1521 else
1522 accu
1524 fold accu (n+1)
1526 List.rev (fold [] 0)
1529 let layout y sh =
1530 if nogeomcmds state.geomcmds
1531 then
1532 match conf.columns with
1533 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1534 | Cmulti c -> layoutN c y sh
1535 | Csplit s -> layoutS s y sh
1536 else []
1539 let clamp incr =
1540 let y = state.y + incr in
1541 let y = max 0 y in
1542 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1546 let itertiles l f =
1547 let tilex = l.pagex mod conf.tilew in
1548 let tiley = l.pagey mod conf.tileh in
1550 let col = l.pagex / conf.tilew in
1551 let row = l.pagey / conf.tileh in
1553 let rec rowloop row y0 dispy h =
1554 if h = 0
1555 then ()
1556 else (
1557 let dh = conf.tileh - y0 in
1558 let dh = min h dh in
1559 let rec colloop col x0 dispx w =
1560 if w = 0
1561 then ()
1562 else (
1563 let dw = conf.tilew - x0 in
1564 let dw = min w dw in
1566 f col row dispx dispy x0 y0 dw dh;
1567 colloop (col+1) 0 (dispx+dw) (w-dw)
1570 colloop col tilex l.pagedispx l.pagevw;
1571 rowloop (row+1) 0 (dispy+dh) (h-dh)
1574 if l.pagevw > 0 && l.pagevh > 0
1575 then rowloop row tiley l.pagedispy l.pagevh;
1578 let gettileopaque l col row =
1579 let key =
1580 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1582 try Some (Hashtbl.find state.tilemap key)
1583 with Not_found -> None
1586 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1587 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1588 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1591 let filledrect x0 y0 x1 y1 =
1592 GlArray.disable `texture_coord;
1593 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
1594 GlArray.vertex `two state.vraw;
1595 GlArray.draw_arrays `triangle_strip 0 4;
1596 GlArray.enable `texture_coord;
1599 let linerect x0 y0 x1 y1 =
1600 GlArray.disable `texture_coord;
1601 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
1602 GlArray.vertex `two state.vraw;
1603 GlArray.draw_arrays `line_loop 0 4;
1604 GlArray.enable `texture_coord;
1607 let drawtiles l color =
1608 GlDraw.color color;
1609 begintiles ();
1610 let f col row x y tilex tiley w h =
1611 match gettileopaque l col row with
1612 | Some (opaque, _, t) ->
1613 let params = x, y, w, h, tilex, tiley in
1614 if conf.invert
1615 then (
1616 Gl.enable `blend;
1617 GlFunc.blend_func `zero `one_minus_src_color;
1619 drawtile params opaque;
1620 if conf.invert
1621 then Gl.disable `blend;
1622 if conf.debug
1623 then (
1624 endtiles ();
1625 let s = Printf.sprintf
1626 "%d[%d,%d] %f sec"
1627 l.pageno col row t
1629 let w = measurestr fstate.fontsize s in
1630 GlDraw.color (0.0, 0.0, 0.0);
1631 filledrect (float (x-2))
1632 (float (y-2))
1633 (float (x+2) +. w)
1634 (float (y + fstate.fontsize + 2));
1635 GlDraw.color (1.0, 1.0, 1.0);
1636 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1637 begintiles ();
1640 | None ->
1641 endtiles ();
1642 let w =
1643 let lw = wadjsb state.winw - x in
1644 min lw w
1645 and h =
1646 let lh = state.winh - y in
1647 min lh h
1649 begin match state.texid with
1650 | Some id ->
1651 Gl.enable `texture_2d;
1652 GlTex.bind_texture `texture_2d id;
1653 let x0 = float x
1654 and y0 = float y
1655 and x1 = float (x+w)
1656 and y1 = float (y+h) in
1658 let tw = float w /. 16.0
1659 and th = float h /. 16.0 in
1660 let tx0 = float tilex /. 16.0
1661 and ty0 = float tiley /. 16.0 in
1662 let tx1 = tx0 +. tw
1663 and ty1 = ty0 +. th in
1664 Raw.sets_float state.vraw ~pos:0
1665 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
1666 Raw.sets_float state.traw ~pos:0
1667 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
1668 GlArray.vertex `two state.vraw;
1669 GlArray.tex_coord `two state.traw;
1670 GlArray.draw_arrays `triangle_strip 0 4;
1672 | None ->
1673 GlDraw.color (1.0, 1.0, 1.0);
1674 filledrect (float x) (float y) (float (x+w)) (float (y+h));
1675 end;
1676 if w > 128 && h > fstate.fontsize + 10
1677 then (
1678 GlDraw.color (0.0, 0.0, 0.0);
1679 let c, r =
1680 if conf.verbose
1681 then (col*conf.tilew, row*conf.tileh)
1682 else col, row
1684 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1686 GlDraw.color color;
1687 begintiles ();
1689 itertiles l f;
1690 endtiles ();
1693 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1695 let tilevisible1 l x y =
1696 let ax0 = l.pagex
1697 and ax1 = l.pagex + l.pagevw
1698 and ay0 = l.pagey
1699 and ay1 = l.pagey + l.pagevh in
1701 let bx0 = x
1702 and by0 = y in
1703 let bx1 = min (bx0 + conf.tilew) l.pagew
1704 and by1 = min (by0 + conf.tileh) l.pageh in
1706 let rx0 = max ax0 bx0
1707 and ry0 = max ay0 by0
1708 and rx1 = min ax1 bx1
1709 and ry1 = min ay1 by1 in
1711 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1712 nonemptyintersection
1715 let tilevisible layout n x y =
1716 let rec findpageinlayout m = function
1717 | l :: rest when l.pageno = n ->
1718 tilevisible1 l x y || (
1719 match conf.columns with
1720 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1721 | _ -> false
1723 | _ :: rest -> findpageinlayout 0 rest
1724 | [] -> false
1726 findpageinlayout 0 layout;
1729 let tileready l x y =
1730 tilevisible1 l x y &&
1731 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1734 let tilepage n p layout =
1735 let rec loop = function
1736 | l :: rest ->
1737 if l.pageno = n
1738 then
1739 let f col row _ _ _ _ _ _ =
1740 if state.currently = Idle
1741 then
1742 match gettileopaque l col row with
1743 | Some _ -> ()
1744 | None ->
1745 let x = col*conf.tilew
1746 and y = row*conf.tileh in
1747 let w =
1748 let w = l.pagew - x in
1749 min w conf.tilew
1751 let h =
1752 let h = l.pageh - y in
1753 min h conf.tileh
1755 let pbo =
1756 if conf.usepbo
1757 then getpbo w h conf.colorspace
1758 else "0"
1760 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1761 state.currently <-
1762 Tiling (
1763 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1764 conf.tilew, conf.tileh
1767 itertiles l f;
1768 else
1769 loop rest
1771 | [] -> ()
1773 if nogeomcmds state.geomcmds
1774 then loop layout;
1777 let preloadlayout y =
1778 let y = if y < state.winh then 0 else y - state.winh in
1779 let h = state.winh*3 in
1780 layout y h;
1783 let load pages =
1784 let rec loop pages =
1785 if state.currently != Idle
1786 then ()
1787 else
1788 match pages with
1789 | l :: rest ->
1790 begin match getopaque l.pageno with
1791 | None ->
1792 wcmd "page %d %d" l.pageno l.pagedimno;
1793 state.currently <- Loading (l, state.gen);
1794 | Some opaque ->
1795 tilepage l.pageno opaque pages;
1796 loop rest
1797 end;
1798 | _ -> ()
1800 if nogeomcmds state.geomcmds
1801 then loop pages
1804 let preload pages =
1805 load pages;
1806 if conf.preload && state.currently = Idle
1807 then load (preloadlayout state.y);
1810 let layoutready layout =
1811 let rec fold all ls =
1812 all && match ls with
1813 | l :: rest ->
1814 let seen = ref false in
1815 let allvisible = ref true in
1816 let foo col row _ _ _ _ _ _ =
1817 seen := true;
1818 allvisible := !allvisible &&
1819 begin match gettileopaque l col row with
1820 | Some _ -> true
1821 | None -> false
1824 itertiles l foo;
1825 fold (!seen && !allvisible) rest
1826 | [] -> true
1828 let alltilesvisible = fold true layout in
1829 alltilesvisible;
1832 let gotoy y =
1833 let y = bound y 0 state.maxy in
1834 let y, layout, proceed =
1835 match conf.maxwait with
1836 | Some time when state.ghyll == noghyll ->
1837 begin match state.throttle with
1838 | None ->
1839 let layout = layout y state.winh in
1840 let ready = layoutready layout in
1841 if not ready
1842 then (
1843 load layout;
1844 state.throttle <- Some (layout, y, now ());
1846 else G.postRedisplay "gotoy showall (None)";
1847 y, layout, ready
1848 | Some (_, _, started) ->
1849 let dt = now () -. started in
1850 if dt > time
1851 then (
1852 state.throttle <- None;
1853 let layout = layout y state.winh in
1854 load layout;
1855 G.postRedisplay "maxwait";
1856 y, layout, true
1858 else -1, [], false
1861 | _ ->
1862 let layout = layout y state.winh in
1863 if not !wtmode || layoutready layout
1864 then G.postRedisplay "gotoy ready";
1865 y, layout, true
1867 if proceed
1868 then (
1869 state.y <- y;
1870 state.layout <- layout;
1871 begin match state.mode with
1872 | LinkNav (Ltexact (pageno, linkno)) ->
1873 let rec loop = function
1874 | [] ->
1875 state.mode <- LinkNav (Ltgendir 0)
1876 | l :: _ when l.pageno = pageno ->
1877 begin match getopaque pageno with
1878 | None ->
1879 state.mode <- LinkNav (Ltgendir 0)
1880 | Some opaque ->
1881 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1882 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1883 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1884 then state.mode <- LinkNav (Ltgendir 0)
1886 | _ :: rest -> loop rest
1888 loop layout
1889 | _ -> ()
1890 end;
1891 begin match state.mode with
1892 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1893 if not (pagevisible layout pageno)
1894 then (
1895 match state.layout with
1896 | [] -> ()
1897 | l :: _ ->
1898 state.mode <- Birdseye (
1899 conf, leftx, l.pageno, hooverpageno, anchor
1902 | LinkNav (Ltgendir dir as lt) ->
1903 let linknav =
1904 let rec loop = function
1905 | [] -> lt
1906 | l :: rest ->
1907 match getopaque l.pageno with
1908 | None -> loop rest
1909 | Some opaque ->
1910 let link =
1911 let ld =
1912 if dir = 0
1913 then LDfirstvisible (l.pagex, l.pagey, dir)
1914 else (
1915 if dir > 0 then LDfirst else LDlast
1918 findlink opaque ld
1920 match link with
1921 | Lnotfound -> loop rest
1922 | Lfound n ->
1923 showlinktype (getlink opaque n);
1924 Ltexact (l.pageno, n)
1926 loop state.layout
1928 state.mode <- LinkNav linknav
1929 | _ -> ()
1930 end;
1931 preload layout;
1933 state.ghyll <- noghyll;
1934 if conf.updatecurs
1935 then (
1936 let mx, my = state.mpos in
1937 updateunder mx my;
1941 let conttiling pageno opaque =
1942 tilepage pageno opaque
1943 (if conf.preload then preloadlayout state.y else state.layout)
1946 let gotoy_and_clear_text y =
1947 if not conf.verbose then state.text <- "";
1948 gotoy y;
1951 let getanchor1 l =
1952 let top =
1953 let coloff = l.pagecol * l.pageh in
1954 float (l.pagey + coloff) /. float l.pageh
1956 let dtop =
1957 if l.pagedispy = 0
1958 then
1960 else (
1961 if conf.presentation
1962 then float l.pagedispy /. float (calcips l.pageh)
1963 else float l.pagedispy /. float conf.interpagespace
1966 (l.pageno, top, dtop)
1969 let getanchor () =
1970 match state.layout with
1971 | l :: _ -> getanchor1 l
1972 | [] ->
1973 let n = page_of_y state.y in
1974 if n = -1
1975 then state.anchor
1976 else
1977 let y, h = getpageyh n in
1978 let dy = y - state.y in
1979 let dtop =
1980 if conf.presentation
1981 then
1982 let ips = calcips h in
1983 float (dy + ips) /. float ips
1984 else
1985 float dy /. float conf.interpagespace
1987 (n, 0.0, dtop)
1990 let getanchory (n, top, dtop) =
1991 let y, h = getpageyh n in
1992 if conf.presentation
1993 then
1994 let ips = calcips h in
1995 y + truncate (top*.float h -. dtop*.float ips) + ips;
1996 else
1997 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
2000 let gotoanchor anchor =
2001 gotoy (getanchory anchor);
2004 let addnav () =
2005 cbput state.hists.nav (getanchor ());
2008 let getnav dir =
2009 let anchor = cbgetc state.hists.nav dir in
2010 getanchory anchor;
2013 let gotoghyll y =
2014 let scroll f n a b =
2015 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
2016 let snake f a b =
2017 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
2018 if f < a
2019 then s (float f /. float a)
2020 else (
2021 if f > b
2022 then 1.0 -. s ((float (f-b) /. float (n-b)))
2023 else 1.0
2026 snake f a b
2027 and summa n a b =
2028 let ins = float a *. 0.5
2029 and outs = float (n-b) *. 0.5 in
2030 let ones = b - a in
2031 ins +. outs +. float ones
2033 let rec set (_N, _A, _B) y sy =
2034 let sum = summa _N _A _B in
2035 let dy = float (y - sy) in
2036 state.ghyll <- (
2037 let rec gf n y1 o =
2038 if n >= _N
2039 then state.ghyll <- noghyll
2040 else
2041 let go n =
2042 let s = scroll n _N _A _B in
2043 let y1 = y1 +. ((s *. dy) /. sum) in
2044 gotoy_and_clear_text (truncate y1);
2045 state.ghyll <- gf (n+1) y1;
2047 match o with
2048 | None -> go n
2049 | Some y' -> set (_N/2, 1, 1) y' state.y
2051 gf 0 (float state.y)
2054 match conf.ghyllscroll with
2055 | None ->
2056 gotoy_and_clear_text y
2057 | Some nab ->
2058 if state.ghyll == noghyll
2059 then set nab y state.y
2060 else state.ghyll (Some y)
2063 let gotopage n top =
2064 let y, h = getpageyh n in
2065 let y = y + (truncate (top *. float h)) in
2066 gotoghyll y
2069 let gotopage1 n top =
2070 let y = getpagey n in
2071 let y = y + top in
2072 gotoghyll y
2075 let invalidate s f =
2076 state.layout <- [];
2077 state.pdims <- [];
2078 state.rects <- [];
2079 state.rects1 <- [];
2080 match state.geomcmds with
2081 | ps, [] when emptystr ps ->
2082 f ();
2083 state.geomcmds <- s, [];
2085 | ps, [] ->
2086 state.geomcmds <- ps, [s, f];
2088 | ps, (s', _) :: rest when s' = s ->
2089 state.geomcmds <- ps, ((s, f) :: rest);
2091 | ps, cmds ->
2092 state.geomcmds <- ps, ((s, f) :: cmds);
2095 let flushpages () =
2096 Hashtbl.iter (fun _ opaque ->
2097 wcmd "freepage %s" opaque;
2098 ) state.pagemap;
2099 Hashtbl.clear state.pagemap;
2102 let flushtiles () =
2103 if not (Queue.is_empty state.tilelru)
2104 then (
2105 Queue.iter (fun (k, p, s) ->
2106 wcmd "freetile %s" p;
2107 state.memused <- state.memused - s;
2108 Hashtbl.remove state.tilemap k;
2109 ) state.tilelru;
2110 state.uioh#infochanged Memused;
2111 Queue.clear state.tilelru;
2113 load state.layout;
2116 let stateh h =
2117 let h = truncate (float h*.conf.zoom) in
2118 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2119 h - d
2122 let opendoc path password =
2123 state.path <- path;
2124 state.password <- password;
2125 state.gen <- state.gen + 1;
2126 state.docinfo <- [];
2128 flushpages ();
2129 setaalevel conf.aalevel;
2130 let titlepath =
2131 if emptystr state.origin
2132 then path
2133 else state.origin
2135 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2136 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
2137 invalidate "reqlayout"
2138 (fun () ->
2139 wcmd "reqlayout %d %d %d %s\000"
2140 conf.angle (FMTE.to_int conf.fitmodel)
2141 (stateh state.winh) state.nameddest
2145 let reload () =
2146 state.anchor <- getanchor ();
2147 opendoc state.path state.password;
2150 let scalecolor c =
2151 let c = c *. conf.colorscale in
2152 (c, c, c);
2155 let scalecolor2 (r, g, b) =
2156 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2159 let docolumns = function
2160 | Csingle _ ->
2161 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2162 let rec loop pageno pdimno pdim y ph pdims =
2163 if pageno = state.pagecount
2164 then ()
2165 else
2166 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2167 match pdims with
2168 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2169 pdimno+1, pdim, rest
2170 | _ ->
2171 pdimno, pdim, pdims
2173 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2174 let y = y +
2175 (if conf.presentation
2176 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2177 else (if pageno = 0 then 0 else conf.interpagespace)
2180 a.(pageno) <- (pdimno, x, y, pdim);
2181 loop (pageno+1) pdimno pdim (y + h) h pdims
2183 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2184 conf.columns <- Csingle a;
2186 | Cmulti ((columns, coverA, coverB), _) ->
2187 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2188 let rec loop pageno pdimno pdim x y rowh pdims =
2189 let rec fixrow m = if m = pageno then () else
2190 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2191 if h < rowh
2192 then (
2193 let y = y + (rowh - h) / 2 in
2194 a.(m) <- (pdimno, x, y, pdim);
2196 fixrow (m+1)
2198 if pageno = state.pagecount
2199 then fixrow (((pageno - 1) / columns) * columns)
2200 else
2201 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2202 match pdims with
2203 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2204 pdimno+1, pdim, rest
2205 | _ ->
2206 pdimno, pdim, pdims
2208 let x, y, rowh' =
2209 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2210 then (
2211 let x = (wadjsb state.winw - w) / 2 in
2212 let ips =
2213 if conf.presentation then calcips h else conf.interpagespace in
2214 x, y + ips + rowh, h
2216 else (
2217 if (pageno - coverA) mod columns = 0
2218 then (
2219 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2220 let y =
2221 if conf.presentation
2222 then
2223 let ips = calcips h in
2224 y + (if pageno = 0 then 0 else calcips rowh + ips)
2225 else
2226 y + (if pageno = 0 then 0 else conf.interpagespace)
2228 x, y + rowh, h
2230 else x, y, max rowh h
2233 let y =
2234 if pageno > 1 && (pageno - coverA) mod columns = 0
2235 then (
2236 let y =
2237 if pageno = columns && conf.presentation
2238 then (
2239 let ips = calcips rowh in
2240 for i = 0 to pred columns
2242 let (pdimno, x, y, pdim) = a.(i) in
2243 a.(i) <- (pdimno, x, y+ips, pdim)
2244 done;
2245 y+ips;
2247 else y
2249 fixrow (pageno - columns);
2252 else y
2254 a.(pageno) <- (pdimno, x, y, pdim);
2255 let x = x + w + xoff*2 + conf.interpagespace in
2256 loop (pageno+1) pdimno pdim x y rowh' pdims
2258 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2259 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2261 | Csplit (c, _) ->
2262 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2263 let rec loop pageno pdimno pdim y pdims =
2264 if pageno = state.pagecount
2265 then ()
2266 else
2267 let pdimno, ((_, w, h, _) as pdim), pdims =
2268 match pdims with
2269 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2270 pdimno+1, pdim, rest
2271 | _ ->
2272 pdimno, pdim, pdims
2274 let cw = w / c in
2275 let rec loop1 n x y =
2276 if n = c then y else (
2277 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2278 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2281 let y = loop1 0 0 y in
2282 loop (pageno+1) pdimno pdim y pdims
2284 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2285 conf.columns <- Csplit (c, a);
2288 let represent () =
2289 docolumns conf.columns;
2290 state.maxy <- calcheight ();
2291 if state.reprf == noreprf
2292 then (
2293 match state.mode with
2294 | Birdseye (_, _, pageno, _, _) ->
2295 let y, h = getpageyh pageno in
2296 let top = (state.winh - h) / 2 in
2297 gotoy (max 0 (y - top))
2298 | _ -> gotoanchor state.anchor
2300 else (
2301 state.reprf ();
2302 state.reprf <- noreprf;
2306 let reshape w h =
2307 GlDraw.viewport 0 0 w h;
2308 let firsttime = state.geomcmds == firstgeomcmds in
2309 if not firsttime && nogeomcmds state.geomcmds
2310 then state.anchor <- getanchor ();
2312 state.winw <- w;
2313 let w = wadjsb (truncate (float w *. conf.zoom)) in
2314 let w = max w 2 in
2315 state.winh <- h;
2316 setfontsize fstate.fontsize;
2317 GlMat.mode `modelview;
2318 GlMat.load_identity ();
2320 GlMat.mode `projection;
2321 GlMat.load_identity ();
2322 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2323 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2324 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2326 let relx =
2327 if conf.zoom <= 1.0
2328 then 0.0
2329 else float state.x /. float state.w
2331 invalidate "geometry"
2332 (fun () ->
2333 state.w <- w;
2334 if not firsttime
2335 then state.x <- truncate (relx *. float w);
2336 let w =
2337 match conf.columns with
2338 | Csingle _ -> w
2339 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2340 | Csplit (c, _) -> w * c
2342 wcmd "geometry %d %d %d"
2343 w (stateh h) (FMTE.to_int conf.fitmodel)
2347 let enttext () =
2348 let len = String.length state.text in
2349 let drawstring s =
2350 let hscrollh =
2351 match state.mode with
2352 | Textentry _ | View | LinkNav _ ->
2353 let h, _, _ = state.uioh#scrollpw in
2355 | _ -> 0
2357 let rect x w =
2358 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
2359 (x+.w) (float (state.winh - hscrollh))
2362 let w = float (wadjsb state.winw - 1) in
2363 if state.progress >= 0.0 && state.progress < 1.0
2364 then (
2365 GlDraw.color (0.3, 0.3, 0.3);
2366 let w1 = w *. state.progress in
2367 rect 0.0 w1;
2368 GlDraw.color (0.0, 0.0, 0.0);
2369 rect w1 (w-.w1)
2371 else (
2372 GlDraw.color (0.0, 0.0, 0.0);
2373 rect 0.0 w;
2376 GlDraw.color (1.0, 1.0, 1.0);
2377 drawstring fstate.fontsize
2378 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2380 let s =
2381 match state.mode with
2382 | Textentry ((prefix, text, _, _, _, _), _) ->
2383 let s =
2384 if len > 0
2385 then
2386 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2387 else
2388 Printf.sprintf "%s%s_" prefix text
2392 | _ -> state.text
2394 let s =
2395 if state.newerrmsgs
2396 then (
2397 if not (istextentry state.mode) && state.uioh#eformsgs
2398 then
2399 let s1 = "(press 'e' to review error messasges)" in
2400 if nonemptystr s then s ^ " " ^ s1 else s1
2401 else s
2403 else s
2405 if nonemptystr s
2406 then drawstring s
2409 let gctiles () =
2410 let len = Queue.length state.tilelru in
2411 let layout = lazy (
2412 match state.throttle with
2413 | None ->
2414 if conf.preload
2415 then preloadlayout state.y
2416 else state.layout
2417 | Some (layout, _, _) ->
2418 layout
2419 ) in
2420 let rec loop qpos =
2421 if state.memused <= conf.memlimit
2422 then ()
2423 else (
2424 if qpos < len
2425 then
2426 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2427 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2428 let (_, pw, ph, _) = getpagedim n in
2430 gen = state.gen
2431 && colorspace = conf.colorspace
2432 && angle = conf.angle
2433 && pagew = pw
2434 && pageh = ph
2435 && (
2436 let x = col*conf.tilew
2437 and y = row*conf.tileh in
2438 tilevisible (Lazy.force_val layout) n x y
2440 then Queue.push lruitem state.tilelru
2441 else (
2442 freepbo p;
2443 wcmd "freetile %s" p;
2444 state.memused <- state.memused - s;
2445 state.uioh#infochanged Memused;
2446 Hashtbl.remove state.tilemap k;
2448 loop (qpos+1)
2451 loop 0
2454 let logcurrently = function
2455 | Idle -> dolog "Idle"
2456 | Loading (l, gen) ->
2457 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2458 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2459 dolog
2460 "Tiling %d[%d,%d] page=%s cs=%s angle"
2461 l.pageno col row pageopaque
2462 (CSTE.to_string colorspace)
2464 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2465 angle gen conf.angle state.gen
2466 tilew tileh
2467 conf.tilew conf.tileh
2469 | Outlining _ ->
2470 dolog "outlining"
2473 let splitatspace =
2474 let r = Str.regexp " " in
2475 fun s -> Str.bounded_split r s 2;
2478 let onpagerect pageno f =
2479 let b =
2480 match conf.columns with
2481 | Cmulti (_, b) -> b
2482 | Csingle b -> b
2483 | Csplit (_, b) -> b
2485 if pageno >= 0 && pageno < Array.length b
2486 then
2487 let (_, _, _, (w, h, _, _)) = b.(pageno) in
2488 f w h
2491 let gotopagexy1 pageno x y =
2492 let _,w1,h1,leftx = getpagedim pageno in
2493 let top = y /. (float h1) in
2494 let left = x /. (float w1) in
2495 let py, w, h = getpageywh pageno in
2496 let wh = state.winh - hscrollh () in
2497 let x = left *. (float w) in
2498 let x = leftx + state.x + truncate x in
2499 let sx =
2500 if x < 0 || x >= wadjsb state.winw
2501 then state.x - x
2502 else state.x
2504 let pdy = truncate (top *. float h) in
2505 let y' = py + pdy in
2506 let dy = y' - state.y in
2507 let sy =
2508 if x != state.x || not (dy > 0 && dy < wh)
2509 then (
2510 if conf.presentation
2511 then
2512 if abs (py - y') > wh
2513 then y'
2514 else py
2515 else y';
2517 else state.y
2519 if state.x != sx || state.y != sy
2520 then (
2521 let x, y =
2522 if !wtmode
2523 then (
2524 let ww = wadjsb state.winw in
2525 let qx = sx / ww
2526 and qy = pdy / wh in
2527 let x = qx * ww
2528 and y = py + qy * wh in
2529 let x = if -x + ww > w1 then -(w1-ww) else x
2530 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2531 let y =
2532 if conf.presentation
2533 then
2534 if abs (py - y') > wh
2535 then y'
2536 else py
2537 else y';
2539 (x, y)
2541 else (sx, sy)
2543 state.x <- x;
2544 gotoy_and_clear_text y;
2546 else gotoy_and_clear_text state.y;
2549 let gotopagexy pageno x y =
2550 match state.mode with
2551 | Birdseye _ -> gotopage pageno 0.0
2552 | _ -> gotopagexy1 pageno x y
2555 let act cmds =
2556 (* dolog "%S" cmds; *)
2557 let cl = splitatspace cmds in
2558 let scan s fmt f =
2559 try Scanf.sscanf s fmt f
2560 with exn ->
2561 dolog "error processing '%S': %s" cmds (exntos exn);
2562 exit 1
2564 match cl with
2565 | "clear" :: [] ->
2566 state.uioh#infochanged Pdim;
2567 state.pdims <- [];
2569 | "clearrects" :: [] ->
2570 state.rects <- state.rects1;
2571 G.postRedisplay "clearrects";
2573 | "continue" :: args :: [] ->
2574 let n = scan args "%u" (fun n -> n) in
2575 state.pagecount <- n;
2576 begin match state.currently with
2577 | Outlining l ->
2578 state.currently <- Idle;
2579 state.outlines <- Array.of_list (List.rev l)
2580 | _ -> ()
2581 end;
2583 let cur, cmds = state.geomcmds in
2584 if emptystr cur
2585 then failwith "umpossible";
2587 begin match List.rev cmds with
2588 | [] ->
2589 state.geomcmds <- "", [];
2590 represent ();
2591 | (s, f) :: rest ->
2592 f ();
2593 state.geomcmds <- s, List.rev rest;
2594 end;
2595 if conf.maxwait = None && not !wtmode
2596 then G.postRedisplay "continue";
2598 | "title" :: args :: [] ->
2599 Wsi.settitle args
2601 | "msg" :: args :: [] ->
2602 showtext ' ' args
2604 | "vmsg" :: args :: [] ->
2605 if conf.verbose
2606 then showtext ' ' args
2608 | "emsg" :: args :: [] ->
2609 Buffer.add_string state.errmsgs args;
2610 state.newerrmsgs <- true;
2611 G.postRedisplay "error message"
2613 | "progress" :: args :: [] ->
2614 let progress, text =
2615 scan args "%f %n"
2616 (fun f pos ->
2617 f, String.sub args pos (String.length args - pos))
2619 state.text <- text;
2620 state.progress <- progress;
2621 G.postRedisplay "progress"
2623 | "firstmatch" :: args :: [] ->
2624 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2625 scan args "%u %d %f %f %f %f %f %f %f %f"
2626 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2627 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2629 let y = (getpagey pageno) + truncate y0 in
2630 addnav ();
2631 gotoy y;
2632 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2634 | "match" :: args :: [] ->
2635 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2636 scan args "%u %d %f %f %f %f %f %f %f %f"
2637 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2638 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2640 state.rects1 <-
2641 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2643 | "page" :: args :: [] ->
2644 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2645 begin match state.currently with
2646 | Loading (l, gen) ->
2647 vlog "page %d took %f sec" l.pageno t;
2648 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2649 begin match state.throttle with
2650 | None ->
2651 let preloadedpages =
2652 if conf.preload
2653 then preloadlayout state.y
2654 else state.layout
2656 let evict () =
2657 let set =
2658 List.fold_left (fun s l -> IntSet.add l.pageno s)
2659 IntSet.empty preloadedpages
2661 let evictedpages =
2662 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2663 if not (IntSet.mem pageno set)
2664 then (
2665 wcmd "freepage %s" opaque;
2666 key :: accu
2668 else accu
2669 ) state.pagemap []
2671 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2673 evict ();
2674 state.currently <- Idle;
2675 if gen = state.gen
2676 then (
2677 tilepage l.pageno pageopaque state.layout;
2678 load state.layout;
2679 load preloadedpages;
2680 if pagevisible state.layout l.pageno
2681 && layoutready state.layout
2682 then G.postRedisplay "page";
2685 | Some (layout, _, _) ->
2686 state.currently <- Idle;
2687 tilepage l.pageno pageopaque layout;
2688 load state.layout
2689 end;
2691 | _ ->
2692 dolog "Inconsistent loading state";
2693 logcurrently state.currently;
2694 exit 1
2697 | "tile" :: args :: [] ->
2698 let (x, y, opaque, size, t) =
2699 scan args "%u %u %s %u %f"
2700 (fun x y p size t -> (x, y, p, size, t))
2702 begin match state.currently with
2703 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2704 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2706 unmappbo opaque;
2707 if tilew != conf.tilew || tileh != conf.tileh
2708 then (
2709 wcmd "freetile %s" opaque;
2710 state.currently <- Idle;
2711 load state.layout;
2713 else (
2714 puttileopaque l col row gen cs angle opaque size t;
2715 state.memused <- state.memused + size;
2716 state.uioh#infochanged Memused;
2717 gctiles ();
2718 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2719 opaque, size) state.tilelru;
2721 let layout =
2722 match state.throttle with
2723 | None -> state.layout
2724 | Some (layout, _, _) -> layout
2727 state.currently <- Idle;
2728 if gen = state.gen
2729 && conf.colorspace = cs
2730 && conf.angle = angle
2731 && tilevisible layout l.pageno x y
2732 then conttiling l.pageno pageopaque;
2734 begin match state.throttle with
2735 | None ->
2736 preload state.layout;
2737 if gen = state.gen
2738 && conf.colorspace = cs
2739 && conf.angle = angle
2740 && tilevisible state.layout l.pageno x y
2741 && (not !wtmode || layoutready state.layout)
2742 then G.postRedisplay "tile nothrottle";
2744 | Some (layout, y, _) ->
2745 let ready = layoutready layout in
2746 if ready
2747 then (
2748 state.y <- y;
2749 state.layout <- layout;
2750 state.throttle <- None;
2751 G.postRedisplay "throttle";
2753 else load layout;
2754 end;
2757 | _ ->
2758 dolog "Inconsistent tiling state";
2759 logcurrently state.currently;
2760 exit 1
2763 | "pdim" :: args :: [] ->
2764 let (n, w, h, _) as pdim =
2765 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2767 let pdim =
2768 match conf.fitmodel, conf.columns with
2769 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2770 | _ -> pdim
2772 state.uioh#infochanged Pdim;
2773 state.pdims <- pdim :: state.pdims
2775 | "o" :: args :: [] ->
2776 let (l, n, t, h, pos) =
2777 scan args "%u %u %d %u %n"
2778 (fun l n t h pos -> l, n, t, h, pos)
2780 let s = String.sub args pos (String.length args - pos) in
2781 let outline = (s, l, Oanchor (n, float t /. float h, 0.0)) in
2782 begin match state.currently with
2783 | Outlining outlines ->
2784 state.currently <- Outlining (outline :: outlines)
2785 | Idle ->
2786 state.currently <- Outlining [outline]
2787 | currently ->
2788 dolog "invalid outlining state";
2789 logcurrently currently
2792 | "ou" :: args :: [] ->
2793 let (l, len, pos) =
2794 scan args "%u %u %n" (fun l len pos -> l, len, pos)
2796 let s = String.sub args pos len in
2797 let pos2 = pos + len + 1 in
2798 let uri = String.sub args pos2 (String.length args - pos2) in
2799 let outline = (s, l, Ouri uri) in
2800 begin match state.currently with
2801 | Outlining outlines ->
2802 state.currently <- Outlining (outline :: outlines)
2803 | Idle ->
2804 state.currently <- Outlining [outline]
2805 | currently ->
2806 dolog "invalid outlining state";
2807 logcurrently currently
2810 | "a" :: args :: [] ->
2811 let (n, l, t) =
2812 scan args "%u %d %d" (fun n l t -> n, l, t)
2814 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2816 | "info" :: args :: [] ->
2817 state.docinfo <- (1, args) :: state.docinfo
2819 | "infoend" :: [] ->
2820 state.uioh#infochanged Docinfo;
2821 state.docinfo <- List.rev state.docinfo
2823 | _ ->
2824 error "unknown cmd `%S'" cmds
2827 let onhist cb =
2828 let rc = cb.rc in
2829 let action = function
2830 | HCprev -> cbget cb ~-1
2831 | HCnext -> cbget cb 1
2832 | HCfirst -> cbget cb ~-(cb.rc)
2833 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2834 and cancel () = cb.rc <- rc
2835 in (action, cancel)
2838 let search pattern forward =
2839 match conf.columns with
2840 | Csplit _ ->
2841 showtext '!' "searching does not work properly in split columns mode"
2842 | _ ->
2843 if nonemptystr pattern
2844 then
2845 let pn, py =
2846 match state.layout with
2847 | [] -> 0, 0
2848 | l :: _ ->
2849 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2851 wcmd "search %d %d %d %d,%s\000"
2852 (btod conf.icase) pn py (btod forward) pattern;
2855 let intentry text key =
2856 let c =
2857 if key >= 32 && key < 127
2858 then Char.chr key
2859 else '\000'
2861 match c with
2862 | '0' .. '9' ->
2863 let text = addchar text c in
2864 TEcont text
2866 | _ ->
2867 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2868 TEcont text
2871 let linknentry text key =
2872 let c =
2873 if key >= 32 && key < 127
2874 then Char.chr key
2875 else '\000'
2877 match c with
2878 | 'a' .. 'z' ->
2879 let text = addchar text c in
2880 TEcont text
2882 | _ ->
2883 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2884 TEcont text
2887 let linkndone f s =
2888 if nonemptystr s
2889 then (
2890 let n =
2891 let l = String.length s in
2892 let rec loop pos n = if pos = l then n else
2893 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2894 loop (pos+1) (n*26 + m)
2895 in loop 0 0
2897 let rec loop n = function
2898 | [] -> ()
2899 | l :: rest ->
2900 match getopaque l.pageno with
2901 | None -> loop n rest
2902 | Some opaque ->
2903 let m = getlinkcount opaque in
2904 if n < m
2905 then (
2906 let under = getlink opaque n in
2907 f under
2909 else loop (n-m) rest
2911 loop n state.layout;
2915 let textentry text key =
2916 if key land 0xff00 = 0xff00
2917 then TEcont text
2918 else TEcont (text ^ toutf8 key)
2921 let reqlayout angle fitmodel =
2922 match state.throttle with
2923 | None ->
2924 if nogeomcmds state.geomcmds
2925 then state.anchor <- getanchor ();
2926 conf.angle <- angle mod 360;
2927 if conf.angle != 0
2928 then (
2929 match state.mode with
2930 | LinkNav _ -> state.mode <- View
2931 | _ -> ()
2933 conf.fitmodel <- fitmodel;
2934 invalidate "reqlayout"
2935 (fun () ->
2936 wcmd "reqlayout %d %d %d"
2937 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2939 | _ -> ()
2942 let settrim trimmargins trimfuzz =
2943 if nogeomcmds state.geomcmds
2944 then state.anchor <- getanchor ();
2945 conf.trimmargins <- trimmargins;
2946 conf.trimfuzz <- trimfuzz;
2947 let x0, y0, x1, y1 = trimfuzz in
2948 invalidate "settrim"
2949 (fun () ->
2950 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2951 flushpages ();
2954 let setzoom zoom =
2955 match state.throttle with
2956 | None ->
2957 let zoom = max 0.0001 zoom in
2958 if zoom <> conf.zoom
2959 then (
2960 state.prevzoom <- (conf.zoom, state.x);
2961 conf.zoom <- zoom;
2962 reshape state.winw state.winh;
2963 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2966 | Some (layout, y, started) ->
2967 let time =
2968 match conf.maxwait with
2969 | None -> 0.0
2970 | Some t -> t
2972 let dt = now () -. started in
2973 if dt > time
2974 then (
2975 state.y <- y;
2976 load layout;
2980 let setcolumns mode columns coverA coverB =
2981 state.prevcolumns <- Some (conf.columns, conf.zoom);
2982 if columns < 0
2983 then (
2984 if isbirdseye mode
2985 then showtext '!' "split mode doesn't work in bird's eye"
2986 else (
2987 conf.columns <- Csplit (-columns, [||]);
2988 state.x <- 0;
2989 conf.zoom <- 1.0;
2992 else (
2993 if columns < 2
2994 then (
2995 conf.columns <- Csingle [||];
2996 state.x <- 0;
2997 setzoom 1.0;
2999 else (
3000 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
3001 conf.zoom <- 1.0;
3004 reshape state.winw state.winh;
3007 let enterbirdseye () =
3008 let zoom = float conf.thumbw /. float state.winw in
3009 let birdseyepageno =
3010 let cy = state.winh / 2 in
3011 let fold = function
3012 | [] -> 0
3013 | l :: rest ->
3014 let rec fold best = function
3015 | [] -> best.pageno
3016 | l :: rest ->
3017 let d = cy - (l.pagedispy + l.pagevh/2)
3018 and dbest = cy - (best.pagedispy + best.pagevh/2) in
3019 if abs d < abs dbest
3020 then fold l rest
3021 else best.pageno
3022 in fold l rest
3024 fold state.layout
3026 state.mode <- Birdseye (
3027 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
3029 conf.zoom <- zoom;
3030 conf.presentation <- false;
3031 conf.interpagespace <- 10;
3032 conf.hlinks <- false;
3033 conf.fitmodel <- FitProportional;
3034 state.x <- 0;
3035 state.mstate <- Mnone;
3036 conf.maxwait <- None;
3037 conf.columns <- (
3038 match conf.beyecolumns with
3039 | Some c ->
3040 conf.zoom <- 1.0;
3041 Cmulti ((c, 0, 0), [||])
3042 | None -> Csingle [||]
3044 Wsi.setcursor Wsi.CURSOR_INHERIT;
3045 if conf.verbose
3046 then
3047 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
3048 (100.0*.zoom)
3049 else
3050 state.text <- ""
3052 reshape state.winw state.winh;
3055 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
3056 state.mode <- View;
3057 conf.zoom <- c.zoom;
3058 conf.presentation <- c.presentation;
3059 conf.interpagespace <- c.interpagespace;
3060 conf.maxwait <- c.maxwait;
3061 conf.hlinks <- c.hlinks;
3062 conf.fitmodel <- c.fitmodel;
3063 conf.beyecolumns <- (
3064 match conf.columns with
3065 | Cmulti ((c, _, _), _) -> Some c
3066 | Csingle _ -> None
3067 | Csplit _ -> failwith "leaving bird's eye split mode"
3069 conf.columns <- (
3070 match c.columns with
3071 | Cmulti (c, _) -> Cmulti (c, [||])
3072 | Csingle _ -> Csingle [||]
3073 | Csplit (c, _) -> Csplit (c, [||])
3075 if conf.verbose
3076 then
3077 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
3078 (100.0*.conf.zoom)
3080 reshape state.winw state.winh;
3081 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
3082 state.x <- leftx;
3085 let togglebirdseye () =
3086 match state.mode with
3087 | Birdseye vals -> leavebirdseye vals true
3088 | View -> enterbirdseye ()
3089 | _ -> ()
3092 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3093 let pageno = max 0 (pageno - incr) in
3094 let rec loop = function
3095 | [] -> gotopage1 pageno 0
3096 | l :: _ when l.pageno = pageno ->
3097 if l.pagedispy >= 0 && l.pagey = 0
3098 then G.postRedisplay "upbirdseye"
3099 else gotopage1 pageno 0
3100 | _ :: rest -> loop rest
3102 loop state.layout;
3103 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
3106 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3107 let pageno = min (state.pagecount - 1) (pageno + incr) in
3108 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3109 let rec loop = function
3110 | [] ->
3111 let y, h = getpageyh pageno in
3112 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
3113 gotoy (clamp dy)
3114 | l :: _ when l.pageno = pageno ->
3115 if l.pagevh != l.pageh
3116 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
3117 else G.postRedisplay "downbirdseye"
3118 | _ :: rest -> loop rest
3120 loop state.layout
3123 let optentry mode _ key =
3124 let btos b = if b then "on" else "off" in
3125 if key >= 32 && key < 127
3126 then
3127 let c = Char.chr key in
3128 match c with
3129 | 's' ->
3130 let ondone s =
3131 try conf.scrollstep <- int_of_string s with exc ->
3132 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3134 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3136 | 'A' ->
3137 let ondone s =
3139 conf.autoscrollstep <- int_of_string s;
3140 if state.autoscroll <> None
3141 then state.autoscroll <- Some conf.autoscrollstep
3142 with exc ->
3143 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3145 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3147 | 'C' ->
3148 let ondone s =
3150 let n, a, b = multicolumns_of_string s in
3151 setcolumns mode n a b;
3152 with exc ->
3153 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3155 TEswitch ("columns: ", "", None, textentry, ondone, true)
3157 | 'Z' ->
3158 let ondone s =
3160 let zoom = float (int_of_string s) /. 100.0 in
3161 setzoom zoom
3162 with exc ->
3163 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3165 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3167 | 't' ->
3168 let ondone s =
3170 conf.thumbw <- bound (int_of_string s) 2 4096;
3171 state.text <-
3172 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3173 begin match mode with
3174 | Birdseye beye ->
3175 leavebirdseye beye false;
3176 enterbirdseye ();
3177 | _ -> ();
3179 with exc ->
3180 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3182 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3184 | 'R' ->
3185 let ondone s =
3186 match try
3187 Some (int_of_string s)
3188 with exc ->
3189 state.text <- Printf.sprintf "bad integer `%s': %s"
3190 s (exntos exc);
3191 None
3192 with
3193 | Some angle -> reqlayout angle conf.fitmodel
3194 | None -> ()
3196 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3198 | 'i' ->
3199 conf.icase <- not conf.icase;
3200 TEdone ("case insensitive search " ^ (btos conf.icase))
3202 | 'p' ->
3203 conf.preload <- not conf.preload;
3204 gotoy state.y;
3205 TEdone ("preload " ^ (btos conf.preload))
3207 | 'v' ->
3208 conf.verbose <- not conf.verbose;
3209 TEdone ("verbose " ^ (btos conf.verbose))
3211 | 'd' ->
3212 conf.debug <- not conf.debug;
3213 TEdone ("debug " ^ (btos conf.debug))
3215 | 'h' ->
3216 conf.maxhfit <- not conf.maxhfit;
3217 state.maxy <- calcheight ();
3218 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3220 | 'c' ->
3221 conf.crophack <- not conf.crophack;
3222 TEdone ("crophack " ^ btos conf.crophack)
3224 | 'a' ->
3225 let s =
3226 match conf.maxwait with
3227 | None ->
3228 conf.maxwait <- Some infinity;
3229 "always wait for page to complete"
3230 | Some _ ->
3231 conf.maxwait <- None;
3232 "show placeholder if page is not ready"
3234 TEdone s
3236 | 'f' ->
3237 conf.underinfo <- not conf.underinfo;
3238 TEdone ("underinfo " ^ btos conf.underinfo)
3240 | 'P' ->
3241 conf.savebmarks <- not conf.savebmarks;
3242 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3244 | 'S' ->
3245 let ondone s =
3247 let pageno, py =
3248 match state.layout with
3249 | [] -> 0, 0
3250 | l :: _ ->
3251 l.pageno, l.pagey
3253 conf.interpagespace <- int_of_string s;
3254 docolumns conf.columns;
3255 state.maxy <- calcheight ();
3256 let y = getpagey pageno in
3257 gotoy (y + py)
3258 with exc ->
3259 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3261 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3263 | 'l' ->
3264 let fm =
3265 match conf.fitmodel with
3266 | FitProportional -> FitWidth
3267 | _ -> FitProportional
3269 reqlayout conf.angle fm;
3270 TEdone ("proportional display " ^ btos (fm == FitProportional))
3272 | 'T' ->
3273 settrim (not conf.trimmargins) conf.trimfuzz;
3274 TEdone ("trim margins " ^ btos conf.trimmargins)
3276 | 'I' ->
3277 conf.invert <- not conf.invert;
3278 TEdone ("invert colors " ^ btos conf.invert)
3280 | 'x' ->
3281 let ondone s =
3282 cbput state.hists.sel s;
3283 conf.selcmd <- s;
3285 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3286 textentry, ondone, true)
3288 | 'M' ->
3289 if conf.pax == None
3290 then conf.pax <- Some (ref (0.0, 0, 0))
3291 else conf.pax <- None;
3292 TEdone ("PAX " ^ btos (conf.pax != None))
3294 | _ ->
3295 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3296 TEstop
3297 else
3298 TEcont state.text
3301 class type lvsource = object
3302 method getitemcount : int
3303 method getitem : int -> (string * int)
3304 method hasaction : int -> bool
3305 method exit :
3306 uioh:uioh ->
3307 cancel:bool ->
3308 active:int ->
3309 first:int ->
3310 pan:int ->
3311 qsearch:string ->
3312 uioh option
3313 method getactive : int
3314 method getfirst : int
3315 method getqsearch : string
3316 method setqsearch : string -> unit
3317 method getpan : int
3318 end;;
3320 class virtual lvsourcebase = object
3321 val mutable m_active = 0
3322 val mutable m_first = 0
3323 val mutable m_qsearch = ""
3324 val mutable m_pan = 0
3325 method getactive = m_active
3326 method getfirst = m_first
3327 method getqsearch = m_qsearch
3328 method getpan = m_pan
3329 method setqsearch s = m_qsearch <- s
3330 end;;
3332 let withoutlastutf8 s =
3333 let len = String.length s in
3334 if len = 0
3335 then s
3336 else
3337 let rec find pos =
3338 if pos = 0
3339 then pos
3340 else
3341 let b = Char.code s.[pos] in
3342 if b land 0b11000000 = 0b11000000
3343 then pos
3344 else find (pos-1)
3346 let first =
3347 if Char.code s.[len-1] land 0x80 = 0
3348 then len-1
3349 else find (len-1)
3351 String.sub s 0 first;
3354 let textentrykeyboard
3355 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3356 let key =
3357 if key >= 0xffb0 && key <= 0xffb9
3358 then key - 0xffb0 + 48 else key
3360 let enttext te =
3361 state.mode <- Textentry (te, onleave);
3362 state.text <- "";
3363 enttext ();
3364 G.postRedisplay "textentrykeyboard enttext";
3366 let histaction cmd =
3367 match opthist with
3368 | None -> ()
3369 | Some (action, _) ->
3370 state.mode <- Textentry (
3371 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3373 G.postRedisplay "textentry histaction"
3375 match key with
3376 | 0xff08 -> (* backspace *)
3377 if emptystr text && cancelonempty
3378 then (
3379 onleave Cancel;
3380 G.postRedisplay "textentrykeyboard after cancel";
3382 else
3383 let s = withoutlastutf8 text in
3384 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3386 | 0xff0d | 0xff8d -> (* (kp) enter *)
3387 ondone text;
3388 onleave Confirm;
3389 G.postRedisplay "textentrykeyboard after confirm"
3391 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3392 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3393 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3394 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3396 | 0xff1b -> (* escape*)
3397 if emptystr text
3398 then (
3399 begin match opthist with
3400 | None -> ()
3401 | Some (_, onhistcancel) -> onhistcancel ()
3402 end;
3403 onleave Cancel;
3404 state.text <- "";
3405 G.postRedisplay "textentrykeyboard after cancel2"
3407 else (
3408 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3411 | 0xff9f | 0xffff -> () (* delete *)
3413 | _ when key != 0
3414 && key land 0xff00 != 0xff00 (* keyboard *)
3415 && key land 0xfe00 != 0xfe00 (* xkb *)
3416 && key land 0xfd00 != 0xfd00 (* 3270 *)
3418 begin match onkey text key with
3419 | TEdone text ->
3420 ondone text;
3421 onleave Confirm;
3422 G.postRedisplay "textentrykeyboard after confirm2";
3424 | TEcont text ->
3425 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3427 | TEstop ->
3428 onleave Cancel;
3429 G.postRedisplay "textentrykeyboard after cancel3"
3431 | TEswitch te ->
3432 state.mode <- Textentry (te, onleave);
3433 G.postRedisplay "textentrykeyboard switch";
3434 end;
3436 | _ ->
3437 vlog "unhandled key %s" (Wsi.keyname key)
3440 let firstof first active =
3441 if first > active || abs (first - active) > fstate.maxrows - 1
3442 then max 0 (active - (fstate.maxrows/2))
3443 else first
3446 let calcfirst first active =
3447 if active > first
3448 then
3449 let rows = active - first in
3450 if rows > fstate.maxrows then active - fstate.maxrows else first
3451 else active
3454 let scrollph y maxy =
3455 let sh = float (maxy + state.winh) /. float state.winh in
3456 let sh = float state.winh /. sh in
3457 let sh = max sh (float conf.scrollh) in
3459 let percent = float y /. float maxy in
3460 let position = (float state.winh -. sh) *. percent in
3462 let position =
3463 if position +. sh > float state.winh
3464 then float state.winh -. sh
3465 else position
3467 position, sh;
3470 let coe s = (s :> uioh);;
3472 class listview ~(source:lvsource) ~trusted ~modehash =
3473 object (self)
3474 val m_pan = source#getpan
3475 val m_first = source#getfirst
3476 val m_active = source#getactive
3477 val m_qsearch = source#getqsearch
3478 val m_prev_uioh = state.uioh
3480 method private elemunder y =
3481 let n = y / (fstate.fontsize+1) in
3482 if m_first + n < source#getitemcount
3483 then (
3484 if source#hasaction (m_first + n)
3485 then Some (m_first + n)
3486 else None
3488 else None
3490 method display =
3491 Gl.enable `blend;
3492 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3493 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3494 filledrect 0. 0. (float state.winw) (float state.winh);
3495 GlDraw.color (1., 1., 1.);
3496 Gl.enable `texture_2d;
3497 let fs = fstate.fontsize in
3498 let nfs = fs + 1 in
3499 let ww = fstate.wwidth in
3500 let tabw = 30.0*.ww in
3501 let itemcount = source#getitemcount in
3502 let rec loop row =
3503 if (row - m_first) > fstate.maxrows
3504 then ()
3505 else (
3506 if row >= 0 && row < itemcount
3507 then (
3508 let (s, level) = source#getitem row in
3509 let y = (row - m_first) * nfs in
3510 let x = 5.0 +. float (level + m_pan) *. ww in
3511 if row = m_active
3512 then (
3513 Gl.disable `texture_2d;
3514 let alpha = if source#hasaction row then 0.9 else 0.3 in
3515 GlDraw.color (1., 1., 1.) ~alpha;
3516 linerect 1. (float (y + 1))
3517 (float (state.winw - conf.scrollbw - 1)) (float (y + fs + 3));
3518 GlDraw.color (1., 1., 1.);
3519 Gl.enable `texture_2d;
3522 let drawtabularstring s =
3523 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3524 if trusted
3525 then
3526 let tabpos = try String.index s '\t' with Not_found -> -1 in
3527 if tabpos > 0
3528 then
3529 let len = String.length s - tabpos - 1 in
3530 let s1 = String.sub s 0 tabpos
3531 and s2 = String.sub s (tabpos + 1) len in
3532 let nx = drawstr x s1 in
3533 let sw = nx -. x in
3534 let x = x +. (max tabw sw) in
3535 drawstr x s2
3536 else
3537 drawstr x s
3538 else
3539 drawstr x s
3541 let _ = drawtabularstring s in
3542 loop (row+1)
3546 loop m_first;
3547 Gl.disable `blend;
3548 Gl.disable `texture_2d;
3550 method updownlevel incr =
3551 let len = source#getitemcount in
3552 let curlevel =
3553 if m_active >= 0 && m_active < len
3554 then snd (source#getitem m_active)
3555 else -1
3557 let rec flow i =
3558 if i = len then i-1 else if i = -1 then 0 else
3559 let _, l = source#getitem i in
3560 if l != curlevel then i else flow (i+incr)
3562 let active = flow m_active in
3563 let first = calcfirst m_first active in
3564 G.postRedisplay "outline updownlevel";
3565 {< m_active = active; m_first = first >}
3567 method private key1 key mask =
3568 let set1 active first qsearch =
3569 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3571 let search active pattern incr =
3572 let active = if active = -1 then m_first else active in
3573 let dosearch re =
3574 let rec loop n =
3575 if n >= 0 && n < source#getitemcount
3576 then (
3577 let s, _ = source#getitem n in
3579 (try ignore (Str.search_forward re s 0); true
3580 with Not_found -> false)
3581 then Some n
3582 else loop (n + incr)
3584 else None
3586 loop active
3589 let re = Str.regexp_case_fold pattern in
3590 dosearch re
3591 with Failure s ->
3592 state.text <- s;
3593 None
3595 let itemcount = source#getitemcount in
3596 let find start incr =
3597 let rec find i =
3598 if i = -1 || i = itemcount
3599 then -1
3600 else (
3601 if source#hasaction i
3602 then i
3603 else find (i + incr)
3606 find start
3608 let set active first =
3609 let first = bound first 0 (itemcount - fstate.maxrows) in
3610 state.text <- "";
3611 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3613 let navigate incr =
3614 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3615 let active, first =
3616 let incr1 = if incr > 0 then 1 else -1 in
3617 if isvisible m_first m_active
3618 then
3619 let next =
3620 let next = m_active + incr in
3621 let next =
3622 if next < 0 || next >= itemcount
3623 then -1
3624 else find next incr1
3626 if abs (m_active - next) > fstate.maxrows
3627 then -1
3628 else next
3630 if next = -1
3631 then
3632 let first = m_first + incr in
3633 let first = bound first 0 (itemcount - fstate.maxrows) in
3634 let next =
3635 let next = m_active + incr in
3636 let next = bound next 0 (itemcount - 1) in
3637 find next ~-incr1
3639 let active =
3640 if next = -1
3641 then m_active
3642 else (
3643 if isvisible first next
3644 then next
3645 else m_active
3648 active, first
3649 else
3650 let first = min next m_first in
3651 let first =
3652 if abs (next - first) > fstate.maxrows
3653 then first + incr
3654 else first
3656 next, first
3657 else
3658 let first = m_first + incr in
3659 let first = bound first 0 (itemcount - 1) in
3660 let active =
3661 let next = m_active + incr in
3662 let next = bound next 0 (itemcount - 1) in
3663 let next = find next incr1 in
3664 let active =
3665 if next = -1 || abs (m_active - first) > fstate.maxrows
3666 then (
3667 let active = if m_active = -1 then next else m_active in
3668 active
3670 else next
3672 if isvisible first active
3673 then active
3674 else -1
3676 active, first
3678 G.postRedisplay "listview navigate";
3679 set active first;
3681 match key with
3682 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3683 let incr = if key = 0x72 then -1 else 1 in
3684 let active, first =
3685 match search (m_active + incr) m_qsearch incr with
3686 | None ->
3687 state.text <- m_qsearch ^ " [not found]";
3688 m_active, m_first
3689 | Some active ->
3690 state.text <- m_qsearch;
3691 active, firstof m_first active
3693 G.postRedisplay "listview ctrl-r/s";
3694 set1 active first m_qsearch;
3696 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3697 if m_active >= 0 && m_active < source#getitemcount
3698 then (
3699 let s, _ = source#getitem m_active in
3700 selstring s;
3702 coe self
3704 | 0xff08 -> (* backspace *)
3705 if emptystr m_qsearch
3706 then coe self
3707 else (
3708 let qsearch = withoutlastutf8 m_qsearch in
3709 if emptystr qsearch
3710 then (
3711 state.text <- "";
3712 G.postRedisplay "listview empty qsearch";
3713 set1 m_active m_first "";
3715 else
3716 let active, first =
3717 match search m_active qsearch ~-1 with
3718 | None ->
3719 state.text <- qsearch ^ " [not found]";
3720 m_active, m_first
3721 | Some active ->
3722 state.text <- qsearch;
3723 active, firstof m_first active
3725 G.postRedisplay "listview backspace qsearch";
3726 set1 active first qsearch
3729 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3730 let pattern = m_qsearch ^ toutf8 key in
3731 let active, first =
3732 match search m_active pattern 1 with
3733 | None ->
3734 state.text <- pattern ^ " [not found]";
3735 m_active, m_first
3736 | Some active ->
3737 state.text <- pattern;
3738 active, firstof m_first active
3740 G.postRedisplay "listview qsearch add";
3741 set1 active first pattern;
3743 | 0xff1b -> (* escape *)
3744 state.text <- "";
3745 if emptystr m_qsearch
3746 then (
3747 G.postRedisplay "list view escape";
3748 begin
3749 match
3750 source#exit (coe self) true m_active m_first m_pan m_qsearch
3751 with
3752 | None -> m_prev_uioh
3753 | Some uioh -> uioh
3756 else (
3757 G.postRedisplay "list view kill qsearch";
3758 source#setqsearch "";
3759 coe {< m_qsearch = "" >}
3762 | 0xff0d | 0xff8d -> (* (kp) enter *)
3763 state.text <- "";
3764 let self = {< m_qsearch = "" >} in
3765 source#setqsearch "";
3766 let opt =
3767 G.postRedisplay "listview enter";
3768 if m_active >= 0 && m_active < source#getitemcount
3769 then (
3770 source#exit (coe self) false m_active m_first m_pan "";
3772 else (
3773 source#exit (coe self) true m_active m_first m_pan "";
3776 begin match opt with
3777 | None -> m_prev_uioh
3778 | Some uioh -> uioh
3781 | 0xff9f | 0xffff -> (* (kp) delete *)
3782 coe self
3784 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3785 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3786 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3787 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3789 | 0xff53 | 0xff98 -> (* (kp) right *)
3790 state.text <- "";
3791 G.postRedisplay "listview right";
3792 coe {< m_pan = m_pan - 1 >}
3794 | 0xff51 | 0xff96 -> (* (kp) left *)
3795 state.text <- "";
3796 G.postRedisplay "listview left";
3797 coe {< m_pan = m_pan + 1 >}
3799 | 0xff50 | 0xff95 -> (* (kp) home *)
3800 let active = find 0 1 in
3801 G.postRedisplay "listview home";
3802 set active 0;
3804 | 0xff57 | 0xff9c -> (* (kp) end *)
3805 let first = max 0 (itemcount - fstate.maxrows) in
3806 let active = find (itemcount - 1) ~-1 in
3807 G.postRedisplay "listview end";
3808 set active first;
3810 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3811 coe self
3813 | _ ->
3814 dolog "listview unknown key %#x" key; coe self
3816 method key key mask =
3817 match state.mode with
3818 | Textentry te -> textentrykeyboard key mask te; coe self
3819 | _ -> self#key1 key mask
3821 method button button down x y _ =
3822 let opt =
3823 match button with
3824 | 1 when x > state.winw - conf.scrollbw ->
3825 G.postRedisplay "listview scroll";
3826 if down
3827 then
3828 let _, position, sh = self#scrollph in
3829 if y > truncate position && y < truncate (position +. sh)
3830 then (
3831 state.mstate <- Mscrolly;
3832 Some (coe self)
3834 else
3835 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3836 let first = truncate (s *. float source#getitemcount) in
3837 let first = min source#getitemcount first in
3838 Some (coe {< m_first = first; m_active = first >})
3839 else (
3840 state.mstate <- Mnone;
3841 Some (coe self);
3843 | 1 when not down ->
3844 begin match self#elemunder y with
3845 | Some n ->
3846 G.postRedisplay "listview click";
3847 source#exit
3848 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3849 | _ ->
3850 Some (coe self)
3852 | n when (n == 4 || n == 5) && not down ->
3853 let len = source#getitemcount in
3854 let first =
3855 if n = 5 && m_first + fstate.maxrows >= len
3856 then
3857 m_first
3858 else
3859 let first = m_first + (if n == 4 then -1 else 1) in
3860 bound first 0 (len - 1)
3862 G.postRedisplay "listview wheel";
3863 Some (coe {< m_first = first >})
3864 | n when (n = 6 || n = 7) && not down ->
3865 let inc = if n = 7 then -1 else 1 in
3866 G.postRedisplay "listview hwheel";
3867 Some (coe {< m_pan = m_pan + inc >})
3868 | _ ->
3869 Some (coe self)
3871 match opt with
3872 | None -> m_prev_uioh
3873 | Some uioh -> uioh
3875 method motion _ y =
3876 match state.mstate with
3877 | Mscrolly ->
3878 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3879 let first = truncate (s *. float source#getitemcount) in
3880 let first = min source#getitemcount first in
3881 G.postRedisplay "listview motion";
3882 coe {< m_first = first; m_active = first >}
3883 | _ -> coe self
3885 method pmotion x y =
3886 if x < state.winw - conf.scrollbw
3887 then
3888 let n =
3889 match self#elemunder y with
3890 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3891 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3893 let o =
3894 if n != m_active
3895 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3896 else self
3898 coe o
3899 else (
3900 Wsi.setcursor Wsi.CURSOR_INHERIT;
3901 coe self
3904 method infochanged _ = ()
3906 method scrollpw = (0, 0.0, 0.0)
3907 method scrollph =
3908 let nfs = fstate.fontsize + 1 in
3909 let y = m_first * nfs in
3910 let itemcount = source#getitemcount in
3911 let maxi = max 0 (itemcount - fstate.maxrows) in
3912 let maxy = maxi * nfs in
3913 let p, h = scrollph y maxy in
3914 conf.scrollbw, p, h
3916 method modehash = modehash
3917 method eformsgs = false
3918 end;;
3920 class outlinelistview ~source =
3921 let settext autonarrow s =
3922 if autonarrow
3923 then state.text <- "[" ^ s ^ "]"
3924 else state.text <- s
3926 object (self)
3927 inherit listview
3928 ~source:(source :> lvsource)
3929 ~trusted:false
3930 ~modehash:(findkeyhash conf "outline")
3931 as super
3933 val m_autonarrow = false
3935 method key key mask =
3936 let maxrows =
3937 if emptystr state.text
3938 then fstate.maxrows
3939 else fstate.maxrows - 2
3941 let calcfirst first active =
3942 if active > first
3943 then
3944 let rows = active - first in
3945 if rows > maxrows then active - maxrows else first
3946 else active
3948 let navigate incr =
3949 let active = m_active + incr in
3950 let active = bound active 0 (source#getitemcount - 1) in
3951 let first = calcfirst m_first active in
3952 G.postRedisplay "outline navigate";
3953 coe {< m_active = active; m_first = first >}
3955 let navscroll first =
3956 let active =
3957 let dist = m_active - first in
3958 if dist < 0
3959 then first
3960 else (
3961 if dist < maxrows
3962 then m_active
3963 else first + maxrows
3966 G.postRedisplay "outline navscroll";
3967 coe {< m_first = first; m_active = active >}
3969 let ctrl = Wsi.withctrl mask in
3970 match key with
3971 | 97 when ctrl -> (* ctrl-a *)
3972 if m_autonarrow
3973 then source#denarrow
3974 else source#narrow m_qsearch;
3975 settext (not m_autonarrow) m_qsearch;
3976 G.postRedisplay "toggle auto narrowing";
3977 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3979 | 47 when emptystr m_qsearch && not m_autonarrow -> (* / *)
3980 settext true "";
3981 G.postRedisplay "toggle auto narrowing";
3982 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3984 | 110 when ctrl -> (* ctrl-n *)
3985 source#narrow m_qsearch;
3986 if not m_autonarrow
3987 then source#add_narrow_pattern m_qsearch;
3988 G.postRedisplay "outline ctrl-n";
3989 coe {< m_first = 0; m_active = 0 >}
3991 | 115 when ctrl -> (* ctrl-s *)
3992 let active = source#calcactive (getanchor ()) in
3993 let first = firstof m_first active in
3994 G.postRedisplay "outline ctrl-s";
3995 coe {< m_first = first; m_active = active >}
3997 | 117 when ctrl -> (* ctrl-u *)
3998 source#del_narrow_pattern;
3999 let pattern = source#renarrow in
4000 G.postRedisplay "outline ctrl-u";
4001 let text =
4002 if emptystr pattern then "" else "Narrowed to " ^ pattern
4004 settext m_autonarrow text;
4005 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
4007 | 108 when ctrl -> (* ctrl-l *)
4008 let first = max 0 (m_active - (fstate.maxrows / 2)) in
4009 G.postRedisplay "outline ctrl-l";
4010 coe {< m_first = first >}
4012 | 0xff1b -> (* escape *)
4013 let o = super#key key mask in
4014 if m_autonarrow
4015 then (
4016 if nonemptystr m_qsearch
4017 then (
4018 source#add_narrow_pattern m_qsearch;
4019 settext true "";
4024 | 0xff0d | 0xff8d when m_autonarrow -> (* (kp) enter *)
4025 if nonemptystr m_qsearch
4026 then source#add_narrow_pattern m_qsearch;
4027 super#key key mask
4029 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
4030 let pattern = m_qsearch ^ toutf8 key in
4031 G.postRedisplay "outlinelistview autonarrow add";
4032 source#narrow pattern;
4033 settext true pattern;
4034 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4036 | key when m_autonarrow && key = 0xff08 -> (* backspace *)
4037 if emptystr m_qsearch
4038 then coe self
4039 else
4040 let pattern = withoutlastutf8 m_qsearch in
4041 G.postRedisplay "outlinelistview autonarrow backspace";
4042 ignore (source#renarrow);
4043 source#narrow pattern;
4044 settext true pattern;
4045 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4047 | 0xff9f | 0xffff -> (* (kp) delete *)
4048 source#remove m_active;
4049 G.postRedisplay "outline delete";
4050 let active = max 0 (m_active-1) in
4051 coe {< m_first = firstof m_first active;
4052 m_active = active >}
4054 | 0xff52 | 0xff97 when ctrl -> (* ctrl-(kp) up *)
4055 navscroll (max 0 (m_first - 1))
4057 | 0xff54 | 0xff99 when ctrl -> (* ctrl-(kp) down *)
4058 navscroll (min (source#getitemcount - 1) (m_first + 1))
4060 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
4061 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
4062 | 0xff55 | 0xff9a -> (* (kp) prior *)
4063 navigate ~-(fstate.maxrows)
4064 | 0xff56 | 0xff9b -> (* (kp) next *)
4065 navigate fstate.maxrows
4067 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
4068 let o =
4069 if ctrl
4070 then (
4071 G.postRedisplay "outline ctrl right";
4072 {< m_pan = m_pan + 1 >}
4074 else self#updownlevel 1
4076 coe o
4078 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
4079 let o =
4080 if ctrl
4081 then (
4082 G.postRedisplay "outline ctrl left";
4083 {< m_pan = m_pan - 1 >}
4085 else self#updownlevel ~-1
4087 coe o
4089 | 0xff50 | 0xff95 -> (* (kp) home *)
4090 G.postRedisplay "outline home";
4091 coe {< m_first = 0; m_active = 0 >}
4093 | 0xff57 | 0xff9c -> (* (kp) end *)
4094 let active = source#getitemcount - 1 in
4095 let first = max 0 (active - fstate.maxrows) in
4096 G.postRedisplay "outline end";
4097 coe {< m_active = active; m_first = first >}
4099 | _ -> super#key key mask
4102 let gotounder under =
4103 let getpath filename =
4104 let path =
4105 if nonemptystr filename
4106 then
4107 if Filename.is_relative filename
4108 then
4109 let dir = Filename.dirname state.path in
4110 let dir =
4111 if Filename.is_implicit dir
4112 then Filename.concat (Sys.getcwd ()) dir
4113 else dir
4115 Filename.concat dir filename
4116 else filename
4117 else ""
4119 if Sys.file_exists path
4120 then path
4121 else ""
4123 match under with
4124 | Ulinkgoto (pageno, top) ->
4125 if pageno >= 0
4126 then (
4127 addnav ();
4128 gotopage1 pageno top;
4131 | Ulinkuri s ->
4132 gotouri s
4134 | Uremote (filename, pageno) ->
4135 let path = getpath filename in
4136 if nonemptystr path
4137 then (
4138 if conf.riani
4139 then
4140 let command = !selfexec ^ " " ^ path in
4141 try popen command []
4142 with exn ->
4143 Printf.eprintf
4144 "failed to execute `%s': %s\n" command (exntos exn);
4145 flush stderr;
4146 else
4147 let anchor = getanchor () in
4148 let ranchor = state.path, state.password, anchor, state.origin in
4149 state.origin <- "";
4150 state.anchor <- (pageno, 0.0, 0.0);
4151 state.ranchors <- ranchor :: state.ranchors;
4152 opendoc path "";
4154 else showtext '!' ("Could not find " ^ filename)
4156 | Uremotedest (filename, destname) ->
4157 let path = getpath filename in
4158 if nonemptystr path
4159 then (
4160 if conf.riani
4161 then
4162 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4163 try popen command []
4164 with exn ->
4165 Printf.eprintf
4166 "failed to execute `%s': %s\n" command (exntos exn);
4167 flush stderr;
4168 else
4169 let anchor = getanchor () in
4170 let ranchor = state.path, state.password, anchor, state.origin in
4171 state.origin <- "";
4172 state.nameddest <- destname;
4173 state.ranchors <- ranchor :: state.ranchors;
4174 opendoc path "";
4176 else showtext '!' ("Could not find " ^ filename)
4178 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4181 let gotooutline (_, _, kind) =
4182 let under =
4183 match kind with
4184 | Oanchor anchor ->
4185 let (pageno, y, _) = anchor in
4186 let y = getanchory
4187 (if conf.presentation then (pageno, y, 1.0) else anchor)
4189 Ulinkgoto (pageno, y)
4190 | Ouri uri -> Ulinkuri uri
4191 | Olaunch cmd -> Ulaunch cmd
4192 | Oremote remote -> Uremote remote
4193 | Oremotedest remotedest -> Uremotedest remotedest
4195 gotounder under;
4198 let outlinesource usebookmarks =
4199 let empty = [||] in
4200 (object (self)
4201 inherit lvsourcebase
4202 val mutable m_items = empty
4203 val mutable m_orig_items = empty
4204 val mutable m_narrow_patterns = []
4205 val mutable m_hadremovals = false
4207 method getitemcount =
4208 Array.length m_items + (if m_hadremovals then 1 else 0)
4210 method getitem n =
4211 if n == Array.length m_items && m_hadremovals
4212 then
4213 ("[Confirm removal]", 0)
4214 else
4215 let s, n, _ = m_items.(n) in
4216 (s, n)
4218 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4219 ignore (uioh, first, qsearch);
4220 let confrimremoval = m_hadremovals && active = Array.length m_items in
4221 let items =
4222 if m_narrow_patterns = []
4223 then m_orig_items
4224 else m_items
4226 if not cancel
4227 then (
4228 if not confrimremoval
4229 then (
4230 gotooutline m_items.(active);
4231 m_items <- items;
4233 else (
4234 state.bookmarks <- Array.to_list m_items;
4235 m_orig_items <- m_items;
4238 else m_items <- items;
4239 m_pan <- pan;
4240 None
4242 method hasaction _ = true
4244 method greetmsg =
4245 if Array.length m_items != Array.length m_orig_items
4246 then
4247 let s =
4248 match m_narrow_patterns with
4249 | one :: [] -> one
4250 | many -> String.concat "\xe2\x80\xa6" (List.rev many)
4252 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4253 else ""
4255 method narrow pattern =
4256 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4257 match reopt with
4258 | None -> ()
4259 | Some re ->
4260 let rec loop accu n =
4261 if n = -1
4262 then m_items <- Array.of_list accu
4263 else
4264 let (s, _, _) as o = m_items.(n) in
4265 let accu =
4266 if (try ignore (Str.search_forward re s 0); true
4267 with Not_found -> false)
4268 then o :: accu
4269 else accu
4271 loop accu (n-1)
4273 loop [] (Array.length m_items - 1)
4275 method denarrow =
4276 m_orig_items <- (
4277 if usebookmarks
4278 then Array.of_list state.bookmarks
4279 else state.outlines
4281 m_items <- m_orig_items
4283 method remove m =
4284 if usebookmarks
4285 then
4286 if m >= 0 && m < Array.length m_items
4287 then (
4288 m_hadremovals <- true;
4289 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4290 let n = if n >= m then n+1 else n in
4291 m_items.(n)
4295 method add_narrow_pattern pattern =
4296 m_narrow_patterns <- pattern :: m_narrow_patterns
4298 method del_narrow_pattern =
4299 match m_narrow_patterns with
4300 | _ :: rest -> m_narrow_patterns <- rest
4301 | [] -> ()
4303 method renarrow =
4304 self#denarrow;
4305 match m_narrow_patterns with
4306 | pattern :: [] -> self#narrow pattern; pattern
4307 | list ->
4308 List.fold_left (fun accu pattern ->
4309 self#narrow pattern;
4310 pattern ^ "\xe2\x80\xa6" ^ accu) "" list
4312 method calcactive anchor =
4313 let rely = getanchory anchor in
4314 let rec loop n best bestd =
4315 if n = Array.length m_items
4316 then best
4317 else
4318 let _, _, kind = m_items.(n) in
4319 match kind with
4320 | Oanchor anchor ->
4321 let orely = getanchory anchor in
4322 let d = abs (orely - rely) in
4323 if d < bestd
4324 then loop (n+1) n d
4325 else loop (n+1) best bestd
4326 | Oremote _ | Olaunch _ | Oremotedest _ | Ouri _ ->
4327 loop (n+1) best bestd
4329 loop 0 ~-1 max_int
4331 method reset anchor items =
4332 m_hadremovals <- false;
4333 if m_orig_items == empty
4334 then (
4335 m_orig_items <- items;
4336 if m_narrow_patterns == []
4337 then m_items <- items;
4339 let active = self#calcactive anchor in
4340 m_active <- active;
4341 m_first <- firstof m_first active
4342 end)
4345 let enterselector usebookmarks =
4346 let source = outlinesource usebookmarks in
4347 fun errmsg ->
4348 let outlines =
4349 if usebookmarks
4350 then Array.of_list state.bookmarks
4351 else state.outlines
4353 if Array.length outlines = 0
4354 then (
4355 showtext ' ' errmsg;
4357 else (
4358 state.text <- source#greetmsg;
4359 Wsi.setcursor Wsi.CURSOR_INHERIT;
4360 let anchor = getanchor () in
4361 source#reset anchor outlines;
4362 state.uioh <- coe (new outlinelistview ~source);
4363 G.postRedisplay "enter selector";
4367 let enteroutlinemode =
4368 let f = enterselector false in
4369 fun ()-> f "Document has no outline";
4372 let enterbookmarkmode =
4373 let f = enterselector true in
4374 fun () -> f "Document has no bookmarks (yet)";
4377 let color_of_string s =
4378 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4379 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4383 let color_to_string (r, g, b) =
4384 let r = truncate (r *. 256.0)
4385 and g = truncate (g *. 256.0)
4386 and b = truncate (b *. 256.0) in
4387 Printf.sprintf "%d/%d/%d" r g b
4390 let irect_of_string s =
4391 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4394 let irect_to_string (x0,y0,x1,y1) =
4395 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4398 let makecheckers () =
4399 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4400 following to say:
4401 converted by Issac Trotts. July 25, 2002 *)
4402 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4403 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4404 let id = GlTex.gen_texture () in
4405 GlTex.bind_texture `texture_2d id;
4406 GlPix.store (`unpack_alignment 1);
4407 GlTex.image2d image;
4408 List.iter (GlTex.parameter ~target:`texture_2d)
4409 [ `mag_filter `nearest; `min_filter `nearest ];
4413 let setcheckers enabled =
4414 match state.texid with
4415 | None ->
4416 if enabled then state.texid <- Some (makecheckers ())
4418 | Some texid ->
4419 if not enabled
4420 then (
4421 GlTex.delete_texture texid;
4422 state.texid <- None;
4426 let int_of_string_with_suffix s =
4427 let l = String.length s in
4428 let s1, shift =
4429 if l > 1
4430 then
4431 let suffix = Char.lowercase s.[l-1] in
4432 match suffix with
4433 | 'k' -> String.sub s 0 (l-1), 10
4434 | 'm' -> String.sub s 0 (l-1), 20
4435 | 'g' -> String.sub s 0 (l-1), 30
4436 | _ -> s, 0
4437 else s, 0
4439 let n = int_of_string s1 in
4440 let m = n lsl shift in
4441 if m < 0 || m < n
4442 then raise (Failure "value too large")
4443 else m
4446 let string_with_suffix_of_int n =
4447 if n = 0
4448 then "0"
4449 else
4450 let units = [(30, "G"); (20, "M"); (10, "K")] in
4451 let prettyint n =
4452 let rec loop s n =
4453 let h = n mod 1000 in
4454 let n = n / 1000 in
4455 if n = 0
4456 then string_of_int h ^ s
4457 else (
4458 let s = Printf.sprintf "_%03d%s" h s in
4459 loop s n
4462 loop "" n
4464 let rec find = function
4465 | [] -> prettyint n
4466 | (shift, suffix) :: rest ->
4467 if (n land ((1 lsl shift) - 1)) = 0
4468 then prettyint (n lsr shift) ^ suffix
4469 else find rest
4471 find units
4474 let defghyllscroll = (40, 8, 32);;
4475 let fastghyllscroll = (5,1,2);;
4476 let neatghyllscroll = (10,1,9);;
4477 let ghyllscroll_of_string s =
4478 match s with
4479 | "default" -> Some defghyllscroll
4480 | "fast" -> Some (5,1,2)
4481 | "neat" -> Some (10,1,9)
4482 | "" | "none" -> None
4483 | _ ->
4484 let (n,a,b) as nab =
4485 Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b) in
4486 if n <= a || n <= b || a >= b
4487 then error "invalid ghyll N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)"
4488 n a b;
4489 Some nab
4492 let ghyllscroll_to_string ((n, a, b) as nab) =
4493 (**) if nab = defghyllscroll then "default"
4494 else if nab = fastghyllscroll then "fast"
4495 else if nab = neatghyllscroll then "neat"
4496 else Printf.sprintf "%d,%d,%d" n a b;
4499 let describe_location () =
4500 let fn = page_of_y state.y in
4501 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4502 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4503 let percent =
4504 if maxy <= 0
4505 then 100.
4506 else (100. *. (float state.y /. float maxy))
4508 if fn = ln
4509 then
4510 Printf.sprintf "page %d of %d [%.2f%%]"
4511 (fn+1) state.pagecount percent
4512 else
4513 Printf.sprintf
4514 "pages %d-%d of %d [%.2f%%]"
4515 (fn+1) (ln+1) state.pagecount percent
4518 let setpresentationmode v =
4519 let n = page_of_y state.y in
4520 state.anchor <- (n, 0.0, 1.0);
4521 conf.presentation <- v;
4522 if conf.fitmodel = FitPage
4523 then reqlayout conf.angle conf.fitmodel;
4524 represent ();
4527 let enterinfomode =
4528 let btos b = if b then "\xe2\x88\x9a" else "" in
4529 let showextended = ref false in
4530 let leave mode = function
4531 | Confirm -> state.mode <- mode
4532 | Cancel -> state.mode <- mode in
4533 let src =
4534 (object
4535 val mutable m_first_time = true
4536 val mutable m_l = []
4537 val mutable m_a = [||]
4538 val mutable m_prev_uioh = nouioh
4539 val mutable m_prev_mode = View
4541 inherit lvsourcebase
4543 method reset prev_mode prev_uioh =
4544 m_a <- Array.of_list (List.rev m_l);
4545 m_l <- [];
4546 m_prev_mode <- prev_mode;
4547 m_prev_uioh <- prev_uioh;
4548 if m_first_time
4549 then (
4550 let rec loop n =
4551 if n >= Array.length m_a
4552 then ()
4553 else
4554 match m_a.(n) with
4555 | _, _, _, Action _ -> m_active <- n
4556 | _ -> loop (n+1)
4558 loop 0;
4559 m_first_time <- false;
4562 method int name get set =
4563 m_l <-
4564 (name, `int get, 1, Action (
4565 fun u ->
4566 let ondone s =
4567 try set (int_of_string s)
4568 with exn ->
4569 state.text <- Printf.sprintf "bad integer `%s': %s"
4570 s (exntos exn)
4572 state.text <- "";
4573 let te = name ^ ": ", "", None, intentry, ondone, true in
4574 state.mode <- Textentry (te, leave m_prev_mode);
4576 )) :: m_l
4578 method int_with_suffix name get set =
4579 m_l <-
4580 (name, `intws get, 1, Action (
4581 fun u ->
4582 let ondone s =
4583 try set (int_of_string_with_suffix s)
4584 with exn ->
4585 state.text <- Printf.sprintf "bad integer `%s': %s"
4586 s (exntos exn)
4588 state.text <- "";
4589 let te =
4590 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4592 state.mode <- Textentry (te, leave m_prev_mode);
4594 )) :: m_l
4596 method bool ?(offset=1) ?(btos=btos) name get set =
4597 m_l <-
4598 (name, `bool (btos, get), offset, Action (
4599 fun u ->
4600 let v = get () in
4601 set (not v);
4603 )) :: m_l
4605 method color name get set =
4606 m_l <-
4607 (name, `color get, 1, Action (
4608 fun u ->
4609 let invalid = (nan, nan, nan) in
4610 let ondone s =
4611 let c =
4612 try color_of_string s
4613 with exn ->
4614 state.text <- Printf.sprintf "bad color `%s': %s"
4615 s (exntos exn);
4616 invalid
4618 if c <> invalid
4619 then set c;
4621 let te = name ^ ": ", "", None, textentry, ondone, true in
4622 state.text <- color_to_string (get ());
4623 state.mode <- Textentry (te, leave m_prev_mode);
4625 )) :: m_l
4627 method string name get set =
4628 m_l <-
4629 (name, `string get, 1, Action (
4630 fun u ->
4631 let ondone s = set s in
4632 let te = name ^ ": ", "", None, textentry, ondone, true in
4633 state.mode <- Textentry (te, leave m_prev_mode);
4635 )) :: m_l
4637 method colorspace name get set =
4638 m_l <-
4639 (name, `string get, 1, Action (
4640 fun _ ->
4641 let source =
4642 (object
4643 inherit lvsourcebase
4645 initializer
4646 m_active <- CSTE.to_int conf.colorspace;
4647 m_first <- 0;
4649 method getitemcount =
4650 Array.length CSTE.names
4651 method getitem n =
4652 (CSTE.names.(n), 0)
4653 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4654 ignore (uioh, first, pan, qsearch);
4655 if not cancel then set active;
4656 None
4657 method hasaction _ = true
4658 end)
4660 state.text <- "";
4661 let modehash = findkeyhash conf "info" in
4662 coe (new listview ~source ~trusted:true ~modehash)
4663 )) :: m_l
4665 method paxmark name get set =
4666 m_l <-
4667 (name, `string get, 1, Action (
4668 fun _ ->
4669 let source =
4670 (object
4671 inherit lvsourcebase
4673 initializer
4674 m_active <- MTE.to_int conf.paxmark;
4675 m_first <- 0;
4677 method getitemcount = Array.length MTE.names
4678 method getitem n = (MTE.names.(n), 0)
4679 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4680 ignore (uioh, first, pan, qsearch);
4681 if not cancel then set active;
4682 None
4683 method hasaction _ = true
4684 end)
4686 state.text <- "";
4687 let modehash = findkeyhash conf "info" in
4688 coe (new listview ~source ~trusted:true ~modehash)
4689 )) :: m_l
4691 method fitmodel name get set =
4692 m_l <-
4693 (name, `string get, 1, Action (
4694 fun _ ->
4695 let source =
4696 (object
4697 inherit lvsourcebase
4699 initializer
4700 m_active <- FMTE.to_int conf.fitmodel;
4701 m_first <- 0;
4703 method getitemcount = Array.length FMTE.names
4704 method getitem n = (FMTE.names.(n), 0)
4705 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4706 ignore (uioh, first, pan, qsearch);
4707 if not cancel then set active;
4708 None
4709 method hasaction _ = true
4710 end)
4712 state.text <- "";
4713 let modehash = findkeyhash conf "info" in
4714 coe (new listview ~source ~trusted:true ~modehash)
4715 )) :: m_l
4717 method caption s offset =
4718 m_l <- (s, `empty, offset, Noaction) :: m_l
4720 method caption2 s f offset =
4721 m_l <- (s, `string f, offset, Noaction) :: m_l
4723 method getitemcount = Array.length m_a
4725 method getitem n =
4726 let tostr = function
4727 | `int f -> string_of_int (f ())
4728 | `intws f -> string_with_suffix_of_int (f ())
4729 | `string f -> f ()
4730 | `color f -> color_to_string (f ())
4731 | `bool (btos, f) -> btos (f ())
4732 | `empty -> ""
4734 let name, t, offset, _ = m_a.(n) in
4735 ((let s = tostr t in
4736 if nonemptystr s
4737 then Printf.sprintf "%s\t%s" name s
4738 else name),
4739 offset)
4741 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4742 let uiohopt =
4743 if not cancel
4744 then (
4745 m_qsearch <- qsearch;
4746 let uioh =
4747 match m_a.(active) with
4748 | _, _, _, Action f -> f uioh
4749 | _ -> uioh
4751 Some uioh
4753 else None
4755 m_active <- active;
4756 m_first <- first;
4757 m_pan <- pan;
4758 uiohopt
4760 method hasaction n =
4761 match m_a.(n) with
4762 | _, _, _, Action _ -> true
4763 | _ -> false
4764 end)
4766 let rec fillsrc prevmode prevuioh =
4767 let sep () = src#caption "" 0 in
4768 let colorp name get set =
4769 src#string name
4770 (fun () -> color_to_string (get ()))
4771 (fun v ->
4773 let c = color_of_string v in
4774 set c
4775 with exn ->
4776 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4779 let oldmode = state.mode in
4780 let birdseye = isbirdseye state.mode in
4782 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4784 src#bool "presentation mode"
4785 (fun () -> conf.presentation)
4786 (fun v -> setpresentationmode v);
4788 src#bool "ignore case in searches"
4789 (fun () -> conf.icase)
4790 (fun v -> conf.icase <- v);
4792 src#bool "preload"
4793 (fun () -> conf.preload)
4794 (fun v -> conf.preload <- v);
4796 src#bool "highlight links"
4797 (fun () -> conf.hlinks)
4798 (fun v -> conf.hlinks <- v);
4800 src#bool "under info"
4801 (fun () -> conf.underinfo)
4802 (fun v -> conf.underinfo <- v);
4804 src#bool "persistent bookmarks"
4805 (fun () -> conf.savebmarks)
4806 (fun v -> conf.savebmarks <- v);
4808 src#fitmodel "fit model"
4809 (fun () -> FMTE.to_string conf.fitmodel)
4810 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4812 src#bool "trim margins"
4813 (fun () -> conf.trimmargins)
4814 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4816 src#bool "persistent location"
4817 (fun () -> conf.jumpback)
4818 (fun v -> conf.jumpback <- v);
4820 sep ();
4821 src#int "inter-page space"
4822 (fun () -> conf.interpagespace)
4823 (fun n ->
4824 conf.interpagespace <- n;
4825 docolumns conf.columns;
4826 let pageno, py =
4827 match state.layout with
4828 | [] -> 0, 0
4829 | l :: _ ->
4830 l.pageno, l.pagey
4832 state.maxy <- calcheight ();
4833 let y = getpagey pageno in
4834 gotoy (y + py)
4837 src#int "page bias"
4838 (fun () -> conf.pagebias)
4839 (fun v -> conf.pagebias <- v);
4841 src#int "scroll step"
4842 (fun () -> conf.scrollstep)
4843 (fun n -> conf.scrollstep <- n);
4845 src#int "horizontal scroll step"
4846 (fun () -> conf.hscrollstep)
4847 (fun v -> conf.hscrollstep <- v);
4849 src#int "auto scroll step"
4850 (fun () ->
4851 match state.autoscroll with
4852 | Some step -> step
4853 | _ -> conf.autoscrollstep)
4854 (fun n ->
4855 if state.autoscroll <> None
4856 then state.autoscroll <- Some n;
4857 conf.autoscrollstep <- n);
4859 src#int "zoom"
4860 (fun () -> truncate (conf.zoom *. 100.))
4861 (fun v -> setzoom ((float v) /. 100.));
4863 src#int "rotation"
4864 (fun () -> conf.angle)
4865 (fun v -> reqlayout v conf.fitmodel);
4867 src#int "scroll bar width"
4868 (fun () -> conf.scrollbw)
4869 (fun v ->
4870 conf.scrollbw <- v;
4871 reshape state.winw state.winh;
4874 src#int "scroll handle height"
4875 (fun () -> conf.scrollh)
4876 (fun v -> conf.scrollh <- v;);
4878 src#int "thumbnail width"
4879 (fun () -> conf.thumbw)
4880 (fun v ->
4881 conf.thumbw <- min 4096 v;
4882 match oldmode with
4883 | Birdseye beye ->
4884 leavebirdseye beye false;
4885 enterbirdseye ()
4886 | _ -> ()
4889 let mode = state.mode in
4890 src#string "columns"
4891 (fun () ->
4892 match conf.columns with
4893 | Csingle _ -> "1"
4894 | Cmulti (multi, _) -> multicolumns_to_string multi
4895 | Csplit (count, _) -> "-" ^ string_of_int count
4897 (fun v ->
4898 let n, a, b = multicolumns_of_string v in
4899 setcolumns mode n a b);
4901 sep ();
4902 src#caption "Pixmap cache" 0;
4903 src#int_with_suffix "size (advisory)"
4904 (fun () -> conf.memlimit)
4905 (fun v -> conf.memlimit <- v);
4907 src#caption2 "used"
4908 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4909 (string_with_suffix_of_int state.memused)
4910 (Hashtbl.length state.tilemap)) 1;
4912 sep ();
4913 src#caption "Layout" 0;
4914 src#caption2 "Dimension"
4915 (fun () ->
4916 Printf.sprintf "%dx%d (virtual %dx%d)"
4917 state.winw state.winh
4918 state.w state.maxy)
4920 if conf.debug
4921 then
4922 src#caption2 "Position" (fun () ->
4923 Printf.sprintf "%dx%d" state.x state.y
4925 else
4926 src#caption2 "Position" (fun () -> describe_location ()) 1
4929 sep ();
4930 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4931 "Save these parameters as global defaults at exit"
4932 (fun () -> conf.bedefault)
4933 (fun v -> conf.bedefault <- v)
4936 sep ();
4937 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4938 src#bool ~offset:0 ~btos "Extended parameters"
4939 (fun () -> !showextended)
4940 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4941 if !showextended
4942 then (
4943 src#bool "checkers"
4944 (fun () -> conf.checkers)
4945 (fun v -> conf.checkers <- v; setcheckers v);
4946 src#bool "update cursor"
4947 (fun () -> conf.updatecurs)
4948 (fun v -> conf.updatecurs <- v);
4949 src#bool "verbose"
4950 (fun () -> conf.verbose)
4951 (fun v -> conf.verbose <- v);
4952 src#bool "invert colors"
4953 (fun () -> conf.invert)
4954 (fun v -> conf.invert <- v);
4955 src#bool "max fit"
4956 (fun () -> conf.maxhfit)
4957 (fun v -> conf.maxhfit <- v);
4958 src#bool "redirect stderr"
4959 (fun () -> conf.redirectstderr)
4960 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4961 src#bool "pax mode"
4962 (fun () -> conf.pax != None)
4963 (fun v ->
4964 if v
4965 then conf.pax <- Some (ref (now (), 0, 0))
4966 else conf.pax <- None);
4967 src#string "uri launcher"
4968 (fun () -> conf.urilauncher)
4969 (fun v -> conf.urilauncher <- v);
4970 src#string "path launcher"
4971 (fun () -> conf.pathlauncher)
4972 (fun v -> conf.pathlauncher <- v);
4973 src#string "tile size"
4974 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4975 (fun v ->
4977 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4978 conf.tilew <- max 64 w;
4979 conf.tileh <- max 64 h;
4980 flushtiles ();
4981 with exn ->
4982 state.text <- Printf.sprintf "bad tile size `%s': %s"
4983 v (exntos exn)
4985 src#int "texture count"
4986 (fun () -> conf.texcount)
4987 (fun v ->
4988 if realloctexts v
4989 then conf.texcount <- v
4990 else showtext '!' " Failed to set texture count please retry later"
4992 src#int "slice height"
4993 (fun () -> conf.sliceheight)
4994 (fun v ->
4995 conf.sliceheight <- v;
4996 wcmd "sliceh %d" conf.sliceheight;
4998 src#int "anti-aliasing level"
4999 (fun () -> conf.aalevel)
5000 (fun v ->
5001 conf.aalevel <- bound v 0 8;
5002 state.anchor <- getanchor ();
5003 opendoc state.path state.password;
5005 src#string "page scroll scaling factor"
5006 (fun () -> string_of_float conf.pgscale)
5007 (fun v ->
5009 let s = float_of_string v in
5010 conf.pgscale <- s
5011 with exn ->
5012 state.text <- Printf.sprintf
5013 "bad page scroll scaling factor `%s': %s" v (exntos exn)
5016 src#int "ui font size"
5017 (fun () -> fstate.fontsize)
5018 (fun v -> setfontsize (bound v 5 100));
5019 src#int "hint font size"
5020 (fun () -> conf.hfsize)
5021 (fun v -> conf.hfsize <- bound v 5 100);
5022 colorp "background color"
5023 (fun () -> conf.bgcolor)
5024 (fun v -> conf.bgcolor <- v);
5025 src#bool "crop hack"
5026 (fun () -> conf.crophack)
5027 (fun v -> conf.crophack <- v);
5028 src#string "trim fuzz"
5029 (fun () -> irect_to_string conf.trimfuzz)
5030 (fun v ->
5032 conf.trimfuzz <- irect_of_string v;
5033 if conf.trimmargins
5034 then settrim true conf.trimfuzz;
5035 with exn ->
5036 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
5038 src#string "throttle"
5039 (fun () ->
5040 match conf.maxwait with
5041 | None -> "show place holder if page is not ready"
5042 | Some time ->
5043 if time = infinity
5044 then "wait for page to fully render"
5045 else
5046 "wait " ^ string_of_float time
5047 ^ " seconds before showing placeholder"
5049 (fun v ->
5051 let f = float_of_string v in
5052 if f <= 0.0
5053 then conf.maxwait <- None
5054 else conf.maxwait <- Some f
5055 with exn ->
5056 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
5058 src#string "ghyll scroll"
5059 (fun () ->
5060 match conf.ghyllscroll with
5061 | None -> ""
5062 | Some nab -> ghyllscroll_to_string nab
5064 (fun v ->
5065 try conf.ghyllscroll <- ghyllscroll_of_string v
5066 with exn ->
5067 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
5069 src#string "selection command"
5070 (fun () -> conf.selcmd)
5071 (fun v -> conf.selcmd <- v);
5072 src#string "synctex command"
5073 (fun () -> conf.stcmd)
5074 (fun v -> conf.stcmd <- v);
5075 src#string "pax command"
5076 (fun () -> conf.paxcmd)
5077 (fun v -> conf.paxcmd <- v);
5078 src#colorspace "color space"
5079 (fun () -> CSTE.to_string conf.colorspace)
5080 (fun v ->
5081 conf.colorspace <- CSTE.of_int v;
5082 wcmd "cs %d" v;
5083 load state.layout;
5085 src#paxmark "pax mark method"
5086 (fun () -> MTE.to_string conf.paxmark)
5087 (fun v -> conf.paxmark <- MTE.of_int v);
5088 if pbousable ()
5089 then
5090 src#bool "use PBO"
5091 (fun () -> conf.usepbo)
5092 (fun v -> conf.usepbo <- v);
5093 src#bool "mouse wheel scrolls pages"
5094 (fun () -> conf.wheelbypage)
5095 (fun v -> conf.wheelbypage <- v);
5096 src#bool "open remote links in a new instance"
5097 (fun () -> conf.riani)
5098 (fun v -> conf.riani <- v);
5101 sep ();
5102 src#caption "Document" 0;
5103 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
5104 src#caption2 "Pages"
5105 (fun () -> string_of_int state.pagecount) 1;
5106 src#caption2 "Dimensions"
5107 (fun () -> string_of_int (List.length state.pdims)) 1;
5108 if conf.trimmargins
5109 then (
5110 sep ();
5111 src#caption "Trimmed margins" 0;
5112 src#caption2 "Dimensions"
5113 (fun () -> string_of_int (List.length state.pdims)) 1;
5116 sep ();
5117 src#caption "OpenGL" 0;
5118 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
5119 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
5121 sep ();
5122 src#caption "Location" 0;
5123 if nonemptystr state.origin
5124 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
5125 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
5127 src#reset prevmode prevuioh;
5129 fun () ->
5130 state.text <- "";
5131 let prevmode = state.mode
5132 and prevuioh = state.uioh in
5133 fillsrc prevmode prevuioh;
5134 let source = (src :> lvsource) in
5135 let modehash = findkeyhash conf "info" in
5136 state.uioh <- coe (object (self)
5137 inherit listview ~source ~trusted:true ~modehash as super
5138 val mutable m_prevmemused = 0
5139 method infochanged = function
5140 | Memused ->
5141 if m_prevmemused != state.memused
5142 then (
5143 m_prevmemused <- state.memused;
5144 G.postRedisplay "memusedchanged";
5146 | Pdim -> G.postRedisplay "pdimchanged"
5147 | Docinfo -> fillsrc prevmode prevuioh
5149 method key key mask =
5150 if not (Wsi.withctrl mask)
5151 then
5152 match key with
5153 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
5154 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
5155 | _ -> super#key key mask
5156 else super#key key mask
5157 end);
5158 G.postRedisplay "info";
5161 let enterhelpmode =
5162 let source =
5163 (object
5164 inherit lvsourcebase
5165 method getitemcount = Array.length state.help
5166 method getitem n =
5167 let s, l, _ = state.help.(n) in
5168 (s, l)
5170 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5171 let optuioh =
5172 if not cancel
5173 then (
5174 m_qsearch <- qsearch;
5175 match state.help.(active) with
5176 | _, _, Action f -> Some (f uioh)
5177 | _ -> Some (uioh)
5179 else None
5181 m_active <- active;
5182 m_first <- first;
5183 m_pan <- pan;
5184 optuioh
5186 method hasaction n =
5187 match state.help.(n) with
5188 | _, _, Action _ -> true
5189 | _ -> false
5191 initializer
5192 m_active <- -1
5193 end)
5194 in fun () ->
5195 let modehash = findkeyhash conf "help" in
5196 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
5197 G.postRedisplay "help";
5200 let entermsgsmode =
5201 let msgsource =
5202 let re = Str.regexp "[\r\n]" in
5203 (object
5204 inherit lvsourcebase
5205 val mutable m_items = [||]
5207 method getitemcount = 1 + Array.length m_items
5209 method getitem n =
5210 if n = 0
5211 then "[Clear]", 0
5212 else m_items.(n-1), 0
5214 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5215 ignore uioh;
5216 if not cancel
5217 then (
5218 if active = 0
5219 then Buffer.clear state.errmsgs;
5220 m_qsearch <- qsearch;
5222 m_active <- active;
5223 m_first <- first;
5224 m_pan <- pan;
5225 None
5227 method hasaction n =
5228 n = 0
5230 method reset =
5231 state.newerrmsgs <- false;
5232 let l = Str.split re (Buffer.contents state.errmsgs) in
5233 m_items <- Array.of_list l
5235 initializer
5236 m_active <- 0
5237 end)
5238 in fun () ->
5239 state.text <- "";
5240 msgsource#reset;
5241 let source = (msgsource :> lvsource) in
5242 let modehash = findkeyhash conf "listview" in
5243 state.uioh <- coe (object
5244 inherit listview ~source ~trusted:false ~modehash as super
5245 method display =
5246 if state.newerrmsgs
5247 then msgsource#reset;
5248 super#display
5249 end);
5250 G.postRedisplay "msgs";
5253 let quickbookmark ?title () =
5254 match state.layout with
5255 | [] -> ()
5256 | l :: _ ->
5257 let title =
5258 match title with
5259 | None ->
5260 let sec = Unix.gettimeofday () in
5261 let tm = Unix.localtime sec in
5262 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
5263 (l.pageno+1)
5264 tm.Unix.tm_mday
5265 tm.Unix.tm_mon
5266 (tm.Unix.tm_year + 1900)
5267 tm.Unix.tm_hour
5268 tm.Unix.tm_min
5269 | Some title -> title
5271 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5274 let setautoscrollspeed step goingdown =
5275 let incr = max 1 ((abs step) / 2) in
5276 let incr = if goingdown then incr else -incr in
5277 let astep = step + incr in
5278 state.autoscroll <- Some astep;
5281 let canpan () =
5282 match conf.columns with
5283 | Csplit _ -> true
5284 | _ -> state.x != 0 || conf.zoom > 1.0
5287 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5289 let existsinrow pageno (columns, coverA, coverB) p =
5290 let last = ((pageno - coverA) mod columns) + columns in
5291 let rec any = function
5292 | [] -> false
5293 | l :: rest ->
5294 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5295 then p l
5296 else (
5297 if not (p l)
5298 then (if l.pageno = last then false else any rest)
5299 else true
5302 any state.layout
5305 let nextpage () =
5306 match state.layout with
5307 | [] ->
5308 let pageno = page_of_y state.y in
5309 gotoghyll (getpagey (pageno+1))
5310 | l :: rest ->
5311 match conf.columns with
5312 | Csingle _ ->
5313 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5314 then
5315 let y = clamp (pgscale state.winh) in
5316 gotoghyll y
5317 else
5318 let pageno = min (l.pageno+1) (state.pagecount-1) in
5319 gotoghyll (getpagey pageno)
5320 | Cmulti ((c, _, _) as cl, _) ->
5321 if conf.presentation
5322 && (existsinrow l.pageno cl
5323 (fun l -> l.pageh > l.pagey + l.pagevh))
5324 then
5325 let y = clamp (pgscale state.winh) in
5326 gotoghyll y
5327 else
5328 let pageno = min (l.pageno+c) (state.pagecount-1) in
5329 gotoghyll (getpagey pageno)
5330 | Csplit (n, _) ->
5331 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5332 then
5333 let pagey, pageh = getpageyh l.pageno in
5334 let pagey = pagey + pageh * l.pagecol in
5335 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5336 gotoghyll (pagey + pageh + ips)
5339 let prevpage () =
5340 match state.layout with
5341 | [] ->
5342 let pageno = page_of_y state.y in
5343 gotoghyll (getpagey (pageno-1))
5344 | l :: _ ->
5345 match conf.columns with
5346 | Csingle _ ->
5347 if conf.presentation && l.pagey != 0
5348 then
5349 gotoghyll (clamp (pgscale ~-(state.winh)))
5350 else
5351 let pageno = max 0 (l.pageno-1) in
5352 gotoghyll (getpagey pageno)
5353 | Cmulti ((c, _, coverB) as cl, _) ->
5354 if conf.presentation &&
5355 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5356 then
5357 gotoghyll (clamp (pgscale ~-(state.winh)))
5358 else
5359 let decr =
5360 if l.pageno = state.pagecount - coverB
5361 then 1
5362 else c
5364 let pageno = max 0 (l.pageno-decr) in
5365 gotoghyll (getpagey pageno)
5366 | Csplit (n, _) ->
5367 let y =
5368 if l.pagecol = 0
5369 then
5370 if l.pageno = 0
5371 then l.pagey
5372 else
5373 let pageno = max 0 (l.pageno-1) in
5374 let pagey, pageh = getpageyh pageno in
5375 pagey + (n-1)*pageh
5376 else
5377 let pagey, pageh = getpageyh l.pageno in
5378 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5380 gotoghyll y
5383 let viewkeyboard key mask =
5384 let enttext te =
5385 let mode = state.mode in
5386 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5387 state.text <- "";
5388 enttext ();
5389 G.postRedisplay "view:enttext"
5391 let ctrl = Wsi.withctrl mask in
5392 let key =
5393 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5395 match key with
5396 | 81 -> (* Q *)
5397 exit 0
5399 | 0xff63 -> (* insert *)
5400 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5401 then (
5402 state.mode <- LinkNav (Ltgendir 0);
5403 gotoy state.y;
5405 else showtext '!' "Keyboard link navigation does not work under rotation"
5407 | 0xff1b | 113 -> (* escape / q *)
5408 begin match state.mstate with
5409 | Mzoomrect _ ->
5410 state.mstate <- Mnone;
5411 Wsi.setcursor Wsi.CURSOR_INHERIT;
5412 G.postRedisplay "kill zoom rect";
5413 | _ ->
5414 begin match state.mode with
5415 | LinkNav _ ->
5416 state.mode <- View;
5417 G.postRedisplay "esc leave linknav"
5418 | _ ->
5419 match state.ranchors with
5420 | [] -> raise Quit
5421 | (path, password, anchor, origin) :: rest ->
5422 state.ranchors <- rest;
5423 state.anchor <- anchor;
5424 state.origin <- origin;
5425 state.nameddest <- "";
5426 opendoc path password
5427 end;
5428 end;
5430 | 0xff08 -> (* backspace *)
5431 gotoghyll (getnav ~-1)
5433 | 111 -> (* o *)
5434 enteroutlinemode ()
5436 | 117 -> (* u *)
5437 state.rects <- [];
5438 state.text <- "";
5439 G.postRedisplay "dehighlight";
5441 | 47 | 63 -> (* / ? *)
5442 let ondone isforw s =
5443 cbput state.hists.pat s;
5444 state.searchpattern <- s;
5445 search s isforw
5447 let s = String.create 1 in
5448 s.[0] <- Char.chr key;
5449 enttext (s, "", Some (onhist state.hists.pat),
5450 textentry, ondone (key = 47), true)
5452 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5453 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5454 setzoom (conf.zoom +. incr)
5456 | 43 | 0xffab -> (* + *)
5457 let ondone s =
5458 let n =
5459 try int_of_string s with exc ->
5460 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5461 max_int
5463 if n != max_int
5464 then (
5465 conf.pagebias <- n;
5466 state.text <- "page bias is now " ^ string_of_int n;
5469 enttext ("page bias: ", "", None, intentry, ondone, true)
5471 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5472 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5473 setzoom (max 0.01 (conf.zoom -. decr))
5475 | 45 | 0xffad -> (* - *)
5476 let ondone msg = state.text <- msg in
5477 enttext (
5478 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5479 optentry state.mode, ondone, true
5482 | 48 when ctrl -> (* ctrl-0 *)
5483 if conf.zoom = 1.0
5484 then (
5485 state.x <- 0;
5486 gotoy state.y
5488 else setzoom 1.0
5490 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5491 let cols =
5492 match conf.columns with
5493 | Csingle _ | Cmulti _ -> 1
5494 | Csplit (n, _) -> n
5496 let h = state.winh -
5497 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5499 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5500 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5501 then setzoom zoom
5503 | 51 when ctrl -> (* ctrl-3 *)
5504 let fm =
5505 match conf.fitmodel with
5506 | FitWidth -> FitProportional
5507 | FitProportional -> FitPage
5508 | FitPage -> FitWidth
5510 state.text <- "fit model: " ^ FMTE.to_string fm;
5511 reqlayout conf.angle fm
5513 | 0xffc6 -> (* f9 *)
5514 togglebirdseye ()
5516 | 57 when ctrl -> (* ctrl-9 *)
5517 togglebirdseye ()
5519 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5520 when not ctrl -> (* 0..9 *)
5521 let ondone s =
5522 let n =
5523 try int_of_string s with exc ->
5524 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5527 if n >= 0
5528 then (
5529 addnav ();
5530 cbput state.hists.pag (string_of_int n);
5531 gotopage1 (n + conf.pagebias - 1) 0;
5534 let pageentry text key =
5535 match Char.unsafe_chr key with
5536 | 'g' -> TEdone text
5537 | _ -> intentry text key
5539 let text = "x" in text.[0] <- Char.chr key;
5540 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5542 | 98 -> (* b *)
5543 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5544 reshape state.winw state.winh;
5546 | 66 -> (* B *)
5547 state.bzoom <- not state.bzoom;
5548 state.rects <- [];
5549 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5551 | 108 -> (* l *)
5552 conf.hlinks <- not conf.hlinks;
5553 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5554 G.postRedisplay "toggle highlightlinks";
5556 | 70 -> (* F *)
5557 state.glinks <- true;
5558 let mode = state.mode in
5559 state.mode <- Textentry (
5560 (":", "", None, linknentry, linkndone gotounder, false),
5561 (fun _ ->
5562 state.glinks <- false;
5563 state.mode <- mode)
5565 state.text <- "";
5566 G.postRedisplay "view:linkent(F)"
5568 | 121 -> (* y *)
5569 state.glinks <- true;
5570 let mode = state.mode in
5571 state.mode <- Textentry (
5573 ":", "", None, linknentry, linkndone (fun under ->
5574 selstring (undertext under);
5575 ), false
5577 fun _ ->
5578 state.glinks <- false;
5579 state.mode <- mode
5581 state.text <- "";
5582 G.postRedisplay "view:linkent"
5584 | 97 -> (* a *)
5585 begin match state.autoscroll with
5586 | Some step ->
5587 conf.autoscrollstep <- step;
5588 state.autoscroll <- None
5589 | None ->
5590 if conf.autoscrollstep = 0
5591 then state.autoscroll <- Some 1
5592 else state.autoscroll <- Some conf.autoscrollstep
5595 | 112 when ctrl -> (* ctrl-p *)
5596 launchpath ()
5598 | 80 -> (* P *)
5599 setpresentationmode (not conf.presentation);
5600 showtext ' ' ("presentation mode " ^
5601 if conf.presentation then "on" else "off");
5603 | 102 -> (* f *)
5604 if List.mem Wsi.Fullscreen state.winstate
5605 then Wsi.reshape conf.cwinw conf.cwinh
5606 else Wsi.fullscreen ()
5608 | 112 | 78 -> (* p|N *)
5609 search state.searchpattern false
5611 | 110 | 0xffc0 -> (* n|F3 *)
5612 search state.searchpattern true
5614 | 116 -> (* t *)
5615 begin match state.layout with
5616 | [] -> ()
5617 | l :: _ ->
5618 gotoghyll (getpagey l.pageno)
5621 | 32 -> (* space *)
5622 nextpage ()
5624 | 0xff9f | 0xffff -> (* delete *)
5625 prevpage ()
5627 | 61 -> (* = *)
5628 showtext ' ' (describe_location ());
5630 | 119 -> (* w *)
5631 begin match state.layout with
5632 | [] -> ()
5633 | l :: _ ->
5634 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5635 G.postRedisplay "w"
5638 | 39 -> (* ' *)
5639 enterbookmarkmode ()
5641 | 104 | 0xffbe -> (* h|F1 *)
5642 enterhelpmode ()
5644 | 105 -> (* i *)
5645 enterinfomode ()
5647 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5648 entermsgsmode ()
5650 | 109 -> (* m *)
5651 let ondone s =
5652 match state.layout with
5653 | l :: _ ->
5654 if nonemptystr s
5655 then
5656 state.bookmarks <-
5657 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5658 | _ -> ()
5660 enttext ("bookmark: ", "", None, textentry, ondone, true)
5662 | 126 -> (* ~ *)
5663 quickbookmark ();
5664 showtext ' ' "Quick bookmark added";
5666 | 122 -> (* z *)
5667 begin match state.layout with
5668 | l :: _ ->
5669 let rect = getpdimrect l.pagedimno in
5670 let w, h =
5671 if conf.crophack
5672 then
5673 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5674 truncate (1.2 *. (rect.(3) -. rect.(0))))
5675 else
5676 (truncate (rect.(1) -. rect.(0)),
5677 truncate (rect.(3) -. rect.(0)))
5679 let w = truncate ((float w)*.conf.zoom)
5680 and h = truncate ((float h)*.conf.zoom) in
5681 if w != 0 && h != 0
5682 then (
5683 state.anchor <- getanchor ();
5684 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5686 G.postRedisplay "z";
5688 | [] -> ()
5691 | 120 -> state.roam () (* x *)
5692 | 60 | 62 -> (* < > *)
5693 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5695 | 91 | 93 -> (* [ ] *)
5696 conf.colorscale <-
5697 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5699 G.postRedisplay "brightness";
5701 | 99 when state.mode = View -> (* [alt-]c *)
5702 if Wsi.withalt mask
5703 then (
5704 if conf.zoom > 1.0
5705 then
5706 let m = (wadjsb state.winw - state.w) / 2 in
5707 state.x <- m;
5708 gotoy_and_clear_text state.y
5710 else
5711 let (c, a, b), z =
5712 match state.prevcolumns with
5713 | None -> (1, 0, 0), 1.0
5714 | Some (columns, z) ->
5715 let cab =
5716 match columns with
5717 | Csplit (c, _) -> -c, 0, 0
5718 | Cmulti ((c, a, b), _) -> c, a, b
5719 | Csingle _ -> 1, 0, 0
5721 cab, z
5723 setcolumns View c a b;
5724 setzoom z
5726 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
5727 -> (* ctrl-shift- (kp) [up|down] *)
5728 let zoom, x = state.prevzoom in
5729 setzoom zoom;
5730 state.x <- x;
5732 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5733 begin match state.autoscroll with
5734 | None ->
5735 begin match state.mode with
5736 | Birdseye beye -> upbirdseye 1 beye
5737 | _ ->
5738 if ctrl
5739 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5740 else (
5741 if not (Wsi.withshift mask) && conf.presentation
5742 then prevpage ()
5743 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5746 | Some n ->
5747 setautoscrollspeed n false
5750 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5751 begin match state.autoscroll with
5752 | None ->
5753 begin match state.mode with
5754 | Birdseye beye -> downbirdseye 1 beye
5755 | _ ->
5756 if ctrl
5757 then gotoy_and_clear_text (clamp (state.winh/2))
5758 else (
5759 if not (Wsi.withshift mask) && conf.presentation
5760 then nextpage ()
5761 else gotoy_and_clear_text (clamp conf.scrollstep)
5764 | Some n ->
5765 setautoscrollspeed n true
5768 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5769 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5770 if canpan ()
5771 then
5772 let dx =
5773 if ctrl
5774 then state.winw / 2
5775 else conf.hscrollstep
5777 let dx = if key = 0xff51 || key = 0xff96 then dx else -dx in
5778 state.x <- panbound (state.x + dx);
5779 gotoy_and_clear_text state.y
5780 else (
5781 state.text <- "";
5782 G.postRedisplay "left/right"
5785 | 0xff55 | 0xff9a -> (* (kp) prior *)
5786 let y =
5787 if ctrl
5788 then
5789 match state.layout with
5790 | [] -> state.y
5791 | l :: _ -> state.y - l.pagey
5792 else
5793 clamp (pgscale (-state.winh))
5795 gotoghyll y
5797 | 0xff56 | 0xff9b -> (* (kp) next *)
5798 let y =
5799 if ctrl
5800 then
5801 match List.rev state.layout with
5802 | [] -> state.y
5803 | l :: _ -> getpagey l.pageno
5804 else
5805 clamp (pgscale state.winh)
5807 gotoghyll y
5809 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5810 gotoghyll 0
5811 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5812 gotoghyll (clamp state.maxy)
5814 | 0xff53 | 0xff98
5815 when Wsi.withalt mask -> (* alt-(kp) right *)
5816 gotoghyll (getnav 1)
5817 | 0xff51 | 0xff96
5818 when Wsi.withalt mask -> (* alt-(kp) left *)
5819 gotoghyll (getnav ~-1)
5821 | 114 -> (* r *)
5822 reload ()
5824 | 118 when conf.debug -> (* v *)
5825 state.rects <- [];
5826 List.iter (fun l ->
5827 match getopaque l.pageno with
5828 | None -> ()
5829 | Some opaque ->
5830 let x0, y0, x1, y1 = pagebbox opaque in
5831 let a,b = float x0, float y0 in
5832 let c,d = float x1, float y0 in
5833 let e,f = float x1, float y1 in
5834 let h,j = float x0, float y1 in
5835 let rect = (a,b,c,d,e,f,h,j) in
5836 debugrect rect;
5837 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5838 ) state.layout;
5839 G.postRedisplay "v";
5841 | _ ->
5842 vlog "huh? %s" (Wsi.keyname key)
5845 let linknavkeyboard key mask linknav =
5846 let getpage pageno =
5847 let rec loop = function
5848 | [] -> None
5849 | l :: _ when l.pageno = pageno -> Some l
5850 | _ :: rest -> loop rest
5851 in loop state.layout
5853 let doexact (pageno, n) =
5854 match getopaque pageno, getpage pageno with
5855 | Some opaque, Some l ->
5856 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5857 then
5858 let under = getlink opaque n in
5859 G.postRedisplay "link gotounder";
5860 gotounder under;
5861 state.mode <- View;
5862 else
5863 let opt, dir =
5864 match key with
5865 | 0xff50 -> (* home *)
5866 Some (findlink opaque LDfirst), -1
5868 | 0xff57 -> (* end *)
5869 Some (findlink opaque LDlast), 1
5871 | 0xff51 -> (* left *)
5872 Some (findlink opaque (LDleft n)), -1
5874 | 0xff53 -> (* right *)
5875 Some (findlink opaque (LDright n)), 1
5877 | 0xff52 -> (* up *)
5878 Some (findlink opaque (LDup n)), -1
5880 | 0xff54 -> (* down *)
5881 Some (findlink opaque (LDdown n)), 1
5883 | _ -> None, 0
5885 let pwl l dir =
5886 begin match findpwl l.pageno dir with
5887 | Pwlnotfound -> ()
5888 | Pwl pageno ->
5889 let notfound dir =
5890 state.mode <- LinkNav (Ltgendir dir);
5891 let y, h = getpageyh pageno in
5892 let y =
5893 if dir < 0
5894 then y + h - state.winh
5895 else y
5897 gotoy y
5899 begin match getopaque pageno, getpage pageno with
5900 | Some opaque, Some _ ->
5901 let link =
5902 let ld = if dir > 0 then LDfirst else LDlast in
5903 findlink opaque ld
5905 begin match link with
5906 | Lfound m ->
5907 showlinktype (getlink opaque m);
5908 state.mode <- LinkNav (Ltexact (pageno, m));
5909 G.postRedisplay "linknav jpage";
5910 | _ -> notfound dir
5911 end;
5912 | _ -> notfound dir
5913 end;
5914 end;
5916 begin match opt with
5917 | Some Lnotfound -> pwl l dir;
5918 | Some (Lfound m) ->
5919 if m = n
5920 then pwl l dir
5921 else (
5922 let _, y0, _, y1 = getlinkrect opaque m in
5923 if y0 < l.pagey
5924 then gotopage1 l.pageno y0
5925 else (
5926 let d = fstate.fontsize + 1 in
5927 if y1 - l.pagey > l.pagevh - d
5928 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5929 else G.postRedisplay "linknav";
5931 showlinktype (getlink opaque m);
5932 state.mode <- LinkNav (Ltexact (l.pageno, m));
5935 | None -> viewkeyboard key mask
5936 end;
5937 | _ -> viewkeyboard key mask
5939 if key = 0xff63
5940 then (
5941 state.mode <- View;
5942 G.postRedisplay "leave linknav"
5944 else
5945 match linknav with
5946 | Ltgendir _ -> viewkeyboard key mask
5947 | Ltexact exact -> doexact exact
5950 let keyboard key mask =
5951 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5952 then wcmd "interrupt"
5953 else state.uioh <- state.uioh#key key mask
5956 let birdseyekeyboard key mask
5957 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5958 let incr =
5959 match conf.columns with
5960 | Csingle _ -> 1
5961 | Cmulti ((c, _, _), _) -> c
5962 | Csplit _ -> failwith "bird's eye split mode"
5964 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5965 match key with
5966 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5967 let y, h = getpageyh pageno in
5968 let top = (state.winh - h) / 2 in
5969 gotoy (max 0 (y - top))
5970 | 0xff0d (* enter *)
5971 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5972 | 0xff1b -> leavebirdseye beye true (* escape *)
5973 | 0xff52 -> upbirdseye incr beye (* up *)
5974 | 0xff54 -> downbirdseye incr beye (* down *)
5975 | 0xff51 -> upbirdseye 1 beye (* left *)
5976 | 0xff53 -> downbirdseye 1 beye (* right *)
5978 | 0xff55 -> (* prior *)
5979 begin match state.layout with
5980 | l :: _ ->
5981 if l.pagey != 0
5982 then (
5983 state.mode <- Birdseye (
5984 oconf, leftx, l.pageno, hooverpageno, anchor
5986 gotopage1 l.pageno 0;
5988 else (
5989 let layout = layout (state.y-state.winh) (pgh state.layout) in
5990 match layout with
5991 | [] -> gotoy (clamp (-state.winh))
5992 | l :: _ ->
5993 state.mode <- Birdseye (
5994 oconf, leftx, l.pageno, hooverpageno, anchor
5996 gotopage1 l.pageno 0
5999 | [] -> gotoy (clamp (-state.winh))
6000 end;
6002 | 0xff56 -> (* next *)
6003 begin match List.rev state.layout with
6004 | l :: _ ->
6005 let layout = layout (state.y + (pgh state.layout)) state.winh in
6006 begin match layout with
6007 | [] ->
6008 let incr = l.pageh - l.pagevh in
6009 if incr = 0
6010 then (
6011 state.mode <-
6012 Birdseye (
6013 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
6015 G.postRedisplay "birdseye pagedown";
6017 else gotoy (clamp (incr + conf.interpagespace*2));
6019 | l :: _ ->
6020 state.mode <-
6021 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
6022 gotopage1 l.pageno 0;
6025 | [] -> gotoy (clamp state.winh)
6026 end;
6028 | 0xff50 -> (* home *)
6029 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
6030 gotopage1 0 0
6032 | 0xff57 -> (* end *)
6033 let pageno = state.pagecount - 1 in
6034 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
6035 if not (pagevisible state.layout pageno)
6036 then
6037 let h =
6038 match List.rev state.pdims with
6039 | [] -> state.winh
6040 | (_, _, h, _) :: _ -> h
6042 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
6043 else G.postRedisplay "birdseye end";
6044 | _ -> viewkeyboard key mask
6047 let drawpage l =
6048 let color =
6049 match state.mode with
6050 | Textentry _ -> scalecolor 0.4
6051 | LinkNav _
6052 | View -> scalecolor 1.0
6053 | Birdseye (_, _, pageno, hooverpageno, _) ->
6054 if l.pageno = hooverpageno
6055 then scalecolor 0.9
6056 else (
6057 if l.pageno = pageno
6058 then scalecolor 1.0
6059 else scalecolor 0.8
6062 drawtiles l color;
6065 let postdrawpage l linkindexbase =
6066 match getopaque l.pageno with
6067 | Some opaque ->
6068 if tileready l l.pagex l.pagey
6069 then
6070 let x = l.pagedispx - l.pagex
6071 and y = l.pagedispy - l.pagey in
6072 let hlmask =
6073 match conf.columns with
6074 | Csingle _ | Cmulti _ ->
6075 (if conf.hlinks then 1 else 0)
6076 + (if state.glinks
6077 && not (isbirdseye state.mode) then 2 else 0)
6078 | _ -> 0
6080 let s =
6081 match state.mode with
6082 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
6083 | _ -> ""
6085 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
6086 else 0
6087 | _ -> 0
6090 let scrollindicator () =
6091 let sbw, ph, sh = state.uioh#scrollph in
6092 let sbh, pw, sw = state.uioh#scrollpw in
6094 GlDraw.color (0.64, 0.64, 0.64);
6095 filledrect
6096 (float (state.winw - sbw)) 0.
6097 (float state.winw) (float state.winh)
6099 filledrect
6100 0. (float (state.winh - sbh))
6101 (float (wadjsb state.winw - 1)) (float state.winh)
6103 GlDraw.color (0.0, 0.0, 0.0);
6105 filledrect
6106 (float (state.winw - sbw)) ph
6107 (float state.winw) (ph +. sh)
6109 filledrect
6110 pw (float (state.winh - sbh))
6111 (pw +. sw) (float state.winh)
6115 let showsel () =
6116 match state.mstate with
6117 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
6120 | Msel ((x0, y0), (x1, y1)) ->
6121 let rec loop = function
6122 | l :: ls ->
6123 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
6124 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
6125 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
6126 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
6127 then
6128 match getopaque l.pageno with
6129 | Some opaque ->
6130 let x0, y0 = pagetranslatepoint l x0 y0 in
6131 let x1, y1 = pagetranslatepoint l x1 y1 in
6132 seltext opaque (x0, y0, x1, y1);
6133 | _ -> ()
6134 else loop ls
6135 | [] -> ()
6137 loop state.layout
6140 let showrects = function [] -> () | rects ->
6141 Gl.enable `blend;
6142 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
6143 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6144 List.iter
6145 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
6146 List.iter (fun l ->
6147 if l.pageno = pageno
6148 then (
6149 let dx = float (l.pagedispx - l.pagex) in
6150 let dy = float (l.pagedispy - l.pagey) in
6151 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
6152 Raw.sets_float state.vraw ~pos:0
6153 [| x0+.dx; y0+.dy;
6154 x1+.dx; y1+.dy;
6155 x3+.dx; y3+.dy;
6156 x2+.dx; y2+.dy |];
6157 GlArray.vertex `two state.vraw;
6158 GlArray.draw_arrays `triangle_strip 0 4;
6160 ) state.layout
6161 ) rects
6163 Gl.disable `blend;
6166 let display () =
6167 GlClear.color (scalecolor2 conf.bgcolor);
6168 GlClear.clear [`color];
6169 List.iter drawpage state.layout;
6170 let rects =
6171 match state.mode with
6172 | LinkNav (Ltexact (pageno, linkno)) ->
6173 begin match getopaque pageno with
6174 | Some opaque ->
6175 let x0, y0, x1, y1 = getlinkrect opaque linkno in
6176 (pageno, 5, (
6177 float x0, float y0,
6178 float x1, float y0,
6179 float x1, float y1,
6180 float x0, float y1)
6181 ) :: state.rects
6182 | None -> state.rects
6184 | _ -> state.rects
6186 showrects rects;
6187 let rec postloop linkindexbase = function
6188 | l :: rest ->
6189 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
6190 postloop linkindexbase rest
6191 | [] -> ()
6193 showsel ();
6194 postloop 0 state.layout;
6195 state.uioh#display;
6196 begin match state.mstate with
6197 | Mzoomrect ((x0, y0), (x1, y1)) ->
6198 Gl.enable `blend;
6199 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
6200 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6201 filledrect (float x0) (float y0) (float x1) (float y1);
6202 Gl.disable `blend;
6203 | _ -> ()
6204 end;
6205 enttext ();
6206 scrollindicator ();
6207 Wsi.swapb ();
6210 let zoomrect x y x1 y1 =
6211 let x0 = min x x1
6212 and x1 = max x x1
6213 and y0 = min y y1 in
6214 gotoy (state.y + y0);
6215 state.anchor <- getanchor ();
6216 let zoom = (float state.w) /. float (x1 - x0) in
6217 let margin =
6218 match conf.fitmodel, conf.columns with
6219 | FitPage, Csplit _ ->
6220 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
6222 | _, _ ->
6223 let adjw = wadjsb state.winw in
6224 if state.w < adjw
6225 then (adjw - state.w) / 2
6226 else 0
6228 state.x <- (state.x + margin) - x0;
6229 setzoom zoom;
6230 Wsi.setcursor Wsi.CURSOR_INHERIT;
6231 state.mstate <- Mnone;
6234 let zoomblock x y =
6235 let g opaque l px py =
6236 match rectofblock opaque px py with
6237 | Some a ->
6238 let x0 = a.(0) -. 20. in
6239 let x1 = a.(1) +. 20. in
6240 let y0 = a.(2) -. 20. in
6241 let zoom = (float state.w) /. (x1 -. x0) in
6242 let pagey = getpagey l.pageno in
6243 gotoy_and_clear_text (pagey + truncate y0);
6244 state.anchor <- getanchor ();
6245 let margin = (state.w - l.pagew)/2 in
6246 state.x <- -truncate x0 - margin;
6247 setzoom zoom;
6248 None
6249 | None -> None
6251 match conf.columns with
6252 | Csplit _ ->
6253 showtext '!' "block zooming does not work properly in split columns mode"
6254 | _ -> onppundermouse g x y ()
6257 let scrollx x =
6258 let winw = wadjsb state.winw - 1 in
6259 let s = float x /. float winw in
6260 let destx = truncate (float (state.w + winw) *. s) in
6261 state.x <- winw - destx;
6262 gotoy_and_clear_text state.y;
6263 state.mstate <- Mscrollx;
6266 let scrolly y =
6267 let s = float y /. float state.winh in
6268 let desty = truncate (float (state.maxy - state.winh) *. s) in
6269 gotoy_and_clear_text desty;
6270 state.mstate <- Mscrolly;
6273 let viewmouse button down x y mask =
6274 match button with
6275 | n when (n == 4 || n == 5) && not down ->
6276 if Wsi.withctrl mask
6277 then (
6278 match state.mstate with
6279 | Mzoom (oldn, i) ->
6280 if oldn = n
6281 then (
6282 if i = 2
6283 then
6284 let incr =
6285 match n with
6286 | 5 ->
6287 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6288 | _ ->
6289 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6291 let zoom = conf.zoom -. incr in
6292 setzoom zoom;
6293 state.mstate <- Mzoom (n, 0);
6294 else
6295 state.mstate <- Mzoom (n, i+1);
6297 else state.mstate <- Mzoom (n, 0)
6299 | _ -> state.mstate <- Mzoom (n, 0)
6301 else (
6302 match state.autoscroll with
6303 | Some step -> setautoscrollspeed step (n=4)
6304 | None ->
6305 if conf.wheelbypage || conf.presentation
6306 then (
6307 if n = 4
6308 then prevpage ()
6309 else nextpage ()
6311 else
6312 let incr =
6313 if n = 4
6314 then -conf.scrollstep
6315 else conf.scrollstep
6317 let incr = incr * 2 in
6318 let y = clamp incr in
6319 gotoy_and_clear_text y
6322 | n when (n = 6 || n = 7) && not down && canpan () ->
6323 state.x <-
6324 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6325 gotoy_and_clear_text state.y
6327 | 1 when Wsi.withshift mask ->
6328 state.mstate <- Mnone;
6329 if not down
6330 then (
6331 match unproject x y with
6332 | Some (pageno, ux, uy) ->
6333 let cmd = Printf.sprintf
6334 "%s %s %d %d %d"
6335 conf.stcmd state.path pageno ux uy
6337 popen cmd []
6338 | None -> ()
6341 | 1 when Wsi.withctrl mask ->
6342 if down
6343 then (
6344 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6345 state.mstate <- Mpan (x, y)
6347 else
6348 state.mstate <- Mnone
6350 | 3 ->
6351 if down
6352 then (
6353 Wsi.setcursor Wsi.CURSOR_CYCLE;
6354 let p = (x, y) in
6355 state.mstate <- Mzoomrect (p, p)
6357 else (
6358 match state.mstate with
6359 | Mzoomrect ((x0, y0), _) ->
6360 if abs (x-x0) > 10 && abs (y - y0) > 10
6361 then zoomrect x0 y0 x y
6362 else (
6363 state.mstate <- Mnone;
6364 Wsi.setcursor Wsi.CURSOR_INHERIT;
6365 G.postRedisplay "kill accidental zoom rect";
6367 | _ ->
6368 Wsi.setcursor Wsi.CURSOR_INHERIT;
6369 state.mstate <- Mnone
6372 | 1 when x > state.winw - vscrollw () ->
6373 if down
6374 then
6375 let _, position, sh = state.uioh#scrollph in
6376 if y > truncate position && y < truncate (position +. sh)
6377 then state.mstate <- Mscrolly
6378 else scrolly y
6379 else
6380 state.mstate <- Mnone
6382 | 1 when y > state.winh - hscrollh () ->
6383 if down
6384 then
6385 let _, position, sw = state.uioh#scrollpw in
6386 if x > truncate position && x < truncate (position +. sw)
6387 then state.mstate <- Mscrollx
6388 else scrollx x
6389 else
6390 state.mstate <- Mnone
6392 | 1 when state.bzoom -> if not down then zoomblock x y
6394 | 1 ->
6395 let dest = if down then getunder x y else Unone in
6396 begin match dest with
6397 | Ulinkgoto _
6398 | Ulinkuri _
6399 | Uremote _ | Uremotedest _
6400 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6401 gotounder dest
6403 | Unone when down ->
6404 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6405 state.mstate <- Mpan (x, y);
6407 | Unone | Utext _ ->
6408 if down
6409 then (
6410 if conf.angle mod 360 = 0
6411 then (
6412 state.mstate <- Msel ((x, y), (x, y));
6413 G.postRedisplay "mouse select";
6416 else (
6417 match state.mstate with
6418 | Mnone -> ()
6420 | Mzoom _ | Mscrollx | Mscrolly ->
6421 state.mstate <- Mnone
6423 | Mzoomrect ((x0, y0), _) ->
6424 zoomrect x0 y0 x y
6426 | Mpan _ ->
6427 Wsi.setcursor Wsi.CURSOR_INHERIT;
6428 state.mstate <- Mnone
6430 | Msel ((x0, y0), (x1, y1)) ->
6431 let rec loop = function
6432 | [] -> ()
6433 | l :: rest ->
6434 let inside =
6435 let a0 = l.pagedispy in
6436 let a1 = a0 + l.pagevh in
6437 let b0 = l.pagedispx in
6438 let b1 = b0 + l.pagevw in
6439 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6440 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6442 if inside
6443 then
6444 match getopaque l.pageno with
6445 | Some opaque ->
6446 begin
6447 match Ne.pipe () with
6448 | Ne.Exn exn ->
6449 showtext '!'
6450 (Printf.sprintf
6451 "can not create sel pipe: %s"
6452 (exntos exn));
6453 | Ne.Res (r, w) ->
6454 let doclose what fd =
6455 Ne.clo fd (fun msg ->
6456 dolog "%s close failed: %s" what msg)
6459 popen conf.selcmd [r, 0; w, -1];
6460 copysel w opaque true;
6461 doclose "pipe/r" r;
6462 G.postRedisplay "copysel";
6463 with exn ->
6464 dolog "can not execute %S: %s"
6465 conf.selcmd (exntos exn);
6466 doclose "pipe/r" r;
6467 doclose "pipe/w" w;
6469 | None -> ()
6470 else loop rest
6472 loop state.layout;
6473 Wsi.setcursor Wsi.CURSOR_INHERIT;
6474 state.mstate <- Mnone;
6478 | _ -> ()
6481 let birdseyemouse button down x y mask
6482 (conf, leftx, _, hooverpageno, anchor) =
6483 match button with
6484 | 1 when down ->
6485 let rec loop = function
6486 | [] -> ()
6487 | l :: rest ->
6488 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6489 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6490 then (
6491 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6493 else loop rest
6495 loop state.layout
6496 | 3 -> ()
6497 | _ -> viewmouse button down x y mask
6500 let uioh = object
6501 method display = ()
6503 method key key mask =
6504 begin match state.mode with
6505 | Textentry textentry -> textentrykeyboard key mask textentry
6506 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6507 | View -> viewkeyboard key mask
6508 | LinkNav linknav -> linknavkeyboard key mask linknav
6509 end;
6510 state.uioh
6512 method button button bstate x y mask =
6513 begin match state.mode with
6514 | LinkNav _
6515 | View -> viewmouse button bstate x y mask
6516 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6517 | Textentry _ -> ()
6518 end;
6519 state.uioh
6521 method motion x y =
6522 begin match state.mode with
6523 | Textentry _ -> ()
6524 | View | Birdseye _ | LinkNav _ ->
6525 match state.mstate with
6526 | Mzoom _ | Mnone -> ()
6528 | Mpan (x0, y0) ->
6529 let dx = x - x0
6530 and dy = y0 - y in
6531 state.mstate <- Mpan (x, y);
6532 if canpan ()
6533 then state.x <- panbound (state.x + dx);
6534 let y = clamp dy in
6535 gotoy_and_clear_text y
6537 | Msel (a, _) ->
6538 state.mstate <- Msel (a, (x, y));
6539 G.postRedisplay "motion select";
6541 | Mscrolly ->
6542 let y = min state.winh (max 0 y) in
6543 scrolly y
6545 | Mscrollx ->
6546 let x = min state.winw (max 0 x) in
6547 scrollx x
6549 | Mzoomrect (p0, _) ->
6550 state.mstate <- Mzoomrect (p0, (x, y));
6551 G.postRedisplay "motion zoomrect";
6552 end;
6553 state.uioh
6555 method pmotion x y =
6556 begin match state.mode with
6557 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6558 let rec loop = function
6559 | [] ->
6560 if hooverpageno != -1
6561 then (
6562 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6563 G.postRedisplay "pmotion birdseye no hoover";
6565 | l :: rest ->
6566 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6567 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6568 then (
6569 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6570 G.postRedisplay "pmotion birdseye hoover";
6572 else loop rest
6574 loop state.layout
6576 | Textentry _ -> ()
6578 | LinkNav _
6579 | View ->
6580 match state.mstate with
6581 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6583 | Mnone ->
6584 updateunder x y;
6585 match conf.pax with
6586 | None -> ()
6587 | Some r ->
6588 let past, _, _ = !r in
6589 let now = now () in
6590 let delta = now -. past in
6591 if delta > 0.01
6592 then paxunder x y
6593 else r := (now, x, y)
6594 end;
6595 state.uioh
6597 method infochanged _ = ()
6599 method scrollph =
6600 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6601 let p, h =
6602 if maxy = 0
6603 then 0.0, float state.winh
6604 else scrollph state.y maxy
6606 vscrollw (), p, h
6608 method scrollpw =
6609 let winw = wadjsb state.winw in
6610 let fwinw = float winw in
6611 let sw =
6612 let sw = fwinw /. float state.w in
6613 let sw = fwinw *. sw in
6614 max sw (float conf.scrollh)
6616 let position =
6617 let maxx = state.w + winw in
6618 let x = winw - state.x in
6619 let percent = float x /. float maxx in
6620 (fwinw -. sw) *. percent
6622 hscrollh (), position, sw
6624 method modehash =
6625 let modename =
6626 match state.mode with
6627 | LinkNav _ -> "links"
6628 | Textentry _ -> "textentry"
6629 | Birdseye _ -> "birdseye"
6630 | View -> "view"
6632 findkeyhash conf modename
6634 method eformsgs = true
6635 end;;
6637 module Config =
6638 struct
6639 open Parser
6641 let fontpath = ref "";;
6643 module KeyMap =
6644 Map.Make (struct type t = (int * int) let compare = compare end);;
6646 let unent s =
6647 let l = String.length s in
6648 let b = Buffer.create l in
6649 unent b s 0 l;
6650 Buffer.contents b;
6653 let home =
6654 try Sys.getenv "HOME"
6655 with exn ->
6656 prerr_endline
6657 ("Can not determine home directory location: " ^ exntos exn);
6661 let modifier_of_string = function
6662 | "alt" -> Wsi.altmask
6663 | "shift" -> Wsi.shiftmask
6664 | "ctrl" | "control" -> Wsi.ctrlmask
6665 | "meta" -> Wsi.metamask
6666 | _ -> 0
6669 let key_of_string =
6670 let r = Str.regexp "-" in
6671 fun s ->
6672 let elems = Str.full_split r s in
6673 let f n k m =
6674 let g s =
6675 let m1 = modifier_of_string s in
6676 if m1 = 0
6677 then (Wsi.namekey s, m)
6678 else (k, m lor m1)
6679 in function
6680 | Str.Delim s when n land 1 = 0 -> g s
6681 | Str.Text s -> g s
6682 | Str.Delim _ -> (k, m)
6684 let rec loop n k m = function
6685 | [] -> (k, m)
6686 | x :: xs ->
6687 let k, m = f n k m x in
6688 loop (n+1) k m xs
6690 loop 0 0 0 elems
6693 let keys_of_string =
6694 let r = Str.regexp "[ \t]" in
6695 fun s ->
6696 let elems = Str.split r s in
6697 List.map key_of_string elems
6700 let copykeyhashes c =
6701 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6704 let config_of c attrs =
6705 let apply c k v =
6707 match k with
6708 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6709 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6710 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6711 | "preload" -> { c with preload = bool_of_string v }
6712 | "page-bias" -> { c with pagebias = int_of_string v }
6713 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6714 | "horizontal-scroll-step" ->
6715 { c with hscrollstep = max (int_of_string v) 1 }
6716 | "auto-scroll-step" ->
6717 { c with autoscrollstep = max 0 (int_of_string v) }
6718 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6719 | "crop-hack" -> { c with crophack = bool_of_string v }
6720 | "throttle" ->
6721 let mw =
6722 match String.lowercase v with
6723 | "true" -> Some infinity
6724 | "false" -> None
6725 | f -> Some (float_of_string f)
6727 { c with maxwait = mw}
6728 | "highlight-links" -> { c with hlinks = bool_of_string v }
6729 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6730 | "vertical-margin" ->
6731 { c with interpagespace = max 0 (int_of_string v) }
6732 | "zoom" ->
6733 let zoom = float_of_string v /. 100. in
6734 let zoom = max zoom 0.0 in
6735 { c with zoom = zoom }
6736 | "presentation" -> { c with presentation = bool_of_string v }
6737 | "rotation-angle" -> { c with angle = int_of_string v }
6738 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6739 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6740 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6741 | "proportional-display" ->
6742 let fm =
6743 if bool_of_string v
6744 then FitProportional
6745 else FitWidth
6747 { c with fitmodel = fm }
6748 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6749 | "pixmap-cache-size" ->
6750 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6751 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6752 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6753 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6754 | "persistent-location" -> { c with jumpback = bool_of_string v }
6755 | "background-color" -> { c with bgcolor = color_of_string v }
6756 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6757 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6758 | "mupdf-store-size" ->
6759 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6760 | "checkers" -> { c with checkers = bool_of_string v }
6761 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6762 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6763 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6764 | "uri-launcher" -> { c with urilauncher = unent v }
6765 | "path-launcher" -> { c with pathlauncher = unent v }
6766 | "color-space" -> { c with colorspace = CSTE.of_string v }
6767 | "invert-colors" -> { c with invert = bool_of_string v }
6768 | "brightness" -> { c with colorscale = float_of_string v }
6769 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6770 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
6771 | "columns" ->
6772 let (n, _, _) as nab = multicolumns_of_string v in
6773 if n < 0
6774 then { c with columns = Csplit (-n, [||]) }
6775 else { c with columns = Cmulti (nab, [||]) }
6776 | "birds-eye-columns" ->
6777 { c with beyecolumns = Some (max (int_of_string v) 2) }
6778 | "selection-command" -> { c with selcmd = unent v }
6779 | "synctex-command" -> { c with stcmd = unent v }
6780 | "pax-command" -> { c with paxcmd = unent v }
6781 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6782 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6783 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6784 | "use-pbo" -> { c with usepbo = bool_of_string v }
6785 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6786 | "horizontal-scrollbar-visible" ->
6787 let b =
6788 if bool_of_string v
6789 then c.scrollb lor scrollbhv
6790 else c.scrollb land (lnot scrollbhv)
6792 { c with scrollb = b }
6793 | "vertical-scrollbar-visible" ->
6794 let b =
6795 if bool_of_string v
6796 then c.scrollb lor scrollbvv
6797 else c.scrollb land (lnot scrollbvv)
6799 { c with scrollb = b }
6800 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6801 | "point-and-x" ->
6802 { c with pax =
6803 if bool_of_string v
6804 then Some (ref (0.0, 0, 0))
6805 else None }
6806 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
6807 | _ -> c
6808 with exn ->
6809 prerr_endline ("Error processing attribute (`" ^
6810 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6813 let rec fold c = function
6814 | [] -> c
6815 | (k, v) :: rest ->
6816 let c = apply c k v in
6817 fold c rest
6819 fold { c with keyhashes = copykeyhashes c } attrs;
6822 let fromstring f pos n v d =
6823 try f v
6824 with exn ->
6825 dolog "Error processing attribute (%S=%S) at %d\n%s"
6826 n v pos (exntos exn)
6831 let bookmark_of attrs =
6832 let rec fold title page rely visy = function
6833 | ("title", v) :: rest -> fold v page rely visy rest
6834 | ("page", v) :: rest -> fold title v rely visy rest
6835 | ("rely", v) :: rest -> fold title page v visy rest
6836 | ("visy", v) :: rest -> fold title page rely v rest
6837 | _ :: rest -> fold title page rely visy rest
6838 | [] -> title, page, rely, visy
6840 fold "invalid" "0" "0" "0" attrs
6843 let doc_of attrs =
6844 let rec fold path page rely pan visy = function
6845 | ("path", v) :: rest -> fold v page rely pan visy rest
6846 | ("page", v) :: rest -> fold path v rely pan visy rest
6847 | ("rely", v) :: rest -> fold path page v pan visy rest
6848 | ("pan", v) :: rest -> fold path page rely v visy rest
6849 | ("visy", v) :: rest -> fold path page rely pan v rest
6850 | _ :: rest -> fold path page rely pan visy rest
6851 | [] -> path, page, rely, pan, visy
6853 fold "" "0" "0" "0" "0" attrs
6856 let map_of attrs =
6857 let rec fold rs ls = function
6858 | ("out", v) :: rest -> fold v ls rest
6859 | ("in", v) :: rest -> fold rs v rest
6860 | _ :: rest -> fold ls rs rest
6861 | [] -> ls, rs
6863 fold "" "" attrs
6866 let setconf dst src =
6867 dst.scrollbw <- src.scrollbw;
6868 dst.scrollh <- src.scrollh;
6869 dst.icase <- src.icase;
6870 dst.preload <- src.preload;
6871 dst.pagebias <- src.pagebias;
6872 dst.verbose <- src.verbose;
6873 dst.scrollstep <- src.scrollstep;
6874 dst.maxhfit <- src.maxhfit;
6875 dst.crophack <- src.crophack;
6876 dst.autoscrollstep <- src.autoscrollstep;
6877 dst.maxwait <- src.maxwait;
6878 dst.hlinks <- src.hlinks;
6879 dst.underinfo <- src.underinfo;
6880 dst.interpagespace <- src.interpagespace;
6881 dst.zoom <- src.zoom;
6882 dst.presentation <- src.presentation;
6883 dst.angle <- src.angle;
6884 dst.cwinw <- src.cwinw;
6885 dst.cwinh <- src.cwinh;
6886 dst.savebmarks <- src.savebmarks;
6887 dst.memlimit <- src.memlimit;
6888 dst.fitmodel <- src.fitmodel;
6889 dst.texcount <- src.texcount;
6890 dst.sliceheight <- src.sliceheight;
6891 dst.thumbw <- src.thumbw;
6892 dst.jumpback <- src.jumpback;
6893 dst.bgcolor <- src.bgcolor;
6894 dst.tilew <- src.tilew;
6895 dst.tileh <- src.tileh;
6896 dst.mustoresize <- src.mustoresize;
6897 dst.checkers <- src.checkers;
6898 dst.aalevel <- src.aalevel;
6899 dst.trimmargins <- src.trimmargins;
6900 dst.trimfuzz <- src.trimfuzz;
6901 dst.urilauncher <- src.urilauncher;
6902 dst.colorspace <- src.colorspace;
6903 dst.invert <- src.invert;
6904 dst.colorscale <- src.colorscale;
6905 dst.redirectstderr <- src.redirectstderr;
6906 dst.ghyllscroll <- src.ghyllscroll;
6907 dst.columns <- src.columns;
6908 dst.beyecolumns <- src.beyecolumns;
6909 dst.selcmd <- src.selcmd;
6910 dst.updatecurs <- src.updatecurs;
6911 dst.pathlauncher <- src.pathlauncher;
6912 dst.keyhashes <- copykeyhashes src;
6913 dst.hfsize <- src.hfsize;
6914 dst.hscrollstep <- src.hscrollstep;
6915 dst.pgscale <- src.pgscale;
6916 dst.usepbo <- src.usepbo;
6917 dst.wheelbypage <- src.wheelbypage;
6918 dst.stcmd <- src.stcmd;
6919 dst.paxcmd <- src.paxcmd;
6920 dst.scrollb <- src.scrollb;
6921 dst.riani <- src.riani;
6922 dst.paxmark <- src.paxmark;
6923 dst.pax <-
6924 if src.pax = None
6925 then None
6926 else Some ((ref (0.0, 0, 0)));
6929 let get s =
6930 let h = Hashtbl.create 10 in
6931 let dc = { defconf with angle = defconf.angle } in
6932 let rec toplevel v t spos _ =
6933 match t with
6934 | Vdata | Vcdata | Vend -> v
6935 | Vopen ("llppconfig", _, closed) ->
6936 if closed
6937 then v
6938 else { v with f = llppconfig }
6939 | Vopen _ ->
6940 error "unexpected subelement at top level" s spos
6941 | Vclose _ -> error "unexpected close at top level" s spos
6943 and llppconfig v t spos _ =
6944 match t with
6945 | Vdata | Vcdata -> v
6946 | Vend -> error "unexpected end of input in llppconfig" s spos
6947 | Vopen ("defaults", attrs, closed) ->
6948 let c = config_of dc attrs in
6949 setconf dc c;
6950 if closed
6951 then v
6952 else { v with f = defaults }
6954 | Vopen ("ui-font", attrs, closed) ->
6955 let rec getsize size = function
6956 | [] -> size
6957 | ("size", v) :: rest ->
6958 let size =
6959 fromstring int_of_string spos "size" v fstate.fontsize in
6960 getsize size rest
6961 | l -> getsize size l
6963 fstate.fontsize <- getsize fstate.fontsize attrs;
6964 if closed
6965 then v
6966 else { v with f = uifont (Buffer.create 10) }
6968 | Vopen ("doc", attrs, closed) ->
6969 let pathent, spage, srely, span, svisy = doc_of attrs in
6970 let path = unent pathent
6971 and pageno = fromstring int_of_string spos "page" spage 0
6972 and rely = fromstring float_of_string spos "rely" srely 0.0
6973 and pan = fromstring int_of_string spos "pan" span 0
6974 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6975 let c = config_of dc attrs in
6976 let anchor = (pageno, rely, visy) in
6977 if closed
6978 then (Hashtbl.add h path (c, [], pan, anchor); v)
6979 else { v with f = doc path pan anchor c [] }
6981 | Vopen _ ->
6982 error "unexpected subelement in llppconfig" s spos
6984 | Vclose "llppconfig" -> { v with f = toplevel }
6985 | Vclose _ -> error "unexpected close in llppconfig" s spos
6987 and defaults v t spos _ =
6988 match t with
6989 | Vdata | Vcdata -> v
6990 | Vend -> error "unexpected end of input in defaults" s spos
6991 | Vopen ("keymap", attrs, closed) ->
6992 let modename =
6993 try List.assoc "mode" attrs
6994 with Not_found -> "global" in
6995 if closed
6996 then v
6997 else
6998 let ret keymap =
6999 let h = findkeyhash dc modename in
7000 KeyMap.iter (Hashtbl.replace h) keymap;
7001 defaults
7003 { v with f = pkeymap ret KeyMap.empty }
7005 | Vopen (_, _, _) ->
7006 error "unexpected subelement in defaults" s spos
7008 | Vclose "defaults" ->
7009 { v with f = llppconfig }
7011 | Vclose _ -> error "unexpected close in defaults" s spos
7013 and uifont b v t spos epos =
7014 match t with
7015 | Vdata | Vcdata ->
7016 Buffer.add_substring b s spos (epos - spos);
7018 | Vopen (_, _, _) ->
7019 error "unexpected subelement in ui-font" s spos
7020 | Vclose "ui-font" ->
7021 if emptystr !fontpath
7022 then fontpath := Buffer.contents b;
7023 { v with f = llppconfig }
7024 | Vclose _ -> error "unexpected close in ui-font" s spos
7025 | Vend -> error "unexpected end of input in ui-font" s spos
7027 and doc path pan anchor c bookmarks v t spos _ =
7028 match t with
7029 | Vdata | Vcdata -> v
7030 | Vend -> error "unexpected end of input in doc" s spos
7031 | Vopen ("bookmarks", _, closed) ->
7032 if closed
7033 then v
7034 else { v with f = pbookmarks path pan anchor c bookmarks }
7036 | Vopen ("keymap", attrs, closed) ->
7037 let modename =
7038 try List.assoc "mode" attrs
7039 with Not_found -> "global"
7041 if closed
7042 then v
7043 else
7044 let ret keymap =
7045 let h = findkeyhash c modename in
7046 KeyMap.iter (Hashtbl.replace h) keymap;
7047 doc path pan anchor c bookmarks
7049 { v with f = pkeymap ret KeyMap.empty }
7051 | Vopen (_, _, _) ->
7052 error "unexpected subelement in doc" s spos
7054 | Vclose "doc" ->
7055 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
7056 { v with f = llppconfig }
7058 | Vclose _ -> error "unexpected close in doc" s spos
7060 and pkeymap ret keymap v t spos _ =
7061 match t with
7062 | Vdata | Vcdata -> v
7063 | Vend -> error "unexpected end of input in keymap" s spos
7064 | Vopen ("map", attrs, closed) ->
7065 let r, l = map_of attrs in
7066 let kss = fromstring keys_of_string spos "in" r [] in
7067 let lss = fromstring keys_of_string spos "out" l [] in
7068 let keymap =
7069 match kss with
7070 | [] -> keymap
7071 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
7072 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
7074 if closed
7075 then { v with f = pkeymap ret keymap }
7076 else
7077 let f () = v in
7078 { v with f = skip "map" f }
7080 | Vopen _ ->
7081 error "unexpected subelement in keymap" s spos
7083 | Vclose "keymap" ->
7084 { v with f = ret keymap }
7086 | Vclose _ -> error "unexpected close in keymap" s spos
7088 and pbookmarks path pan anchor c bookmarks v t spos _ =
7089 match t with
7090 | Vdata | Vcdata -> v
7091 | Vend -> error "unexpected end of input in bookmarks" s spos
7092 | Vopen ("item", attrs, closed) ->
7093 let titleent, spage, srely, svisy = bookmark_of attrs in
7094 let page = fromstring int_of_string spos "page" spage 0
7095 and rely = fromstring float_of_string spos "rely" srely 0.0
7096 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7097 let bookmarks =
7098 (unent titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
7100 if closed
7101 then { v with f = pbookmarks path pan anchor c bookmarks }
7102 else
7103 let f () = v in
7104 { v with f = skip "item" f }
7106 | Vopen _ ->
7107 error "unexpected subelement in bookmarks" s spos
7109 | Vclose "bookmarks" ->
7110 { v with f = doc path pan anchor c bookmarks }
7112 | Vclose _ -> error "unexpected close in bookmarks" s spos
7114 and skip tag f v t spos _ =
7115 match t with
7116 | Vdata | Vcdata -> v
7117 | Vend ->
7118 error ("unexpected end of input in skipped " ^ tag) s spos
7119 | Vopen (tag', _, closed) ->
7120 if closed
7121 then v
7122 else
7123 let f' () = { v with f = skip tag f } in
7124 { v with f = skip tag' f' }
7125 | Vclose ctag ->
7126 if tag = ctag
7127 then f ()
7128 else error ("unexpected close in skipped " ^ tag) s spos
7131 parse { f = toplevel; accu = () } s;
7132 h, dc;
7135 let do_load f ic =
7137 let len = in_channel_length ic in
7138 let s = String.create len in
7139 really_input ic s 0 len;
7140 f s;
7141 with
7142 | Parse_error (msg, s, pos) ->
7143 let subs = subs s pos in
7144 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
7146 | exn ->
7147 failwith ("config load error: " ^ exntos exn)
7150 let defconfpath =
7151 let dir =
7153 let dir = Filename.concat home ".config" in
7154 if Sys.is_directory dir then dir else home
7155 with _ -> home
7157 Filename.concat dir "llpp.conf"
7160 let confpath = ref defconfpath;;
7162 let load1 f =
7163 if Sys.file_exists !confpath
7164 then
7165 match
7166 (try Some (open_in_bin !confpath)
7167 with exn ->
7168 prerr_endline
7169 ("Error opening configuration file `" ^ !confpath ^ "': " ^
7170 exntos exn);
7171 None
7173 with
7174 | Some ic ->
7175 let success =
7177 f (do_load get ic)
7178 with exn ->
7179 prerr_endline
7180 ("Error loading configuration from `" ^ !confpath ^ "': " ^
7181 exntos exn);
7182 false
7184 close_in ic;
7185 success
7187 | None -> false
7188 else
7189 f (Hashtbl.create 0, defconf)
7192 let load () =
7193 let f (h, dc) =
7194 let pc, pb, px, pa =
7196 let key =
7197 if emptystr state.origin
7198 then state.path
7199 else state.origin
7201 Hashtbl.find h (Filename.basename key)
7202 with Not_found -> dc, [], 0, emptyanchor
7204 setconf defconf dc;
7205 setconf conf pc;
7206 state.bookmarks <- pb;
7207 state.x <- px;
7208 if conf.jumpback
7209 then state.anchor <- pa;
7210 cbput state.hists.nav pa;
7211 true
7213 load1 f
7216 let add_attrs bb always dc c =
7217 let ob s a b =
7218 if always || a != b
7219 then Printf.bprintf bb "\n %s='%b'" s a
7220 and op s a b =
7221 if always || a <> b
7222 then Printf.bprintf bb "\n %s='%b'" s (a != None)
7223 and oi s a b =
7224 if always || a != b
7225 then Printf.bprintf bb "\n %s='%d'" s a
7226 and oI s a b =
7227 if always || a != b
7228 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
7229 and oz s a b =
7230 if always || a <> b
7231 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
7232 and oF s a b =
7233 if always || a <> b
7234 then Printf.bprintf bb "\n %s='%f'" s a
7235 and oc s a b =
7236 if always || a <> b
7237 then
7238 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
7239 and oC s a b =
7240 if always || a <> b
7241 then
7242 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
7243 and oR s a b =
7244 if always || a <> b
7245 then
7246 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
7247 and os s a b =
7248 if always || a <> b
7249 then
7250 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7251 and og s a b =
7252 if always || a <> b
7253 then
7254 match a with
7255 | Some (_N, _A, _B) ->
7256 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7257 | None ->
7258 match b with
7259 | None -> ()
7260 | _ ->
7261 Printf.bprintf bb "\n %s='none'" s
7262 and oW s a b =
7263 if always || a <> b
7264 then
7265 let v =
7266 match a with
7267 | None -> "false"
7268 | Some f ->
7269 if f = infinity
7270 then "true"
7271 else string_of_float f
7273 Printf.bprintf bb "\n %s='%s'" s v
7274 and oco s a b =
7275 if always || a <> b
7276 then
7277 match a with
7278 | Cmulti ((n, a, b), _) when n > 1 ->
7279 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7280 | Csplit (n, _) when n > 1 ->
7281 Printf.bprintf bb "\n %s='%d'" s ~-n
7282 | _ -> ()
7283 and obeco s a b =
7284 if always || a <> b
7285 then
7286 match a with
7287 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7288 | _ -> ()
7289 and oFm s a b =
7290 if always || a <> b
7291 then
7292 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7293 and oSv s a b m =
7294 if always || a <> b
7295 then
7296 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7297 and oPm s a b =
7298 if always || a <> b
7299 then
7300 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
7302 oi "width" c.cwinw dc.cwinw;
7303 oi "height" c.cwinh dc.cwinh;
7304 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7305 oi "scroll-handle-height" c.scrollh dc.scrollh;
7306 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7307 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7308 ob "case-insensitive-search" c.icase dc.icase;
7309 ob "preload" c.preload dc.preload;
7310 oi "page-bias" c.pagebias dc.pagebias;
7311 oi "scroll-step" c.scrollstep dc.scrollstep;
7312 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7313 ob "max-height-fit" c.maxhfit dc.maxhfit;
7314 ob "crop-hack" c.crophack dc.crophack;
7315 oW "throttle" c.maxwait dc.maxwait;
7316 ob "highlight-links" c.hlinks dc.hlinks;
7317 ob "under-cursor-info" c.underinfo dc.underinfo;
7318 oi "vertical-margin" c.interpagespace dc.interpagespace;
7319 oz "zoom" c.zoom dc.zoom;
7320 ob "presentation" c.presentation dc.presentation;
7321 oi "rotation-angle" c.angle dc.angle;
7322 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7323 oFm "fit-model" c.fitmodel dc.fitmodel;
7324 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7325 oi "tex-count" c.texcount dc.texcount;
7326 oi "slice-height" c.sliceheight dc.sliceheight;
7327 oi "thumbnail-width" c.thumbw dc.thumbw;
7328 ob "persistent-location" c.jumpback dc.jumpback;
7329 oc "background-color" c.bgcolor dc.bgcolor;
7330 oi "tile-width" c.tilew dc.tilew;
7331 oi "tile-height" c.tileh dc.tileh;
7332 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7333 ob "checkers" c.checkers dc.checkers;
7334 oi "aalevel" c.aalevel dc.aalevel;
7335 ob "trim-margins" c.trimmargins dc.trimmargins;
7336 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7337 os "uri-launcher" c.urilauncher dc.urilauncher;
7338 os "path-launcher" c.pathlauncher dc.pathlauncher;
7339 oC "color-space" c.colorspace dc.colorspace;
7340 ob "invert-colors" c.invert dc.invert;
7341 oF "brightness" c.colorscale dc.colorscale;
7342 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7343 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7344 oco "columns" c.columns dc.columns;
7345 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7346 os "selection-command" c.selcmd dc.selcmd;
7347 os "synctex-command" c.stcmd dc.stcmd;
7348 os "pax-command" c.paxcmd dc.paxcmd;
7349 ob "update-cursor" c.updatecurs dc.updatecurs;
7350 oi "hint-font-size" c.hfsize dc.hfsize;
7351 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7352 oF "page-scroll-scale" c.pgscale dc.pgscale;
7353 ob "use-pbo" c.usepbo dc.usepbo;
7354 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7355 ob "remote-in-a-new-instance" c.riani dc.riani;
7356 op "point-and-x" c.pax dc.pax;
7357 oPm "point-and-x-mark" c.paxmark dc.paxmark;
7360 let keymapsbuf always dc c =
7361 let bb = Buffer.create 16 in
7362 let rec loop = function
7363 | [] -> ()
7364 | (modename, h) :: rest ->
7365 let dh = findkeyhash dc modename in
7366 if always || h <> dh
7367 then (
7368 if Hashtbl.length h > 0
7369 then (
7370 if Buffer.length bb > 0
7371 then Buffer.add_char bb '\n';
7372 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7373 Hashtbl.iter (fun i o ->
7374 let isdifferent = always ||
7376 let dO = Hashtbl.find dh i in
7377 dO <> o
7378 with Not_found -> true
7380 if isdifferent
7381 then
7382 let addkm (k, m) =
7383 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7384 if Wsi.withalt m then Buffer.add_string bb "alt-";
7385 if Wsi.withshift m then Buffer.add_string bb "shift-";
7386 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7387 Buffer.add_string bb (Wsi.keyname k);
7389 let addkms l =
7390 let rec loop = function
7391 | [] -> ()
7392 | km :: [] -> addkm km
7393 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7395 loop l
7397 Buffer.add_string bb "<map in='";
7398 addkm i;
7399 match o with
7400 | KMinsrt km ->
7401 Buffer.add_string bb "' out='";
7402 addkm km;
7403 Buffer.add_string bb "'/>\n"
7405 | KMinsrl kms ->
7406 Buffer.add_string bb "' out='";
7407 addkms kms;
7408 Buffer.add_string bb "'/>\n"
7410 | KMmulti (ins, kms) ->
7411 Buffer.add_char bb ' ';
7412 addkms ins;
7413 Buffer.add_string bb "' out='";
7414 addkms kms;
7415 Buffer.add_string bb "'/>\n"
7416 ) h;
7417 Buffer.add_string bb "</keymap>";
7420 loop rest
7422 loop c.keyhashes;
7426 let save () =
7427 let uifontsize = fstate.fontsize in
7428 let bb = Buffer.create 32768 in
7429 let relx = float state.x /. float state.winw in
7430 let w, h, x =
7431 let cx w = truncate (relx *. float w) in
7432 List.fold_left
7433 (fun (w, h, x) ws ->
7434 match ws with
7435 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7436 | Wsi.MaxVert -> (w, conf.cwinh, x)
7437 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7439 (state.winw, state.winh, state.x) state.winstate
7441 conf.cwinw <- w;
7442 conf.cwinh <- h;
7443 let f (h, dc) =
7444 let dc = if conf.bedefault then conf else dc in
7445 Buffer.add_string bb "<llppconfig>\n";
7447 if nonemptystr !fontpath
7448 then
7449 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7450 uifontsize
7451 !fontpath
7452 else (
7453 if uifontsize <> 14
7454 then
7455 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7458 Buffer.add_string bb "<defaults";
7459 add_attrs bb true dc dc;
7460 let kb = keymapsbuf true dc dc in
7461 if Buffer.length kb > 0
7462 then (
7463 Buffer.add_string bb ">\n";
7464 Buffer.add_buffer bb kb;
7465 Buffer.add_string bb "\n</defaults>\n";
7467 else Buffer.add_string bb "/>\n";
7469 let adddoc path pan anchor c bookmarks =
7470 if bookmarks == [] && c = dc && anchor = emptyanchor
7471 then ()
7472 else (
7473 Printf.bprintf bb "<doc path='%s'"
7474 (enent path 0 (String.length path));
7476 if anchor <> emptyanchor
7477 then (
7478 let n, rely, visy = anchor in
7479 Printf.bprintf bb " page='%d'" n;
7480 if rely > 1e-6
7481 then
7482 Printf.bprintf bb " rely='%f'" rely
7484 if abs_float visy > 1e-6
7485 then
7486 Printf.bprintf bb " visy='%f'" visy
7490 if pan != 0
7491 then Printf.bprintf bb " pan='%d'" pan;
7493 add_attrs bb false dc c;
7494 let kb = keymapsbuf false dc c in
7496 begin match bookmarks with
7497 | [] ->
7498 if Buffer.length kb > 0
7499 then (
7500 Buffer.add_string bb ">\n";
7501 Buffer.add_buffer bb kb;
7502 Buffer.add_string bb "\n</doc>\n";
7504 else Buffer.add_string bb "/>\n"
7505 | _ ->
7506 Buffer.add_string bb ">\n<bookmarks>\n";
7507 List.iter (fun (title, _, kind) ->
7508 begin match kind with
7509 | Oanchor (page, rely, visy) ->
7510 Printf.bprintf bb
7511 "<item title='%s' page='%d'"
7512 (enent title 0 (String.length title))
7513 page
7515 if rely > 1e-6
7516 then
7517 Printf.bprintf bb " rely='%f'" rely
7519 if abs_float visy > 1e-6
7520 then
7521 Printf.bprintf bb " visy='%f'" visy
7523 | Ouri _ | Oremote _ | Oremotedest _ | Olaunch _ ->
7524 failwith "unexpected link in bookmarks"
7525 end;
7526 Buffer.add_string bb "/>\n";
7527 ) bookmarks;
7528 Buffer.add_string bb "</bookmarks>";
7529 if Buffer.length kb > 0
7530 then (
7531 Buffer.add_string bb "\n";
7532 Buffer.add_buffer bb kb;
7534 Buffer.add_string bb "\n</doc>\n";
7535 end;
7539 let pan, conf =
7540 match state.mode with
7541 | Birdseye (c, pan, _, _, _) ->
7542 let beyecolumns =
7543 match conf.columns with
7544 | Cmulti ((c, _, _), _) -> Some c
7545 | Csingle _ -> None
7546 | Csplit _ -> None
7547 and columns =
7548 match c.columns with
7549 | Cmulti (c, _) -> Cmulti (c, [||])
7550 | Csingle _ -> Csingle [||]
7551 | Csplit _ -> failwith "quit from bird's eye while split"
7553 pan, { c with beyecolumns = beyecolumns; columns = columns }
7554 | _ -> x, conf
7556 let basename = Filename.basename
7557 (if emptystr state.origin then state.path else state.origin)
7559 adddoc basename pan (getanchor ())
7560 (let conf =
7561 let autoscrollstep =
7562 match state.autoscroll with
7563 | Some step -> step
7564 | None -> conf.autoscrollstep
7566 match state.mode with
7567 | Birdseye (bc, _, _, _, _) ->
7568 { conf with
7569 zoom = bc.zoom;
7570 presentation = bc.presentation;
7571 interpagespace = bc.interpagespace;
7572 maxwait = bc.maxwait;
7573 autoscrollstep = autoscrollstep }
7574 | _ -> { conf with autoscrollstep = autoscrollstep }
7575 in conf)
7576 (if conf.savebmarks then state.bookmarks else []);
7578 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7579 if basename <> path
7580 then adddoc path x anchor c bookmarks
7581 ) h;
7582 Buffer.add_string bb "</llppconfig>\n";
7583 true;
7585 if load1 f && Buffer.length bb > 0
7586 then
7588 let tmp = !confpath ^ ".tmp" in
7589 let oc = open_out_bin tmp in
7590 Buffer.output_buffer oc bb;
7591 close_out oc;
7592 Unix.rename tmp !confpath;
7593 with exn ->
7594 prerr_endline
7595 ("error while saving configuration: " ^ exntos exn)
7597 end;;
7599 let adderrmsg src msg =
7600 Buffer.add_string state.errmsgs msg;
7601 state.newerrmsgs <- true;
7602 G.postRedisplay src
7605 let adderrfmt src fmt =
7606 Format.kprintf (fun s -> adderrmsg src s) fmt;
7609 let ract cmds =
7610 let cl = splitatspace cmds in
7611 let scan s fmt f =
7612 try Scanf.sscanf s fmt f
7613 with exn ->
7614 adderrfmt "remote exec"
7615 "error processing '%S': %s\n" cmds (exntos exn)
7617 match cl with
7618 | "reload" :: [] -> reload ()
7619 | "goto" :: args :: [] ->
7620 scan args "%u %f %f"
7621 (fun pageno x y ->
7622 let cmd, _ = state.geomcmds in
7623 if emptystr cmd
7624 then gotopagexy pageno x y
7625 else
7626 let f prevf () =
7627 gotopagexy pageno x y;
7628 prevf ()
7630 state.reprf <- f state.reprf
7632 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7633 | "rect" :: args :: [] ->
7634 scan args "%u %u %f %f %f %f"
7635 (fun pageno color x0 y0 x1 y1 ->
7636 onpagerect pageno (fun w h ->
7637 let _,w1,h1,_ = getpagedim pageno in
7638 let sw = float w1 /. float w
7639 and sh = float h1 /. float h in
7640 let x0s = x0 *. sw
7641 and x1s = x1 *. sw
7642 and y0s = y0 *. sh
7643 and y1s = y1 *. sh in
7644 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7645 debugrect rect;
7646 state.rects <- (pageno, color, rect) :: state.rects;
7647 G.postRedisplay "rect";
7650 | "activatewin" :: [] -> Wsi.activatewin ()
7651 | "quit" :: [] -> raise Quit
7652 | _ ->
7653 adderrfmt "remote command"
7654 "error processing remote command: %S\n" cmds;
7657 let remote =
7658 let scratch = String.create 80 in
7659 let buf = Buffer.create 80 in
7660 fun fd ->
7661 let rec tempfr () =
7662 try Some (Unix.read fd scratch 0 80)
7663 with
7664 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7665 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7666 | exn -> raise exn
7668 match tempfr () with
7669 | None -> Some fd
7670 | Some n ->
7671 if n = 0
7672 then (
7673 Unix.close fd;
7674 if Buffer.length buf > 0
7675 then (
7676 let s = Buffer.contents buf in
7677 Buffer.clear buf;
7678 ract s;
7680 None
7682 else
7683 let rec eat ppos =
7684 let nlpos =
7686 let pos = String.index_from scratch ppos '\n' in
7687 if pos >= n then -1 else pos
7688 with Not_found -> -1
7690 if nlpos >= 0
7691 then (
7692 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7693 let s = Buffer.contents buf in
7694 Buffer.clear buf;
7695 ract s;
7696 eat (nlpos+1);
7698 else (
7699 Buffer.add_substring buf scratch ppos (n-ppos);
7700 Some fd
7702 in eat 0
7705 let remoteopen path =
7706 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7707 with exn ->
7708 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7709 None
7712 let () =
7713 let trimcachepath = ref "" in
7714 let rcmdpath = ref "" in
7715 selfexec := Sys.executable_name;
7716 Arg.parse
7717 (Arg.align
7718 [("-p", Arg.String (fun s -> state.password <- s),
7719 "<password> Set password");
7721 ("-f", Arg.String
7722 (fun s ->
7723 Config.fontpath := s;
7724 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7726 "<path> Set path to the user interface font");
7728 ("-c", Arg.String
7729 (fun s ->
7730 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7731 Config.confpath := s),
7732 "<path> Set path to the configuration file");
7734 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7735 "<path> Set path to the trim cache file");
7737 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7738 "<named-destination> Set named destination");
7740 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7741 ("-cxack", Arg.Set cxack, " Cut corners");
7743 ("-remote", Arg.String (fun s -> rcmdpath := s),
7744 "<path> Set path to the remote commands source");
7746 ("-origin", Arg.String (fun s -> state.origin <- s),
7747 "<original-path> Set original path");
7749 ("-v", Arg.Unit (fun () ->
7750 Printf.printf
7751 "%s\nconfiguration path: %s\n"
7752 (version ())
7753 Config.defconfpath
7755 exit 0), " Print version and exit");
7758 (fun s -> state.path <- s)
7759 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7761 if !wtmode
7762 then selfexec := !selfexec ^ " -wtmode";
7764 if emptystr state.path
7765 then (prerr_endline "file name missing"; exit 1);
7767 if not (Config.load ())
7768 then prerr_endline "failed to load configuration";
7770 let wsfd, winw, winh = Wsi.init (object
7771 val mutable m_hack = false
7772 method expose = if not m_hack then G.postRedisplay "expose"
7773 method visible = G.postRedisplay "visible"
7774 method display = m_hack <- false; display ()
7775 method reshape w h =
7776 m_hack <- w < state.winw && h < state.winh;
7777 reshape w h
7778 method mouse b d x y m = state.uioh <- state.uioh#button b d x y m
7779 method motion x y =
7780 state.mpos <- (x, y);
7781 state.uioh <- state.uioh#motion x y
7782 method pmotion x y =
7783 state.mpos <- (x, y);
7784 state.uioh <- state.uioh#pmotion x y
7785 method key k m =
7786 let mascm = m land (
7787 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7788 ) in
7789 match state.keystate with
7790 | KSnone ->
7791 let km = k, mascm in
7792 begin
7793 match
7794 let modehash = state.uioh#modehash in
7795 try Hashtbl.find modehash km
7796 with Not_found ->
7797 try Hashtbl.find (findkeyhash conf "global") km
7798 with Not_found -> KMinsrt (k, m)
7799 with
7800 | KMinsrt (k, m) -> keyboard k m
7801 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7802 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7804 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7805 List.iter (fun (k, m) -> keyboard k m) insrt;
7806 state.keystate <- KSnone
7807 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7808 state.keystate <- KSinto (keys, insrt)
7809 | _ ->
7810 state.keystate <- KSnone
7812 method enter x y =
7813 state.mpos <- (x, y);
7814 state.uioh <- state.uioh#pmotion x y
7815 method leave = state.mpos <- (-1, -1)
7816 method winstate wsl = state.winstate <- wsl; m_hack <- false
7817 method quit = raise Quit
7818 end) conf.cwinw conf.cwinh (platform = Posx) in
7820 state.wsfd <- wsfd;
7822 if not (
7823 List.exists GlMisc.check_extension
7824 [ "GL_ARB_texture_rectangle"
7825 ; "GL_EXT_texture_recangle"
7826 ; "GL_NV_texture_rectangle" ]
7828 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7830 if (
7831 let r = GlMisc.get_string `renderer in
7832 let p = "Mesa DRI Intel(" in
7833 let l = String.length p in
7834 String.length r > l && String.sub r 0 l = p
7836 then (
7837 defconf.sliceheight <- 1024;
7838 defconf.texcount <- 32;
7839 defconf.usepbo <- true;
7842 let cr, sw =
7843 match Ne.pipe () with
7844 | Ne.Exn exn ->
7845 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7846 exit 1
7847 | Ne.Res rw -> rw
7848 and sr, cw =
7849 match Ne.pipe () with
7850 | Ne.Exn exn ->
7851 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7852 exit 1
7853 | Ne.Res rw -> rw
7856 cloexec cr;
7857 cloexec sw;
7858 cloexec sr;
7859 cloexec cw;
7861 setcheckers conf.checkers;
7862 redirectstderr ();
7863 if conf.redirectstderr
7864 then
7865 at_exit (fun () ->
7866 let s = Buffer.contents state.errmsgs ^
7867 (match state.errfd with
7868 | Some fd ->
7869 let s = String.create (80*24) in
7870 let n =
7872 let r, _, _ = Unix.select [fd] [] [] 0.0 in
7873 if List.mem fd r
7874 then Unix.read fd s 0 (String.length s)
7875 else 0
7876 with _ -> 0
7878 if n = 0
7879 then ""
7880 else String.sub s 0 n
7881 | None -> ""
7884 try ignore (Unix.write state.stderr s 0 (String.length s))
7885 with exn -> print_endline (exntos exn)
7889 init (cr, cw) (
7890 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7891 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7892 !Config.fontpath, !trimcachepath,
7893 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7895 List.iter GlArray.enable [`texture_coord; `vertex];
7896 state.sr <- sr;
7897 state.sw <- sw;
7898 state.text <- "Opening " ^ (mbtoutf8 state.path);
7899 reshape winw winh;
7900 opendoc state.path state.password;
7901 state.uioh <- uioh;
7902 display ();
7903 Wsi.mapwin ();
7904 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7905 let optrfd =
7906 ref (
7907 if nonemptystr !rcmdpath
7908 then remoteopen !rcmdpath
7909 else None
7913 let rec loop deadline =
7914 let r =
7915 match state.errfd with
7916 | None -> [state.sr; state.wsfd]
7917 | Some fd -> [state.sr; state.wsfd; fd]
7919 let r =
7920 match !optrfd with
7921 | None -> r
7922 | Some fd -> fd :: r
7924 if state.redisplay
7925 then (
7926 state.redisplay <- false;
7927 display ();
7929 let timeout =
7930 let now = now () in
7931 if deadline > now
7932 then (
7933 if deadline = infinity
7934 then ~-.1.0
7935 else max 0.0 (deadline -. now)
7937 else 0.0
7939 let r, _, _ =
7940 try Unix.select r [] [] timeout
7941 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7943 begin match r with
7944 | [] ->
7945 state.ghyll None;
7946 let newdeadline =
7947 if state.ghyll == noghyll
7948 then
7949 match state.autoscroll with
7950 | Some step when step != 0 ->
7951 let y = state.y + step in
7952 let y =
7953 if y < 0
7954 then state.maxy
7955 else if y >= state.maxy then 0 else y
7957 gotoy y;
7958 if state.mode = View
7959 then state.text <- "";
7960 deadline +. 0.01
7961 | _ -> infinity
7962 else deadline +. 0.01
7964 loop newdeadline
7966 | l ->
7967 let rec checkfds = function
7968 | [] -> ()
7969 | fd :: rest when fd = state.sr ->
7970 let cmd = readcmd state.sr in
7971 act cmd;
7972 checkfds rest
7974 | fd :: rest when fd = state.wsfd ->
7975 Wsi.readresp fd;
7976 checkfds rest
7978 | fd :: rest when Some fd = !optrfd ->
7979 begin match remote fd with
7980 | None -> optrfd := remoteopen !rcmdpath;
7981 | opt -> optrfd := opt
7982 end;
7983 checkfds rest
7985 | fd :: rest ->
7986 let s = String.create 80 in
7987 let n = tempfailureretry (Unix.read fd s 0) 80 in
7988 if conf.redirectstderr
7989 then (
7990 Buffer.add_substring state.errmsgs s 0 n;
7991 state.newerrmsgs <- true;
7992 state.redisplay <- true;
7994 else (
7995 prerr_string (String.sub s 0 n);
7996 flush stderr;
7998 checkfds rest
8000 checkfds l;
8001 let newdeadline =
8002 let deadline1 =
8003 if deadline = infinity
8004 then now () +. 0.01
8005 else deadline
8007 match state.autoscroll with
8008 | Some step when step != 0 -> deadline1
8009 | _ -> if state.ghyll == noghyll then infinity else deadline1
8011 loop newdeadline
8012 end;
8015 loop infinity;
8016 with Quit ->
8017 Config.save ();