Fix renarrow
[llpp.git] / main.ml
blobc17fa7f35a4aacbd1ea0432fabba686dd59bca1c
1 open Utils;;
3 exception Quit;;
5 type under =
6 | Unone
7 | Ulinkuri of string
8 | Ulinkgoto of (int * int)
9 | Utext of facename
10 | Uunexpected of string
11 | Ulaunch of string
12 | Unamed of string
13 | Uremote of (string * int)
14 | Uremotedest of (string * string)
15 and facename = string;;
17 type mark =
18 | Mark_page
19 | Mark_block
20 | Mark_line
21 | Mark_word
24 type params = (angle * fitmodel * trimparams
25 * texcount * sliceheight * memsize
26 * colorspace * fontpath * trimcachepath
27 * haspbo)
28 and pageno = int
29 and width = int
30 and height = int
31 and leftx = int
32 and opaque = string
33 and recttype = int
34 and pixmapsize = int
35 and angle = int
36 and trimmargins = bool
37 and interpagespace = int
38 and texcount = int
39 and sliceheight = int
40 and gen = int
41 and top = float
42 and dtop = float
43 and fontpath = string
44 and trimcachepath = string
45 and memsize = int
46 and aalevel = int
47 and irect = (int * int * int * int)
48 and trimparams = (trimmargins * irect)
49 and colorspace = | Rgb | Bgr | Gray
50 and fitmodel = | FitWidth | FitProportional | FitPage
51 and haspbo = bool
54 type x = int
55 and y = int
56 and tilex = int
57 and tiley = int
58 and tileparams = (x * y * width * height * tilex * tiley)
61 type link =
62 | Lnotfound
63 | Lfound of int
64 and linkdir =
65 | LDfirst
66 | LDlast
67 | LDfirstvisible of (int * int * int)
68 | LDleft of int
69 | LDright of int
70 | LDdown of int
71 | LDup of int
74 type pagewithlinks =
75 | Pwlnotfound
76 | Pwl of int
79 type keymap =
80 | KMinsrt of key
81 | KMinsrl of key list
82 | KMmulti of key list * key list
83 and key = int * int
84 and keyhash = (key, keymap) Hashtbl.t
85 and keystate =
86 | KSnone
87 | KSinto of (key list * key list)
90 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
91 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
93 type pipe = (Unix.file_descr * Unix.file_descr);;
95 external init : pipe -> params -> unit = "ml_init";;
96 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
97 external copysel : Unix.file_descr -> opaque -> bool -> unit = "ml_copysel";;
98 external getpdimrect : int -> float array = "ml_getpdimrect";;
99 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
100 external markunder : string -> int -> int -> mark -> bool = "ml_markunder";;
101 external clearmark : string -> unit = "ml_clearmark";;
102 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
103 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
104 external measurestr : int -> string -> float = "ml_measure_string";;
105 external postprocess :
106 opaque -> int -> int -> int -> (int * string * int) -> int
107 = "ml_postprocess";;
108 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
109 external platform : unit -> platform = "ml_platform";;
110 external setaalevel : int -> unit = "ml_setaalevel";;
111 external realloctexts : int -> bool = "ml_realloctexts";;
112 external findlink : opaque -> linkdir -> link = "ml_findlink";;
113 external getlink : opaque -> int -> under = "ml_getlink";;
114 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
115 external getlinkcount : opaque -> int = "ml_getlinkcount";;
116 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
117 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
118 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
119 external freepbo : string -> unit = "ml_freepbo";;
120 external unmappbo : string -> unit = "ml_unmappbo";;
121 external pbousable : unit -> bool = "ml_pbo_usable";;
122 external unproject : opaque -> int -> int -> (int * int) option
123 = "ml_unproject";;
124 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
125 external rectofblock : opaque -> int -> int -> float array option
126 = "ml_rectofblock";;
127 external fz_version : unit -> string = "ml_fz_version";;
128 external begintiles : unit -> unit = "ml_begintiles";;
129 external endtiles : unit -> unit = "ml_endtiles";;
131 let platform_to_string = function
132 | Punknown -> "unknown"
133 | Plinux -> "Linux"
134 | Posx -> "OSX"
135 | Psun -> "Sun"
136 | Pfreebsd -> "FreeBSD"
137 | Pdragonflybsd -> "DragonflyBSD"
138 | Popenbsd -> "OpenBSD"
139 | Pnetbsd -> "NetBSD"
140 | Pcygwin -> "Cygwin"
143 let platform = platform ();;
145 let now = Unix.gettimeofday;;
147 let selfexec = ref "";;
149 let popen cmd fda =
150 if platform = Pcygwin
151 then (
152 let sh = "/bin/sh" in
153 let args = [|sh; "-c"; cmd|] in
154 let rec std si so se = function
155 | [] -> si, so, se
156 | (fd, 0) :: rest -> std fd so se rest
157 | (fd, -1) :: rest ->
158 Unix.set_close_on_exec fd;
159 std si so se rest
160 | (_, n) :: _ ->
161 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
163 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
164 ignore (Unix.create_process sh args si so se)
166 else popen cmd fda;
169 type mpos = int * int
170 and mstate =
171 | Msel of (mpos * mpos)
172 | Mpan of mpos
173 | Mscrolly | Mscrollx
174 | Mzoom of (int * int)
175 | Mzoomrect of (mpos * mpos)
176 | Mnone
179 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
180 and onkey = string -> int -> te
181 and ondone = string -> unit
182 and histcancel = unit -> unit
183 and onhist = ((histcmd -> string) * histcancel)
184 and histcmd = HCnext | HCprev | HCfirst | HClast
185 and cancelonempty = bool
186 and te =
187 | TEstop
188 | TEdone of string
189 | TEcont of string
190 | TEswitch of textentry
193 type 'a circbuf =
194 { store : 'a array
195 ; mutable rc : int
196 ; mutable wc : int
197 ; mutable len : int
201 let bound v minv maxv =
202 max minv (min maxv v);
205 let cbnew n v =
206 { store = Array.create n v
207 ; rc = 0
208 ; wc = 0
209 ; len = 0
213 let cbcap b = Array.length b.store;;
215 let cbput b v =
216 let cap = cbcap b in
217 b.store.(b.wc) <- v;
218 b.wc <- (b.wc + 1) mod cap;
219 b.rc <- b.wc;
220 b.len <- min (b.len + 1) cap;
223 let cbempty b = b.len = 0;;
225 let cbgetg b circular dir =
226 if cbempty b
227 then b.store.(0)
228 else
229 let rc = b.rc + dir in
230 let rc =
231 if circular
232 then (
233 if rc = -1
234 then b.len-1
235 else (
236 if rc >= b.len
237 then 0
238 else rc
241 else bound rc 0 (b.len-1)
243 b.rc <- rc;
244 b.store.(rc);
247 let cbget b = cbgetg b false;;
248 let cbgetc b = cbgetg b true;;
250 let drawstring size x y s =
251 Gl.enable `blend;
252 Gl.enable `texture_2d;
253 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
254 ignore (drawstr size x y s);
255 Gl.disable `blend;
256 Gl.disable `texture_2d;
259 let drawstring1 size x y s =
260 drawstr size x y s;
263 let drawstring2 size x y fmt =
264 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
267 type page =
268 { pageno : int
269 ; pagedimno : int
270 ; pagew : int
271 ; pageh : int
272 ; pagex : int
273 ; pagey : int
274 ; pagevw : int
275 ; pagevh : int
276 ; pagedispx : int
277 ; pagedispy : int
278 ; pagecol : int
282 let debugl l =
283 dolog "l %d dim=%d {" l.pageno l.pagedimno;
284 dolog " WxH %dx%d" l.pagew l.pageh;
285 dolog " vWxH %dx%d" l.pagevw l.pagevh;
286 dolog " pagex,y %d,%d" l.pagex l.pagey;
287 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
288 dolog " column %d" l.pagecol;
289 dolog "}";
292 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
293 dolog "rect {";
294 dolog " x0,y0=(% f, % f)" x0 y0;
295 dolog " x1,y1=(% f, % f)" x1 y1;
296 dolog " x2,y2=(% f, % f)" x2 y2;
297 dolog " x3,y3=(% f, % f)" x3 y3;
298 dolog "}";
301 type multicolumns = multicol * pagegeom
302 and singlecolumn = pagegeom
303 and splitcolumns = columncount * pagegeom
304 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
305 and multicol = columncount * covercount * covercount
306 and pdimno = int
307 and columncount = int
308 and covercount = int;;
310 type scrollb = int;;
311 let scrollbvv = 1;;
312 let scrollbhv = 2;;
314 type conf =
315 { mutable scrollbw : int
316 ; mutable scrollh : int
317 ; mutable scrollb : scrollb
318 ; mutable icase : bool
319 ; mutable preload : bool
320 ; mutable pagebias : int
321 ; mutable verbose : bool
322 ; mutable debug : bool
323 ; mutable scrollstep : int
324 ; mutable hscrollstep : int
325 ; mutable maxhfit : bool
326 ; mutable crophack : bool
327 ; mutable autoscrollstep : int
328 ; mutable maxwait : float option
329 ; mutable hlinks : bool
330 ; mutable underinfo : bool
331 ; mutable interpagespace : interpagespace
332 ; mutable zoom : float
333 ; mutable presentation : bool
334 ; mutable angle : angle
335 ; mutable cwinw : int
336 ; mutable cwinh : int
337 ; mutable savebmarks : bool
338 ; mutable fitmodel : fitmodel
339 ; mutable trimmargins : trimmargins
340 ; mutable trimfuzz : irect
341 ; mutable memlimit : memsize
342 ; mutable texcount : texcount
343 ; mutable sliceheight : sliceheight
344 ; mutable thumbw : width
345 ; mutable jumpback : bool
346 ; mutable bgcolor : (float * float * float)
347 ; mutable bedefault : bool
348 ; mutable tilew : int
349 ; mutable tileh : int
350 ; mutable mustoresize : memsize
351 ; mutable checkers : bool
352 ; mutable aalevel : int
353 ; mutable urilauncher : string
354 ; mutable pathlauncher : string
355 ; mutable colorspace : colorspace
356 ; mutable invert : bool
357 ; mutable colorscale : float
358 ; mutable redirectstderr : bool
359 ; mutable ghyllscroll : (int * int * int) option
360 ; mutable columns : columns
361 ; mutable beyecolumns : columncount option
362 ; mutable selcmd : string
363 ; mutable paxcmd : string
364 ; mutable updatecurs : bool
365 ; mutable keyhashes : (string * keyhash) list
366 ; mutable hfsize : int
367 ; mutable pgscale : float
368 ; mutable usepbo : bool
369 ; mutable wheelbypage : bool
370 ; mutable stcmd : string
371 ; mutable riani : bool
372 ; mutable pax : (float * int * int) ref option
373 ; mutable paxmark : mark
375 and columns =
376 | Csingle of singlecolumn
377 | Cmulti of multicolumns
378 | Csplit of splitcolumns
381 type anchor = pageno * top * dtop;;
383 type outline = string * int * anchor;;
385 type rect = float * float * float * float * float * float * float * float;;
387 type tile = opaque * pixmapsize * elapsed
388 and elapsed = float;;
389 type pagemapkey = pageno * gen;;
390 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
391 and row = int
392 and col = int;;
394 let emptyanchor = (0, 0.0, 0.0);;
396 type infochange = | Memused | Docinfo | Pdim;;
398 class type uioh = object
399 method display : unit
400 method key : int -> int -> uioh
401 method button : int -> bool -> int -> int -> int -> uioh
402 method motion : int -> int -> uioh
403 method pmotion : int -> int -> uioh
404 method infochanged : infochange -> unit
405 method scrollpw : (int * float * float)
406 method scrollph : (int * float * float)
407 method modehash : keyhash
408 method eformsgs : bool
409 end;;
411 type mode =
412 | Birdseye of (conf * leftx * pageno * pageno * anchor)
413 | Textentry of (textentry * onleave)
414 | View
415 | LinkNav of linktarget
416 and onleave = leavetextentrystatus -> unit
417 and leavetextentrystatus = | Cancel | Confirm
418 and helpitem = string * int * action
419 and action =
420 | Noaction
421 | Action of (uioh -> uioh)
422 and linktarget =
423 | Ltexact of (pageno * int)
424 | Ltgendir of int
427 let isbirdseye = function Birdseye _ -> true | _ -> false;;
428 let istextentry = function Textentry _ -> true | _ -> false;;
430 type currently =
431 | Idle
432 | Loading of (page * gen)
433 | Tiling of (
434 page * opaque * colorspace * angle * gen * col * row * width * height
436 | Outlining of outline list
439 let emptykeyhash = Hashtbl.create 0;;
440 let nouioh : uioh = object (self)
441 method display = ()
442 method key _ _ = self
443 method button _ _ _ _ _ = self
444 method motion _ _ = self
445 method pmotion _ _ = self
446 method infochanged _ = ()
447 method scrollpw = (0, nan, nan)
448 method scrollph = (0, nan, nan)
449 method modehash = emptykeyhash
450 method eformsgs = false
451 end;;
453 type state =
454 { mutable sr : Unix.file_descr
455 ; mutable sw : Unix.file_descr
456 ; mutable wsfd : Unix.file_descr
457 ; mutable errfd : Unix.file_descr option
458 ; mutable stderr : Unix.file_descr
459 ; mutable errmsgs : Buffer.t
460 ; mutable newerrmsgs : bool
461 ; mutable w : int
462 ; mutable x : int
463 ; mutable y : int
464 ; mutable anchor : anchor
465 ; mutable ranchors : (string * string * anchor * string) list
466 ; mutable maxy : int
467 ; mutable layout : page list
468 ; pagemap : (pagemapkey, opaque) Hashtbl.t
469 ; tilemap : (tilemapkey, tile) Hashtbl.t
470 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
471 ; mutable pdims : (pageno * width * height * leftx) list
472 ; mutable pagecount : int
473 ; mutable currently : currently
474 ; mutable mstate : mstate
475 ; mutable searchpattern : string
476 ; mutable rects : (pageno * recttype * rect) list
477 ; mutable rects1 : (pageno * recttype * rect) list
478 ; mutable text : string
479 ; mutable winstate : Wsi.winstate list
480 ; mutable mode : mode
481 ; mutable uioh : uioh
482 ; mutable outlines : outline array
483 ; mutable bookmarks : outline list
484 ; mutable path : string
485 ; mutable password : string
486 ; mutable nameddest : string
487 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
488 ; mutable memused : memsize
489 ; mutable gen : gen
490 ; mutable throttle : (page list * int * float) option
491 ; mutable autoscroll : int option
492 ; mutable ghyll : (int option -> unit)
493 ; mutable help : helpitem array
494 ; mutable docinfo : (int * string) list
495 ; mutable texid : GlTex.texture_id option
496 ; hists : hists
497 ; mutable prevzoom : (float * int)
498 ; mutable progress : float
499 ; mutable redisplay : bool
500 ; mutable mpos : mpos
501 ; mutable keystate : keystate
502 ; mutable glinks : bool
503 ; mutable prevcolumns : (columns * float) option
504 ; mutable winw : int
505 ; mutable winh : int
506 ; mutable reprf : (unit -> unit)
507 ; mutable origin : string
508 ; mutable roam : (unit -> unit)
509 ; mutable bzoom : bool
510 ; mutable traw : [`float] Raw.t
511 ; mutable vraw : [`float] Raw.t
513 and hists =
514 { pat : string circbuf
515 ; pag : string circbuf
516 ; nav : anchor circbuf
517 ; sel : string circbuf
521 let defconf =
522 { scrollbw = 7
523 ; scrollh = 12
524 ; scrollb = scrollbhv lor scrollbvv
525 ; icase = true
526 ; preload = true
527 ; pagebias = 0
528 ; verbose = false
529 ; debug = false
530 ; scrollstep = 24
531 ; hscrollstep = 24
532 ; maxhfit = true
533 ; crophack = false
534 ; autoscrollstep = 2
535 ; maxwait = None
536 ; hlinks = false
537 ; underinfo = false
538 ; interpagespace = 2
539 ; zoom = 1.0
540 ; presentation = false
541 ; angle = 0
542 ; cwinw = 900
543 ; cwinh = 900
544 ; savebmarks = true
545 ; fitmodel = FitProportional
546 ; trimmargins = false
547 ; trimfuzz = (0,0,0,0)
548 ; memlimit = 32 lsl 20
549 ; texcount = 256
550 ; sliceheight = 24
551 ; thumbw = 76
552 ; jumpback = true
553 ; bgcolor = (0.5, 0.5, 0.5)
554 ; bedefault = false
555 ; tilew = 2048
556 ; tileh = 2048
557 ; mustoresize = 256 lsl 20
558 ; checkers = true
559 ; aalevel = 8
560 ; urilauncher =
561 (match platform with
562 | Plinux | Pfreebsd | Pdragonflybsd
563 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
564 | Posx -> "open \"%s\""
565 | Pcygwin -> "cygstart \"%s\""
566 | Punknown -> "echo %s")
567 ; pathlauncher = "lp \"%s\""
568 ; selcmd =
569 (match platform with
570 | Plinux | Pfreebsd | Pdragonflybsd
571 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
572 | Posx -> "pbcopy"
573 | Pcygwin -> "wsel"
574 | Punknown -> "cat")
575 ; paxcmd = "cat"
576 ; colorspace = Rgb
577 ; invert = false
578 ; colorscale = 1.0
579 ; redirectstderr = false
580 ; ghyllscroll = None
581 ; columns = Csingle [||]
582 ; beyecolumns = None
583 ; updatecurs = false
584 ; hfsize = 12
585 ; pgscale = 1.0
586 ; usepbo = false
587 ; wheelbypage = false
588 ; stcmd = "echo SyncTex"
589 ; riani = false
590 ; pax = None
591 ; paxmark = Mark_word
592 ; keyhashes =
593 let mk n = (n, Hashtbl.create 1) in
594 [ mk "global"
595 ; mk "info"
596 ; mk "help"
597 ; mk "outline"
598 ; mk "listview"
599 ; mk "birdseye"
600 ; mk "textentry"
601 ; mk "links"
602 ; mk "view"
607 let wtmode = ref false;;
608 let cxack = ref false;;
610 let findkeyhash c name =
611 try List.assoc name c.keyhashes
612 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
615 let conf = { defconf with angle = defconf.angle };;
617 let pgscale h = truncate (float h *. conf.pgscale);;
619 type fontstate =
620 { mutable fontsize : int
621 ; mutable wwidth : float
622 ; mutable maxrows : int
626 let fstate =
627 { fontsize = 14
628 ; wwidth = nan
629 ; maxrows = -1
633 let geturl s =
634 let colonpos = try String.index s ':' with Not_found -> -1 in
635 let len = String.length s in
636 if colonpos >= 0 && colonpos + 3 < len
637 then (
638 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
639 then
640 let schemestartpos =
641 try String.rindex_from s colonpos ' '
642 with Not_found -> -1
644 let scheme =
645 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
647 match scheme with
648 | "http" | "ftp" | "mailto" ->
649 let epos =
650 try String.index_from s colonpos ' '
651 with Not_found -> len
653 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
654 | _ -> ""
655 else ""
657 else ""
660 let gotouri uri =
661 if emptystr conf.urilauncher
662 then print_endline uri
663 else (
664 let url = geturl uri in
665 if emptystr url
666 then Printf.eprintf "obtained empty url from uri %S" uri
667 else
668 let re = Str.regexp "%s" in
669 let command = Str.global_replace re url conf.urilauncher in
670 try popen command []
671 with exn ->
672 Printf.eprintf
673 "failed to execute `%s': %s\n" command (exntos exn);
674 flush stderr;
678 let version () =
679 Printf.sprintf "llpp version %s, fitz %s, ocaml %s (%s/%dbit)"
680 Help.version (fz_version ()) Sys.ocaml_version
681 (platform_to_string platform) Sys.word_size
684 let makehelp () =
685 let strings = version () :: "" :: Help.keys in
686 Array.of_list (
687 List.map (fun s ->
688 let url = geturl s in
689 if nonemptystr url
690 then (s, 0, Action (fun u -> gotouri url; u))
691 else (s, 0, Noaction)
692 ) strings);
695 let noghyll _ = ();;
696 let firstgeomcmds = "", [];;
697 let noreprf () = ();;
699 let state =
700 { sr = Unix.stdin
701 ; sw = Unix.stdin
702 ; wsfd = Unix.stdin
703 ; errfd = None
704 ; stderr = Unix.stderr
705 ; errmsgs = Buffer.create 0
706 ; newerrmsgs = false
707 ; x = 0
708 ; y = 0
709 ; w = 0
710 ; anchor = emptyanchor
711 ; ranchors = []
712 ; layout = []
713 ; maxy = max_int
714 ; tilelru = Queue.create ()
715 ; pagemap = Hashtbl.create 10
716 ; tilemap = Hashtbl.create 10
717 ; pdims = []
718 ; pagecount = 0
719 ; currently = Idle
720 ; mstate = Mnone
721 ; rects = []
722 ; rects1 = []
723 ; text = ""
724 ; mode = View
725 ; winstate = []
726 ; searchpattern = ""
727 ; outlines = [||]
728 ; bookmarks = []
729 ; path = ""
730 ; password = ""
731 ; nameddest = ""
732 ; geomcmds = firstgeomcmds
733 ; hists =
734 { nav = cbnew 10 emptyanchor
735 ; pat = cbnew 10 ""
736 ; pag = cbnew 10 ""
737 ; sel = cbnew 10 ""
739 ; memused = 0
740 ; gen = 0
741 ; throttle = None
742 ; autoscroll = None
743 ; ghyll = noghyll
744 ; help = makehelp ()
745 ; docinfo = []
746 ; texid = None
747 ; prevzoom = (1.0, 0)
748 ; progress = -1.0
749 ; uioh = nouioh
750 ; redisplay = true
751 ; mpos = (-1, -1)
752 ; keystate = KSnone
753 ; glinks = false
754 ; prevcolumns = None
755 ; winw = -1
756 ; winh = -1
757 ; reprf = noreprf
758 ; origin = ""
759 ; roam = (fun () -> ())
760 ; bzoom = false
761 ; traw = Raw.create_static `float 8
762 ; vraw = Raw.create_static `float 8
766 let hscrollh () =
767 if (conf.scrollb land scrollbhv = 0)
768 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
769 then 0
770 else conf.scrollbw
773 let vscrollw () =
774 if (conf.scrollb land scrollbvv = 0)
775 then 0
776 else conf.scrollbw
779 let wadjsb w = w - vscrollw ();;
781 let setfontsize n =
782 fstate.fontsize <- n;
783 fstate.wwidth <- measurestr fstate.fontsize "w";
784 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
787 let vlog fmt =
788 if conf.verbose
789 then
790 Printf.kprintf prerr_endline fmt
791 else
792 Printf.kprintf ignore fmt
795 let launchpath () =
796 if emptystr conf.pathlauncher
797 then print_endline state.path
798 else (
799 let re = Str.regexp "%s" in
800 let command = Str.global_replace re state.path conf.pathlauncher in
801 try popen command []
802 with exn ->
803 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
804 flush stderr;
808 module Ne = struct
809 type 'a t = | Res of 'a | Exn of exn;;
811 let pipe () =
812 try Res (Unix.pipe ())
813 with exn -> Exn exn
816 let clo fd f =
817 try tempfailureretry Unix.close fd
818 with exn -> f (exntos exn)
821 let dup fd =
822 try Res (tempfailureretry Unix.dup fd)
823 with exn -> Exn exn
826 let dup2 fd1 fd2 =
827 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
828 with exn -> Exn exn
830 end;;
832 let redirectstderr () =
833 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
834 if conf.redirectstderr
835 then
836 match Ne.pipe () with
837 | Ne.Exn exn ->
838 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
840 | Ne.Res (r, w) ->
841 begin match Ne.dup Unix.stderr with
842 | Ne.Exn exn ->
843 dolog "failed to dup stderr: %s" (exntos exn);
844 Ne.clo r (clofail "pipe/r");
845 Ne.clo w (clofail "pipe/w");
847 | Ne.Res dupstderr ->
848 begin match Ne.dup2 w Unix.stderr with
849 | Ne.Exn exn ->
850 dolog "failed to dup2 to stderr: %s" (exntos exn);
851 Ne.clo dupstderr (clofail "stderr duplicate");
852 Ne.clo r (clofail "redir pipe/r");
853 Ne.clo w (clofail "redir pipe/w");
855 | Ne.Res () ->
856 state.stderr <- dupstderr;
857 state.errfd <- Some r;
858 end;
860 else (
861 state.newerrmsgs <- false;
862 begin match state.errfd with
863 | Some fd ->
864 begin match Ne.dup2 state.stderr Unix.stderr with
865 | Ne.Exn exn ->
866 dolog "failed to dup2 original stderr: %s" (exntos exn)
867 | Ne.Res () ->
868 Ne.clo fd (clofail "dup of stderr");
869 state.errfd <- None;
870 end;
871 | None -> ()
872 end;
873 prerr_string (Buffer.contents state.errmsgs);
874 flush stderr;
875 Buffer.clear state.errmsgs;
879 module G =
880 struct
881 let postRedisplay who =
882 if conf.verbose
883 then prerr_endline ("redisplay for " ^ who);
884 state.redisplay <- true;
886 end;;
888 let getopaque pageno =
889 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
890 with Not_found -> None
893 let putopaque pageno opaque =
894 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
897 let pagetranslatepoint l x y =
898 let dy = y - l.pagedispy in
899 let y = dy + l.pagey in
900 let dx = x - l.pagedispx in
901 let x = dx + l.pagex in
902 (x, y);
905 let onppundermouse g x y d =
906 let rec f = function
907 | l :: rest ->
908 begin match getopaque l.pageno with
909 | Some opaque ->
910 let x0 = l.pagedispx in
911 let x1 = x0 + l.pagevw in
912 let y0 = l.pagedispy in
913 let y1 = y0 + l.pagevh in
914 if y >= y0 && y <= y1 && x >= x0 && x <= x1
915 then
916 let px, py = pagetranslatepoint l x y in
917 match g opaque l px py with
918 | Some res -> res
919 | None -> f rest
920 else f rest
921 | _ ->
922 f rest
924 | [] -> d
926 f state.layout
929 let getunder x y =
930 let g opaque l px py =
931 if state.bzoom
932 then (
933 match rectofblock opaque px py with
934 | Some a ->
935 let rect = (a.(0),a.(2),a.(1),a.(2),a.(1),a.(3),a.(0),a.(3)) in
936 state.rects <- [l.pageno, l.pageno mod 3, rect];
937 G.postRedisplay "getunder";
938 | None -> ()
940 match whatsunder opaque px py with
941 | Unone -> None
942 | under -> Some under
944 onppundermouse g x y Unone
947 let unproject x y =
948 let g opaque l x y =
949 match unproject opaque x y with
950 | Some (x, y) -> Some (Some (l.pageno, x, y))
951 | None -> None
953 onppundermouse g x y None;
956 let showtext c s =
957 state.text <- Printf.sprintf "%c%s" c s;
958 G.postRedisplay "showtext";
961 let paxunder x y =
962 let g opaque l px py =
963 if markunder opaque px py conf.paxmark
964 then (
965 Some (fun () ->
966 match getopaque l.pageno with
967 | None -> ()
968 | Some opaque ->
969 match Ne.pipe () with
970 | Ne.Exn exn ->
971 showtext '!'
972 (Printf.sprintf
973 "can not create mark pipe: %s"
974 (exntos exn));
975 | Ne.Res (r, w) ->
976 let doclose what fd =
977 Ne.clo fd (fun msg ->
978 dolog "%s close failed: %s" what msg)
981 popen conf.paxcmd [r, 0; w, -1];
982 copysel w opaque false;
983 doclose "pipe/r" r;
984 G.postRedisplay "paxunder";
985 with exn ->
986 dolog "can not execute %S: %s"
987 conf.paxcmd (exntos exn);
988 doclose "pipe/r" r;
989 doclose "pipe/w" w;
992 else None
994 G.postRedisplay "paxunder";
995 if conf.paxmark = Mark_page
996 then
997 List.iter (fun l ->
998 match getopaque l.pageno with
999 | None -> ()
1000 | Some opaque -> clearmark opaque) state.layout;
1001 state.roam <-
1002 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
1005 let selstring s =
1006 match Ne.pipe () with
1007 | Ne.Exn exn ->
1008 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
1009 | Ne.Res (r, w) ->
1010 let popened =
1011 try popen conf.selcmd [r, 0; w, -1]; true
1012 with exn ->
1013 showtext '!'
1014 (Printf.sprintf "failed to execute %s: %s"
1015 conf.selcmd (exntos exn));
1016 false
1018 let clo cap fd =
1019 Ne.clo fd (fun msg ->
1020 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
1023 if popened
1024 then (
1026 let l = String.length s in
1027 let n = tempfailureretry (Unix.write w s 0) l in
1028 if n != l
1029 then
1030 showtext '!'
1031 (Printf.sprintf
1032 "failed to write %d characters to sel pipe, wrote %d"
1035 with exn ->
1036 showtext '!'
1037 (Printf.sprintf "failed to write to sel pipe: %s"
1038 (exntos exn)
1041 else dolog "%s" s;
1042 clo "pipe/r" r;
1043 clo "pipe/w" w;
1046 let undertext = function
1047 | Unone -> "none"
1048 | Ulinkuri s -> s
1049 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
1050 | Utext s -> "font: " ^ s
1051 | Uunexpected s -> "unexpected: " ^ s
1052 | Ulaunch s -> "launch: " ^ s
1053 | Unamed s -> "named: " ^ s
1054 | Uremote (filename, pageno) ->
1055 Printf.sprintf "%s: page %d" filename (pageno+1)
1056 | Uremotedest (filename, destname) ->
1057 Printf.sprintf "%s: destination %S" filename destname
1060 let updateunder x y =
1061 match getunder x y with
1062 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
1063 | Ulinkuri uri ->
1064 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1065 Wsi.setcursor Wsi.CURSOR_INFO
1066 | Ulinkgoto (pageno, _) ->
1067 if conf.underinfo
1068 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
1069 Wsi.setcursor Wsi.CURSOR_INFO
1070 | Utext s ->
1071 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1072 Wsi.setcursor Wsi.CURSOR_TEXT
1073 | Uunexpected s ->
1074 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
1075 Wsi.setcursor Wsi.CURSOR_INHERIT
1076 | Ulaunch s ->
1077 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
1078 Wsi.setcursor Wsi.CURSOR_INHERIT
1079 | Unamed s ->
1080 if conf.underinfo then showtext 'n' ("amed: " ^ s);
1081 Wsi.setcursor Wsi.CURSOR_INHERIT
1082 | Uremote (filename, pageno) ->
1083 if conf.underinfo then showtext 'r'
1084 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
1085 Wsi.setcursor Wsi.CURSOR_INFO
1086 | Uremotedest (filename, destname) ->
1087 if conf.underinfo then showtext 'r'
1088 (Printf.sprintf "emote destination: %s (%S)" filename destname);
1089 Wsi.setcursor Wsi.CURSOR_INFO
1092 let showlinktype under =
1093 if conf.underinfo
1094 then
1095 match under with
1096 | Unone -> ()
1097 | under ->
1098 let s = undertext under in
1099 showtext ' ' s
1102 let addchar s c =
1103 let b = Buffer.create (String.length s + 1) in
1104 Buffer.add_string b s;
1105 Buffer.add_char b c;
1106 Buffer.contents b;
1109 module type TextEnumType =
1111 type t
1112 val name : string
1113 val names : string array
1114 end;;
1116 module TextEnumMake (Ten : TextEnumType) =
1117 struct
1118 let names = Ten.names;;
1119 let to_int (t : Ten.t) = Obj.magic t;;
1120 let to_string t = names.(to_int t);;
1121 let of_int n : Ten.t = Obj.magic n;;
1122 let of_string s =
1123 let rec find i =
1124 if i = Array.length names
1125 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
1126 else (
1127 if Ten.names.(i) = s
1128 then of_int i
1129 else find (i+1)
1131 in find 0;;
1132 end;;
1134 module CSTE = TextEnumMake (struct
1135 type t = colorspace;;
1136 let name = "colorspace";;
1137 let names = [|"rgb"; "bgr"; "gray"|];;
1138 end);;
1140 module MTE = TextEnumMake (struct
1141 type t = mark;;
1142 let name = "mark";;
1143 let names = [|"page"; "block"; "line"; "word"|];;
1144 end);;
1146 module FMTE = TextEnumMake (struct
1147 type t= fitmodel;;
1148 let name = "fitmodel";;
1149 let names = [|"width"; "proportional"; "page"|];;
1150 end);;
1152 let intentry_with_suffix text key =
1153 let c =
1154 if key >= 32 && key < 127
1155 then Char.chr key
1156 else '\000'
1158 match Char.lowercase c with
1159 | '0' .. '9' ->
1160 let text = addchar text c in
1161 TEcont text
1163 | 'k' | 'm' | 'g' ->
1164 let text = addchar text c in
1165 TEcont text
1167 | _ ->
1168 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1169 TEcont text
1172 let multicolumns_to_string (n, a, b) =
1173 if a = 0 && b = 0
1174 then Printf.sprintf "%d" n
1175 else Printf.sprintf "%d,%d,%d" n a b;
1178 let multicolumns_of_string s =
1180 (int_of_string s, 0, 0)
1181 with _ ->
1182 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1183 if a > 1 || b > 1
1184 then failwith "subtly broken"; (n, a, b)
1188 let readcmd fd =
1189 let s = "xxxx" in
1190 let n = tempfailureretry (Unix.read fd s 0) 4 in
1191 if n != 4 then error "incomplete read(len) = %d" n;
1192 let len = 0
1193 lor (Char.code s.[0] lsl 24)
1194 lor (Char.code s.[1] lsl 16)
1195 lor (Char.code s.[2] lsl 8)
1196 lor (Char.code s.[3] lsl 0)
1198 let s = String.create len in
1199 let n = tempfailureretry (Unix.read fd s 0) len in
1200 if n != len then error "incomplete read(data) %d vs %d" n len;
1204 let btod b = if b then 1 else 0;;
1206 let wcmd fmt =
1207 let b = Buffer.create 16 in
1208 Buffer.add_string b "llll";
1209 Printf.kbprintf
1210 (fun b ->
1211 let s = Buffer.contents b in
1212 let n = String.length s in
1213 let len = n - 4 in
1214 (* dolog "wcmd %S" (String.sub s 4 len); *)
1215 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1216 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1217 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1218 s.[3] <- Char.chr (len land 0xff);
1219 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1220 if n' != n then error "write failed %d vs %d" n' n;
1221 ) b fmt;
1224 let calcips h =
1225 let d = state.winh - h in
1226 max conf.interpagespace ((d + 1) / 2)
1229 let rowyh (c, coverA, coverB) b n =
1230 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1231 then
1232 let _, _, vy, (_, _, h, _) = b.(n) in
1233 (vy, h)
1234 else
1235 let n' = n - coverA in
1236 let d = n' mod c in
1237 let s = n - d in
1238 let e = min state.pagecount (s + c) in
1239 let rec find m miny maxh = if m = e then miny, maxh else
1240 let _, _, y, (_, _, h, _) = b.(m) in
1241 let miny = min miny y in
1242 let maxh = max maxh h in
1243 find (m+1) miny maxh
1244 in find s max_int 0
1247 let calcheight () =
1248 match conf.columns with
1249 | Cmulti ((_, _, _) as cl, b) ->
1250 if Array.length b > 0
1251 then
1252 let y, h = rowyh cl b (Array.length b - 1) in
1253 y + h + (if conf.presentation then calcips h else 0)
1254 else 0
1255 | Csingle b ->
1256 if Array.length b > 0
1257 then
1258 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1259 y + h + (if conf.presentation then calcips h else 0)
1260 else 0
1261 | Csplit (_, b) ->
1262 if Array.length b > 0
1263 then
1264 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1265 y + h
1266 else 0
1269 let getpageywh pageno =
1270 let pageno = bound pageno 0 (state.pagecount-1) in
1271 match conf.columns with
1272 | Csingle b ->
1273 if Array.length b = 0
1274 then 0, 0, 0
1275 else
1276 let (_, _, y, (_, w, h, _)) = b.(pageno) in
1277 let y =
1278 if conf.presentation
1279 then y - calcips h
1280 else y
1282 y, w, h
1283 | Cmulti (cl, b) ->
1284 if Array.length b = 0
1285 then 0, 0, 0
1286 else
1287 let y, h = rowyh cl b pageno in
1288 let (_, _, _, (_, w, _, _)) = b.(pageno) in
1289 let y =
1290 if conf.presentation
1291 then y - calcips h
1292 else y
1294 y, w, h
1295 | Csplit (c, b) ->
1296 if Array.length b = 0
1297 then 0, 0, 0
1298 else
1299 let n = pageno*c in
1300 let (_, _, y, (_, w, h, _)) = b.(n) in
1301 y, w / c, h
1304 let getpageyh pageno =
1305 let y,_,h = getpageywh pageno in
1306 y, h;
1309 let getpagedim pageno =
1310 let rec f ppdim l =
1311 match l with
1312 | (n, _, _, _) as pdim :: rest ->
1313 if n >= pageno
1314 then (if n = pageno then pdim else ppdim)
1315 else f pdim rest
1317 | [] -> ppdim
1319 f (-1, -1, -1, -1) state.pdims
1322 let getpagey pageno = fst (getpageyh pageno);;
1324 let nogeomcmds cmds =
1325 match cmds with
1326 | s, [] -> emptystr s
1327 | _ -> false
1330 let page_of_y y =
1331 let ((c, coverA, coverB) as cl), b =
1332 match conf.columns with
1333 | Csingle b -> (1, 0, 0), b
1334 | Cmulti (c, b) -> c, b
1335 | Csplit (_, b) -> (1, 0, 0), b
1337 if Array.length b = 0
1338 then -1
1339 else
1340 let rec bsearch nmin nmax =
1341 if nmin > nmax
1342 then bound nmin 0 (state.pagecount-1)
1343 else
1344 let n = (nmax + nmin) / 2 in
1345 let vy, h = rowyh cl b n in
1346 let y0, y1 =
1347 if conf.presentation
1348 then
1349 let ips = calcips h in
1350 let y0 = vy - ips in
1351 let y1 = vy + h + ips in
1352 y0, y1
1353 else (
1354 if n = 0
1355 then 0, vy + h + conf.interpagespace
1356 else
1357 let y0 = vy - conf.interpagespace in
1358 y0, y0 + h + conf.interpagespace
1361 if y >= y0 && y < y1
1362 then (
1363 if c = 1
1364 then n
1365 else (
1366 if n > coverA
1367 then
1368 if n < state.pagecount - coverB
1369 then ((n-coverA)/c)*c + coverA
1370 else n
1371 else n
1374 else (
1375 if y > y0
1376 then bsearch (n+1) nmax
1377 else bsearch nmin (n-1)
1380 bsearch 0 (state.pagecount-1);
1383 let layoutN ((columns, coverA, coverB), b) y sh =
1384 let sh = sh - (hscrollh ()) in
1385 let rec fold accu n =
1386 if n = Array.length b
1387 then accu
1388 else
1389 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1390 if (vy - y) > sh &&
1391 (n = coverA - 1
1392 || n = state.pagecount - coverB
1393 || (n - coverA) mod columns = columns - 1)
1394 then accu
1395 else
1396 let accu =
1397 if vy + h > y
1398 then
1399 let pagey = max 0 (y - vy) in
1400 let pagedispy = if pagey > 0 then 0 else vy - y in
1401 let pagedispx, pagex =
1402 let pdx =
1403 if n = coverA - 1 || n = state.pagecount - coverB
1404 then state.x + (wadjsb state.winw - w) / 2
1405 else dx + xoff + state.x
1407 if pdx < 0
1408 then 0, -pdx
1409 else pdx, 0
1411 let pagevw =
1412 let vw = wadjsb state.winw - pagedispx in
1413 let pw = w - pagex in
1414 min vw pw
1416 let pagevh = min (h - pagey) (sh - pagedispy) in
1417 if pagevw > 0 && pagevh > 0
1418 then
1419 let e =
1420 { pageno = n
1421 ; pagedimno = pdimno
1422 ; pagew = w
1423 ; pageh = h
1424 ; pagex = pagex
1425 ; pagey = pagey
1426 ; pagevw = pagevw
1427 ; pagevh = pagevh
1428 ; pagedispx = pagedispx
1429 ; pagedispy = pagedispy
1430 ; pagecol = 0
1433 e :: accu
1434 else
1435 accu
1436 else
1437 accu
1439 fold accu (n+1)
1441 if Array.length b = 0
1442 then []
1443 else List.rev (fold [] (page_of_y y))
1446 let layoutS (columns, b) y sh =
1447 let sh = sh - hscrollh () in
1448 let rec fold accu n =
1449 if n = Array.length b
1450 then accu
1451 else
1452 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1453 if (vy - y) > sh
1454 then accu
1455 else
1456 let accu =
1457 if vy + pageh > y
1458 then
1459 let x = xoff + state.x in
1460 let pagey = max 0 (y - vy) in
1461 let pagedispy = if pagey > 0 then 0 else vy - y in
1462 let pagedispx, pagex =
1463 if px = 0
1464 then (
1465 if x < 0
1466 then 0, -x
1467 else x, 0
1469 else (
1470 let px = px - x in
1471 if px < 0
1472 then -px, 0
1473 else 0, px
1476 let pagecolw = pagew/columns in
1477 let pagedispx =
1478 if pagecolw < state.winw
1479 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
1480 else pagedispx
1482 let pagevw =
1483 let vw = wadjsb state.winw - pagedispx in
1484 let pw = pagew - pagex in
1485 min vw pw
1487 let pagevw = min pagevw pagecolw in
1488 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1489 if pagevw > 0 && pagevh > 0
1490 then
1491 let e =
1492 { pageno = n/columns
1493 ; pagedimno = pdimno
1494 ; pagew = pagew
1495 ; pageh = pageh
1496 ; pagex = pagex
1497 ; pagey = pagey
1498 ; pagevw = pagevw
1499 ; pagevh = pagevh
1500 ; pagedispx = pagedispx
1501 ; pagedispy = pagedispy
1502 ; pagecol = n mod columns
1505 e :: accu
1506 else
1507 accu
1508 else
1509 accu
1511 fold accu (n+1)
1513 List.rev (fold [] 0)
1516 let layout y sh =
1517 if nogeomcmds state.geomcmds
1518 then
1519 match conf.columns with
1520 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1521 | Cmulti c -> layoutN c y sh
1522 | Csplit s -> layoutS s y sh
1523 else []
1526 let clamp incr =
1527 let y = state.y + incr in
1528 let y = max 0 y in
1529 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1533 let itertiles l f =
1534 let tilex = l.pagex mod conf.tilew in
1535 let tiley = l.pagey mod conf.tileh in
1537 let col = l.pagex / conf.tilew in
1538 let row = l.pagey / conf.tileh in
1540 let rec rowloop row y0 dispy h =
1541 if h = 0
1542 then ()
1543 else (
1544 let dh = conf.tileh - y0 in
1545 let dh = min h dh in
1546 let rec colloop col x0 dispx w =
1547 if w = 0
1548 then ()
1549 else (
1550 let dw = conf.tilew - x0 in
1551 let dw = min w dw in
1553 f col row dispx dispy x0 y0 dw dh;
1554 colloop (col+1) 0 (dispx+dw) (w-dw)
1557 colloop col tilex l.pagedispx l.pagevw;
1558 rowloop (row+1) 0 (dispy+dh) (h-dh)
1561 if l.pagevw > 0 && l.pagevh > 0
1562 then rowloop row tiley l.pagedispy l.pagevh;
1565 let gettileopaque l col row =
1566 let key =
1567 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1569 try Some (Hashtbl.find state.tilemap key)
1570 with Not_found -> None
1573 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1574 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1575 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1578 let filledrect x0 y0 x1 y1 =
1579 GlArray.disable `texture_coord;
1580 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
1581 GlArray.vertex `two state.vraw;
1582 GlArray.draw_arrays `triangle_strip 0 4;
1583 GlArray.enable `texture_coord;
1586 let linerect x0 y0 x1 y1 =
1587 GlArray.disable `texture_coord;
1588 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
1589 GlArray.vertex `two state.vraw;
1590 GlArray.draw_arrays `line_loop 0 4;
1591 GlArray.enable `texture_coord;
1594 let drawtiles l color =
1595 GlDraw.color color;
1596 begintiles ();
1597 let f col row x y tilex tiley w h =
1598 match gettileopaque l col row with
1599 | Some (opaque, _, t) ->
1600 let params = x, y, w, h, tilex, tiley in
1601 if conf.invert
1602 then (
1603 Gl.enable `blend;
1604 GlFunc.blend_func `zero `one_minus_src_color;
1606 drawtile params opaque;
1607 if conf.invert
1608 then Gl.disable `blend;
1609 if conf.debug
1610 then (
1611 endtiles ();
1612 let s = Printf.sprintf
1613 "%d[%d,%d] %f sec"
1614 l.pageno col row t
1616 let w = measurestr fstate.fontsize s in
1617 GlDraw.color (0.0, 0.0, 0.0);
1618 filledrect (float (x-2))
1619 (float (y-2))
1620 (float (x+2) +. w)
1621 (float (y + fstate.fontsize + 2));
1622 GlDraw.color (1.0, 1.0, 1.0);
1623 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1624 begintiles ();
1627 | None ->
1628 endtiles ();
1629 let w =
1630 let lw = wadjsb state.winw - x in
1631 min lw w
1632 and h =
1633 let lh = state.winh - y in
1634 min lh h
1636 begin match state.texid with
1637 | Some id ->
1638 Gl.enable `texture_2d;
1639 GlTex.bind_texture `texture_2d id;
1640 let x0 = float x
1641 and y0 = float y
1642 and x1 = float (x+w)
1643 and y1 = float (y+h) in
1645 let tw = float w /. 16.0
1646 and th = float h /. 16.0 in
1647 let tx0 = float tilex /. 16.0
1648 and ty0 = float tiley /. 16.0 in
1649 let tx1 = tx0 +. tw
1650 and ty1 = ty0 +. th in
1651 Raw.sets_float state.vraw ~pos:0
1652 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
1653 Raw.sets_float state.traw ~pos:0
1654 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
1655 GlArray.vertex `two state.vraw;
1656 GlArray.tex_coord `two state.traw;
1657 GlArray.draw_arrays `triangle_strip 0 4;
1659 | None ->
1660 GlDraw.color (1.0, 1.0, 1.0);
1661 filledrect (float x) (float y) (float (x+w)) (float (y+h));
1662 end;
1663 if w > 128 && h > fstate.fontsize + 10
1664 then (
1665 GlDraw.color (0.0, 0.0, 0.0);
1666 let c, r =
1667 if conf.verbose
1668 then (col*conf.tilew, row*conf.tileh)
1669 else col, row
1671 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1673 GlDraw.color color;
1674 begintiles ();
1676 itertiles l f;
1677 endtiles ();
1680 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1682 let tilevisible1 l x y =
1683 let ax0 = l.pagex
1684 and ax1 = l.pagex + l.pagevw
1685 and ay0 = l.pagey
1686 and ay1 = l.pagey + l.pagevh in
1688 let bx0 = x
1689 and by0 = y in
1690 let bx1 = min (bx0 + conf.tilew) l.pagew
1691 and by1 = min (by0 + conf.tileh) l.pageh in
1693 let rx0 = max ax0 bx0
1694 and ry0 = max ay0 by0
1695 and rx1 = min ax1 bx1
1696 and ry1 = min ay1 by1 in
1698 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1699 nonemptyintersection
1702 let tilevisible layout n x y =
1703 let rec findpageinlayout m = function
1704 | l :: rest when l.pageno = n ->
1705 tilevisible1 l x y || (
1706 match conf.columns with
1707 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1708 | _ -> false
1710 | _ :: rest -> findpageinlayout 0 rest
1711 | [] -> false
1713 findpageinlayout 0 layout;
1716 let tileready l x y =
1717 tilevisible1 l x y &&
1718 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1721 let tilepage n p layout =
1722 let rec loop = function
1723 | l :: rest ->
1724 if l.pageno = n
1725 then
1726 let f col row _ _ _ _ _ _ =
1727 if state.currently = Idle
1728 then
1729 match gettileopaque l col row with
1730 | Some _ -> ()
1731 | None ->
1732 let x = col*conf.tilew
1733 and y = row*conf.tileh in
1734 let w =
1735 let w = l.pagew - x in
1736 min w conf.tilew
1738 let h =
1739 let h = l.pageh - y in
1740 min h conf.tileh
1742 let pbo =
1743 if conf.usepbo
1744 then getpbo w h conf.colorspace
1745 else "0"
1747 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1748 state.currently <-
1749 Tiling (
1750 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1751 conf.tilew, conf.tileh
1754 itertiles l f;
1755 else
1756 loop rest
1758 | [] -> ()
1760 if nogeomcmds state.geomcmds
1761 then loop layout;
1764 let preloadlayout y =
1765 let y = if y < state.winh then 0 else y - state.winh in
1766 let h = state.winh*3 in
1767 layout y h;
1770 let load pages =
1771 let rec loop pages =
1772 if state.currently != Idle
1773 then ()
1774 else
1775 match pages with
1776 | l :: rest ->
1777 begin match getopaque l.pageno with
1778 | None ->
1779 wcmd "page %d %d" l.pageno l.pagedimno;
1780 state.currently <- Loading (l, state.gen);
1781 | Some opaque ->
1782 tilepage l.pageno opaque pages;
1783 loop rest
1784 end;
1785 | _ -> ()
1787 if nogeomcmds state.geomcmds
1788 then loop pages
1791 let preload pages =
1792 load pages;
1793 if conf.preload && state.currently = Idle
1794 then load (preloadlayout state.y);
1797 let layoutready layout =
1798 let rec fold all ls =
1799 all && match ls with
1800 | l :: rest ->
1801 let seen = ref false in
1802 let allvisible = ref true in
1803 let foo col row _ _ _ _ _ _ =
1804 seen := true;
1805 allvisible := !allvisible &&
1806 begin match gettileopaque l col row with
1807 | Some _ -> true
1808 | None -> false
1811 itertiles l foo;
1812 fold (!seen && !allvisible) rest
1813 | [] -> true
1815 let alltilesvisible = fold true layout in
1816 alltilesvisible;
1819 let gotoy y =
1820 let y = bound y 0 state.maxy in
1821 let y, layout, proceed =
1822 match conf.maxwait with
1823 | Some time when state.ghyll == noghyll ->
1824 begin match state.throttle with
1825 | None ->
1826 let layout = layout y state.winh in
1827 let ready = layoutready layout in
1828 if not ready
1829 then (
1830 load layout;
1831 state.throttle <- Some (layout, y, now ());
1833 else G.postRedisplay "gotoy showall (None)";
1834 y, layout, ready
1835 | Some (_, _, started) ->
1836 let dt = now () -. started in
1837 if dt > time
1838 then (
1839 state.throttle <- None;
1840 let layout = layout y state.winh in
1841 load layout;
1842 G.postRedisplay "maxwait";
1843 y, layout, true
1845 else -1, [], false
1848 | _ ->
1849 let layout = layout y state.winh in
1850 if not !wtmode || layoutready layout
1851 then G.postRedisplay "gotoy ready";
1852 y, layout, true
1854 if proceed
1855 then (
1856 state.y <- y;
1857 state.layout <- layout;
1858 begin match state.mode with
1859 | LinkNav (Ltexact (pageno, linkno)) ->
1860 let rec loop = function
1861 | [] ->
1862 state.mode <- LinkNav (Ltgendir 0)
1863 | l :: _ when l.pageno = pageno ->
1864 begin match getopaque pageno with
1865 | None ->
1866 state.mode <- LinkNav (Ltgendir 0)
1867 | Some opaque ->
1868 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1869 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1870 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1871 then state.mode <- LinkNav (Ltgendir 0)
1873 | _ :: rest -> loop rest
1875 loop layout
1876 | _ -> ()
1877 end;
1878 begin match state.mode with
1879 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1880 if not (pagevisible layout pageno)
1881 then (
1882 match state.layout with
1883 | [] -> ()
1884 | l :: _ ->
1885 state.mode <- Birdseye (
1886 conf, leftx, l.pageno, hooverpageno, anchor
1889 | LinkNav (Ltgendir dir as lt) ->
1890 let linknav =
1891 let rec loop = function
1892 | [] -> lt
1893 | l :: rest ->
1894 match getopaque l.pageno with
1895 | None -> loop rest
1896 | Some opaque ->
1897 let link =
1898 let ld =
1899 if dir = 0
1900 then LDfirstvisible (l.pagex, l.pagey, dir)
1901 else (
1902 if dir > 0 then LDfirst else LDlast
1905 findlink opaque ld
1907 match link with
1908 | Lnotfound -> loop rest
1909 | Lfound n ->
1910 showlinktype (getlink opaque n);
1911 Ltexact (l.pageno, n)
1913 loop state.layout
1915 state.mode <- LinkNav linknav
1916 | _ -> ()
1917 end;
1918 preload layout;
1920 state.ghyll <- noghyll;
1921 if conf.updatecurs
1922 then (
1923 let mx, my = state.mpos in
1924 updateunder mx my;
1928 let conttiling pageno opaque =
1929 tilepage pageno opaque
1930 (if conf.preload then preloadlayout state.y else state.layout)
1933 let gotoy_and_clear_text y =
1934 if not conf.verbose then state.text <- "";
1935 gotoy y;
1938 let getanchor1 l =
1939 let top =
1940 let coloff = l.pagecol * l.pageh in
1941 float (l.pagey + coloff) /. float l.pageh
1943 let dtop =
1944 if l.pagedispy = 0
1945 then
1947 else (
1948 if conf.presentation
1949 then float l.pagedispy /. float (calcips l.pageh)
1950 else float l.pagedispy /. float conf.interpagespace
1953 (l.pageno, top, dtop)
1956 let getanchor () =
1957 match state.layout with
1958 | l :: _ -> getanchor1 l
1959 | [] ->
1960 let n = page_of_y state.y in
1961 if n = -1
1962 then state.anchor
1963 else
1964 let y, h = getpageyh n in
1965 let dy = y - state.y in
1966 let dtop =
1967 if conf.presentation
1968 then
1969 let ips = calcips h in
1970 float (dy + ips) /. float ips
1971 else
1972 float dy /. float conf.interpagespace
1974 (n, 0.0, dtop)
1977 let getanchory (n, top, dtop) =
1978 let y, h = getpageyh n in
1979 if conf.presentation
1980 then
1981 let ips = calcips h in
1982 y + truncate (top*.float h -. dtop*.float ips) + ips;
1983 else
1984 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1987 let gotoanchor anchor =
1988 gotoy (getanchory anchor);
1991 let addnav () =
1992 cbput state.hists.nav (getanchor ());
1995 let getnav dir =
1996 let anchor = cbgetc state.hists.nav dir in
1997 getanchory anchor;
2000 let gotoghyll y =
2001 let scroll f n a b =
2002 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
2003 let snake f a b =
2004 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
2005 if f < a
2006 then s (float f /. float a)
2007 else (
2008 if f > b
2009 then 1.0 -. s ((float (f-b) /. float (n-b)))
2010 else 1.0
2013 snake f a b
2014 and summa n a b =
2015 let ins = float a *. 0.5
2016 and outs = float (n-b) *. 0.5 in
2017 let ones = b - a in
2018 ins +. outs +. float ones
2020 let rec set (_N, _A, _B) y sy =
2021 let sum = summa _N _A _B in
2022 let dy = float (y - sy) in
2023 state.ghyll <- (
2024 let rec gf n y1 o =
2025 if n >= _N
2026 then state.ghyll <- noghyll
2027 else
2028 let go n =
2029 let s = scroll n _N _A _B in
2030 let y1 = y1 +. ((s *. dy) /. sum) in
2031 gotoy_and_clear_text (truncate y1);
2032 state.ghyll <- gf (n+1) y1;
2034 match o with
2035 | None -> go n
2036 | Some y' -> set (_N/2, 1, 1) y' state.y
2038 gf 0 (float state.y)
2041 match conf.ghyllscroll with
2042 | None ->
2043 gotoy_and_clear_text y
2044 | Some nab ->
2045 if state.ghyll == noghyll
2046 then set nab y state.y
2047 else state.ghyll (Some y)
2050 let gotopage n top =
2051 let y, h = getpageyh n in
2052 let y = y + (truncate (top *. float h)) in
2053 gotoghyll y
2056 let gotopage1 n top =
2057 let y = getpagey n in
2058 let y = y + top in
2059 gotoghyll y
2062 let invalidate s f =
2063 state.layout <- [];
2064 state.pdims <- [];
2065 state.rects <- [];
2066 state.rects1 <- [];
2067 match state.geomcmds with
2068 | ps, [] when emptystr ps ->
2069 f ();
2070 state.geomcmds <- s, [];
2072 | ps, [] ->
2073 state.geomcmds <- ps, [s, f];
2075 | ps, (s', _) :: rest when s' = s ->
2076 state.geomcmds <- ps, ((s, f) :: rest);
2078 | ps, cmds ->
2079 state.geomcmds <- ps, ((s, f) :: cmds);
2082 let flushpages () =
2083 Hashtbl.iter (fun _ opaque ->
2084 wcmd "freepage %s" opaque;
2085 ) state.pagemap;
2086 Hashtbl.clear state.pagemap;
2089 let flushtiles () =
2090 if not (Queue.is_empty state.tilelru)
2091 then (
2092 Queue.iter (fun (k, p, s) ->
2093 wcmd "freetile %s" p;
2094 state.memused <- state.memused - s;
2095 Hashtbl.remove state.tilemap k;
2096 ) state.tilelru;
2097 state.uioh#infochanged Memused;
2098 Queue.clear state.tilelru;
2100 load state.layout;
2103 let stateh h =
2104 let h = truncate (float h*.conf.zoom) in
2105 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2106 h - d
2109 let opendoc path password =
2110 state.path <- path;
2111 state.password <- password;
2112 state.gen <- state.gen + 1;
2113 state.docinfo <- [];
2115 flushpages ();
2116 setaalevel conf.aalevel;
2117 let titlepath =
2118 if emptystr state.origin
2119 then path
2120 else state.origin
2122 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2123 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
2124 invalidate "reqlayout"
2125 (fun () ->
2126 wcmd "reqlayout %d %d %d %s\000"
2127 conf.angle (FMTE.to_int conf.fitmodel)
2128 (stateh state.winh) state.nameddest
2132 let reload () =
2133 state.anchor <- getanchor ();
2134 opendoc state.path state.password;
2137 let scalecolor c =
2138 let c = c *. conf.colorscale in
2139 (c, c, c);
2142 let scalecolor2 (r, g, b) =
2143 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2146 let docolumns = function
2147 | Csingle _ ->
2148 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2149 let rec loop pageno pdimno pdim y ph pdims =
2150 if pageno = state.pagecount
2151 then ()
2152 else
2153 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2154 match pdims with
2155 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2156 pdimno+1, pdim, rest
2157 | _ ->
2158 pdimno, pdim, pdims
2160 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2161 let y = y +
2162 (if conf.presentation
2163 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2164 else (if pageno = 0 then 0 else conf.interpagespace)
2167 a.(pageno) <- (pdimno, x, y, pdim);
2168 loop (pageno+1) pdimno pdim (y + h) h pdims
2170 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2171 conf.columns <- Csingle a;
2173 | Cmulti ((columns, coverA, coverB), _) ->
2174 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2175 let rec loop pageno pdimno pdim x y rowh pdims =
2176 let rec fixrow m = if m = pageno then () else
2177 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2178 if h < rowh
2179 then (
2180 let y = y + (rowh - h) / 2 in
2181 a.(m) <- (pdimno, x, y, pdim);
2183 fixrow (m+1)
2185 if pageno = state.pagecount
2186 then fixrow (((pageno - 1) / columns) * columns)
2187 else
2188 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2189 match pdims with
2190 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2191 pdimno+1, pdim, rest
2192 | _ ->
2193 pdimno, pdim, pdims
2195 let x, y, rowh' =
2196 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2197 then (
2198 let x = (wadjsb state.winw - w) / 2 in
2199 let ips =
2200 if conf.presentation then calcips h else conf.interpagespace in
2201 x, y + ips + rowh, h
2203 else (
2204 if (pageno - coverA) mod columns = 0
2205 then (
2206 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2207 let y =
2208 if conf.presentation
2209 then
2210 let ips = calcips h in
2211 y + (if pageno = 0 then 0 else calcips rowh + ips)
2212 else
2213 y + (if pageno = 0 then 0 else conf.interpagespace)
2215 x, y + rowh, h
2217 else x, y, max rowh h
2220 let y =
2221 if pageno > 1 && (pageno - coverA) mod columns = 0
2222 then (
2223 let y =
2224 if pageno = columns && conf.presentation
2225 then (
2226 let ips = calcips rowh in
2227 for i = 0 to pred columns
2229 let (pdimno, x, y, pdim) = a.(i) in
2230 a.(i) <- (pdimno, x, y+ips, pdim)
2231 done;
2232 y+ips;
2234 else y
2236 fixrow (pageno - columns);
2239 else y
2241 a.(pageno) <- (pdimno, x, y, pdim);
2242 let x = x + w + xoff*2 + conf.interpagespace in
2243 loop (pageno+1) pdimno pdim x y rowh' pdims
2245 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2246 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2248 | Csplit (c, _) ->
2249 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2250 let rec loop pageno pdimno pdim y pdims =
2251 if pageno = state.pagecount
2252 then ()
2253 else
2254 let pdimno, ((_, w, h, _) as pdim), pdims =
2255 match pdims with
2256 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2257 pdimno+1, pdim, rest
2258 | _ ->
2259 pdimno, pdim, pdims
2261 let cw = w / c in
2262 let rec loop1 n x y =
2263 if n = c then y else (
2264 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2265 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2268 let y = loop1 0 0 y in
2269 loop (pageno+1) pdimno pdim y pdims
2271 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2272 conf.columns <- Csplit (c, a);
2275 let represent () =
2276 docolumns conf.columns;
2277 state.maxy <- calcheight ();
2278 if state.reprf == noreprf
2279 then (
2280 match state.mode with
2281 | Birdseye (_, _, pageno, _, _) ->
2282 let y, h = getpageyh pageno in
2283 let top = (state.winh - h) / 2 in
2284 gotoy (max 0 (y - top))
2285 | _ -> gotoanchor state.anchor
2287 else (
2288 state.reprf ();
2289 state.reprf <- noreprf;
2293 let reshape w h =
2294 GlDraw.viewport 0 0 w h;
2295 let firsttime = state.geomcmds == firstgeomcmds in
2296 if not firsttime && nogeomcmds state.geomcmds
2297 then state.anchor <- getanchor ();
2299 state.winw <- w;
2300 let w = wadjsb (truncate (float w *. conf.zoom)) in
2301 let w = max w 2 in
2302 state.winh <- h;
2303 setfontsize fstate.fontsize;
2304 GlMat.mode `modelview;
2305 GlMat.load_identity ();
2307 GlMat.mode `projection;
2308 GlMat.load_identity ();
2309 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2310 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2311 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2313 let relx =
2314 if conf.zoom <= 1.0
2315 then 0.0
2316 else float state.x /. float state.w
2318 invalidate "geometry"
2319 (fun () ->
2320 state.w <- w;
2321 if not firsttime
2322 then state.x <- truncate (relx *. float w);
2323 let w =
2324 match conf.columns with
2325 | Csingle _ -> w
2326 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2327 | Csplit (c, _) -> w * c
2329 wcmd "geometry %d %d %d"
2330 w (stateh h) (FMTE.to_int conf.fitmodel)
2334 let enttext () =
2335 let len = String.length state.text in
2336 let drawstring s =
2337 let hscrollh =
2338 match state.mode with
2339 | Textentry _ | View | LinkNav _ ->
2340 let h, _, _ = state.uioh#scrollpw in
2342 | _ -> 0
2344 let rect x w =
2345 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
2346 (x+.w) (float (state.winh - hscrollh))
2349 let w = float (wadjsb state.winw - 1) in
2350 if state.progress >= 0.0 && state.progress < 1.0
2351 then (
2352 GlDraw.color (0.3, 0.3, 0.3);
2353 let w1 = w *. state.progress in
2354 rect 0.0 w1;
2355 GlDraw.color (0.0, 0.0, 0.0);
2356 rect w1 (w-.w1)
2358 else (
2359 GlDraw.color (0.0, 0.0, 0.0);
2360 rect 0.0 w;
2363 GlDraw.color (1.0, 1.0, 1.0);
2364 drawstring fstate.fontsize
2365 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2367 let s =
2368 match state.mode with
2369 | Textentry ((prefix, text, _, _, _, _), _) ->
2370 let s =
2371 if len > 0
2372 then
2373 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2374 else
2375 Printf.sprintf "%s%s_" prefix text
2379 | _ -> state.text
2381 let s =
2382 if state.newerrmsgs
2383 then (
2384 if not (istextentry state.mode) && state.uioh#eformsgs
2385 then
2386 let s1 = "(press 'e' to review error messasges)" in
2387 if nonemptystr s then s ^ " " ^ s1 else s1
2388 else s
2390 else s
2392 if nonemptystr s
2393 then drawstring s
2396 let gctiles () =
2397 let len = Queue.length state.tilelru in
2398 let layout = lazy (
2399 match state.throttle with
2400 | None ->
2401 if conf.preload
2402 then preloadlayout state.y
2403 else state.layout
2404 | Some (layout, _, _) ->
2405 layout
2406 ) in
2407 let rec loop qpos =
2408 if state.memused <= conf.memlimit
2409 then ()
2410 else (
2411 if qpos < len
2412 then
2413 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2414 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2415 let (_, pw, ph, _) = getpagedim n in
2417 gen = state.gen
2418 && colorspace = conf.colorspace
2419 && angle = conf.angle
2420 && pagew = pw
2421 && pageh = ph
2422 && (
2423 let x = col*conf.tilew
2424 and y = row*conf.tileh in
2425 tilevisible (Lazy.force_val layout) n x y
2427 then Queue.push lruitem state.tilelru
2428 else (
2429 freepbo p;
2430 wcmd "freetile %s" p;
2431 state.memused <- state.memused - s;
2432 state.uioh#infochanged Memused;
2433 Hashtbl.remove state.tilemap k;
2435 loop (qpos+1)
2438 loop 0
2441 let logcurrently = function
2442 | Idle -> dolog "Idle"
2443 | Loading (l, gen) ->
2444 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2445 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2446 dolog
2447 "Tiling %d[%d,%d] page=%s cs=%s angle"
2448 l.pageno col row pageopaque
2449 (CSTE.to_string colorspace)
2451 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2452 angle gen conf.angle state.gen
2453 tilew tileh
2454 conf.tilew conf.tileh
2456 | Outlining _ ->
2457 dolog "outlining"
2460 let splitatspace =
2461 let r = Str.regexp " " in
2462 fun s -> Str.bounded_split r s 2;
2465 let onpagerect pageno f =
2466 let b =
2467 match conf.columns with
2468 | Cmulti (_, b) -> b
2469 | Csingle b -> b
2470 | Csplit (_, b) -> b
2472 if pageno >= 0 && pageno < Array.length b
2473 then
2474 let (_, _, _, (w, h, _, _)) = b.(pageno) in
2475 f w h
2478 let gotopagexy1 pageno x y =
2479 let _,w1,h1,leftx = getpagedim pageno in
2480 let top = y /. (float h1) in
2481 let left = x /. (float w1) in
2482 let py, w, h = getpageywh pageno in
2483 let wh = state.winh - hscrollh () in
2484 let x = left *. (float w) in
2485 let x = leftx + state.x + truncate x in
2486 let sx =
2487 if x < 0 || x >= wadjsb state.winw
2488 then state.x - x
2489 else state.x
2491 let pdy = truncate (top *. float h) in
2492 let y' = py + pdy in
2493 let dy = y' - state.y in
2494 let sy =
2495 if x != state.x || not (dy > 0 && dy < wh)
2496 then (
2497 if conf.presentation
2498 then
2499 if abs (py - y') > wh
2500 then y'
2501 else py
2502 else y';
2504 else state.y
2506 if state.x != sx || state.y != sy
2507 then (
2508 let x, y =
2509 if !wtmode
2510 then (
2511 let ww = wadjsb state.winw in
2512 let qx = sx / ww
2513 and qy = pdy / wh in
2514 let x = qx * ww
2515 and y = py + qy * wh in
2516 let x = if -x + ww > w1 then -(w1-ww) else x
2517 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2518 let y =
2519 if conf.presentation
2520 then
2521 if abs (py - y') > wh
2522 then y'
2523 else py
2524 else y';
2526 (x, y)
2528 else (sx, sy)
2530 state.x <- x;
2531 gotoy_and_clear_text y;
2533 else gotoy_and_clear_text state.y;
2536 let gotopagexy pageno x y =
2537 match state.mode with
2538 | Birdseye _ -> gotopage pageno 0.0
2539 | _ -> gotopagexy1 pageno x y
2542 let act cmds =
2543 (* dolog "%S" cmds; *)
2544 let cl = splitatspace cmds in
2545 let scan s fmt f =
2546 try Scanf.sscanf s fmt f
2547 with exn ->
2548 dolog "error processing '%S': %s" cmds (exntos exn);
2549 exit 1
2551 match cl with
2552 | "clear" :: [] ->
2553 state.uioh#infochanged Pdim;
2554 state.pdims <- [];
2556 | "clearrects" :: [] ->
2557 state.rects <- state.rects1;
2558 G.postRedisplay "clearrects";
2560 | "continue" :: args :: [] ->
2561 let n = scan args "%u" (fun n -> n) in
2562 state.pagecount <- n;
2563 begin match state.currently with
2564 | Outlining l ->
2565 state.currently <- Idle;
2566 state.outlines <- Array.of_list (List.rev l)
2567 | _ -> ()
2568 end;
2570 let cur, cmds = state.geomcmds in
2571 if emptystr cur
2572 then failwith "umpossible";
2574 begin match List.rev cmds with
2575 | [] ->
2576 state.geomcmds <- "", [];
2577 represent ();
2578 | (s, f) :: rest ->
2579 f ();
2580 state.geomcmds <- s, List.rev rest;
2581 end;
2582 if conf.maxwait = None && not !wtmode
2583 then G.postRedisplay "continue";
2585 | "title" :: args :: [] ->
2586 Wsi.settitle args
2588 | "msg" :: args :: [] ->
2589 showtext ' ' args
2591 | "vmsg" :: args :: [] ->
2592 if conf.verbose
2593 then showtext ' ' args
2595 | "emsg" :: args :: [] ->
2596 Buffer.add_string state.errmsgs args;
2597 state.newerrmsgs <- true;
2598 G.postRedisplay "error message"
2600 | "progress" :: args :: [] ->
2601 let progress, text =
2602 scan args "%f %n"
2603 (fun f pos ->
2604 f, String.sub args pos (String.length args - pos))
2606 state.text <- text;
2607 state.progress <- progress;
2608 G.postRedisplay "progress"
2610 | "firstmatch" :: args :: [] ->
2611 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2612 scan args "%u %d %f %f %f %f %f %f %f %f"
2613 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2614 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2616 let y = (getpagey pageno) + truncate y0 in
2617 addnav ();
2618 gotoy y;
2619 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2621 | "match" :: args :: [] ->
2622 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2623 scan args "%u %d %f %f %f %f %f %f %f %f"
2624 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2625 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2627 state.rects1 <-
2628 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2630 | "page" :: args :: [] ->
2631 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2632 begin match state.currently with
2633 | Loading (l, gen) ->
2634 vlog "page %d took %f sec" l.pageno t;
2635 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2636 begin match state.throttle with
2637 | None ->
2638 let preloadedpages =
2639 if conf.preload
2640 then preloadlayout state.y
2641 else state.layout
2643 let evict () =
2644 let set =
2645 List.fold_left (fun s l -> IntSet.add l.pageno s)
2646 IntSet.empty preloadedpages
2648 let evictedpages =
2649 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2650 if not (IntSet.mem pageno set)
2651 then (
2652 wcmd "freepage %s" opaque;
2653 key :: accu
2655 else accu
2656 ) state.pagemap []
2658 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2660 evict ();
2661 state.currently <- Idle;
2662 if gen = state.gen
2663 then (
2664 tilepage l.pageno pageopaque state.layout;
2665 load state.layout;
2666 load preloadedpages;
2667 if pagevisible state.layout l.pageno
2668 && layoutready state.layout
2669 then G.postRedisplay "page";
2672 | Some (layout, _, _) ->
2673 state.currently <- Idle;
2674 tilepage l.pageno pageopaque layout;
2675 load state.layout
2676 end;
2678 | _ ->
2679 dolog "Inconsistent loading state";
2680 logcurrently state.currently;
2681 exit 1
2684 | "tile" :: args :: [] ->
2685 let (x, y, opaque, size, t) =
2686 scan args "%u %u %s %u %f"
2687 (fun x y p size t -> (x, y, p, size, t))
2689 begin match state.currently with
2690 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2691 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2693 unmappbo opaque;
2694 if tilew != conf.tilew || tileh != conf.tileh
2695 then (
2696 wcmd "freetile %s" opaque;
2697 state.currently <- Idle;
2698 load state.layout;
2700 else (
2701 puttileopaque l col row gen cs angle opaque size t;
2702 state.memused <- state.memused + size;
2703 state.uioh#infochanged Memused;
2704 gctiles ();
2705 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2706 opaque, size) state.tilelru;
2708 let layout =
2709 match state.throttle with
2710 | None -> state.layout
2711 | Some (layout, _, _) -> layout
2714 state.currently <- Idle;
2715 if gen = state.gen
2716 && conf.colorspace = cs
2717 && conf.angle = angle
2718 && tilevisible layout l.pageno x y
2719 then conttiling l.pageno pageopaque;
2721 begin match state.throttle with
2722 | None ->
2723 preload state.layout;
2724 if gen = state.gen
2725 && conf.colorspace = cs
2726 && conf.angle = angle
2727 && tilevisible state.layout l.pageno x y
2728 && (not !wtmode || layoutready state.layout)
2729 then G.postRedisplay "tile nothrottle";
2731 | Some (layout, y, _) ->
2732 let ready = layoutready layout in
2733 if ready
2734 then (
2735 state.y <- y;
2736 state.layout <- layout;
2737 state.throttle <- None;
2738 G.postRedisplay "throttle";
2740 else load layout;
2741 end;
2744 | _ ->
2745 dolog "Inconsistent tiling state";
2746 logcurrently state.currently;
2747 exit 1
2750 | "pdim" :: args :: [] ->
2751 let (n, w, h, _) as pdim =
2752 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2754 let pdim =
2755 match conf.fitmodel, conf.columns with
2756 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2757 | _ -> pdim
2759 state.uioh#infochanged Pdim;
2760 state.pdims <- pdim :: state.pdims
2762 | "o" :: args :: [] ->
2763 let (l, n, t, h, pos) =
2764 scan args "%u %u %d %u %n"
2765 (fun l n t h pos -> l, n, t, h, pos)
2767 let s = String.sub args pos (String.length args - pos) in
2768 let outline = (s, l, (n, float t /. float h, 0.0)) in
2769 begin match state.currently with
2770 | Outlining outlines ->
2771 state.currently <- Outlining (outline :: outlines)
2772 | Idle ->
2773 state.currently <- Outlining [outline]
2774 | currently ->
2775 dolog "invalid outlining state";
2776 logcurrently currently
2779 | "a" :: args :: [] ->
2780 let (n, l, t) =
2781 scan args "%u %d %d" (fun n l t -> n, l, t)
2783 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2785 | "info" :: args :: [] ->
2786 state.docinfo <- (1, args) :: state.docinfo
2788 | "infoend" :: [] ->
2789 state.uioh#infochanged Docinfo;
2790 state.docinfo <- List.rev state.docinfo
2792 | _ ->
2793 error "unknown cmd `%S'" cmds
2796 let onhist cb =
2797 let rc = cb.rc in
2798 let action = function
2799 | HCprev -> cbget cb ~-1
2800 | HCnext -> cbget cb 1
2801 | HCfirst -> cbget cb ~-(cb.rc)
2802 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2803 and cancel () = cb.rc <- rc
2804 in (action, cancel)
2807 let search pattern forward =
2808 match conf.columns with
2809 | Csplit _ ->
2810 showtext '!' "searching does not work properly in split columns mode"
2811 | _ ->
2812 if nonemptystr pattern
2813 then
2814 let pn, py =
2815 match state.layout with
2816 | [] -> 0, 0
2817 | l :: _ ->
2818 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2820 wcmd "search %d %d %d %d,%s\000"
2821 (btod conf.icase) pn py (btod forward) pattern;
2824 let intentry text key =
2825 let c =
2826 if key >= 32 && key < 127
2827 then Char.chr key
2828 else '\000'
2830 match c with
2831 | '0' .. '9' ->
2832 let text = addchar text c in
2833 TEcont text
2835 | _ ->
2836 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2837 TEcont text
2840 let linknentry text key =
2841 let c =
2842 if key >= 32 && key < 127
2843 then Char.chr key
2844 else '\000'
2846 match c with
2847 | 'a' .. 'z' ->
2848 let text = addchar text c in
2849 TEcont text
2851 | _ ->
2852 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2853 TEcont text
2856 let linkndone f s =
2857 if nonemptystr s
2858 then (
2859 let n =
2860 let l = String.length s in
2861 let rec loop pos n = if pos = l then n else
2862 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2863 loop (pos+1) (n*26 + m)
2864 in loop 0 0
2866 let rec loop n = function
2867 | [] -> ()
2868 | l :: rest ->
2869 match getopaque l.pageno with
2870 | None -> loop n rest
2871 | Some opaque ->
2872 let m = getlinkcount opaque in
2873 if n < m
2874 then (
2875 let under = getlink opaque n in
2876 f under
2878 else loop (n-m) rest
2880 loop n state.layout;
2884 let textentry text key =
2885 if key land 0xff00 = 0xff00
2886 then TEcont text
2887 else TEcont (text ^ toutf8 key)
2890 let reqlayout angle fitmodel =
2891 match state.throttle with
2892 | None ->
2893 if nogeomcmds state.geomcmds
2894 then state.anchor <- getanchor ();
2895 conf.angle <- angle mod 360;
2896 if conf.angle != 0
2897 then (
2898 match state.mode with
2899 | LinkNav _ -> state.mode <- View
2900 | _ -> ()
2902 conf.fitmodel <- fitmodel;
2903 invalidate "reqlayout"
2904 (fun () ->
2905 wcmd "reqlayout %d %d %d"
2906 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2908 | _ -> ()
2911 let settrim trimmargins trimfuzz =
2912 if nogeomcmds state.geomcmds
2913 then state.anchor <- getanchor ();
2914 conf.trimmargins <- trimmargins;
2915 conf.trimfuzz <- trimfuzz;
2916 let x0, y0, x1, y1 = trimfuzz in
2917 invalidate "settrim"
2918 (fun () ->
2919 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2920 flushpages ();
2923 let setzoom zoom =
2924 match state.throttle with
2925 | None ->
2926 let zoom = max 0.0001 zoom in
2927 if zoom <> conf.zoom
2928 then (
2929 state.prevzoom <- (conf.zoom, state.x);
2930 conf.zoom <- zoom;
2931 reshape state.winw state.winh;
2932 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2935 | Some (layout, y, started) ->
2936 let time =
2937 match conf.maxwait with
2938 | None -> 0.0
2939 | Some t -> t
2941 let dt = now () -. started in
2942 if dt > time
2943 then (
2944 state.y <- y;
2945 load layout;
2949 let setcolumns mode columns coverA coverB =
2950 state.prevcolumns <- Some (conf.columns, conf.zoom);
2951 if columns < 0
2952 then (
2953 if isbirdseye mode
2954 then showtext '!' "split mode doesn't work in bird's eye"
2955 else (
2956 conf.columns <- Csplit (-columns, [||]);
2957 state.x <- 0;
2958 conf.zoom <- 1.0;
2961 else (
2962 if columns < 2
2963 then (
2964 conf.columns <- Csingle [||];
2965 state.x <- 0;
2966 setzoom 1.0;
2968 else (
2969 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2970 conf.zoom <- 1.0;
2973 reshape state.winw state.winh;
2976 let enterbirdseye () =
2977 let zoom = float conf.thumbw /. float state.winw in
2978 let birdseyepageno =
2979 let cy = state.winh / 2 in
2980 let fold = function
2981 | [] -> 0
2982 | l :: rest ->
2983 let rec fold best = function
2984 | [] -> best.pageno
2985 | l :: rest ->
2986 let d = cy - (l.pagedispy + l.pagevh/2)
2987 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2988 if abs d < abs dbest
2989 then fold l rest
2990 else best.pageno
2991 in fold l rest
2993 fold state.layout
2995 state.mode <- Birdseye (
2996 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2998 conf.zoom <- zoom;
2999 conf.presentation <- false;
3000 conf.interpagespace <- 10;
3001 conf.hlinks <- false;
3002 conf.fitmodel <- FitProportional;
3003 state.x <- 0;
3004 state.mstate <- Mnone;
3005 conf.maxwait <- None;
3006 conf.columns <- (
3007 match conf.beyecolumns with
3008 | Some c ->
3009 conf.zoom <- 1.0;
3010 Cmulti ((c, 0, 0), [||])
3011 | None -> Csingle [||]
3013 Wsi.setcursor Wsi.CURSOR_INHERIT;
3014 if conf.verbose
3015 then
3016 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
3017 (100.0*.zoom)
3018 else
3019 state.text <- ""
3021 reshape state.winw state.winh;
3024 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
3025 state.mode <- View;
3026 conf.zoom <- c.zoom;
3027 conf.presentation <- c.presentation;
3028 conf.interpagespace <- c.interpagespace;
3029 conf.maxwait <- c.maxwait;
3030 conf.hlinks <- c.hlinks;
3031 conf.fitmodel <- c.fitmodel;
3032 conf.beyecolumns <- (
3033 match conf.columns with
3034 | Cmulti ((c, _, _), _) -> Some c
3035 | Csingle _ -> None
3036 | Csplit _ -> failwith "leaving bird's eye split mode"
3038 conf.columns <- (
3039 match c.columns with
3040 | Cmulti (c, _) -> Cmulti (c, [||])
3041 | Csingle _ -> Csingle [||]
3042 | Csplit (c, _) -> Csplit (c, [||])
3044 if conf.verbose
3045 then
3046 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
3047 (100.0*.conf.zoom)
3049 reshape state.winw state.winh;
3050 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
3051 state.x <- leftx;
3054 let togglebirdseye () =
3055 match state.mode with
3056 | Birdseye vals -> leavebirdseye vals true
3057 | View -> enterbirdseye ()
3058 | _ -> ()
3061 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3062 let pageno = max 0 (pageno - incr) in
3063 let rec loop = function
3064 | [] -> gotopage1 pageno 0
3065 | l :: _ when l.pageno = pageno ->
3066 if l.pagedispy >= 0 && l.pagey = 0
3067 then G.postRedisplay "upbirdseye"
3068 else gotopage1 pageno 0
3069 | _ :: rest -> loop rest
3071 loop state.layout;
3072 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
3075 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3076 let pageno = min (state.pagecount - 1) (pageno + incr) in
3077 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3078 let rec loop = function
3079 | [] ->
3080 let y, h = getpageyh pageno in
3081 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
3082 gotoy (clamp dy)
3083 | l :: _ when l.pageno = pageno ->
3084 if l.pagevh != l.pageh
3085 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
3086 else G.postRedisplay "downbirdseye"
3087 | _ :: rest -> loop rest
3089 loop state.layout
3092 let optentry mode _ key =
3093 let btos b = if b then "on" else "off" in
3094 if key >= 32 && key < 127
3095 then
3096 let c = Char.chr key in
3097 match c with
3098 | 's' ->
3099 let ondone s =
3100 try conf.scrollstep <- int_of_string s with exc ->
3101 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3103 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3105 | 'A' ->
3106 let ondone s =
3108 conf.autoscrollstep <- int_of_string s;
3109 if state.autoscroll <> None
3110 then state.autoscroll <- Some conf.autoscrollstep
3111 with exc ->
3112 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3114 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3116 | 'C' ->
3117 let ondone s =
3119 let n, a, b = multicolumns_of_string s in
3120 setcolumns mode n a b;
3121 with exc ->
3122 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3124 TEswitch ("columns: ", "", None, textentry, ondone, true)
3126 | 'Z' ->
3127 let ondone s =
3129 let zoom = float (int_of_string s) /. 100.0 in
3130 setzoom zoom
3131 with exc ->
3132 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3134 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3136 | 't' ->
3137 let ondone s =
3139 conf.thumbw <- bound (int_of_string s) 2 4096;
3140 state.text <-
3141 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3142 begin match mode with
3143 | Birdseye beye ->
3144 leavebirdseye beye false;
3145 enterbirdseye ();
3146 | _ -> ();
3148 with exc ->
3149 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3151 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3153 | 'R' ->
3154 let ondone s =
3155 match try
3156 Some (int_of_string s)
3157 with exc ->
3158 state.text <- Printf.sprintf "bad integer `%s': %s"
3159 s (exntos exc);
3160 None
3161 with
3162 | Some angle -> reqlayout angle conf.fitmodel
3163 | None -> ()
3165 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3167 | 'i' ->
3168 conf.icase <- not conf.icase;
3169 TEdone ("case insensitive search " ^ (btos conf.icase))
3171 | 'p' ->
3172 conf.preload <- not conf.preload;
3173 gotoy state.y;
3174 TEdone ("preload " ^ (btos conf.preload))
3176 | 'v' ->
3177 conf.verbose <- not conf.verbose;
3178 TEdone ("verbose " ^ (btos conf.verbose))
3180 | 'd' ->
3181 conf.debug <- not conf.debug;
3182 TEdone ("debug " ^ (btos conf.debug))
3184 | 'h' ->
3185 conf.maxhfit <- not conf.maxhfit;
3186 state.maxy <- calcheight ();
3187 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3189 | 'c' ->
3190 conf.crophack <- not conf.crophack;
3191 TEdone ("crophack " ^ btos conf.crophack)
3193 | 'a' ->
3194 let s =
3195 match conf.maxwait with
3196 | None ->
3197 conf.maxwait <- Some infinity;
3198 "always wait for page to complete"
3199 | Some _ ->
3200 conf.maxwait <- None;
3201 "show placeholder if page is not ready"
3203 TEdone s
3205 | 'f' ->
3206 conf.underinfo <- not conf.underinfo;
3207 TEdone ("underinfo " ^ btos conf.underinfo)
3209 | 'P' ->
3210 conf.savebmarks <- not conf.savebmarks;
3211 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3213 | 'S' ->
3214 let ondone s =
3216 let pageno, py =
3217 match state.layout with
3218 | [] -> 0, 0
3219 | l :: _ ->
3220 l.pageno, l.pagey
3222 conf.interpagespace <- int_of_string s;
3223 docolumns conf.columns;
3224 state.maxy <- calcheight ();
3225 let y = getpagey pageno in
3226 gotoy (y + py)
3227 with exc ->
3228 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3230 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3232 | 'l' ->
3233 let fm =
3234 match conf.fitmodel with
3235 | FitProportional -> FitWidth
3236 | _ -> FitProportional
3238 reqlayout conf.angle fm;
3239 TEdone ("proportional display " ^ btos (fm == FitProportional))
3241 | 'T' ->
3242 settrim (not conf.trimmargins) conf.trimfuzz;
3243 TEdone ("trim margins " ^ btos conf.trimmargins)
3245 | 'I' ->
3246 conf.invert <- not conf.invert;
3247 TEdone ("invert colors " ^ btos conf.invert)
3249 | 'x' ->
3250 let ondone s =
3251 cbput state.hists.sel s;
3252 conf.selcmd <- s;
3254 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3255 textentry, ondone, true)
3257 | 'M' ->
3258 if conf.pax == None
3259 then conf.pax <- Some (ref (0.0, 0, 0))
3260 else conf.pax <- None;
3261 TEdone ("PAX " ^ btos (conf.pax != None))
3263 | _ ->
3264 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3265 TEstop
3266 else
3267 TEcont state.text
3270 class type lvsource = object
3271 method getitemcount : int
3272 method getitem : int -> (string * int)
3273 method hasaction : int -> bool
3274 method exit :
3275 uioh:uioh ->
3276 cancel:bool ->
3277 active:int ->
3278 first:int ->
3279 pan:int ->
3280 qsearch:string ->
3281 uioh option
3282 method getactive : int
3283 method getfirst : int
3284 method getqsearch : string
3285 method setqsearch : string -> unit
3286 method getpan : int
3287 end;;
3289 class virtual lvsourcebase = object
3290 val mutable m_active = 0
3291 val mutable m_first = 0
3292 val mutable m_qsearch = ""
3293 val mutable m_pan = 0
3294 method getactive = m_active
3295 method getfirst = m_first
3296 method getqsearch = m_qsearch
3297 method getpan = m_pan
3298 method setqsearch s = m_qsearch <- s
3299 end;;
3301 let withoutlastutf8 s =
3302 let len = String.length s in
3303 if len = 0
3304 then s
3305 else
3306 let rec find pos =
3307 if pos = 0
3308 then pos
3309 else
3310 let b = Char.code s.[pos] in
3311 if b land 0b11000000 = 0b11000000
3312 then pos
3313 else find (pos-1)
3315 let first =
3316 if Char.code s.[len-1] land 0x80 = 0
3317 then len-1
3318 else find (len-1)
3320 String.sub s 0 first;
3323 let textentrykeyboard
3324 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3325 let key =
3326 if key >= 0xffb0 && key <= 0xffb9
3327 then key - 0xffb0 + 48 else key
3329 let enttext te =
3330 state.mode <- Textentry (te, onleave);
3331 state.text <- "";
3332 enttext ();
3333 G.postRedisplay "textentrykeyboard enttext";
3335 let histaction cmd =
3336 match opthist with
3337 | None -> ()
3338 | Some (action, _) ->
3339 state.mode <- Textentry (
3340 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3342 G.postRedisplay "textentry histaction"
3344 match key with
3345 | 0xff08 -> (* backspace *)
3346 if emptystr text && cancelonempty
3347 then (
3348 onleave Cancel;
3349 G.postRedisplay "textentrykeyboard after cancel";
3351 else
3352 let s = withoutlastutf8 text in
3353 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3355 | 0xff0d | 0xff8d -> (* (kp) enter *)
3356 ondone text;
3357 onleave Confirm;
3358 G.postRedisplay "textentrykeyboard after confirm"
3360 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3361 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3362 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3363 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3365 | 0xff1b -> (* escape*)
3366 if emptystr text
3367 then (
3368 begin match opthist with
3369 | None -> ()
3370 | Some (_, onhistcancel) -> onhistcancel ()
3371 end;
3372 onleave Cancel;
3373 state.text <- "";
3374 G.postRedisplay "textentrykeyboard after cancel2"
3376 else (
3377 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3380 | 0xff9f | 0xffff -> () (* delete *)
3382 | _ when key != 0
3383 && key land 0xff00 != 0xff00 (* keyboard *)
3384 && key land 0xfe00 != 0xfe00 (* xkb *)
3385 && key land 0xfd00 != 0xfd00 (* 3270 *)
3387 begin match onkey text key with
3388 | TEdone text ->
3389 ondone text;
3390 onleave Confirm;
3391 G.postRedisplay "textentrykeyboard after confirm2";
3393 | TEcont text ->
3394 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3396 | TEstop ->
3397 onleave Cancel;
3398 G.postRedisplay "textentrykeyboard after cancel3"
3400 | TEswitch te ->
3401 state.mode <- Textentry (te, onleave);
3402 G.postRedisplay "textentrykeyboard switch";
3403 end;
3405 | _ ->
3406 vlog "unhandled key %s" (Wsi.keyname key)
3409 let firstof first active =
3410 if first > active || abs (first - active) > fstate.maxrows - 1
3411 then max 0 (active - (fstate.maxrows/2))
3412 else first
3415 let calcfirst first active =
3416 if active > first
3417 then
3418 let rows = active - first in
3419 if rows > fstate.maxrows then active - fstate.maxrows else first
3420 else active
3423 let scrollph y maxy =
3424 let sh = float (maxy + state.winh) /. float state.winh in
3425 let sh = float state.winh /. sh in
3426 let sh = max sh (float conf.scrollh) in
3428 let percent = float y /. float maxy in
3429 let position = (float state.winh -. sh) *. percent in
3431 let position =
3432 if position +. sh > float state.winh
3433 then float state.winh -. sh
3434 else position
3436 position, sh;
3439 let coe s = (s :> uioh);;
3441 class listview ~(source:lvsource) ~trusted ~modehash =
3442 object (self)
3443 val m_pan = source#getpan
3444 val m_first = source#getfirst
3445 val m_active = source#getactive
3446 val m_qsearch = source#getqsearch
3447 val m_prev_uioh = state.uioh
3449 method private elemunder y =
3450 let n = y / (fstate.fontsize+1) in
3451 if m_first + n < source#getitemcount
3452 then (
3453 if source#hasaction (m_first + n)
3454 then Some (m_first + n)
3455 else None
3457 else None
3459 method display =
3460 Gl.enable `blend;
3461 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3462 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3463 filledrect 0. 0. (float state.winw) (float state.winh);
3464 GlDraw.color (1., 1., 1.);
3465 Gl.enable `texture_2d;
3466 let fs = fstate.fontsize in
3467 let nfs = fs + 1 in
3468 let ww = fstate.wwidth in
3469 let tabw = 30.0*.ww in
3470 let itemcount = source#getitemcount in
3471 let rec loop row =
3472 if (row - m_first) > fstate.maxrows
3473 then ()
3474 else (
3475 if row >= 0 && row < itemcount
3476 then (
3477 let (s, level) = source#getitem row in
3478 let y = (row - m_first) * nfs in
3479 let x = 5.0 +. float (level + m_pan) *. ww in
3480 if row = m_active
3481 then (
3482 Gl.disable `texture_2d;
3483 let alpha = if source#hasaction row then 0.9 else 0.3 in
3484 GlDraw.color (1., 1., 1.) ~alpha;
3485 linerect 1. (float (y + 1))
3486 (float (state.winw - conf.scrollbw - 1)) (float (y + fs + 3));
3487 GlDraw.color (1., 1., 1.);
3488 Gl.enable `texture_2d;
3491 let drawtabularstring s =
3492 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3493 if trusted
3494 then
3495 let tabpos = try String.index s '\t' with Not_found -> -1 in
3496 if tabpos > 0
3497 then
3498 let len = String.length s - tabpos - 1 in
3499 let s1 = String.sub s 0 tabpos
3500 and s2 = String.sub s (tabpos + 1) len in
3501 let nx = drawstr x s1 in
3502 let sw = nx -. x in
3503 let x = x +. (max tabw sw) in
3504 drawstr x s2
3505 else
3506 drawstr x s
3507 else
3508 drawstr x s
3510 let _ = drawtabularstring s in
3511 loop (row+1)
3515 loop m_first;
3516 Gl.disable `blend;
3517 Gl.disable `texture_2d;
3519 method updownlevel incr =
3520 let len = source#getitemcount in
3521 let curlevel =
3522 if m_active >= 0 && m_active < len
3523 then snd (source#getitem m_active)
3524 else -1
3526 let rec flow i =
3527 if i = len then i-1 else if i = -1 then 0 else
3528 let _, l = source#getitem i in
3529 if l != curlevel then i else flow (i+incr)
3531 let active = flow m_active in
3532 let first = calcfirst m_first active in
3533 G.postRedisplay "outline updownlevel";
3534 {< m_active = active; m_first = first >}
3536 method private key1 key mask =
3537 let set1 active first qsearch =
3538 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3540 let search active pattern incr =
3541 let active = if active = -1 then m_first else active in
3542 let dosearch re =
3543 let rec loop n =
3544 if n >= 0 && n < source#getitemcount
3545 then (
3546 let s, _ = source#getitem n in
3548 (try ignore (Str.search_forward re s 0); true
3549 with Not_found -> false)
3550 then Some n
3551 else loop (n + incr)
3553 else None
3555 loop active
3558 let re = Str.regexp_case_fold pattern in
3559 dosearch re
3560 with Failure s ->
3561 state.text <- s;
3562 None
3564 let itemcount = source#getitemcount in
3565 let find start incr =
3566 let rec find i =
3567 if i = -1 || i = itemcount
3568 then -1
3569 else (
3570 if source#hasaction i
3571 then i
3572 else find (i + incr)
3575 find start
3577 let set active first =
3578 let first = bound first 0 (itemcount - fstate.maxrows) in
3579 state.text <- "";
3580 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3582 let navigate incr =
3583 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3584 let active, first =
3585 let incr1 = if incr > 0 then 1 else -1 in
3586 if isvisible m_first m_active
3587 then
3588 let next =
3589 let next = m_active + incr in
3590 let next =
3591 if next < 0 || next >= itemcount
3592 then -1
3593 else find next incr1
3595 if abs (m_active - next) > fstate.maxrows
3596 then -1
3597 else next
3599 if next = -1
3600 then
3601 let first = m_first + incr in
3602 let first = bound first 0 (itemcount - fstate.maxrows) in
3603 let next =
3604 let next = m_active + incr in
3605 let next = bound next 0 (itemcount - 1) in
3606 find next ~-incr1
3608 let active =
3609 if next = -1
3610 then m_active
3611 else (
3612 if isvisible first next
3613 then next
3614 else m_active
3617 active, first
3618 else
3619 let first = min next m_first in
3620 let first =
3621 if abs (next - first) > fstate.maxrows
3622 then first + incr
3623 else first
3625 next, first
3626 else
3627 let first = m_first + incr in
3628 let first = bound first 0 (itemcount - 1) in
3629 let active =
3630 let next = m_active + incr in
3631 let next = bound next 0 (itemcount - 1) in
3632 let next = find next incr1 in
3633 let active =
3634 if next = -1 || abs (m_active - first) > fstate.maxrows
3635 then (
3636 let active = if m_active = -1 then next else m_active in
3637 active
3639 else next
3641 if isvisible first active
3642 then active
3643 else -1
3645 active, first
3647 G.postRedisplay "listview navigate";
3648 set active first;
3650 match key with
3651 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3652 let incr = if key = 0x72 then -1 else 1 in
3653 let active, first =
3654 match search (m_active + incr) m_qsearch incr with
3655 | None ->
3656 state.text <- m_qsearch ^ " [not found]";
3657 m_active, m_first
3658 | Some active ->
3659 state.text <- m_qsearch;
3660 active, firstof m_first active
3662 G.postRedisplay "listview ctrl-r/s";
3663 set1 active first m_qsearch;
3665 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3666 if m_active >= 0 && m_active < source#getitemcount
3667 then (
3668 let s, _ = source#getitem m_active in
3669 selstring s;
3671 coe self
3673 | 0xff08 -> (* backspace *)
3674 if emptystr m_qsearch
3675 then coe self
3676 else (
3677 let qsearch = withoutlastutf8 m_qsearch in
3678 if emptystr qsearch
3679 then (
3680 state.text <- "";
3681 G.postRedisplay "listview empty qsearch";
3682 set1 m_active m_first "";
3684 else
3685 let active, first =
3686 match search m_active qsearch ~-1 with
3687 | None ->
3688 state.text <- qsearch ^ " [not found]";
3689 m_active, m_first
3690 | Some active ->
3691 state.text <- qsearch;
3692 active, firstof m_first active
3694 G.postRedisplay "listview backspace qsearch";
3695 set1 active first qsearch
3698 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3699 let pattern = m_qsearch ^ toutf8 key in
3700 let active, first =
3701 match search m_active pattern 1 with
3702 | None ->
3703 state.text <- pattern ^ " [not found]";
3704 m_active, m_first
3705 | Some active ->
3706 state.text <- pattern;
3707 active, firstof m_first active
3709 G.postRedisplay "listview qsearch add";
3710 set1 active first pattern;
3712 | 0xff1b -> (* escape *)
3713 state.text <- "";
3714 if emptystr m_qsearch
3715 then (
3716 G.postRedisplay "list view escape";
3717 begin
3718 match
3719 source#exit (coe self) true m_active m_first m_pan m_qsearch
3720 with
3721 | None -> m_prev_uioh
3722 | Some uioh -> uioh
3725 else (
3726 G.postRedisplay "list view kill qsearch";
3727 source#setqsearch "";
3728 coe {< m_qsearch = "" >}
3731 | 0xff0d | 0xff8d -> (* (kp) enter *)
3732 state.text <- "";
3733 let self = {< m_qsearch = "" >} in
3734 source#setqsearch "";
3735 let opt =
3736 G.postRedisplay "listview enter";
3737 if m_active >= 0 && m_active < source#getitemcount
3738 then (
3739 source#exit (coe self) false m_active m_first m_pan "";
3741 else (
3742 source#exit (coe self) true m_active m_first m_pan "";
3745 begin match opt with
3746 | None -> m_prev_uioh
3747 | Some uioh -> uioh
3750 | 0xff9f | 0xffff -> (* (kp) delete *)
3751 coe self
3753 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3754 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3755 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3756 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3758 | 0xff53 | 0xff98 -> (* (kp) right *)
3759 state.text <- "";
3760 G.postRedisplay "listview right";
3761 coe {< m_pan = m_pan - 1 >}
3763 | 0xff51 | 0xff96 -> (* (kp) left *)
3764 state.text <- "";
3765 G.postRedisplay "listview left";
3766 coe {< m_pan = m_pan + 1 >}
3768 | 0xff50 | 0xff95 -> (* (kp) home *)
3769 let active = find 0 1 in
3770 G.postRedisplay "listview home";
3771 set active 0;
3773 | 0xff57 | 0xff9c -> (* (kp) end *)
3774 let first = max 0 (itemcount - fstate.maxrows) in
3775 let active = find (itemcount - 1) ~-1 in
3776 G.postRedisplay "listview end";
3777 set active first;
3779 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3780 coe self
3782 | _ ->
3783 dolog "listview unknown key %#x" key; coe self
3785 method key key mask =
3786 match state.mode with
3787 | Textentry te -> textentrykeyboard key mask te; coe self
3788 | _ -> self#key1 key mask
3790 method button button down x y _ =
3791 let opt =
3792 match button with
3793 | 1 when x > state.winw - conf.scrollbw ->
3794 G.postRedisplay "listview scroll";
3795 if down
3796 then
3797 let _, position, sh = self#scrollph in
3798 if y > truncate position && y < truncate (position +. sh)
3799 then (
3800 state.mstate <- Mscrolly;
3801 Some (coe self)
3803 else
3804 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3805 let first = truncate (s *. float source#getitemcount) in
3806 let first = min source#getitemcount first in
3807 Some (coe {< m_first = first; m_active = first >})
3808 else (
3809 state.mstate <- Mnone;
3810 Some (coe self);
3812 | 1 when not down ->
3813 begin match self#elemunder y with
3814 | Some n ->
3815 G.postRedisplay "listview click";
3816 source#exit
3817 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3818 | _ ->
3819 Some (coe self)
3821 | n when (n == 4 || n == 5) && not down ->
3822 let len = source#getitemcount in
3823 let first =
3824 if n = 5 && m_first + fstate.maxrows >= len
3825 then
3826 m_first
3827 else
3828 let first = m_first + (if n == 4 then -1 else 1) in
3829 bound first 0 (len - 1)
3831 G.postRedisplay "listview wheel";
3832 Some (coe {< m_first = first >})
3833 | n when (n = 6 || n = 7) && not down ->
3834 let inc = if n = 7 then -1 else 1 in
3835 G.postRedisplay "listview hwheel";
3836 Some (coe {< m_pan = m_pan + inc >})
3837 | _ ->
3838 Some (coe self)
3840 match opt with
3841 | None -> m_prev_uioh
3842 | Some uioh -> uioh
3844 method motion _ y =
3845 match state.mstate with
3846 | Mscrolly ->
3847 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3848 let first = truncate (s *. float source#getitemcount) in
3849 let first = min source#getitemcount first in
3850 G.postRedisplay "listview motion";
3851 coe {< m_first = first; m_active = first >}
3852 | _ -> coe self
3854 method pmotion x y =
3855 if x < state.winw - conf.scrollbw
3856 then
3857 let n =
3858 match self#elemunder y with
3859 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3860 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3862 let o =
3863 if n != m_active
3864 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3865 else self
3867 coe o
3868 else (
3869 Wsi.setcursor Wsi.CURSOR_INHERIT;
3870 coe self
3873 method infochanged _ = ()
3875 method scrollpw = (0, 0.0, 0.0)
3876 method scrollph =
3877 let nfs = fstate.fontsize + 1 in
3878 let y = m_first * nfs in
3879 let itemcount = source#getitemcount in
3880 let maxi = max 0 (itemcount - fstate.maxrows) in
3881 let maxy = maxi * nfs in
3882 let p, h = scrollph y maxy in
3883 conf.scrollbw, p, h
3885 method modehash = modehash
3886 method eformsgs = false
3887 end;;
3889 class outlinelistview ~source =
3890 let settext autonarrow s =
3891 if autonarrow
3892 then state.text <- "[" ^ s ^ "]"
3893 else state.text <- s
3895 object (self)
3896 inherit listview
3897 ~source:(source :> lvsource)
3898 ~trusted:false
3899 ~modehash:(findkeyhash conf "outline")
3900 as super
3902 val m_autonarrow = false
3904 method key key mask =
3905 let maxrows =
3906 if emptystr state.text
3907 then fstate.maxrows
3908 else fstate.maxrows - 2
3910 let calcfirst first active =
3911 if active > first
3912 then
3913 let rows = active - first in
3914 if rows > maxrows then active - maxrows else first
3915 else active
3917 let navigate incr =
3918 let active = m_active + incr in
3919 let active = bound active 0 (source#getitemcount - 1) in
3920 let first = calcfirst m_first active in
3921 G.postRedisplay "outline navigate";
3922 coe {< m_active = active; m_first = first >}
3924 let navscroll first =
3925 let active =
3926 let dist = m_active - first in
3927 if dist < 0
3928 then first
3929 else (
3930 if dist < maxrows
3931 then m_active
3932 else first + maxrows
3935 G.postRedisplay "outline navscroll";
3936 coe {< m_first = first; m_active = active >}
3938 let ctrl = Wsi.withctrl mask in
3939 match key with
3940 | 97 when ctrl -> (* ctrl-a *)
3941 if m_autonarrow
3942 then source#denarrow
3943 else source#narrow m_qsearch;
3944 settext (not m_autonarrow) m_qsearch;
3945 G.postRedisplay "toggle auto narrowing";
3946 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3948 | 47 when emptystr m_qsearch && not m_autonarrow -> (* / *)
3949 settext true "";
3950 G.postRedisplay "toggle auto narrowing";
3951 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3953 | 110 when ctrl -> (* ctrl-n *)
3954 source#narrow m_qsearch;
3955 if not m_autonarrow
3956 then source#add_narrow_pattern m_qsearch;
3957 G.postRedisplay "outline ctrl-n";
3958 coe {< m_first = 0; m_active = 0 >}
3960 | 115 when ctrl -> (* ctrl-s *)
3961 let active = source#calcactive (getanchor ()) in
3962 let first = firstof m_first active in
3963 G.postRedisplay "outline ctrl-s";
3964 coe {< m_first = first; m_active = active >}
3966 | 117 when ctrl -> (* ctrl-u *)
3967 source#del_narrow_pattern;
3968 let pattern = source#renarrow in
3969 G.postRedisplay "outline ctrl-u";
3970 let text =
3971 if emptystr pattern then "" else "Narrowed to " ^ pattern
3973 settext m_autonarrow text;
3974 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
3976 | 108 when ctrl -> (* ctrl-l *)
3977 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3978 G.postRedisplay "outline ctrl-l";
3979 coe {< m_first = first >}
3981 | 0xff1b -> (* escape *)
3982 let o = super#key key mask in
3983 if m_autonarrow
3984 then (
3985 if nonemptystr m_qsearch
3986 then (
3987 source#add_narrow_pattern m_qsearch;
3988 settext true "";
3993 | 0xff0d | 0xff8d when m_autonarrow -> (* (kp) enter *)
3994 if nonemptystr m_qsearch
3995 then source#add_narrow_pattern m_qsearch;
3996 super#key key mask
3998 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3999 let pattern = m_qsearch ^ toutf8 key in
4000 G.postRedisplay "outlinelistview autonarrow add";
4001 source#narrow pattern;
4002 settext true pattern;
4003 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4005 | key when m_autonarrow && key = 0xff08 -> (* backspace *)
4006 if emptystr m_qsearch
4007 then coe self
4008 else
4009 let pattern = withoutlastutf8 m_qsearch in
4010 G.postRedisplay "outlinelistview autonarrow backspace";
4011 ignore (source#renarrow);
4012 source#narrow pattern;
4013 settext true pattern;
4014 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4016 | 0xff9f | 0xffff -> (* (kp) delete *)
4017 source#remove m_active;
4018 G.postRedisplay "outline delete";
4019 let active = max 0 (m_active-1) in
4020 coe {< m_first = firstof m_first active;
4021 m_active = active >}
4023 | 0xff52 | 0xff97 when ctrl -> (* ctrl-(kp) up *)
4024 navscroll (max 0 (m_first - 1))
4026 | 0xff54 | 0xff99 when ctrl -> (* ctrl-(kp) down *)
4027 navscroll (min (source#getitemcount - 1) (m_first + 1))
4029 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
4030 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
4031 | 0xff55 | 0xff9a -> (* (kp) prior *)
4032 navigate ~-(fstate.maxrows)
4033 | 0xff56 | 0xff9b -> (* (kp) next *)
4034 navigate fstate.maxrows
4036 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
4037 let o =
4038 if ctrl
4039 then (
4040 G.postRedisplay "outline ctrl right";
4041 {< m_pan = m_pan + 1 >}
4043 else self#updownlevel 1
4045 coe o
4047 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
4048 let o =
4049 if ctrl
4050 then (
4051 G.postRedisplay "outline ctrl left";
4052 {< m_pan = m_pan - 1 >}
4054 else self#updownlevel ~-1
4056 coe o
4058 | 0xff50 | 0xff95 -> (* (kp) home *)
4059 G.postRedisplay "outline home";
4060 coe {< m_first = 0; m_active = 0 >}
4062 | 0xff57 | 0xff9c -> (* (kp) end *)
4063 let active = source#getitemcount - 1 in
4064 let first = max 0 (active - fstate.maxrows) in
4065 G.postRedisplay "outline end";
4066 coe {< m_active = active; m_first = first >}
4068 | _ -> super#key key mask
4071 let outlinesource usebookmarks =
4072 let empty = [||] in
4073 (object (self)
4074 inherit lvsourcebase
4075 val mutable m_items = empty
4076 val mutable m_orig_items = empty
4077 val mutable m_narrow_patterns = []
4078 val mutable m_hadremovals = false
4080 method getitemcount =
4081 Array.length m_items + (if m_hadremovals then 1 else 0)
4083 method getitem n =
4084 if n == Array.length m_items && m_hadremovals
4085 then
4086 ("[Confirm removal]", 0)
4087 else
4088 let s, n, _ = m_items.(n) in
4089 (s, n)
4091 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4092 ignore (uioh, first, qsearch);
4093 let confrimremoval = m_hadremovals && active = Array.length m_items in
4094 let items =
4095 if m_narrow_patterns = []
4096 then m_orig_items
4097 else m_items
4099 if not cancel
4100 then (
4101 if not confrimremoval
4102 then (
4103 let _, _, ((pageno, y, _) as anchor) = m_items.(active) in
4104 let y = getanchory
4105 (if conf.presentation then (pageno, y, 1.0) else anchor)
4107 gotoghyll y;
4108 m_items <- items;
4110 else (
4111 state.bookmarks <- Array.to_list m_items;
4112 m_orig_items <- m_items;
4115 else m_items <- items;
4116 m_pan <- pan;
4117 None
4119 method hasaction _ = true
4121 method greetmsg =
4122 if Array.length m_items != Array.length m_orig_items
4123 then
4124 let s =
4125 match m_narrow_patterns with
4126 | one :: [] -> one
4127 | many -> String.concat "\xe2\x80\xa6" (List.rev many)
4129 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4130 else ""
4132 method narrow pattern =
4133 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4134 match reopt with
4135 | None -> ()
4136 | Some re ->
4137 let rec loop accu n =
4138 if n = -1
4139 then m_items <- Array.of_list accu
4140 else
4141 let (s, _, _) as o = m_items.(n) in
4142 let accu =
4143 if (try ignore (Str.search_forward re s 0); true
4144 with Not_found -> false)
4145 then o :: accu
4146 else accu
4148 loop accu (n-1)
4150 loop [] (Array.length m_items - 1)
4152 method denarrow =
4153 m_orig_items <- (
4154 if usebookmarks
4155 then Array.of_list state.bookmarks
4156 else state.outlines
4158 m_items <- m_orig_items
4160 method remove m =
4161 if usebookmarks
4162 then
4163 if m >= 0 && m < Array.length m_items
4164 then (
4165 m_hadremovals <- true;
4166 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4167 let n = if n >= m then n+1 else n in
4168 m_items.(n)
4172 method add_narrow_pattern pattern =
4173 m_narrow_patterns <- pattern :: m_narrow_patterns
4175 method del_narrow_pattern =
4176 match m_narrow_patterns with
4177 | _ :: rest -> m_narrow_patterns <- rest
4178 | [] -> ()
4180 method renarrow =
4181 self#denarrow;
4182 match m_narrow_patterns with
4183 | pattern :: [] -> self#narrow pattern; pattern
4184 | list ->
4185 List.fold_left (fun accu pattern ->
4186 self#narrow pattern;
4187 pattern ^ "\xe2\x80\xa6" ^ accu) "" list
4189 method calcactive anchor =
4190 let rely = getanchory anchor in
4191 let rec loop n best bestd =
4192 if n = Array.length m_items
4193 then best
4194 else
4195 let (_, _, anchor) = m_items.(n) in
4196 let orely = getanchory anchor in
4197 let d = abs (orely - rely) in
4198 if d < bestd
4199 then loop (n+1) n d
4200 else loop (n+1) best bestd
4202 loop 0 ~-1 max_int
4204 method reset anchor items =
4205 m_hadremovals <- false;
4206 if m_orig_items == empty
4207 then (
4208 m_orig_items <- items;
4209 if m_narrow_patterns == []
4210 then m_items <- items;
4212 let active = self#calcactive anchor in
4213 m_active <- active;
4214 m_first <- firstof m_first active
4215 end)
4218 let enterselector usebookmarks =
4219 let source = outlinesource usebookmarks in
4220 fun errmsg ->
4221 let outlines =
4222 if usebookmarks
4223 then Array.of_list state.bookmarks
4224 else state.outlines
4226 if Array.length outlines = 0
4227 then (
4228 showtext ' ' errmsg;
4230 else (
4231 state.text <- source#greetmsg;
4232 Wsi.setcursor Wsi.CURSOR_INHERIT;
4233 let anchor = getanchor () in
4234 source#reset anchor outlines;
4235 state.uioh <- coe (new outlinelistview ~source);
4236 G.postRedisplay "enter selector";
4240 let enteroutlinemode =
4241 let f = enterselector false in
4242 fun ()-> f "Document has no outline";
4245 let enterbookmarkmode =
4246 let f = enterselector true in
4247 fun () -> f "Document has no bookmarks (yet)";
4250 let color_of_string s =
4251 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4252 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4256 let color_to_string (r, g, b) =
4257 let r = truncate (r *. 256.0)
4258 and g = truncate (g *. 256.0)
4259 and b = truncate (b *. 256.0) in
4260 Printf.sprintf "%d/%d/%d" r g b
4263 let irect_of_string s =
4264 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4267 let irect_to_string (x0,y0,x1,y1) =
4268 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4271 let makecheckers () =
4272 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4273 following to say:
4274 converted by Issac Trotts. July 25, 2002 *)
4275 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4276 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4277 let id = GlTex.gen_texture () in
4278 GlTex.bind_texture `texture_2d id;
4279 GlPix.store (`unpack_alignment 1);
4280 GlTex.image2d image;
4281 List.iter (GlTex.parameter ~target:`texture_2d)
4282 [ `mag_filter `nearest; `min_filter `nearest ];
4286 let setcheckers enabled =
4287 match state.texid with
4288 | None ->
4289 if enabled then state.texid <- Some (makecheckers ())
4291 | Some texid ->
4292 if not enabled
4293 then (
4294 GlTex.delete_texture texid;
4295 state.texid <- None;
4299 let int_of_string_with_suffix s =
4300 let l = String.length s in
4301 let s1, shift =
4302 if l > 1
4303 then
4304 let suffix = Char.lowercase s.[l-1] in
4305 match suffix with
4306 | 'k' -> String.sub s 0 (l-1), 10
4307 | 'm' -> String.sub s 0 (l-1), 20
4308 | 'g' -> String.sub s 0 (l-1), 30
4309 | _ -> s, 0
4310 else s, 0
4312 let n = int_of_string s1 in
4313 let m = n lsl shift in
4314 if m < 0 || m < n
4315 then raise (Failure "value too large")
4316 else m
4319 let string_with_suffix_of_int n =
4320 if n = 0
4321 then "0"
4322 else
4323 let units = [(30, "G"); (20, "M"); (10, "K")] in
4324 let prettyint n =
4325 let rec loop s n =
4326 let h = n mod 1000 in
4327 let n = n / 1000 in
4328 if n = 0
4329 then string_of_int h ^ s
4330 else (
4331 let s = Printf.sprintf "_%03d%s" h s in
4332 loop s n
4335 loop "" n
4337 let rec find = function
4338 | [] -> prettyint n
4339 | (shift, suffix) :: rest ->
4340 if (n land ((1 lsl shift) - 1)) = 0
4341 then prettyint (n lsr shift) ^ suffix
4342 else find rest
4344 find units
4347 let defghyllscroll = (40, 8, 32);;
4348 let fastghyllscroll = (5,1,2);;
4349 let neatghyllscroll = (10,1,9);;
4350 let ghyllscroll_of_string s =
4351 match s with
4352 | "default" -> Some defghyllscroll
4353 | "fast" -> Some (5,1,2)
4354 | "neat" -> Some (10,1,9)
4355 | "" | "none" -> None
4356 | _ ->
4357 let (n,a,b) as nab =
4358 Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b) in
4359 if n <= a || n <= b || a >= b
4360 then error "invalid ghyll N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)"
4361 n a b;
4362 Some nab
4365 let ghyllscroll_to_string ((n, a, b) as nab) =
4366 (**) if nab = defghyllscroll then "default"
4367 else if nab = fastghyllscroll then "fast"
4368 else if nab = neatghyllscroll then "neat"
4369 else Printf.sprintf "%d,%d,%d" n a b;
4372 let describe_location () =
4373 let fn = page_of_y state.y in
4374 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4375 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4376 let percent =
4377 if maxy <= 0
4378 then 100.
4379 else (100. *. (float state.y /. float maxy))
4381 if fn = ln
4382 then
4383 Printf.sprintf "page %d of %d [%.2f%%]"
4384 (fn+1) state.pagecount percent
4385 else
4386 Printf.sprintf
4387 "pages %d-%d of %d [%.2f%%]"
4388 (fn+1) (ln+1) state.pagecount percent
4391 let setpresentationmode v =
4392 let n = page_of_y state.y in
4393 state.anchor <- (n, 0.0, 1.0);
4394 conf.presentation <- v;
4395 if conf.fitmodel = FitPage
4396 then reqlayout conf.angle conf.fitmodel;
4397 represent ();
4400 let enterinfomode =
4401 let btos b = if b then "\xe2\x88\x9a" else "" in
4402 let showextended = ref false in
4403 let leave mode = function
4404 | Confirm -> state.mode <- mode
4405 | Cancel -> state.mode <- mode in
4406 let src =
4407 (object
4408 val mutable m_first_time = true
4409 val mutable m_l = []
4410 val mutable m_a = [||]
4411 val mutable m_prev_uioh = nouioh
4412 val mutable m_prev_mode = View
4414 inherit lvsourcebase
4416 method reset prev_mode prev_uioh =
4417 m_a <- Array.of_list (List.rev m_l);
4418 m_l <- [];
4419 m_prev_mode <- prev_mode;
4420 m_prev_uioh <- prev_uioh;
4421 if m_first_time
4422 then (
4423 let rec loop n =
4424 if n >= Array.length m_a
4425 then ()
4426 else
4427 match m_a.(n) with
4428 | _, _, _, Action _ -> m_active <- n
4429 | _ -> loop (n+1)
4431 loop 0;
4432 m_first_time <- false;
4435 method int name get set =
4436 m_l <-
4437 (name, `int get, 1, Action (
4438 fun u ->
4439 let ondone s =
4440 try set (int_of_string s)
4441 with exn ->
4442 state.text <- Printf.sprintf "bad integer `%s': %s"
4443 s (exntos exn)
4445 state.text <- "";
4446 let te = name ^ ": ", "", None, intentry, ondone, true in
4447 state.mode <- Textentry (te, leave m_prev_mode);
4449 )) :: m_l
4451 method int_with_suffix name get set =
4452 m_l <-
4453 (name, `intws get, 1, Action (
4454 fun u ->
4455 let ondone s =
4456 try set (int_of_string_with_suffix s)
4457 with exn ->
4458 state.text <- Printf.sprintf "bad integer `%s': %s"
4459 s (exntos exn)
4461 state.text <- "";
4462 let te =
4463 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4465 state.mode <- Textentry (te, leave m_prev_mode);
4467 )) :: m_l
4469 method bool ?(offset=1) ?(btos=btos) name get set =
4470 m_l <-
4471 (name, `bool (btos, get), offset, Action (
4472 fun u ->
4473 let v = get () in
4474 set (not v);
4476 )) :: m_l
4478 method color name get set =
4479 m_l <-
4480 (name, `color get, 1, Action (
4481 fun u ->
4482 let invalid = (nan, nan, nan) in
4483 let ondone s =
4484 let c =
4485 try color_of_string s
4486 with exn ->
4487 state.text <- Printf.sprintf "bad color `%s': %s"
4488 s (exntos exn);
4489 invalid
4491 if c <> invalid
4492 then set c;
4494 let te = name ^ ": ", "", None, textentry, ondone, true in
4495 state.text <- color_to_string (get ());
4496 state.mode <- Textentry (te, leave m_prev_mode);
4498 )) :: m_l
4500 method string name get set =
4501 m_l <-
4502 (name, `string get, 1, Action (
4503 fun u ->
4504 let ondone s = set s in
4505 let te = name ^ ": ", "", None, textentry, ondone, true in
4506 state.mode <- Textentry (te, leave m_prev_mode);
4508 )) :: m_l
4510 method colorspace name get set =
4511 m_l <-
4512 (name, `string get, 1, Action (
4513 fun _ ->
4514 let source =
4515 (object
4516 inherit lvsourcebase
4518 initializer
4519 m_active <- CSTE.to_int conf.colorspace;
4520 m_first <- 0;
4522 method getitemcount =
4523 Array.length CSTE.names
4524 method getitem n =
4525 (CSTE.names.(n), 0)
4526 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4527 ignore (uioh, first, pan, qsearch);
4528 if not cancel then set active;
4529 None
4530 method hasaction _ = true
4531 end)
4533 state.text <- "";
4534 let modehash = findkeyhash conf "info" in
4535 coe (new listview ~source ~trusted:true ~modehash)
4536 )) :: m_l
4538 method paxmark name get set =
4539 m_l <-
4540 (name, `string get, 1, Action (
4541 fun _ ->
4542 let source =
4543 (object
4544 inherit lvsourcebase
4546 initializer
4547 m_active <- MTE.to_int conf.paxmark;
4548 m_first <- 0;
4550 method getitemcount = Array.length MTE.names
4551 method getitem n = (MTE.names.(n), 0)
4552 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4553 ignore (uioh, first, pan, qsearch);
4554 if not cancel then set active;
4555 None
4556 method hasaction _ = true
4557 end)
4559 state.text <- "";
4560 let modehash = findkeyhash conf "info" in
4561 coe (new listview ~source ~trusted:true ~modehash)
4562 )) :: m_l
4564 method fitmodel name get set =
4565 m_l <-
4566 (name, `string get, 1, Action (
4567 fun _ ->
4568 let source =
4569 (object
4570 inherit lvsourcebase
4572 initializer
4573 m_active <- FMTE.to_int conf.fitmodel;
4574 m_first <- 0;
4576 method getitemcount = Array.length FMTE.names
4577 method getitem n = (FMTE.names.(n), 0)
4578 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4579 ignore (uioh, first, pan, qsearch);
4580 if not cancel then set active;
4581 None
4582 method hasaction _ = true
4583 end)
4585 state.text <- "";
4586 let modehash = findkeyhash conf "info" in
4587 coe (new listview ~source ~trusted:true ~modehash)
4588 )) :: m_l
4590 method caption s offset =
4591 m_l <- (s, `empty, offset, Noaction) :: m_l
4593 method caption2 s f offset =
4594 m_l <- (s, `string f, offset, Noaction) :: m_l
4596 method getitemcount = Array.length m_a
4598 method getitem n =
4599 let tostr = function
4600 | `int f -> string_of_int (f ())
4601 | `intws f -> string_with_suffix_of_int (f ())
4602 | `string f -> f ()
4603 | `color f -> color_to_string (f ())
4604 | `bool (btos, f) -> btos (f ())
4605 | `empty -> ""
4607 let name, t, offset, _ = m_a.(n) in
4608 ((let s = tostr t in
4609 if nonemptystr s
4610 then Printf.sprintf "%s\t%s" name s
4611 else name),
4612 offset)
4614 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4615 let uiohopt =
4616 if not cancel
4617 then (
4618 m_qsearch <- qsearch;
4619 let uioh =
4620 match m_a.(active) with
4621 | _, _, _, Action f -> f uioh
4622 | _ -> uioh
4624 Some uioh
4626 else None
4628 m_active <- active;
4629 m_first <- first;
4630 m_pan <- pan;
4631 uiohopt
4633 method hasaction n =
4634 match m_a.(n) with
4635 | _, _, _, Action _ -> true
4636 | _ -> false
4637 end)
4639 let rec fillsrc prevmode prevuioh =
4640 let sep () = src#caption "" 0 in
4641 let colorp name get set =
4642 src#string name
4643 (fun () -> color_to_string (get ()))
4644 (fun v ->
4646 let c = color_of_string v in
4647 set c
4648 with exn ->
4649 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4652 let oldmode = state.mode in
4653 let birdseye = isbirdseye state.mode in
4655 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4657 src#bool "presentation mode"
4658 (fun () -> conf.presentation)
4659 (fun v -> setpresentationmode v);
4661 src#bool "ignore case in searches"
4662 (fun () -> conf.icase)
4663 (fun v -> conf.icase <- v);
4665 src#bool "preload"
4666 (fun () -> conf.preload)
4667 (fun v -> conf.preload <- v);
4669 src#bool "highlight links"
4670 (fun () -> conf.hlinks)
4671 (fun v -> conf.hlinks <- v);
4673 src#bool "under info"
4674 (fun () -> conf.underinfo)
4675 (fun v -> conf.underinfo <- v);
4677 src#bool "persistent bookmarks"
4678 (fun () -> conf.savebmarks)
4679 (fun v -> conf.savebmarks <- v);
4681 src#fitmodel "fit model"
4682 (fun () -> FMTE.to_string conf.fitmodel)
4683 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4685 src#bool "trim margins"
4686 (fun () -> conf.trimmargins)
4687 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4689 src#bool "persistent location"
4690 (fun () -> conf.jumpback)
4691 (fun v -> conf.jumpback <- v);
4693 sep ();
4694 src#int "inter-page space"
4695 (fun () -> conf.interpagespace)
4696 (fun n ->
4697 conf.interpagespace <- n;
4698 docolumns conf.columns;
4699 let pageno, py =
4700 match state.layout with
4701 | [] -> 0, 0
4702 | l :: _ ->
4703 l.pageno, l.pagey
4705 state.maxy <- calcheight ();
4706 let y = getpagey pageno in
4707 gotoy (y + py)
4710 src#int "page bias"
4711 (fun () -> conf.pagebias)
4712 (fun v -> conf.pagebias <- v);
4714 src#int "scroll step"
4715 (fun () -> conf.scrollstep)
4716 (fun n -> conf.scrollstep <- n);
4718 src#int "horizontal scroll step"
4719 (fun () -> conf.hscrollstep)
4720 (fun v -> conf.hscrollstep <- v);
4722 src#int "auto scroll step"
4723 (fun () ->
4724 match state.autoscroll with
4725 | Some step -> step
4726 | _ -> conf.autoscrollstep)
4727 (fun n ->
4728 if state.autoscroll <> None
4729 then state.autoscroll <- Some n;
4730 conf.autoscrollstep <- n);
4732 src#int "zoom"
4733 (fun () -> truncate (conf.zoom *. 100.))
4734 (fun v -> setzoom ((float v) /. 100.));
4736 src#int "rotation"
4737 (fun () -> conf.angle)
4738 (fun v -> reqlayout v conf.fitmodel);
4740 src#int "scroll bar width"
4741 (fun () -> conf.scrollbw)
4742 (fun v ->
4743 conf.scrollbw <- v;
4744 reshape state.winw state.winh;
4747 src#int "scroll handle height"
4748 (fun () -> conf.scrollh)
4749 (fun v -> conf.scrollh <- v;);
4751 src#int "thumbnail width"
4752 (fun () -> conf.thumbw)
4753 (fun v ->
4754 conf.thumbw <- min 4096 v;
4755 match oldmode with
4756 | Birdseye beye ->
4757 leavebirdseye beye false;
4758 enterbirdseye ()
4759 | _ -> ()
4762 let mode = state.mode in
4763 src#string "columns"
4764 (fun () ->
4765 match conf.columns with
4766 | Csingle _ -> "1"
4767 | Cmulti (multi, _) -> multicolumns_to_string multi
4768 | Csplit (count, _) -> "-" ^ string_of_int count
4770 (fun v ->
4771 let n, a, b = multicolumns_of_string v in
4772 setcolumns mode n a b);
4774 sep ();
4775 src#caption "Pixmap cache" 0;
4776 src#int_with_suffix "size (advisory)"
4777 (fun () -> conf.memlimit)
4778 (fun v -> conf.memlimit <- v);
4780 src#caption2 "used"
4781 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4782 (string_with_suffix_of_int state.memused)
4783 (Hashtbl.length state.tilemap)) 1;
4785 sep ();
4786 src#caption "Layout" 0;
4787 src#caption2 "Dimension"
4788 (fun () ->
4789 Printf.sprintf "%dx%d (virtual %dx%d)"
4790 state.winw state.winh
4791 state.w state.maxy)
4793 if conf.debug
4794 then
4795 src#caption2 "Position" (fun () ->
4796 Printf.sprintf "%dx%d" state.x state.y
4798 else
4799 src#caption2 "Position" (fun () -> describe_location ()) 1
4802 sep ();
4803 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4804 "Save these parameters as global defaults at exit"
4805 (fun () -> conf.bedefault)
4806 (fun v -> conf.bedefault <- v)
4809 sep ();
4810 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4811 src#bool ~offset:0 ~btos "Extended parameters"
4812 (fun () -> !showextended)
4813 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4814 if !showextended
4815 then (
4816 src#bool "checkers"
4817 (fun () -> conf.checkers)
4818 (fun v -> conf.checkers <- v; setcheckers v);
4819 src#bool "update cursor"
4820 (fun () -> conf.updatecurs)
4821 (fun v -> conf.updatecurs <- v);
4822 src#bool "verbose"
4823 (fun () -> conf.verbose)
4824 (fun v -> conf.verbose <- v);
4825 src#bool "invert colors"
4826 (fun () -> conf.invert)
4827 (fun v -> conf.invert <- v);
4828 src#bool "max fit"
4829 (fun () -> conf.maxhfit)
4830 (fun v -> conf.maxhfit <- v);
4831 src#bool "redirect stderr"
4832 (fun () -> conf.redirectstderr)
4833 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4834 src#bool "pax mode"
4835 (fun () -> conf.pax != None)
4836 (fun v ->
4837 if v
4838 then conf.pax <- Some (ref (now (), 0, 0))
4839 else conf.pax <- None);
4840 src#string "uri launcher"
4841 (fun () -> conf.urilauncher)
4842 (fun v -> conf.urilauncher <- v);
4843 src#string "path launcher"
4844 (fun () -> conf.pathlauncher)
4845 (fun v -> conf.pathlauncher <- v);
4846 src#string "tile size"
4847 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4848 (fun v ->
4850 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4851 conf.tilew <- max 64 w;
4852 conf.tileh <- max 64 h;
4853 flushtiles ();
4854 with exn ->
4855 state.text <- Printf.sprintf "bad tile size `%s': %s"
4856 v (exntos exn)
4858 src#int "texture count"
4859 (fun () -> conf.texcount)
4860 (fun v ->
4861 if realloctexts v
4862 then conf.texcount <- v
4863 else showtext '!' " Failed to set texture count please retry later"
4865 src#int "slice height"
4866 (fun () -> conf.sliceheight)
4867 (fun v ->
4868 conf.sliceheight <- v;
4869 wcmd "sliceh %d" conf.sliceheight;
4871 src#int "anti-aliasing level"
4872 (fun () -> conf.aalevel)
4873 (fun v ->
4874 conf.aalevel <- bound v 0 8;
4875 state.anchor <- getanchor ();
4876 opendoc state.path state.password;
4878 src#string "page scroll scaling factor"
4879 (fun () -> string_of_float conf.pgscale)
4880 (fun v ->
4882 let s = float_of_string v in
4883 conf.pgscale <- s
4884 with exn ->
4885 state.text <- Printf.sprintf
4886 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4889 src#int "ui font size"
4890 (fun () -> fstate.fontsize)
4891 (fun v -> setfontsize (bound v 5 100));
4892 src#int "hint font size"
4893 (fun () -> conf.hfsize)
4894 (fun v -> conf.hfsize <- bound v 5 100);
4895 colorp "background color"
4896 (fun () -> conf.bgcolor)
4897 (fun v -> conf.bgcolor <- v);
4898 src#bool "crop hack"
4899 (fun () -> conf.crophack)
4900 (fun v -> conf.crophack <- v);
4901 src#string "trim fuzz"
4902 (fun () -> irect_to_string conf.trimfuzz)
4903 (fun v ->
4905 conf.trimfuzz <- irect_of_string v;
4906 if conf.trimmargins
4907 then settrim true conf.trimfuzz;
4908 with exn ->
4909 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4911 src#string "throttle"
4912 (fun () ->
4913 match conf.maxwait with
4914 | None -> "show place holder if page is not ready"
4915 | Some time ->
4916 if time = infinity
4917 then "wait for page to fully render"
4918 else
4919 "wait " ^ string_of_float time
4920 ^ " seconds before showing placeholder"
4922 (fun v ->
4924 let f = float_of_string v in
4925 if f <= 0.0
4926 then conf.maxwait <- None
4927 else conf.maxwait <- Some f
4928 with exn ->
4929 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4931 src#string "ghyll scroll"
4932 (fun () ->
4933 match conf.ghyllscroll with
4934 | None -> ""
4935 | Some nab -> ghyllscroll_to_string nab
4937 (fun v ->
4938 try conf.ghyllscroll <- ghyllscroll_of_string v
4939 with exn ->
4940 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4942 src#string "selection command"
4943 (fun () -> conf.selcmd)
4944 (fun v -> conf.selcmd <- v);
4945 src#string "synctex command"
4946 (fun () -> conf.stcmd)
4947 (fun v -> conf.stcmd <- v);
4948 src#string "pax command"
4949 (fun () -> conf.paxcmd)
4950 (fun v -> conf.paxcmd <- v);
4951 src#colorspace "color space"
4952 (fun () -> CSTE.to_string conf.colorspace)
4953 (fun v ->
4954 conf.colorspace <- CSTE.of_int v;
4955 wcmd "cs %d" v;
4956 load state.layout;
4958 src#paxmark "pax mark method"
4959 (fun () -> MTE.to_string conf.paxmark)
4960 (fun v -> conf.paxmark <- MTE.of_int v);
4961 if pbousable ()
4962 then
4963 src#bool "use PBO"
4964 (fun () -> conf.usepbo)
4965 (fun v -> conf.usepbo <- v);
4966 src#bool "mouse wheel scrolls pages"
4967 (fun () -> conf.wheelbypage)
4968 (fun v -> conf.wheelbypage <- v);
4969 src#bool "open remote links in a new instance"
4970 (fun () -> conf.riani)
4971 (fun v -> conf.riani <- v);
4974 sep ();
4975 src#caption "Document" 0;
4976 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4977 src#caption2 "Pages"
4978 (fun () -> string_of_int state.pagecount) 1;
4979 src#caption2 "Dimensions"
4980 (fun () -> string_of_int (List.length state.pdims)) 1;
4981 if conf.trimmargins
4982 then (
4983 sep ();
4984 src#caption "Trimmed margins" 0;
4985 src#caption2 "Dimensions"
4986 (fun () -> string_of_int (List.length state.pdims)) 1;
4989 sep ();
4990 src#caption "OpenGL" 0;
4991 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4992 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4994 sep ();
4995 src#caption "Location" 0;
4996 if nonemptystr state.origin
4997 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4998 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
5000 src#reset prevmode prevuioh;
5002 fun () ->
5003 state.text <- "";
5004 let prevmode = state.mode
5005 and prevuioh = state.uioh in
5006 fillsrc prevmode prevuioh;
5007 let source = (src :> lvsource) in
5008 let modehash = findkeyhash conf "info" in
5009 state.uioh <- coe (object (self)
5010 inherit listview ~source ~trusted:true ~modehash as super
5011 val mutable m_prevmemused = 0
5012 method infochanged = function
5013 | Memused ->
5014 if m_prevmemused != state.memused
5015 then (
5016 m_prevmemused <- state.memused;
5017 G.postRedisplay "memusedchanged";
5019 | Pdim -> G.postRedisplay "pdimchanged"
5020 | Docinfo -> fillsrc prevmode prevuioh
5022 method key key mask =
5023 if not (Wsi.withctrl mask)
5024 then
5025 match key with
5026 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
5027 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
5028 | _ -> super#key key mask
5029 else super#key key mask
5030 end);
5031 G.postRedisplay "info";
5034 let enterhelpmode =
5035 let source =
5036 (object
5037 inherit lvsourcebase
5038 method getitemcount = Array.length state.help
5039 method getitem n =
5040 let s, l, _ = state.help.(n) in
5041 (s, l)
5043 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5044 let optuioh =
5045 if not cancel
5046 then (
5047 m_qsearch <- qsearch;
5048 match state.help.(active) with
5049 | _, _, Action f -> Some (f uioh)
5050 | _ -> Some (uioh)
5052 else None
5054 m_active <- active;
5055 m_first <- first;
5056 m_pan <- pan;
5057 optuioh
5059 method hasaction n =
5060 match state.help.(n) with
5061 | _, _, Action _ -> true
5062 | _ -> false
5064 initializer
5065 m_active <- -1
5066 end)
5067 in fun () ->
5068 let modehash = findkeyhash conf "help" in
5069 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
5070 G.postRedisplay "help";
5073 let entermsgsmode =
5074 let msgsource =
5075 let re = Str.regexp "[\r\n]" in
5076 (object
5077 inherit lvsourcebase
5078 val mutable m_items = [||]
5080 method getitemcount = 1 + Array.length m_items
5082 method getitem n =
5083 if n = 0
5084 then "[Clear]", 0
5085 else m_items.(n-1), 0
5087 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5088 ignore uioh;
5089 if not cancel
5090 then (
5091 if active = 0
5092 then Buffer.clear state.errmsgs;
5093 m_qsearch <- qsearch;
5095 m_active <- active;
5096 m_first <- first;
5097 m_pan <- pan;
5098 None
5100 method hasaction n =
5101 n = 0
5103 method reset =
5104 state.newerrmsgs <- false;
5105 let l = Str.split re (Buffer.contents state.errmsgs) in
5106 m_items <- Array.of_list l
5108 initializer
5109 m_active <- 0
5110 end)
5111 in fun () ->
5112 state.text <- "";
5113 msgsource#reset;
5114 let source = (msgsource :> lvsource) in
5115 let modehash = findkeyhash conf "listview" in
5116 state.uioh <- coe (object
5117 inherit listview ~source ~trusted:false ~modehash as super
5118 method display =
5119 if state.newerrmsgs
5120 then msgsource#reset;
5121 super#display
5122 end);
5123 G.postRedisplay "msgs";
5126 let quickbookmark ?title () =
5127 match state.layout with
5128 | [] -> ()
5129 | l :: _ ->
5130 let title =
5131 match title with
5132 | None ->
5133 let sec = Unix.gettimeofday () in
5134 let tm = Unix.localtime sec in
5135 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
5136 (l.pageno+1)
5137 tm.Unix.tm_mday
5138 tm.Unix.tm_mon
5139 (tm.Unix.tm_year + 1900)
5140 tm.Unix.tm_hour
5141 tm.Unix.tm_min
5142 | Some title -> title
5144 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
5147 let setautoscrollspeed step goingdown =
5148 let incr = max 1 ((abs step) / 2) in
5149 let incr = if goingdown then incr else -incr in
5150 let astep = step + incr in
5151 state.autoscroll <- Some astep;
5154 let gotounder under =
5155 let getpath filename =
5156 let path =
5157 if nonemptystr filename
5158 then
5159 if Filename.is_relative filename
5160 then
5161 let dir = Filename.dirname state.path in
5162 let dir =
5163 if Filename.is_implicit dir
5164 then Filename.concat (Sys.getcwd ()) dir
5165 else dir
5167 Filename.concat dir filename
5168 else filename
5169 else ""
5171 if Sys.file_exists path
5172 then path
5173 else ""
5175 match under with
5176 | Ulinkgoto (pageno, top) ->
5177 if pageno >= 0
5178 then (
5179 addnav ();
5180 gotopage1 pageno top;
5183 | Ulinkuri s ->
5184 gotouri s
5186 | Uremote (filename, pageno) ->
5187 let path = getpath filename in
5188 if nonemptystr path
5189 then (
5190 if conf.riani
5191 then
5192 let command = !selfexec ^ " " ^ path in
5193 try popen command []
5194 with exn ->
5195 Printf.eprintf
5196 "failed to execute `%s': %s\n" command (exntos exn);
5197 flush stderr;
5198 else
5199 let anchor = getanchor () in
5200 let ranchor = state.path, state.password, anchor, state.origin in
5201 state.origin <- "";
5202 state.anchor <- (pageno, 0.0, 0.0);
5203 state.ranchors <- ranchor :: state.ranchors;
5204 opendoc path "";
5206 else showtext '!' ("Could not find " ^ filename)
5208 | Uremotedest (filename, destname) ->
5209 let path = getpath filename in
5210 if nonemptystr path
5211 then (
5212 if conf.riani
5213 then
5214 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
5215 try popen command []
5216 with exn ->
5217 Printf.eprintf
5218 "failed to execute `%s': %s\n" command (exntos exn);
5219 flush stderr;
5220 else
5221 let anchor = getanchor () in
5222 let ranchor = state.path, state.password, anchor, state.origin in
5223 state.origin <- "";
5224 state.nameddest <- destname;
5225 state.ranchors <- ranchor :: state.ranchors;
5226 opendoc path "";
5228 else showtext '!' ("Could not find " ^ filename)
5230 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
5233 let canpan () =
5234 match conf.columns with
5235 | Csplit _ -> true
5236 | _ -> state.x != 0 || conf.zoom > 1.0
5239 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5241 let existsinrow pageno (columns, coverA, coverB) p =
5242 let last = ((pageno - coverA) mod columns) + columns in
5243 let rec any = function
5244 | [] -> false
5245 | l :: rest ->
5246 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5247 then p l
5248 else (
5249 if not (p l)
5250 then (if l.pageno = last then false else any rest)
5251 else true
5254 any state.layout
5257 let nextpage () =
5258 match state.layout with
5259 | [] ->
5260 let pageno = page_of_y state.y in
5261 gotoghyll (getpagey (pageno+1))
5262 | l :: rest ->
5263 match conf.columns with
5264 | Csingle _ ->
5265 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5266 then
5267 let y = clamp (pgscale state.winh) in
5268 gotoghyll y
5269 else
5270 let pageno = min (l.pageno+1) (state.pagecount-1) in
5271 gotoghyll (getpagey pageno)
5272 | Cmulti ((c, _, _) as cl, _) ->
5273 if conf.presentation
5274 && (existsinrow l.pageno cl
5275 (fun l -> l.pageh > l.pagey + l.pagevh))
5276 then
5277 let y = clamp (pgscale state.winh) in
5278 gotoghyll y
5279 else
5280 let pageno = min (l.pageno+c) (state.pagecount-1) in
5281 gotoghyll (getpagey pageno)
5282 | Csplit (n, _) ->
5283 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5284 then
5285 let pagey, pageh = getpageyh l.pageno in
5286 let pagey = pagey + pageh * l.pagecol in
5287 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5288 gotoghyll (pagey + pageh + ips)
5291 let prevpage () =
5292 match state.layout with
5293 | [] ->
5294 let pageno = page_of_y state.y in
5295 gotoghyll (getpagey (pageno-1))
5296 | l :: _ ->
5297 match conf.columns with
5298 | Csingle _ ->
5299 if conf.presentation && l.pagey != 0
5300 then
5301 gotoghyll (clamp (pgscale ~-(state.winh)))
5302 else
5303 let pageno = max 0 (l.pageno-1) in
5304 gotoghyll (getpagey pageno)
5305 | Cmulti ((c, _, coverB) as cl, _) ->
5306 if conf.presentation &&
5307 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5308 then
5309 gotoghyll (clamp (pgscale ~-(state.winh)))
5310 else
5311 let decr =
5312 if l.pageno = state.pagecount - coverB
5313 then 1
5314 else c
5316 let pageno = max 0 (l.pageno-decr) in
5317 gotoghyll (getpagey pageno)
5318 | Csplit (n, _) ->
5319 let y =
5320 if l.pagecol = 0
5321 then
5322 if l.pageno = 0
5323 then l.pagey
5324 else
5325 let pageno = max 0 (l.pageno-1) in
5326 let pagey, pageh = getpageyh pageno in
5327 pagey + (n-1)*pageh
5328 else
5329 let pagey, pageh = getpageyh l.pageno in
5330 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5332 gotoghyll y
5335 let viewkeyboard key mask =
5336 let enttext te =
5337 let mode = state.mode in
5338 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5339 state.text <- "";
5340 enttext ();
5341 G.postRedisplay "view:enttext"
5343 let ctrl = Wsi.withctrl mask in
5344 let key =
5345 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5347 match key with
5348 | 81 -> (* Q *)
5349 exit 0
5351 | 0xff63 -> (* insert *)
5352 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5353 then (
5354 state.mode <- LinkNav (Ltgendir 0);
5355 gotoy state.y;
5357 else showtext '!' "Keyboard link navigation does not work under rotation"
5359 | 0xff1b | 113 -> (* escape / q *)
5360 begin match state.mstate with
5361 | Mzoomrect _ ->
5362 state.mstate <- Mnone;
5363 Wsi.setcursor Wsi.CURSOR_INHERIT;
5364 G.postRedisplay "kill zoom rect";
5365 | _ ->
5366 begin match state.mode with
5367 | LinkNav _ ->
5368 state.mode <- View;
5369 G.postRedisplay "esc leave linknav"
5370 | _ ->
5371 match state.ranchors with
5372 | [] -> raise Quit
5373 | (path, password, anchor, origin) :: rest ->
5374 state.ranchors <- rest;
5375 state.anchor <- anchor;
5376 state.origin <- origin;
5377 state.nameddest <- "";
5378 opendoc path password
5379 end;
5380 end;
5382 | 0xff08 -> (* backspace *)
5383 gotoghyll (getnav ~-1)
5385 | 111 -> (* o *)
5386 enteroutlinemode ()
5388 | 117 -> (* u *)
5389 state.rects <- [];
5390 state.text <- "";
5391 G.postRedisplay "dehighlight";
5393 | 47 | 63 -> (* / ? *)
5394 let ondone isforw s =
5395 cbput state.hists.pat s;
5396 state.searchpattern <- s;
5397 search s isforw
5399 let s = String.create 1 in
5400 s.[0] <- Char.chr key;
5401 enttext (s, "", Some (onhist state.hists.pat),
5402 textentry, ondone (key = 47), true)
5404 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5405 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5406 setzoom (conf.zoom +. incr)
5408 | 43 | 0xffab -> (* + *)
5409 let ondone s =
5410 let n =
5411 try int_of_string s with exc ->
5412 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5413 max_int
5415 if n != max_int
5416 then (
5417 conf.pagebias <- n;
5418 state.text <- "page bias is now " ^ string_of_int n;
5421 enttext ("page bias: ", "", None, intentry, ondone, true)
5423 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5424 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5425 setzoom (max 0.01 (conf.zoom -. decr))
5427 | 45 | 0xffad -> (* - *)
5428 let ondone msg = state.text <- msg in
5429 enttext (
5430 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5431 optentry state.mode, ondone, true
5434 | 48 when ctrl -> (* ctrl-0 *)
5435 if conf.zoom = 1.0
5436 then (
5437 state.x <- 0;
5438 gotoy state.y
5440 else setzoom 1.0
5442 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5443 let cols =
5444 match conf.columns with
5445 | Csingle _ | Cmulti _ -> 1
5446 | Csplit (n, _) -> n
5448 let h = state.winh -
5449 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5451 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5452 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5453 then setzoom zoom
5455 | 51 when ctrl -> (* ctrl-3 *)
5456 let fm =
5457 match conf.fitmodel with
5458 | FitWidth -> FitProportional
5459 | FitProportional -> FitPage
5460 | FitPage -> FitWidth
5462 state.text <- "fit model: " ^ FMTE.to_string fm;
5463 reqlayout conf.angle fm
5465 | 0xffc6 -> (* f9 *)
5466 togglebirdseye ()
5468 | 57 when ctrl -> (* ctrl-9 *)
5469 togglebirdseye ()
5471 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5472 when not ctrl -> (* 0..9 *)
5473 let ondone s =
5474 let n =
5475 try int_of_string s with exc ->
5476 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5479 if n >= 0
5480 then (
5481 addnav ();
5482 cbput state.hists.pag (string_of_int n);
5483 gotopage1 (n + conf.pagebias - 1) 0;
5486 let pageentry text key =
5487 match Char.unsafe_chr key with
5488 | 'g' -> TEdone text
5489 | _ -> intentry text key
5491 let text = "x" in text.[0] <- Char.chr key;
5492 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5494 | 98 -> (* b *)
5495 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5496 reshape state.winw state.winh;
5498 | 66 -> (* B *)
5499 state.bzoom <- not state.bzoom;
5500 state.rects <- [];
5501 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5503 | 108 -> (* l *)
5504 conf.hlinks <- not conf.hlinks;
5505 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5506 G.postRedisplay "toggle highlightlinks";
5508 | 70 -> (* F *)
5509 state.glinks <- true;
5510 let mode = state.mode in
5511 state.mode <- Textentry (
5512 (":", "", None, linknentry, linkndone gotounder, false),
5513 (fun _ ->
5514 state.glinks <- false;
5515 state.mode <- mode)
5517 state.text <- "";
5518 G.postRedisplay "view:linkent(F)"
5520 | 121 -> (* y *)
5521 state.glinks <- true;
5522 let mode = state.mode in
5523 state.mode <- Textentry (
5525 ":", "", None, linknentry, linkndone (fun under ->
5526 selstring (undertext under);
5527 ), false
5529 fun _ ->
5530 state.glinks <- false;
5531 state.mode <- mode
5533 state.text <- "";
5534 G.postRedisplay "view:linkent"
5536 | 97 -> (* a *)
5537 begin match state.autoscroll with
5538 | Some step ->
5539 conf.autoscrollstep <- step;
5540 state.autoscroll <- None
5541 | None ->
5542 if conf.autoscrollstep = 0
5543 then state.autoscroll <- Some 1
5544 else state.autoscroll <- Some conf.autoscrollstep
5547 | 112 when ctrl -> (* ctrl-p *)
5548 launchpath ()
5550 | 80 -> (* P *)
5551 setpresentationmode (not conf.presentation);
5552 showtext ' ' ("presentation mode " ^
5553 if conf.presentation then "on" else "off");
5555 | 102 -> (* f *)
5556 if List.mem Wsi.Fullscreen state.winstate
5557 then Wsi.reshape conf.cwinw conf.cwinh
5558 else Wsi.fullscreen ()
5560 | 112 | 78 -> (* p|N *)
5561 search state.searchpattern false
5563 | 110 | 0xffc0 -> (* n|F3 *)
5564 search state.searchpattern true
5566 | 116 -> (* t *)
5567 begin match state.layout with
5568 | [] -> ()
5569 | l :: _ ->
5570 gotoghyll (getpagey l.pageno)
5573 | 32 -> (* space *)
5574 nextpage ()
5576 | 0xff9f | 0xffff -> (* delete *)
5577 prevpage ()
5579 | 61 -> (* = *)
5580 showtext ' ' (describe_location ());
5582 | 119 -> (* w *)
5583 begin match state.layout with
5584 | [] -> ()
5585 | l :: _ ->
5586 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5587 G.postRedisplay "w"
5590 | 39 -> (* ' *)
5591 enterbookmarkmode ()
5593 | 104 | 0xffbe -> (* h|F1 *)
5594 enterhelpmode ()
5596 | 105 -> (* i *)
5597 enterinfomode ()
5599 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5600 entermsgsmode ()
5602 | 109 -> (* m *)
5603 let ondone s =
5604 match state.layout with
5605 | l :: _ ->
5606 if nonemptystr s
5607 then
5608 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5609 | _ -> ()
5611 enttext ("bookmark: ", "", None, textentry, ondone, true)
5613 | 126 -> (* ~ *)
5614 quickbookmark ();
5615 showtext ' ' "Quick bookmark added";
5617 | 122 -> (* z *)
5618 begin match state.layout with
5619 | l :: _ ->
5620 let rect = getpdimrect l.pagedimno in
5621 let w, h =
5622 if conf.crophack
5623 then
5624 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5625 truncate (1.2 *. (rect.(3) -. rect.(0))))
5626 else
5627 (truncate (rect.(1) -. rect.(0)),
5628 truncate (rect.(3) -. rect.(0)))
5630 let w = truncate ((float w)*.conf.zoom)
5631 and h = truncate ((float h)*.conf.zoom) in
5632 if w != 0 && h != 0
5633 then (
5634 state.anchor <- getanchor ();
5635 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5637 G.postRedisplay "z";
5639 | [] -> ()
5642 | 120 -> state.roam () (* x *)
5643 | 60 | 62 -> (* < > *)
5644 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5646 | 91 | 93 -> (* [ ] *)
5647 conf.colorscale <-
5648 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5650 G.postRedisplay "brightness";
5652 | 99 when state.mode = View -> (* [alt-]c *)
5653 if Wsi.withalt mask
5654 then (
5655 if conf.zoom > 1.0
5656 then
5657 let m = (wadjsb state.winw - state.w) / 2 in
5658 state.x <- m;
5659 gotoy_and_clear_text state.y
5661 else
5662 let (c, a, b), z =
5663 match state.prevcolumns with
5664 | None -> (1, 0, 0), 1.0
5665 | Some (columns, z) ->
5666 let cab =
5667 match columns with
5668 | Csplit (c, _) -> -c, 0, 0
5669 | Cmulti ((c, a, b), _) -> c, a, b
5670 | Csingle _ -> 1, 0, 0
5672 cab, z
5674 setcolumns View c a b;
5675 setzoom z
5677 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
5678 -> (* ctrl-shift- (kp) [up|down] *)
5679 let zoom, x = state.prevzoom in
5680 setzoom zoom;
5681 state.x <- x;
5683 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5684 begin match state.autoscroll with
5685 | None ->
5686 begin match state.mode with
5687 | Birdseye beye -> upbirdseye 1 beye
5688 | _ ->
5689 if ctrl
5690 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5691 else (
5692 if not (Wsi.withshift mask) && conf.presentation
5693 then prevpage ()
5694 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5697 | Some n ->
5698 setautoscrollspeed n false
5701 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5702 begin match state.autoscroll with
5703 | None ->
5704 begin match state.mode with
5705 | Birdseye beye -> downbirdseye 1 beye
5706 | _ ->
5707 if ctrl
5708 then gotoy_and_clear_text (clamp (state.winh/2))
5709 else (
5710 if not (Wsi.withshift mask) && conf.presentation
5711 then nextpage ()
5712 else gotoy_and_clear_text (clamp conf.scrollstep)
5715 | Some n ->
5716 setautoscrollspeed n true
5719 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5720 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5721 if canpan ()
5722 then
5723 let dx =
5724 if ctrl
5725 then state.winw / 2
5726 else conf.hscrollstep
5728 let dx = if key = 0xff51 || key = 0xff96 then dx else -dx in
5729 state.x <- panbound (state.x + dx);
5730 gotoy_and_clear_text state.y
5731 else (
5732 state.text <- "";
5733 G.postRedisplay "left/right"
5736 | 0xff55 | 0xff9a -> (* (kp) prior *)
5737 let y =
5738 if ctrl
5739 then
5740 match state.layout with
5741 | [] -> state.y
5742 | l :: _ -> state.y - l.pagey
5743 else
5744 clamp (pgscale (-state.winh))
5746 gotoghyll y
5748 | 0xff56 | 0xff9b -> (* (kp) next *)
5749 let y =
5750 if ctrl
5751 then
5752 match List.rev state.layout with
5753 | [] -> state.y
5754 | l :: _ -> getpagey l.pageno
5755 else
5756 clamp (pgscale state.winh)
5758 gotoghyll y
5760 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5761 gotoghyll 0
5762 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5763 gotoghyll (clamp state.maxy)
5765 | 0xff53 | 0xff98
5766 when Wsi.withalt mask -> (* alt-(kp) right *)
5767 gotoghyll (getnav 1)
5768 | 0xff51 | 0xff96
5769 when Wsi.withalt mask -> (* alt-(kp) left *)
5770 gotoghyll (getnav ~-1)
5772 | 114 -> (* r *)
5773 reload ()
5775 | 118 when conf.debug -> (* v *)
5776 state.rects <- [];
5777 List.iter (fun l ->
5778 match getopaque l.pageno with
5779 | None -> ()
5780 | Some opaque ->
5781 let x0, y0, x1, y1 = pagebbox opaque in
5782 let a,b = float x0, float y0 in
5783 let c,d = float x1, float y0 in
5784 let e,f = float x1, float y1 in
5785 let h,j = float x0, float y1 in
5786 let rect = (a,b,c,d,e,f,h,j) in
5787 debugrect rect;
5788 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5789 ) state.layout;
5790 G.postRedisplay "v";
5792 | _ ->
5793 vlog "huh? %s" (Wsi.keyname key)
5796 let linknavkeyboard key mask linknav =
5797 let getpage pageno =
5798 let rec loop = function
5799 | [] -> None
5800 | l :: _ when l.pageno = pageno -> Some l
5801 | _ :: rest -> loop rest
5802 in loop state.layout
5804 let doexact (pageno, n) =
5805 match getopaque pageno, getpage pageno with
5806 | Some opaque, Some l ->
5807 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5808 then
5809 let under = getlink opaque n in
5810 G.postRedisplay "link gotounder";
5811 gotounder under;
5812 state.mode <- View;
5813 else
5814 let opt, dir =
5815 match key with
5816 | 0xff50 -> (* home *)
5817 Some (findlink opaque LDfirst), -1
5819 | 0xff57 -> (* end *)
5820 Some (findlink opaque LDlast), 1
5822 | 0xff51 -> (* left *)
5823 Some (findlink opaque (LDleft n)), -1
5825 | 0xff53 -> (* right *)
5826 Some (findlink opaque (LDright n)), 1
5828 | 0xff52 -> (* up *)
5829 Some (findlink opaque (LDup n)), -1
5831 | 0xff54 -> (* down *)
5832 Some (findlink opaque (LDdown n)), 1
5834 | _ -> None, 0
5836 let pwl l dir =
5837 begin match findpwl l.pageno dir with
5838 | Pwlnotfound -> ()
5839 | Pwl pageno ->
5840 let notfound dir =
5841 state.mode <- LinkNav (Ltgendir dir);
5842 let y, h = getpageyh pageno in
5843 let y =
5844 if dir < 0
5845 then y + h - state.winh
5846 else y
5848 gotoy y
5850 begin match getopaque pageno, getpage pageno with
5851 | Some opaque, Some _ ->
5852 let link =
5853 let ld = if dir > 0 then LDfirst else LDlast in
5854 findlink opaque ld
5856 begin match link with
5857 | Lfound m ->
5858 showlinktype (getlink opaque m);
5859 state.mode <- LinkNav (Ltexact (pageno, m));
5860 G.postRedisplay "linknav jpage";
5861 | _ -> notfound dir
5862 end;
5863 | _ -> notfound dir
5864 end;
5865 end;
5867 begin match opt with
5868 | Some Lnotfound -> pwl l dir;
5869 | Some (Lfound m) ->
5870 if m = n
5871 then pwl l dir
5872 else (
5873 let _, y0, _, y1 = getlinkrect opaque m in
5874 if y0 < l.pagey
5875 then gotopage1 l.pageno y0
5876 else (
5877 let d = fstate.fontsize + 1 in
5878 if y1 - l.pagey > l.pagevh - d
5879 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5880 else G.postRedisplay "linknav";
5882 showlinktype (getlink opaque m);
5883 state.mode <- LinkNav (Ltexact (l.pageno, m));
5886 | None -> viewkeyboard key mask
5887 end;
5888 | _ -> viewkeyboard key mask
5890 if key = 0xff63
5891 then (
5892 state.mode <- View;
5893 G.postRedisplay "leave linknav"
5895 else
5896 match linknav with
5897 | Ltgendir _ -> viewkeyboard key mask
5898 | Ltexact exact -> doexact exact
5901 let keyboard key mask =
5902 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5903 then wcmd "interrupt"
5904 else state.uioh <- state.uioh#key key mask
5907 let birdseyekeyboard key mask
5908 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5909 let incr =
5910 match conf.columns with
5911 | Csingle _ -> 1
5912 | Cmulti ((c, _, _), _) -> c
5913 | Csplit _ -> failwith "bird's eye split mode"
5915 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5916 match key with
5917 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5918 let y, h = getpageyh pageno in
5919 let top = (state.winh - h) / 2 in
5920 gotoy (max 0 (y - top))
5921 | 0xff0d (* enter *)
5922 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5923 | 0xff1b -> leavebirdseye beye true (* escape *)
5924 | 0xff52 -> upbirdseye incr beye (* up *)
5925 | 0xff54 -> downbirdseye incr beye (* down *)
5926 | 0xff51 -> upbirdseye 1 beye (* left *)
5927 | 0xff53 -> downbirdseye 1 beye (* right *)
5929 | 0xff55 -> (* prior *)
5930 begin match state.layout with
5931 | l :: _ ->
5932 if l.pagey != 0
5933 then (
5934 state.mode <- Birdseye (
5935 oconf, leftx, l.pageno, hooverpageno, anchor
5937 gotopage1 l.pageno 0;
5939 else (
5940 let layout = layout (state.y-state.winh) (pgh state.layout) in
5941 match layout with
5942 | [] -> gotoy (clamp (-state.winh))
5943 | l :: _ ->
5944 state.mode <- Birdseye (
5945 oconf, leftx, l.pageno, hooverpageno, anchor
5947 gotopage1 l.pageno 0
5950 | [] -> gotoy (clamp (-state.winh))
5951 end;
5953 | 0xff56 -> (* next *)
5954 begin match List.rev state.layout with
5955 | l :: _ ->
5956 let layout = layout (state.y + (pgh state.layout)) state.winh in
5957 begin match layout with
5958 | [] ->
5959 let incr = l.pageh - l.pagevh in
5960 if incr = 0
5961 then (
5962 state.mode <-
5963 Birdseye (
5964 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5966 G.postRedisplay "birdseye pagedown";
5968 else gotoy (clamp (incr + conf.interpagespace*2));
5970 | l :: _ ->
5971 state.mode <-
5972 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5973 gotopage1 l.pageno 0;
5976 | [] -> gotoy (clamp state.winh)
5977 end;
5979 | 0xff50 -> (* home *)
5980 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5981 gotopage1 0 0
5983 | 0xff57 -> (* end *)
5984 let pageno = state.pagecount - 1 in
5985 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5986 if not (pagevisible state.layout pageno)
5987 then
5988 let h =
5989 match List.rev state.pdims with
5990 | [] -> state.winh
5991 | (_, _, h, _) :: _ -> h
5993 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5994 else G.postRedisplay "birdseye end";
5995 | _ -> viewkeyboard key mask
5998 let drawpage l =
5999 let color =
6000 match state.mode with
6001 | Textentry _ -> scalecolor 0.4
6002 | LinkNav _
6003 | View -> scalecolor 1.0
6004 | Birdseye (_, _, pageno, hooverpageno, _) ->
6005 if l.pageno = hooverpageno
6006 then scalecolor 0.9
6007 else (
6008 if l.pageno = pageno
6009 then scalecolor 1.0
6010 else scalecolor 0.8
6013 drawtiles l color;
6016 let postdrawpage l linkindexbase =
6017 match getopaque l.pageno with
6018 | Some opaque ->
6019 if tileready l l.pagex l.pagey
6020 then
6021 let x = l.pagedispx - l.pagex
6022 and y = l.pagedispy - l.pagey in
6023 let hlmask =
6024 match conf.columns with
6025 | Csingle _ | Cmulti _ ->
6026 (if conf.hlinks then 1 else 0)
6027 + (if state.glinks
6028 && not (isbirdseye state.mode) then 2 else 0)
6029 | _ -> 0
6031 let s =
6032 match state.mode with
6033 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
6034 | _ -> ""
6036 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
6037 else 0
6038 | _ -> 0
6041 let scrollindicator () =
6042 let sbw, ph, sh = state.uioh#scrollph in
6043 let sbh, pw, sw = state.uioh#scrollpw in
6045 GlDraw.color (0.64, 0.64, 0.64);
6046 filledrect
6047 (float (state.winw - sbw)) 0.
6048 (float state.winw) (float state.winh)
6050 filledrect
6051 0. (float (state.winh - sbh))
6052 (float (wadjsb state.winw - 1)) (float state.winh)
6054 GlDraw.color (0.0, 0.0, 0.0);
6056 filledrect
6057 (float (state.winw - sbw)) ph
6058 (float state.winw) (ph +. sh)
6060 filledrect
6061 pw (float (state.winh - sbh))
6062 (pw +. sw) (float state.winh)
6066 let showsel () =
6067 match state.mstate with
6068 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
6071 | Msel ((x0, y0), (x1, y1)) ->
6072 let rec loop = function
6073 | l :: ls ->
6074 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
6075 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
6076 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
6077 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
6078 then
6079 match getopaque l.pageno with
6080 | Some opaque ->
6081 let x0, y0 = pagetranslatepoint l x0 y0 in
6082 let x1, y1 = pagetranslatepoint l x1 y1 in
6083 seltext opaque (x0, y0, x1, y1);
6084 | _ -> ()
6085 else loop ls
6086 | [] -> ()
6088 loop state.layout
6091 let showrects = function [] -> () | rects ->
6092 Gl.enable `blend;
6093 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
6094 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6095 List.iter
6096 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
6097 List.iter (fun l ->
6098 if l.pageno = pageno
6099 then (
6100 let dx = float (l.pagedispx - l.pagex) in
6101 let dy = float (l.pagedispy - l.pagey) in
6102 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
6103 Raw.sets_float state.vraw ~pos:0
6104 [| x0+.dx; y0+.dy;
6105 x1+.dx; y1+.dy;
6106 x3+.dx; y3+.dy;
6107 x2+.dx; y2+.dy |];
6108 GlArray.vertex `two state.vraw;
6109 GlArray.draw_arrays `triangle_strip 0 4;
6111 ) state.layout
6112 ) rects
6114 Gl.disable `blend;
6117 let display () =
6118 GlClear.color (scalecolor2 conf.bgcolor);
6119 GlClear.clear [`color];
6120 List.iter drawpage state.layout;
6121 let rects =
6122 match state.mode with
6123 | LinkNav (Ltexact (pageno, linkno)) ->
6124 begin match getopaque pageno with
6125 | Some opaque ->
6126 let x0, y0, x1, y1 = getlinkrect opaque linkno in
6127 (pageno, 5, (
6128 float x0, float y0,
6129 float x1, float y0,
6130 float x1, float y1,
6131 float x0, float y1)
6132 ) :: state.rects
6133 | None -> state.rects
6135 | _ -> state.rects
6137 showrects rects;
6138 let rec postloop linkindexbase = function
6139 | l :: rest ->
6140 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
6141 postloop linkindexbase rest
6142 | [] -> ()
6144 showsel ();
6145 postloop 0 state.layout;
6146 state.uioh#display;
6147 begin match state.mstate with
6148 | Mzoomrect ((x0, y0), (x1, y1)) ->
6149 Gl.enable `blend;
6150 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
6151 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6152 filledrect (float x0) (float y0) (float x1) (float y1);
6153 Gl.disable `blend;
6154 | _ -> ()
6155 end;
6156 enttext ();
6157 scrollindicator ();
6158 Wsi.swapb ();
6161 let zoomrect x y x1 y1 =
6162 let x0 = min x x1
6163 and x1 = max x x1
6164 and y0 = min y y1 in
6165 gotoy (state.y + y0);
6166 state.anchor <- getanchor ();
6167 let zoom = (float state.w) /. float (x1 - x0) in
6168 let margin =
6169 match conf.fitmodel, conf.columns with
6170 | FitPage, Csplit _ ->
6171 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
6173 | _, _ ->
6174 let adjw = wadjsb state.winw in
6175 if state.w < adjw
6176 then (adjw - state.w) / 2
6177 else 0
6179 state.x <- (state.x + margin) - x0;
6180 setzoom zoom;
6181 Wsi.setcursor Wsi.CURSOR_INHERIT;
6182 state.mstate <- Mnone;
6185 let zoomblock x y =
6186 let g opaque l px py =
6187 match rectofblock opaque px py with
6188 | Some a ->
6189 let x0 = a.(0) -. 20. in
6190 let x1 = a.(1) +. 20. in
6191 let y0 = a.(2) -. 20. in
6192 let zoom = (float state.w) /. (x1 -. x0) in
6193 let pagey = getpagey l.pageno in
6194 gotoy_and_clear_text (pagey + truncate y0);
6195 state.anchor <- getanchor ();
6196 let margin = (state.w - l.pagew)/2 in
6197 state.x <- -truncate x0 - margin;
6198 setzoom zoom;
6199 None
6200 | None -> None
6202 match conf.columns with
6203 | Csplit _ ->
6204 showtext '!' "block zooming does not work properly in split columns mode"
6205 | _ -> onppundermouse g x y ()
6208 let scrollx x =
6209 let winw = wadjsb state.winw - 1 in
6210 let s = float x /. float winw in
6211 let destx = truncate (float (state.w + winw) *. s) in
6212 state.x <- winw - destx;
6213 gotoy_and_clear_text state.y;
6214 state.mstate <- Mscrollx;
6217 let scrolly y =
6218 let s = float y /. float state.winh in
6219 let desty = truncate (float (state.maxy - state.winh) *. s) in
6220 gotoy_and_clear_text desty;
6221 state.mstate <- Mscrolly;
6224 let viewmouse button down x y mask =
6225 match button with
6226 | n when (n == 4 || n == 5) && not down ->
6227 if Wsi.withctrl mask
6228 then (
6229 match state.mstate with
6230 | Mzoom (oldn, i) ->
6231 if oldn = n
6232 then (
6233 if i = 2
6234 then
6235 let incr =
6236 match n with
6237 | 5 ->
6238 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6239 | _ ->
6240 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6242 let zoom = conf.zoom -. incr in
6243 setzoom zoom;
6244 state.mstate <- Mzoom (n, 0);
6245 else
6246 state.mstate <- Mzoom (n, i+1);
6248 else state.mstate <- Mzoom (n, 0)
6250 | _ -> state.mstate <- Mzoom (n, 0)
6252 else (
6253 match state.autoscroll with
6254 | Some step -> setautoscrollspeed step (n=4)
6255 | None ->
6256 if conf.wheelbypage || conf.presentation
6257 then (
6258 if n = 4
6259 then prevpage ()
6260 else nextpage ()
6262 else
6263 let incr =
6264 if n = 4
6265 then -conf.scrollstep
6266 else conf.scrollstep
6268 let incr = incr * 2 in
6269 let y = clamp incr in
6270 gotoy_and_clear_text y
6273 | n when (n = 6 || n = 7) && not down && canpan () ->
6274 state.x <-
6275 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6276 gotoy_and_clear_text state.y
6278 | 1 when Wsi.withshift mask ->
6279 state.mstate <- Mnone;
6280 if not down
6281 then (
6282 match unproject x y with
6283 | Some (pageno, ux, uy) ->
6284 let cmd = Printf.sprintf
6285 "%s %s %d %d %d"
6286 conf.stcmd state.path pageno ux uy
6288 popen cmd []
6289 | None -> ()
6292 | 1 when Wsi.withctrl mask ->
6293 if down
6294 then (
6295 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6296 state.mstate <- Mpan (x, y)
6298 else
6299 state.mstate <- Mnone
6301 | 3 ->
6302 if down
6303 then (
6304 Wsi.setcursor Wsi.CURSOR_CYCLE;
6305 let p = (x, y) in
6306 state.mstate <- Mzoomrect (p, p)
6308 else (
6309 match state.mstate with
6310 | Mzoomrect ((x0, y0), _) ->
6311 if abs (x-x0) > 10 && abs (y - y0) > 10
6312 then zoomrect x0 y0 x y
6313 else (
6314 state.mstate <- Mnone;
6315 Wsi.setcursor Wsi.CURSOR_INHERIT;
6316 G.postRedisplay "kill accidental zoom rect";
6318 | _ ->
6319 Wsi.setcursor Wsi.CURSOR_INHERIT;
6320 state.mstate <- Mnone
6323 | 1 when x > state.winw - vscrollw () ->
6324 if down
6325 then
6326 let _, position, sh = state.uioh#scrollph in
6327 if y > truncate position && y < truncate (position +. sh)
6328 then state.mstate <- Mscrolly
6329 else scrolly y
6330 else
6331 state.mstate <- Mnone
6333 | 1 when y > state.winh - hscrollh () ->
6334 if down
6335 then
6336 let _, position, sw = state.uioh#scrollpw in
6337 if x > truncate position && x < truncate (position +. sw)
6338 then state.mstate <- Mscrollx
6339 else scrollx x
6340 else
6341 state.mstate <- Mnone
6343 | 1 when state.bzoom -> if not down then zoomblock x y
6345 | 1 ->
6346 let dest = if down then getunder x y else Unone in
6347 begin match dest with
6348 | Ulinkgoto _
6349 | Ulinkuri _
6350 | Uremote _ | Uremotedest _
6351 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6352 gotounder dest
6354 | Unone when down ->
6355 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6356 state.mstate <- Mpan (x, y);
6358 | Unone | Utext _ ->
6359 if down
6360 then (
6361 if conf.angle mod 360 = 0
6362 then (
6363 state.mstate <- Msel ((x, y), (x, y));
6364 G.postRedisplay "mouse select";
6367 else (
6368 match state.mstate with
6369 | Mnone -> ()
6371 | Mzoom _ | Mscrollx | Mscrolly ->
6372 state.mstate <- Mnone
6374 | Mzoomrect ((x0, y0), _) ->
6375 zoomrect x0 y0 x y
6377 | Mpan _ ->
6378 Wsi.setcursor Wsi.CURSOR_INHERIT;
6379 state.mstate <- Mnone
6381 | Msel ((x0, y0), (x1, y1)) ->
6382 let rec loop = function
6383 | [] -> ()
6384 | l :: rest ->
6385 let inside =
6386 let a0 = l.pagedispy in
6387 let a1 = a0 + l.pagevh in
6388 let b0 = l.pagedispx in
6389 let b1 = b0 + l.pagevw in
6390 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6391 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6393 if inside
6394 then
6395 match getopaque l.pageno with
6396 | Some opaque ->
6397 begin
6398 match Ne.pipe () with
6399 | Ne.Exn exn ->
6400 showtext '!'
6401 (Printf.sprintf
6402 "can not create sel pipe: %s"
6403 (exntos exn));
6404 | Ne.Res (r, w) ->
6405 let doclose what fd =
6406 Ne.clo fd (fun msg ->
6407 dolog "%s close failed: %s" what msg)
6410 popen conf.selcmd [r, 0; w, -1];
6411 copysel w opaque true;
6412 doclose "pipe/r" r;
6413 G.postRedisplay "copysel";
6414 with exn ->
6415 dolog "can not execute %S: %s"
6416 conf.selcmd (exntos exn);
6417 doclose "pipe/r" r;
6418 doclose "pipe/w" w;
6420 | None -> ()
6421 else loop rest
6423 loop state.layout;
6424 Wsi.setcursor Wsi.CURSOR_INHERIT;
6425 state.mstate <- Mnone;
6429 | _ -> ()
6432 let birdseyemouse button down x y mask
6433 (conf, leftx, _, hooverpageno, anchor) =
6434 match button with
6435 | 1 when down ->
6436 let rec loop = function
6437 | [] -> ()
6438 | l :: rest ->
6439 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6440 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6441 then (
6442 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6444 else loop rest
6446 loop state.layout
6447 | 3 -> ()
6448 | _ -> viewmouse button down x y mask
6451 let uioh = object
6452 method display = ()
6454 method key key mask =
6455 begin match state.mode with
6456 | Textentry textentry -> textentrykeyboard key mask textentry
6457 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6458 | View -> viewkeyboard key mask
6459 | LinkNav linknav -> linknavkeyboard key mask linknav
6460 end;
6461 state.uioh
6463 method button button bstate x y mask =
6464 begin match state.mode with
6465 | LinkNav _
6466 | View -> viewmouse button bstate x y mask
6467 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6468 | Textentry _ -> ()
6469 end;
6470 state.uioh
6472 method motion x y =
6473 begin match state.mode with
6474 | Textentry _ -> ()
6475 | View | Birdseye _ | LinkNav _ ->
6476 match state.mstate with
6477 | Mzoom _ | Mnone -> ()
6479 | Mpan (x0, y0) ->
6480 let dx = x - x0
6481 and dy = y0 - y in
6482 state.mstate <- Mpan (x, y);
6483 if canpan ()
6484 then state.x <- panbound (state.x + dx);
6485 let y = clamp dy in
6486 gotoy_and_clear_text y
6488 | Msel (a, _) ->
6489 state.mstate <- Msel (a, (x, y));
6490 G.postRedisplay "motion select";
6492 | Mscrolly ->
6493 let y = min state.winh (max 0 y) in
6494 scrolly y
6496 | Mscrollx ->
6497 let x = min state.winw (max 0 x) in
6498 scrollx x
6500 | Mzoomrect (p0, _) ->
6501 state.mstate <- Mzoomrect (p0, (x, y));
6502 G.postRedisplay "motion zoomrect";
6503 end;
6504 state.uioh
6506 method pmotion x y =
6507 begin match state.mode with
6508 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6509 let rec loop = function
6510 | [] ->
6511 if hooverpageno != -1
6512 then (
6513 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6514 G.postRedisplay "pmotion birdseye no hoover";
6516 | l :: rest ->
6517 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6518 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6519 then (
6520 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6521 G.postRedisplay "pmotion birdseye hoover";
6523 else loop rest
6525 loop state.layout
6527 | Textentry _ -> ()
6529 | LinkNav _
6530 | View ->
6531 match state.mstate with
6532 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6534 | Mnone ->
6535 updateunder x y;
6536 match conf.pax with
6537 | None -> ()
6538 | Some r ->
6539 let past, _, _ = !r in
6540 let now = now () in
6541 let delta = now -. past in
6542 if delta > 0.01
6543 then paxunder x y
6544 else r := (now, x, y)
6545 end;
6546 state.uioh
6548 method infochanged _ = ()
6550 method scrollph =
6551 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6552 let p, h =
6553 if maxy = 0
6554 then 0.0, float state.winh
6555 else scrollph state.y maxy
6557 vscrollw (), p, h
6559 method scrollpw =
6560 let winw = wadjsb state.winw in
6561 let fwinw = float winw in
6562 let sw =
6563 let sw = fwinw /. float state.w in
6564 let sw = fwinw *. sw in
6565 max sw (float conf.scrollh)
6567 let position =
6568 let maxx = state.w + winw in
6569 let x = winw - state.x in
6570 let percent = float x /. float maxx in
6571 (fwinw -. sw) *. percent
6573 hscrollh (), position, sw
6575 method modehash =
6576 let modename =
6577 match state.mode with
6578 | LinkNav _ -> "links"
6579 | Textentry _ -> "textentry"
6580 | Birdseye _ -> "birdseye"
6581 | View -> "view"
6583 findkeyhash conf modename
6585 method eformsgs = true
6586 end;;
6588 module Config =
6589 struct
6590 open Parser
6592 let fontpath = ref "";;
6594 module KeyMap =
6595 Map.Make (struct type t = (int * int) let compare = compare end);;
6597 let unent s =
6598 let l = String.length s in
6599 let b = Buffer.create l in
6600 unent b s 0 l;
6601 Buffer.contents b;
6604 let home =
6605 try Sys.getenv "HOME"
6606 with exn ->
6607 prerr_endline
6608 ("Can not determine home directory location: " ^ exntos exn);
6612 let modifier_of_string = function
6613 | "alt" -> Wsi.altmask
6614 | "shift" -> Wsi.shiftmask
6615 | "ctrl" | "control" -> Wsi.ctrlmask
6616 | "meta" -> Wsi.metamask
6617 | _ -> 0
6620 let key_of_string =
6621 let r = Str.regexp "-" in
6622 fun s ->
6623 let elems = Str.full_split r s in
6624 let f n k m =
6625 let g s =
6626 let m1 = modifier_of_string s in
6627 if m1 = 0
6628 then (Wsi.namekey s, m)
6629 else (k, m lor m1)
6630 in function
6631 | Str.Delim s when n land 1 = 0 -> g s
6632 | Str.Text s -> g s
6633 | Str.Delim _ -> (k, m)
6635 let rec loop n k m = function
6636 | [] -> (k, m)
6637 | x :: xs ->
6638 let k, m = f n k m x in
6639 loop (n+1) k m xs
6641 loop 0 0 0 elems
6644 let keys_of_string =
6645 let r = Str.regexp "[ \t]" in
6646 fun s ->
6647 let elems = Str.split r s in
6648 List.map key_of_string elems
6651 let copykeyhashes c =
6652 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6655 let config_of c attrs =
6656 let apply c k v =
6658 match k with
6659 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6660 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6661 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6662 | "preload" -> { c with preload = bool_of_string v }
6663 | "page-bias" -> { c with pagebias = int_of_string v }
6664 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6665 | "horizontal-scroll-step" ->
6666 { c with hscrollstep = max (int_of_string v) 1 }
6667 | "auto-scroll-step" ->
6668 { c with autoscrollstep = max 0 (int_of_string v) }
6669 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6670 | "crop-hack" -> { c with crophack = bool_of_string v }
6671 | "throttle" ->
6672 let mw =
6673 match String.lowercase v with
6674 | "true" -> Some infinity
6675 | "false" -> None
6676 | f -> Some (float_of_string f)
6678 { c with maxwait = mw}
6679 | "highlight-links" -> { c with hlinks = bool_of_string v }
6680 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6681 | "vertical-margin" ->
6682 { c with interpagespace = max 0 (int_of_string v) }
6683 | "zoom" ->
6684 let zoom = float_of_string v /. 100. in
6685 let zoom = max zoom 0.0 in
6686 { c with zoom = zoom }
6687 | "presentation" -> { c with presentation = bool_of_string v }
6688 | "rotation-angle" -> { c with angle = int_of_string v }
6689 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6690 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6691 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6692 | "proportional-display" ->
6693 let fm =
6694 if bool_of_string v
6695 then FitProportional
6696 else FitWidth
6698 { c with fitmodel = fm }
6699 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6700 | "pixmap-cache-size" ->
6701 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6702 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6703 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6704 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6705 | "persistent-location" -> { c with jumpback = bool_of_string v }
6706 | "background-color" -> { c with bgcolor = color_of_string v }
6707 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6708 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6709 | "mupdf-store-size" ->
6710 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6711 | "checkers" -> { c with checkers = bool_of_string v }
6712 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6713 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6714 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6715 | "uri-launcher" -> { c with urilauncher = unent v }
6716 | "path-launcher" -> { c with pathlauncher = unent v }
6717 | "color-space" -> { c with colorspace = CSTE.of_string v }
6718 | "invert-colors" -> { c with invert = bool_of_string v }
6719 | "brightness" -> { c with colorscale = float_of_string v }
6720 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6721 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
6722 | "columns" ->
6723 let (n, _, _) as nab = multicolumns_of_string v in
6724 if n < 0
6725 then { c with columns = Csplit (-n, [||]) }
6726 else { c with columns = Cmulti (nab, [||]) }
6727 | "birds-eye-columns" ->
6728 { c with beyecolumns = Some (max (int_of_string v) 2) }
6729 | "selection-command" -> { c with selcmd = unent v }
6730 | "synctex-command" -> { c with stcmd = unent v }
6731 | "pax-command" -> { c with paxcmd = unent v }
6732 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6733 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6734 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6735 | "use-pbo" -> { c with usepbo = bool_of_string v }
6736 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6737 | "horizontal-scrollbar-visible" ->
6738 let b =
6739 if bool_of_string v
6740 then c.scrollb lor scrollbhv
6741 else c.scrollb land (lnot scrollbhv)
6743 { c with scrollb = b }
6744 | "vertical-scrollbar-visible" ->
6745 let b =
6746 if bool_of_string v
6747 then c.scrollb lor scrollbvv
6748 else c.scrollb land (lnot scrollbvv)
6750 { c with scrollb = b }
6751 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6752 | "point-and-x" ->
6753 { c with pax =
6754 if bool_of_string v
6755 then Some (ref (0.0, 0, 0))
6756 else None }
6757 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
6758 | _ -> c
6759 with exn ->
6760 prerr_endline ("Error processing attribute (`" ^
6761 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6764 let rec fold c = function
6765 | [] -> c
6766 | (k, v) :: rest ->
6767 let c = apply c k v in
6768 fold c rest
6770 fold { c with keyhashes = copykeyhashes c } attrs;
6773 let fromstring f pos n v d =
6774 try f v
6775 with exn ->
6776 dolog "Error processing attribute (%S=%S) at %d\n%s"
6777 n v pos (exntos exn)
6782 let bookmark_of attrs =
6783 let rec fold title page rely visy = function
6784 | ("title", v) :: rest -> fold v page rely visy rest
6785 | ("page", v) :: rest -> fold title v rely visy rest
6786 | ("rely", v) :: rest -> fold title page v visy rest
6787 | ("visy", v) :: rest -> fold title page rely v rest
6788 | _ :: rest -> fold title page rely visy rest
6789 | [] -> title, page, rely, visy
6791 fold "invalid" "0" "0" "0" attrs
6794 let doc_of attrs =
6795 let rec fold path page rely pan visy = function
6796 | ("path", v) :: rest -> fold v page rely pan visy rest
6797 | ("page", v) :: rest -> fold path v rely pan visy rest
6798 | ("rely", v) :: rest -> fold path page v pan visy rest
6799 | ("pan", v) :: rest -> fold path page rely v visy rest
6800 | ("visy", v) :: rest -> fold path page rely pan v rest
6801 | _ :: rest -> fold path page rely pan visy rest
6802 | [] -> path, page, rely, pan, visy
6804 fold "" "0" "0" "0" "0" attrs
6807 let map_of attrs =
6808 let rec fold rs ls = function
6809 | ("out", v) :: rest -> fold v ls rest
6810 | ("in", v) :: rest -> fold rs v rest
6811 | _ :: rest -> fold ls rs rest
6812 | [] -> ls, rs
6814 fold "" "" attrs
6817 let setconf dst src =
6818 dst.scrollbw <- src.scrollbw;
6819 dst.scrollh <- src.scrollh;
6820 dst.icase <- src.icase;
6821 dst.preload <- src.preload;
6822 dst.pagebias <- src.pagebias;
6823 dst.verbose <- src.verbose;
6824 dst.scrollstep <- src.scrollstep;
6825 dst.maxhfit <- src.maxhfit;
6826 dst.crophack <- src.crophack;
6827 dst.autoscrollstep <- src.autoscrollstep;
6828 dst.maxwait <- src.maxwait;
6829 dst.hlinks <- src.hlinks;
6830 dst.underinfo <- src.underinfo;
6831 dst.interpagespace <- src.interpagespace;
6832 dst.zoom <- src.zoom;
6833 dst.presentation <- src.presentation;
6834 dst.angle <- src.angle;
6835 dst.cwinw <- src.cwinw;
6836 dst.cwinh <- src.cwinh;
6837 dst.savebmarks <- src.savebmarks;
6838 dst.memlimit <- src.memlimit;
6839 dst.fitmodel <- src.fitmodel;
6840 dst.texcount <- src.texcount;
6841 dst.sliceheight <- src.sliceheight;
6842 dst.thumbw <- src.thumbw;
6843 dst.jumpback <- src.jumpback;
6844 dst.bgcolor <- src.bgcolor;
6845 dst.tilew <- src.tilew;
6846 dst.tileh <- src.tileh;
6847 dst.mustoresize <- src.mustoresize;
6848 dst.checkers <- src.checkers;
6849 dst.aalevel <- src.aalevel;
6850 dst.trimmargins <- src.trimmargins;
6851 dst.trimfuzz <- src.trimfuzz;
6852 dst.urilauncher <- src.urilauncher;
6853 dst.colorspace <- src.colorspace;
6854 dst.invert <- src.invert;
6855 dst.colorscale <- src.colorscale;
6856 dst.redirectstderr <- src.redirectstderr;
6857 dst.ghyllscroll <- src.ghyllscroll;
6858 dst.columns <- src.columns;
6859 dst.beyecolumns <- src.beyecolumns;
6860 dst.selcmd <- src.selcmd;
6861 dst.updatecurs <- src.updatecurs;
6862 dst.pathlauncher <- src.pathlauncher;
6863 dst.keyhashes <- copykeyhashes src;
6864 dst.hfsize <- src.hfsize;
6865 dst.hscrollstep <- src.hscrollstep;
6866 dst.pgscale <- src.pgscale;
6867 dst.usepbo <- src.usepbo;
6868 dst.wheelbypage <- src.wheelbypage;
6869 dst.stcmd <- src.stcmd;
6870 dst.paxcmd <- src.paxcmd;
6871 dst.scrollb <- src.scrollb;
6872 dst.riani <- src.riani;
6873 dst.paxmark <- src.paxmark;
6874 dst.pax <-
6875 if src.pax = None
6876 then None
6877 else Some ((ref (0.0, 0, 0)));
6880 let get s =
6881 let h = Hashtbl.create 10 in
6882 let dc = { defconf with angle = defconf.angle } in
6883 let rec toplevel v t spos _ =
6884 match t with
6885 | Vdata | Vcdata | Vend -> v
6886 | Vopen ("llppconfig", _, closed) ->
6887 if closed
6888 then v
6889 else { v with f = llppconfig }
6890 | Vopen _ ->
6891 error "unexpected subelement at top level" s spos
6892 | Vclose _ -> error "unexpected close at top level" s spos
6894 and llppconfig v t spos _ =
6895 match t with
6896 | Vdata | Vcdata -> v
6897 | Vend -> error "unexpected end of input in llppconfig" s spos
6898 | Vopen ("defaults", attrs, closed) ->
6899 let c = config_of dc attrs in
6900 setconf dc c;
6901 if closed
6902 then v
6903 else { v with f = defaults }
6905 | Vopen ("ui-font", attrs, closed) ->
6906 let rec getsize size = function
6907 | [] -> size
6908 | ("size", v) :: rest ->
6909 let size =
6910 fromstring int_of_string spos "size" v fstate.fontsize in
6911 getsize size rest
6912 | l -> getsize size l
6914 fstate.fontsize <- getsize fstate.fontsize attrs;
6915 if closed
6916 then v
6917 else { v with f = uifont (Buffer.create 10) }
6919 | Vopen ("doc", attrs, closed) ->
6920 let pathent, spage, srely, span, svisy = doc_of attrs in
6921 let path = unent pathent
6922 and pageno = fromstring int_of_string spos "page" spage 0
6923 and rely = fromstring float_of_string spos "rely" srely 0.0
6924 and pan = fromstring int_of_string spos "pan" span 0
6925 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6926 let c = config_of dc attrs in
6927 let anchor = (pageno, rely, visy) in
6928 if closed
6929 then (Hashtbl.add h path (c, [], pan, anchor); v)
6930 else { v with f = doc path pan anchor c [] }
6932 | Vopen _ ->
6933 error "unexpected subelement in llppconfig" s spos
6935 | Vclose "llppconfig" -> { v with f = toplevel }
6936 | Vclose _ -> error "unexpected close in llppconfig" s spos
6938 and defaults v t spos _ =
6939 match t with
6940 | Vdata | Vcdata -> v
6941 | Vend -> error "unexpected end of input in defaults" s spos
6942 | Vopen ("keymap", attrs, closed) ->
6943 let modename =
6944 try List.assoc "mode" attrs
6945 with Not_found -> "global" in
6946 if closed
6947 then v
6948 else
6949 let ret keymap =
6950 let h = findkeyhash dc modename in
6951 KeyMap.iter (Hashtbl.replace h) keymap;
6952 defaults
6954 { v with f = pkeymap ret KeyMap.empty }
6956 | Vopen (_, _, _) ->
6957 error "unexpected subelement in defaults" s spos
6959 | Vclose "defaults" ->
6960 { v with f = llppconfig }
6962 | Vclose _ -> error "unexpected close in defaults" s spos
6964 and uifont b v t spos epos =
6965 match t with
6966 | Vdata | Vcdata ->
6967 Buffer.add_substring b s spos (epos - spos);
6969 | Vopen (_, _, _) ->
6970 error "unexpected subelement in ui-font" s spos
6971 | Vclose "ui-font" ->
6972 if emptystr !fontpath
6973 then fontpath := Buffer.contents b;
6974 { v with f = llppconfig }
6975 | Vclose _ -> error "unexpected close in ui-font" s spos
6976 | Vend -> error "unexpected end of input in ui-font" s spos
6978 and doc path pan anchor c bookmarks v t spos _ =
6979 match t with
6980 | Vdata | Vcdata -> v
6981 | Vend -> error "unexpected end of input in doc" s spos
6982 | Vopen ("bookmarks", _, closed) ->
6983 if closed
6984 then v
6985 else { v with f = pbookmarks path pan anchor c bookmarks }
6987 | Vopen ("keymap", attrs, closed) ->
6988 let modename =
6989 try List.assoc "mode" attrs
6990 with Not_found -> "global"
6992 if closed
6993 then v
6994 else
6995 let ret keymap =
6996 let h = findkeyhash c modename in
6997 KeyMap.iter (Hashtbl.replace h) keymap;
6998 doc path pan anchor c bookmarks
7000 { v with f = pkeymap ret KeyMap.empty }
7002 | Vopen (_, _, _) ->
7003 error "unexpected subelement in doc" s spos
7005 | Vclose "doc" ->
7006 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
7007 { v with f = llppconfig }
7009 | Vclose _ -> error "unexpected close in doc" s spos
7011 and pkeymap ret keymap v t spos _ =
7012 match t with
7013 | Vdata | Vcdata -> v
7014 | Vend -> error "unexpected end of input in keymap" s spos
7015 | Vopen ("map", attrs, closed) ->
7016 let r, l = map_of attrs in
7017 let kss = fromstring keys_of_string spos "in" r [] in
7018 let lss = fromstring keys_of_string spos "out" l [] in
7019 let keymap =
7020 match kss with
7021 | [] -> keymap
7022 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
7023 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
7025 if closed
7026 then { v with f = pkeymap ret keymap }
7027 else
7028 let f () = v in
7029 { v with f = skip "map" f }
7031 | Vopen _ ->
7032 error "unexpected subelement in keymap" s spos
7034 | Vclose "keymap" ->
7035 { v with f = ret keymap }
7037 | Vclose _ -> error "unexpected close in keymap" s spos
7039 and pbookmarks path pan anchor c bookmarks v t spos _ =
7040 match t with
7041 | Vdata | Vcdata -> v
7042 | Vend -> error "unexpected end of input in bookmarks" s spos
7043 | Vopen ("item", attrs, closed) ->
7044 let titleent, spage, srely, svisy = bookmark_of attrs in
7045 let page = fromstring int_of_string spos "page" spage 0
7046 and rely = fromstring float_of_string spos "rely" srely 0.0
7047 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7048 let bookmarks =
7049 (unent titleent, 0, (page, rely, visy)) :: bookmarks
7051 if closed
7052 then { v with f = pbookmarks path pan anchor c bookmarks }
7053 else
7054 let f () = v in
7055 { v with f = skip "item" f }
7057 | Vopen _ ->
7058 error "unexpected subelement in bookmarks" s spos
7060 | Vclose "bookmarks" ->
7061 { v with f = doc path pan anchor c bookmarks }
7063 | Vclose _ -> error "unexpected close in bookmarks" s spos
7065 and skip tag f v t spos _ =
7066 match t with
7067 | Vdata | Vcdata -> v
7068 | Vend ->
7069 error ("unexpected end of input in skipped " ^ tag) s spos
7070 | Vopen (tag', _, closed) ->
7071 if closed
7072 then v
7073 else
7074 let f' () = { v with f = skip tag f } in
7075 { v with f = skip tag' f' }
7076 | Vclose ctag ->
7077 if tag = ctag
7078 then f ()
7079 else error ("unexpected close in skipped " ^ tag) s spos
7082 parse { f = toplevel; accu = () } s;
7083 h, dc;
7086 let do_load f ic =
7088 let len = in_channel_length ic in
7089 let s = String.create len in
7090 really_input ic s 0 len;
7091 f s;
7092 with
7093 | Parse_error (msg, s, pos) ->
7094 let subs = subs s pos in
7095 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
7097 | exn ->
7098 failwith ("config load error: " ^ exntos exn)
7101 let defconfpath =
7102 let dir =
7104 let dir = Filename.concat home ".config" in
7105 if Sys.is_directory dir then dir else home
7106 with _ -> home
7108 Filename.concat dir "llpp.conf"
7111 let confpath = ref defconfpath;;
7113 let load1 f =
7114 if Sys.file_exists !confpath
7115 then
7116 match
7117 (try Some (open_in_bin !confpath)
7118 with exn ->
7119 prerr_endline
7120 ("Error opening configuration file `" ^ !confpath ^ "': " ^
7121 exntos exn);
7122 None
7124 with
7125 | Some ic ->
7126 let success =
7128 f (do_load get ic)
7129 with exn ->
7130 prerr_endline
7131 ("Error loading configuration from `" ^ !confpath ^ "': " ^
7132 exntos exn);
7133 false
7135 close_in ic;
7136 success
7138 | None -> false
7139 else
7140 f (Hashtbl.create 0, defconf)
7143 let load () =
7144 let f (h, dc) =
7145 let pc, pb, px, pa =
7147 let key =
7148 if emptystr state.origin
7149 then state.path
7150 else state.origin
7152 Hashtbl.find h (Filename.basename key)
7153 with Not_found -> dc, [], 0, emptyanchor
7155 setconf defconf dc;
7156 setconf conf pc;
7157 state.bookmarks <- pb;
7158 state.x <- px;
7159 if conf.jumpback
7160 then state.anchor <- pa;
7161 cbput state.hists.nav pa;
7162 true
7164 load1 f
7167 let add_attrs bb always dc c =
7168 let ob s a b =
7169 if always || a != b
7170 then Printf.bprintf bb "\n %s='%b'" s a
7171 and op s a b =
7172 if always || a <> b
7173 then Printf.bprintf bb "\n %s='%b'" s (a != None)
7174 and oi s a b =
7175 if always || a != b
7176 then Printf.bprintf bb "\n %s='%d'" s a
7177 and oI s a b =
7178 if always || a != b
7179 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
7180 and oz s a b =
7181 if always || a <> b
7182 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
7183 and oF s a b =
7184 if always || a <> b
7185 then Printf.bprintf bb "\n %s='%f'" s a
7186 and oc s a b =
7187 if always || a <> b
7188 then
7189 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
7190 and oC s a b =
7191 if always || a <> b
7192 then
7193 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
7194 and oR s a b =
7195 if always || a <> b
7196 then
7197 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
7198 and os s a b =
7199 if always || a <> b
7200 then
7201 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7202 and og s a b =
7203 if always || a <> b
7204 then
7205 match a with
7206 | Some (_N, _A, _B) ->
7207 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7208 | None ->
7209 match b with
7210 | None -> ()
7211 | _ ->
7212 Printf.bprintf bb "\n %s='none'" s
7213 and oW s a b =
7214 if always || a <> b
7215 then
7216 let v =
7217 match a with
7218 | None -> "false"
7219 | Some f ->
7220 if f = infinity
7221 then "true"
7222 else string_of_float f
7224 Printf.bprintf bb "\n %s='%s'" s v
7225 and oco s a b =
7226 if always || a <> b
7227 then
7228 match a with
7229 | Cmulti ((n, a, b), _) when n > 1 ->
7230 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7231 | Csplit (n, _) when n > 1 ->
7232 Printf.bprintf bb "\n %s='%d'" s ~-n
7233 | _ -> ()
7234 and obeco s a b =
7235 if always || a <> b
7236 then
7237 match a with
7238 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7239 | _ -> ()
7240 and oFm s a b =
7241 if always || a <> b
7242 then
7243 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7244 and oSv s a b m =
7245 if always || a <> b
7246 then
7247 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7248 and oPm s a b =
7249 if always || a <> b
7250 then
7251 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
7253 oi "width" c.cwinw dc.cwinw;
7254 oi "height" c.cwinh dc.cwinh;
7255 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7256 oi "scroll-handle-height" c.scrollh dc.scrollh;
7257 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7258 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7259 ob "case-insensitive-search" c.icase dc.icase;
7260 ob "preload" c.preload dc.preload;
7261 oi "page-bias" c.pagebias dc.pagebias;
7262 oi "scroll-step" c.scrollstep dc.scrollstep;
7263 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7264 ob "max-height-fit" c.maxhfit dc.maxhfit;
7265 ob "crop-hack" c.crophack dc.crophack;
7266 oW "throttle" c.maxwait dc.maxwait;
7267 ob "highlight-links" c.hlinks dc.hlinks;
7268 ob "under-cursor-info" c.underinfo dc.underinfo;
7269 oi "vertical-margin" c.interpagespace dc.interpagespace;
7270 oz "zoom" c.zoom dc.zoom;
7271 ob "presentation" c.presentation dc.presentation;
7272 oi "rotation-angle" c.angle dc.angle;
7273 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7274 oFm "fit-model" c.fitmodel dc.fitmodel;
7275 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7276 oi "tex-count" c.texcount dc.texcount;
7277 oi "slice-height" c.sliceheight dc.sliceheight;
7278 oi "thumbnail-width" c.thumbw dc.thumbw;
7279 ob "persistent-location" c.jumpback dc.jumpback;
7280 oc "background-color" c.bgcolor dc.bgcolor;
7281 oi "tile-width" c.tilew dc.tilew;
7282 oi "tile-height" c.tileh dc.tileh;
7283 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7284 ob "checkers" c.checkers dc.checkers;
7285 oi "aalevel" c.aalevel dc.aalevel;
7286 ob "trim-margins" c.trimmargins dc.trimmargins;
7287 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7288 os "uri-launcher" c.urilauncher dc.urilauncher;
7289 os "path-launcher" c.pathlauncher dc.pathlauncher;
7290 oC "color-space" c.colorspace dc.colorspace;
7291 ob "invert-colors" c.invert dc.invert;
7292 oF "brightness" c.colorscale dc.colorscale;
7293 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7294 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7295 oco "columns" c.columns dc.columns;
7296 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7297 os "selection-command" c.selcmd dc.selcmd;
7298 os "synctex-command" c.stcmd dc.stcmd;
7299 os "pax-command" c.paxcmd dc.paxcmd;
7300 ob "update-cursor" c.updatecurs dc.updatecurs;
7301 oi "hint-font-size" c.hfsize dc.hfsize;
7302 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7303 oF "page-scroll-scale" c.pgscale dc.pgscale;
7304 ob "use-pbo" c.usepbo dc.usepbo;
7305 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7306 ob "remote-in-a-new-instance" c.riani dc.riani;
7307 op "point-and-x" c.pax dc.pax;
7308 oPm "point-and-x-mark" c.paxmark dc.paxmark;
7311 let keymapsbuf always dc c =
7312 let bb = Buffer.create 16 in
7313 let rec loop = function
7314 | [] -> ()
7315 | (modename, h) :: rest ->
7316 let dh = findkeyhash dc modename in
7317 if always || h <> dh
7318 then (
7319 if Hashtbl.length h > 0
7320 then (
7321 if Buffer.length bb > 0
7322 then Buffer.add_char bb '\n';
7323 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7324 Hashtbl.iter (fun i o ->
7325 let isdifferent = always ||
7327 let dO = Hashtbl.find dh i in
7328 dO <> o
7329 with Not_found -> true
7331 if isdifferent
7332 then
7333 let addkm (k, m) =
7334 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7335 if Wsi.withalt m then Buffer.add_string bb "alt-";
7336 if Wsi.withshift m then Buffer.add_string bb "shift-";
7337 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7338 Buffer.add_string bb (Wsi.keyname k);
7340 let addkms l =
7341 let rec loop = function
7342 | [] -> ()
7343 | km :: [] -> addkm km
7344 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7346 loop l
7348 Buffer.add_string bb "<map in='";
7349 addkm i;
7350 match o with
7351 | KMinsrt km ->
7352 Buffer.add_string bb "' out='";
7353 addkm km;
7354 Buffer.add_string bb "'/>\n"
7356 | KMinsrl kms ->
7357 Buffer.add_string bb "' out='";
7358 addkms kms;
7359 Buffer.add_string bb "'/>\n"
7361 | KMmulti (ins, kms) ->
7362 Buffer.add_char bb ' ';
7363 addkms ins;
7364 Buffer.add_string bb "' out='";
7365 addkms kms;
7366 Buffer.add_string bb "'/>\n"
7367 ) h;
7368 Buffer.add_string bb "</keymap>";
7371 loop rest
7373 loop c.keyhashes;
7377 let save () =
7378 let uifontsize = fstate.fontsize in
7379 let bb = Buffer.create 32768 in
7380 let relx = float state.x /. float state.winw in
7381 let w, h, x =
7382 let cx w = truncate (relx *. float w) in
7383 List.fold_left
7384 (fun (w, h, x) ws ->
7385 match ws with
7386 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7387 | Wsi.MaxVert -> (w, conf.cwinh, x)
7388 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7390 (state.winw, state.winh, state.x) state.winstate
7392 conf.cwinw <- w;
7393 conf.cwinh <- h;
7394 let f (h, dc) =
7395 let dc = if conf.bedefault then conf else dc in
7396 Buffer.add_string bb "<llppconfig>\n";
7398 if nonemptystr !fontpath
7399 then
7400 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7401 uifontsize
7402 !fontpath
7403 else (
7404 if uifontsize <> 14
7405 then
7406 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7409 Buffer.add_string bb "<defaults";
7410 add_attrs bb true dc dc;
7411 let kb = keymapsbuf true dc dc in
7412 if Buffer.length kb > 0
7413 then (
7414 Buffer.add_string bb ">\n";
7415 Buffer.add_buffer bb kb;
7416 Buffer.add_string bb "\n</defaults>\n";
7418 else Buffer.add_string bb "/>\n";
7420 let adddoc path pan anchor c bookmarks =
7421 if bookmarks == [] && c = dc && anchor = emptyanchor
7422 then ()
7423 else (
7424 Printf.bprintf bb "<doc path='%s'"
7425 (enent path 0 (String.length path));
7427 if anchor <> emptyanchor
7428 then (
7429 let n, rely, visy = anchor in
7430 Printf.bprintf bb " page='%d'" n;
7431 if rely > 1e-6
7432 then
7433 Printf.bprintf bb " rely='%f'" rely
7435 if abs_float visy > 1e-6
7436 then
7437 Printf.bprintf bb " visy='%f'" visy
7441 if pan != 0
7442 then Printf.bprintf bb " pan='%d'" pan;
7444 add_attrs bb false dc c;
7445 let kb = keymapsbuf false dc c in
7447 begin match bookmarks with
7448 | [] ->
7449 if Buffer.length kb > 0
7450 then (
7451 Buffer.add_string bb ">\n";
7452 Buffer.add_buffer bb kb;
7453 Buffer.add_string bb "\n</doc>\n";
7455 else Buffer.add_string bb "/>\n"
7456 | _ ->
7457 Buffer.add_string bb ">\n<bookmarks>\n";
7458 List.iter (fun (title, _level, (page, rely, visy)) ->
7459 Printf.bprintf bb
7460 "<item title='%s' page='%d'"
7461 (enent title 0 (String.length title))
7462 page
7464 if rely > 1e-6
7465 then
7466 Printf.bprintf bb " rely='%f'" rely
7468 if abs_float visy > 1e-6
7469 then
7470 Printf.bprintf bb " visy='%f'" visy
7472 Buffer.add_string bb "/>\n";
7473 ) bookmarks;
7474 Buffer.add_string bb "</bookmarks>";
7475 if Buffer.length kb > 0
7476 then (
7477 Buffer.add_string bb "\n";
7478 Buffer.add_buffer bb kb;
7480 Buffer.add_string bb "\n</doc>\n";
7481 end;
7485 let pan, conf =
7486 match state.mode with
7487 | Birdseye (c, pan, _, _, _) ->
7488 let beyecolumns =
7489 match conf.columns with
7490 | Cmulti ((c, _, _), _) -> Some c
7491 | Csingle _ -> None
7492 | Csplit _ -> None
7493 and columns =
7494 match c.columns with
7495 | Cmulti (c, _) -> Cmulti (c, [||])
7496 | Csingle _ -> Csingle [||]
7497 | Csplit _ -> failwith "quit from bird's eye while split"
7499 pan, { c with beyecolumns = beyecolumns; columns = columns }
7500 | _ -> x, conf
7502 let basename = Filename.basename
7503 (if emptystr state.origin then state.path else state.origin)
7505 adddoc basename pan (getanchor ())
7506 (let conf =
7507 let autoscrollstep =
7508 match state.autoscroll with
7509 | Some step -> step
7510 | None -> conf.autoscrollstep
7512 match state.mode with
7513 | Birdseye (bc, _, _, _, _) ->
7514 { conf with
7515 zoom = bc.zoom;
7516 presentation = bc.presentation;
7517 interpagespace = bc.interpagespace;
7518 maxwait = bc.maxwait;
7519 autoscrollstep = autoscrollstep }
7520 | _ -> { conf with autoscrollstep = autoscrollstep }
7521 in conf)
7522 (if conf.savebmarks then state.bookmarks else []);
7524 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7525 if basename <> path
7526 then adddoc path x anchor c bookmarks
7527 ) h;
7528 Buffer.add_string bb "</llppconfig>\n";
7529 true;
7531 if load1 f && Buffer.length bb > 0
7532 then
7534 let tmp = !confpath ^ ".tmp" in
7535 let oc = open_out_bin tmp in
7536 Buffer.output_buffer oc bb;
7537 close_out oc;
7538 Unix.rename tmp !confpath;
7539 with exn ->
7540 prerr_endline
7541 ("error while saving configuration: " ^ exntos exn)
7543 end;;
7545 let adderrmsg src msg =
7546 Buffer.add_string state.errmsgs msg;
7547 state.newerrmsgs <- true;
7548 G.postRedisplay src
7551 let adderrfmt src fmt =
7552 Format.kprintf (fun s -> adderrmsg src s) fmt;
7555 let ract cmds =
7556 let cl = splitatspace cmds in
7557 let scan s fmt f =
7558 try Scanf.sscanf s fmt f
7559 with exn ->
7560 adderrfmt "remote exec"
7561 "error processing '%S': %s\n" cmds (exntos exn)
7563 match cl with
7564 | "reload" :: [] -> reload ()
7565 | "goto" :: args :: [] ->
7566 scan args "%u %f %f"
7567 (fun pageno x y ->
7568 let cmd, _ = state.geomcmds in
7569 if emptystr cmd
7570 then gotopagexy pageno x y
7571 else
7572 let f prevf () =
7573 gotopagexy pageno x y;
7574 prevf ()
7576 state.reprf <- f state.reprf
7578 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7579 | "rect" :: args :: [] ->
7580 scan args "%u %u %f %f %f %f"
7581 (fun pageno color x0 y0 x1 y1 ->
7582 onpagerect pageno (fun w h ->
7583 let _,w1,h1,_ = getpagedim pageno in
7584 let sw = float w1 /. float w
7585 and sh = float h1 /. float h in
7586 let x0s = x0 *. sw
7587 and x1s = x1 *. sw
7588 and y0s = y0 *. sh
7589 and y1s = y1 *. sh in
7590 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7591 debugrect rect;
7592 state.rects <- (pageno, color, rect) :: state.rects;
7593 G.postRedisplay "rect";
7596 | "activatewin" :: [] -> Wsi.activatewin ()
7597 | "quit" :: [] -> raise Quit
7598 | _ ->
7599 adderrfmt "remote command"
7600 "error processing remote command: %S\n" cmds;
7603 let remote =
7604 let scratch = String.create 80 in
7605 let buf = Buffer.create 80 in
7606 fun fd ->
7607 let rec tempfr () =
7608 try Some (Unix.read fd scratch 0 80)
7609 with
7610 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7611 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7612 | exn -> raise exn
7614 match tempfr () with
7615 | None -> Some fd
7616 | Some n ->
7617 if n = 0
7618 then (
7619 Unix.close fd;
7620 if Buffer.length buf > 0
7621 then (
7622 let s = Buffer.contents buf in
7623 Buffer.clear buf;
7624 ract s;
7626 None
7628 else
7629 let rec eat ppos =
7630 let nlpos =
7632 let pos = String.index_from scratch ppos '\n' in
7633 if pos >= n then -1 else pos
7634 with Not_found -> -1
7636 if nlpos >= 0
7637 then (
7638 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7639 let s = Buffer.contents buf in
7640 Buffer.clear buf;
7641 ract s;
7642 eat (nlpos+1);
7644 else (
7645 Buffer.add_substring buf scratch ppos (n-ppos);
7646 Some fd
7648 in eat 0
7651 let remoteopen path =
7652 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7653 with exn ->
7654 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7655 None
7658 let () =
7659 let trimcachepath = ref "" in
7660 let rcmdpath = ref "" in
7661 selfexec := Sys.executable_name;
7662 Arg.parse
7663 (Arg.align
7664 [("-p", Arg.String (fun s -> state.password <- s),
7665 "<password> Set password");
7667 ("-f", Arg.String
7668 (fun s ->
7669 Config.fontpath := s;
7670 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7672 "<path> Set path to the user interface font");
7674 ("-c", Arg.String
7675 (fun s ->
7676 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7677 Config.confpath := s),
7678 "<path> Set path to the configuration file");
7680 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7681 "<path> Set path to the trim cache file");
7683 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7684 "<named-destination> Set named destination");
7686 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7687 ("-cxack", Arg.Set cxack, " Cut corners");
7689 ("-remote", Arg.String (fun s -> rcmdpath := s),
7690 "<path> Set path to the remote commands source");
7692 ("-origin", Arg.String (fun s -> state.origin <- s),
7693 "<original-path> Set original path");
7695 ("-v", Arg.Unit (fun () ->
7696 Printf.printf
7697 "%s\nconfiguration path: %s\n"
7698 (version ())
7699 Config.defconfpath
7701 exit 0), " Print version and exit");
7704 (fun s -> state.path <- s)
7705 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7707 if !wtmode
7708 then selfexec := !selfexec ^ " -wtmode";
7710 if emptystr state.path
7711 then (prerr_endline "file name missing"; exit 1);
7713 if not (Config.load ())
7714 then prerr_endline "failed to load configuration";
7716 let wsfd, winw, winh = Wsi.init (object
7717 val mutable m_hack = false
7718 method expose = if not m_hack then G.postRedisplay "expose"
7719 method visible = G.postRedisplay "visible"
7720 method display = m_hack <- false; display ()
7721 method reshape w h =
7722 m_hack <- w < state.winw && h < state.winh;
7723 reshape w h
7724 method mouse b d x y m = state.uioh <- state.uioh#button b d x y m
7725 method motion x y =
7726 state.mpos <- (x, y);
7727 state.uioh <- state.uioh#motion x y
7728 method pmotion x y =
7729 state.mpos <- (x, y);
7730 state.uioh <- state.uioh#pmotion x y
7731 method key k m =
7732 let mascm = m land (
7733 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7734 ) in
7735 match state.keystate with
7736 | KSnone ->
7737 let km = k, mascm in
7738 begin
7739 match
7740 let modehash = state.uioh#modehash in
7741 try Hashtbl.find modehash km
7742 with Not_found ->
7743 try Hashtbl.find (findkeyhash conf "global") km
7744 with Not_found -> KMinsrt (k, m)
7745 with
7746 | KMinsrt (k, m) -> keyboard k m
7747 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7748 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7750 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7751 List.iter (fun (k, m) -> keyboard k m) insrt;
7752 state.keystate <- KSnone
7753 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7754 state.keystate <- KSinto (keys, insrt)
7755 | _ ->
7756 state.keystate <- KSnone
7758 method enter x y =
7759 state.mpos <- (x, y);
7760 state.uioh <- state.uioh#pmotion x y
7761 method leave = state.mpos <- (-1, -1)
7762 method winstate wsl = state.winstate <- wsl; m_hack <- false
7763 method quit = raise Quit
7764 end) conf.cwinw conf.cwinh (platform = Posx) in
7766 state.wsfd <- wsfd;
7768 if not (
7769 List.exists GlMisc.check_extension
7770 [ "GL_ARB_texture_rectangle"
7771 ; "GL_EXT_texture_recangle"
7772 ; "GL_NV_texture_rectangle" ]
7774 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7776 if (
7777 let r = GlMisc.get_string `renderer in
7778 let p = "Mesa DRI Intel(" in
7779 let l = String.length p in
7780 String.length r > l && String.sub r 0 l = p
7782 then (
7783 defconf.sliceheight <- 1024;
7784 defconf.texcount <- 32;
7785 defconf.usepbo <- true;
7788 let cr, sw =
7789 match Ne.pipe () with
7790 | Ne.Exn exn ->
7791 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7792 exit 1
7793 | Ne.Res rw -> rw
7794 and sr, cw =
7795 match Ne.pipe () with
7796 | Ne.Exn exn ->
7797 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7798 exit 1
7799 | Ne.Res rw -> rw
7802 cloexec cr;
7803 cloexec sw;
7804 cloexec sr;
7805 cloexec cw;
7807 setcheckers conf.checkers;
7808 redirectstderr ();
7809 if conf.redirectstderr
7810 then
7811 at_exit (fun () ->
7812 let s = Buffer.contents state.errmsgs ^
7813 (match state.errfd with
7814 | Some fd ->
7815 let s = String.create (80*24) in
7816 let n =
7818 let r, _, _ = Unix.select [fd] [] [] 0.0 in
7819 if List.mem fd r
7820 then Unix.read fd s 0 (String.length s)
7821 else 0
7822 with _ -> 0
7824 if n = 0
7825 then ""
7826 else String.sub s 0 n
7827 | None -> ""
7830 try ignore (Unix.write state.stderr s 0 (String.length s))
7831 with exn -> print_endline (exntos exn)
7835 init (cr, cw) (
7836 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7837 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7838 !Config.fontpath, !trimcachepath,
7839 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7841 List.iter GlArray.enable [`texture_coord; `vertex];
7842 state.sr <- sr;
7843 state.sw <- sw;
7844 state.text <- "Opening " ^ (mbtoutf8 state.path);
7845 reshape winw winh;
7846 opendoc state.path state.password;
7847 state.uioh <- uioh;
7848 display ();
7849 Wsi.mapwin ();
7850 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7851 let optrfd =
7852 ref (
7853 if nonemptystr !rcmdpath
7854 then remoteopen !rcmdpath
7855 else None
7859 let rec loop deadline =
7860 let r =
7861 match state.errfd with
7862 | None -> [state.sr; state.wsfd]
7863 | Some fd -> [state.sr; state.wsfd; fd]
7865 let r =
7866 match !optrfd with
7867 | None -> r
7868 | Some fd -> fd :: r
7870 if state.redisplay
7871 then (
7872 state.redisplay <- false;
7873 display ();
7875 let timeout =
7876 let now = now () in
7877 if deadline > now
7878 then (
7879 if deadline = infinity
7880 then ~-.1.0
7881 else max 0.0 (deadline -. now)
7883 else 0.0
7885 let r, _, _ =
7886 try Unix.select r [] [] timeout
7887 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7889 begin match r with
7890 | [] ->
7891 state.ghyll None;
7892 let newdeadline =
7893 if state.ghyll == noghyll
7894 then
7895 match state.autoscroll with
7896 | Some step when step != 0 ->
7897 let y = state.y + step in
7898 let y =
7899 if y < 0
7900 then state.maxy
7901 else if y >= state.maxy then 0 else y
7903 gotoy y;
7904 if state.mode = View
7905 then state.text <- "";
7906 deadline +. 0.01
7907 | _ -> infinity
7908 else deadline +. 0.01
7910 loop newdeadline
7912 | l ->
7913 let rec checkfds = function
7914 | [] -> ()
7915 | fd :: rest when fd = state.sr ->
7916 let cmd = readcmd state.sr in
7917 act cmd;
7918 checkfds rest
7920 | fd :: rest when fd = state.wsfd ->
7921 Wsi.readresp fd;
7922 checkfds rest
7924 | fd :: rest when Some fd = !optrfd ->
7925 begin match remote fd with
7926 | None -> optrfd := remoteopen !rcmdpath;
7927 | opt -> optrfd := opt
7928 end;
7929 checkfds rest
7931 | fd :: rest ->
7932 let s = String.create 80 in
7933 let n = tempfailureretry (Unix.read fd s 0) 80 in
7934 if conf.redirectstderr
7935 then (
7936 Buffer.add_substring state.errmsgs s 0 n;
7937 state.newerrmsgs <- true;
7938 state.redisplay <- true;
7940 else (
7941 prerr_string (String.sub s 0 n);
7942 flush stderr;
7944 checkfds rest
7946 checkfds l;
7947 let newdeadline =
7948 let deadline1 =
7949 if deadline = infinity
7950 then now () +. 0.01
7951 else deadline
7953 match state.autoscroll with
7954 | Some step when step != 0 -> deadline1
7955 | _ -> if state.ghyll == noghyll then infinity else deadline1
7957 loop newdeadline
7958 end;
7961 loop infinity;
7962 with Quit ->
7963 Config.save ();