ctrl-s in outline
[llpp.git] / main.ml
blob8ec081f610173c92e44f999784cde30665c950a7
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 let s = Printf.sprintf
1612 "%d[%d,%d] %f sec"
1613 l.pageno col row t
1615 let w = measurestr fstate.fontsize s in
1616 GlDraw.color (0.0, 0.0, 0.0);
1617 filledrect (float (x-2))
1618 (float (y-2))
1619 (float (x+2) +. w)
1620 (float (y + fstate.fontsize + 2));
1621 GlDraw.color (1.0, 1.0, 1.0);
1622 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1625 | None ->
1626 endtiles ();
1627 let w =
1628 let lw = wadjsb state.winw - x in
1629 min lw w
1630 and h =
1631 let lh = state.winh - y in
1632 min lh h
1634 begin match state.texid with
1635 | Some id ->
1636 Gl.enable `texture_2d;
1637 GlTex.bind_texture `texture_2d id;
1638 let x0 = float x
1639 and y0 = float y
1640 and x1 = float (x+w)
1641 and y1 = float (y+h) in
1643 let tw = float w /. 16.0
1644 and th = float h /. 16.0 in
1645 let tx0 = float tilex /. 16.0
1646 and ty0 = float tiley /. 16.0 in
1647 let tx1 = tx0 +. tw
1648 and ty1 = ty0 +. th in
1649 Raw.sets_float state.vraw ~pos:0
1650 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
1651 Raw.sets_float state.traw ~pos:0
1652 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
1653 GlArray.vertex `two state.vraw;
1654 GlArray.tex_coord `two state.traw;
1655 GlArray.draw_arrays `triangle_strip 0 4;
1657 | None ->
1658 GlDraw.color (1.0, 1.0, 1.0);
1659 filledrect (float x) (float y) (float (x+w)) (float (y+h));
1660 end;
1661 if w > 128 && h > fstate.fontsize + 10
1662 then (
1663 GlDraw.color (0.0, 0.0, 0.0);
1664 let c, r =
1665 if conf.verbose
1666 then (col*conf.tilew, row*conf.tileh)
1667 else col, row
1669 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1671 GlDraw.color color;
1672 begintiles ();
1674 itertiles l f;
1675 endtiles ();
1678 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1680 let tilevisible1 l x y =
1681 let ax0 = l.pagex
1682 and ax1 = l.pagex + l.pagevw
1683 and ay0 = l.pagey
1684 and ay1 = l.pagey + l.pagevh in
1686 let bx0 = x
1687 and by0 = y in
1688 let bx1 = min (bx0 + conf.tilew) l.pagew
1689 and by1 = min (by0 + conf.tileh) l.pageh in
1691 let rx0 = max ax0 bx0
1692 and ry0 = max ay0 by0
1693 and rx1 = min ax1 bx1
1694 and ry1 = min ay1 by1 in
1696 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1697 nonemptyintersection
1700 let tilevisible layout n x y =
1701 let rec findpageinlayout m = function
1702 | l :: rest when l.pageno = n ->
1703 tilevisible1 l x y || (
1704 match conf.columns with
1705 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1706 | _ -> false
1708 | _ :: rest -> findpageinlayout 0 rest
1709 | [] -> false
1711 findpageinlayout 0 layout;
1714 let tileready l x y =
1715 tilevisible1 l x y &&
1716 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1719 let tilepage n p layout =
1720 let rec loop = function
1721 | l :: rest ->
1722 if l.pageno = n
1723 then
1724 let f col row _ _ _ _ _ _ =
1725 if state.currently = Idle
1726 then
1727 match gettileopaque l col row with
1728 | Some _ -> ()
1729 | None ->
1730 let x = col*conf.tilew
1731 and y = row*conf.tileh in
1732 let w =
1733 let w = l.pagew - x in
1734 min w conf.tilew
1736 let h =
1737 let h = l.pageh - y in
1738 min h conf.tileh
1740 let pbo =
1741 if conf.usepbo
1742 then getpbo w h conf.colorspace
1743 else "0"
1745 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1746 state.currently <-
1747 Tiling (
1748 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1749 conf.tilew, conf.tileh
1752 itertiles l f;
1753 else
1754 loop rest
1756 | [] -> ()
1758 if nogeomcmds state.geomcmds
1759 then loop layout;
1762 let preloadlayout y =
1763 let y = if y < state.winh then 0 else y - state.winh in
1764 let h = state.winh*3 in
1765 layout y h;
1768 let load pages =
1769 let rec loop pages =
1770 if state.currently != Idle
1771 then ()
1772 else
1773 match pages with
1774 | l :: rest ->
1775 begin match getopaque l.pageno with
1776 | None ->
1777 wcmd "page %d %d" l.pageno l.pagedimno;
1778 state.currently <- Loading (l, state.gen);
1779 | Some opaque ->
1780 tilepage l.pageno opaque pages;
1781 loop rest
1782 end;
1783 | _ -> ()
1785 if nogeomcmds state.geomcmds
1786 then loop pages
1789 let preload pages =
1790 load pages;
1791 if conf.preload && state.currently = Idle
1792 then load (preloadlayout state.y);
1795 let layoutready layout =
1796 let rec fold all ls =
1797 all && match ls with
1798 | l :: rest ->
1799 let seen = ref false in
1800 let allvisible = ref true in
1801 let foo col row _ _ _ _ _ _ =
1802 seen := true;
1803 allvisible := !allvisible &&
1804 begin match gettileopaque l col row with
1805 | Some _ -> true
1806 | None -> false
1809 itertiles l foo;
1810 fold (!seen && !allvisible) rest
1811 | [] -> true
1813 let alltilesvisible = fold true layout in
1814 alltilesvisible;
1817 let gotoy y =
1818 let y = bound y 0 state.maxy in
1819 let y, layout, proceed =
1820 match conf.maxwait with
1821 | Some time when state.ghyll == noghyll ->
1822 begin match state.throttle with
1823 | None ->
1824 let layout = layout y state.winh in
1825 let ready = layoutready layout in
1826 if not ready
1827 then (
1828 load layout;
1829 state.throttle <- Some (layout, y, now ());
1831 else G.postRedisplay "gotoy showall (None)";
1832 y, layout, ready
1833 | Some (_, _, started) ->
1834 let dt = now () -. started in
1835 if dt > time
1836 then (
1837 state.throttle <- None;
1838 let layout = layout y state.winh in
1839 load layout;
1840 G.postRedisplay "maxwait";
1841 y, layout, true
1843 else -1, [], false
1846 | _ ->
1847 let layout = layout y state.winh in
1848 if not !wtmode || layoutready layout
1849 then G.postRedisplay "gotoy ready";
1850 y, layout, true
1852 if proceed
1853 then (
1854 state.y <- y;
1855 state.layout <- layout;
1856 begin match state.mode with
1857 | LinkNav (Ltexact (pageno, linkno)) ->
1858 let rec loop = function
1859 | [] ->
1860 state.mode <- LinkNav (Ltgendir 0)
1861 | l :: _ when l.pageno = pageno ->
1862 begin match getopaque pageno with
1863 | None ->
1864 state.mode <- LinkNav (Ltgendir 0)
1865 | Some opaque ->
1866 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1867 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1868 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1869 then state.mode <- LinkNav (Ltgendir 0)
1871 | _ :: rest -> loop rest
1873 loop layout
1874 | _ -> ()
1875 end;
1876 begin match state.mode with
1877 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1878 if not (pagevisible layout pageno)
1879 then (
1880 match state.layout with
1881 | [] -> ()
1882 | l :: _ ->
1883 state.mode <- Birdseye (
1884 conf, leftx, l.pageno, hooverpageno, anchor
1887 | LinkNav (Ltgendir dir as lt) ->
1888 let linknav =
1889 let rec loop = function
1890 | [] -> lt
1891 | l :: rest ->
1892 match getopaque l.pageno with
1893 | None -> loop rest
1894 | Some opaque ->
1895 let link =
1896 let ld =
1897 if dir = 0
1898 then LDfirstvisible (l.pagex, l.pagey, dir)
1899 else (
1900 if dir > 0 then LDfirst else LDlast
1903 findlink opaque ld
1905 match link with
1906 | Lnotfound -> loop rest
1907 | Lfound n ->
1908 showlinktype (getlink opaque n);
1909 Ltexact (l.pageno, n)
1911 loop state.layout
1913 state.mode <- LinkNav linknav
1914 | _ -> ()
1915 end;
1916 preload layout;
1918 state.ghyll <- noghyll;
1919 if conf.updatecurs
1920 then (
1921 let mx, my = state.mpos in
1922 updateunder mx my;
1926 let conttiling pageno opaque =
1927 tilepage pageno opaque
1928 (if conf.preload then preloadlayout state.y else state.layout)
1931 let gotoy_and_clear_text y =
1932 if not conf.verbose then state.text <- "";
1933 gotoy y;
1936 let getanchor1 l =
1937 let top =
1938 let coloff = l.pagecol * l.pageh in
1939 float (l.pagey + coloff) /. float l.pageh
1941 let dtop =
1942 if l.pagedispy = 0
1943 then
1945 else (
1946 if conf.presentation
1947 then float l.pagedispy /. float (calcips l.pageh)
1948 else float l.pagedispy /. float conf.interpagespace
1951 (l.pageno, top, dtop)
1954 let getanchor () =
1955 match state.layout with
1956 | l :: _ -> getanchor1 l
1957 | [] ->
1958 let n = page_of_y state.y in
1959 if n = -1
1960 then state.anchor
1961 else
1962 let y, h = getpageyh n in
1963 let dy = y - state.y in
1964 let dtop =
1965 if conf.presentation
1966 then
1967 let ips = calcips h in
1968 float (dy + ips) /. float ips
1969 else
1970 float dy /. float conf.interpagespace
1972 (n, 0.0, dtop)
1975 let getanchory (n, top, dtop) =
1976 let y, h = getpageyh n in
1977 if conf.presentation
1978 then
1979 let ips = calcips h in
1980 y + truncate (top*.float h -. dtop*.float ips) + ips;
1981 else
1982 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1985 let gotoanchor anchor =
1986 gotoy (getanchory anchor);
1989 let addnav () =
1990 cbput state.hists.nav (getanchor ());
1993 let getnav dir =
1994 let anchor = cbgetc state.hists.nav dir in
1995 getanchory anchor;
1998 let gotoghyll y =
1999 let scroll f n a b =
2000 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
2001 let snake f a b =
2002 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
2003 if f < a
2004 then s (float f /. float a)
2005 else (
2006 if f > b
2007 then 1.0 -. s ((float (f-b) /. float (n-b)))
2008 else 1.0
2011 snake f a b
2012 and summa n a b =
2013 let ins = float a *. 0.5
2014 and outs = float (n-b) *. 0.5 in
2015 let ones = b - a in
2016 ins +. outs +. float ones
2018 let rec set (_N, _A, _B) y sy =
2019 let sum = summa _N _A _B in
2020 let dy = float (y - sy) in
2021 state.ghyll <- (
2022 let rec gf n y1 o =
2023 if n >= _N
2024 then state.ghyll <- noghyll
2025 else
2026 let go n =
2027 let s = scroll n _N _A _B in
2028 let y1 = y1 +. ((s *. dy) /. sum) in
2029 gotoy_and_clear_text (truncate y1);
2030 state.ghyll <- gf (n+1) y1;
2032 match o with
2033 | None -> go n
2034 | Some y' -> set (_N/2, 1, 1) y' state.y
2036 gf 0 (float state.y)
2039 match conf.ghyllscroll with
2040 | None ->
2041 gotoy_and_clear_text y
2042 | Some nab ->
2043 if state.ghyll == noghyll
2044 then set nab y state.y
2045 else state.ghyll (Some y)
2048 let gotopage n top =
2049 let y, h = getpageyh n in
2050 let y = y + (truncate (top *. float h)) in
2051 gotoghyll y
2054 let gotopage1 n top =
2055 let y = getpagey n in
2056 let y = y + top in
2057 gotoghyll y
2060 let invalidate s f =
2061 state.layout <- [];
2062 state.pdims <- [];
2063 state.rects <- [];
2064 state.rects1 <- [];
2065 match state.geomcmds with
2066 | ps, [] when emptystr ps ->
2067 f ();
2068 state.geomcmds <- s, [];
2070 | ps, [] ->
2071 state.geomcmds <- ps, [s, f];
2073 | ps, (s', _) :: rest when s' = s ->
2074 state.geomcmds <- ps, ((s, f) :: rest);
2076 | ps, cmds ->
2077 state.geomcmds <- ps, ((s, f) :: cmds);
2080 let flushpages () =
2081 Hashtbl.iter (fun _ opaque ->
2082 wcmd "freepage %s" opaque;
2083 ) state.pagemap;
2084 Hashtbl.clear state.pagemap;
2087 let flushtiles () =
2088 if not (Queue.is_empty state.tilelru)
2089 then (
2090 Queue.iter (fun (k, p, s) ->
2091 wcmd "freetile %s" p;
2092 state.memused <- state.memused - s;
2093 Hashtbl.remove state.tilemap k;
2094 ) state.tilelru;
2095 state.uioh#infochanged Memused;
2096 Queue.clear state.tilelru;
2098 load state.layout;
2101 let stateh h =
2102 let h = truncate (float h*.conf.zoom) in
2103 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2104 h - d
2107 let opendoc path password =
2108 state.path <- path;
2109 state.password <- password;
2110 state.gen <- state.gen + 1;
2111 state.docinfo <- [];
2113 flushpages ();
2114 setaalevel conf.aalevel;
2115 let titlepath =
2116 if emptystr state.origin
2117 then path
2118 else state.origin
2120 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2121 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
2122 invalidate "reqlayout"
2123 (fun () ->
2124 wcmd "reqlayout %d %d %d %s\000"
2125 conf.angle (FMTE.to_int conf.fitmodel)
2126 (stateh state.winh) state.nameddest
2130 let reload () =
2131 state.anchor <- getanchor ();
2132 opendoc state.path state.password;
2135 let scalecolor c =
2136 let c = c *. conf.colorscale in
2137 (c, c, c);
2140 let scalecolor2 (r, g, b) =
2141 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2144 let docolumns = function
2145 | Csingle _ ->
2146 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2147 let rec loop pageno pdimno pdim y ph pdims =
2148 if pageno = state.pagecount
2149 then ()
2150 else
2151 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2152 match pdims with
2153 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2154 pdimno+1, pdim, rest
2155 | _ ->
2156 pdimno, pdim, pdims
2158 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2159 let y = y +
2160 (if conf.presentation
2161 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2162 else (if pageno = 0 then 0 else conf.interpagespace)
2165 a.(pageno) <- (pdimno, x, y, pdim);
2166 loop (pageno+1) pdimno pdim (y + h) h pdims
2168 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2169 conf.columns <- Csingle a;
2171 | Cmulti ((columns, coverA, coverB), _) ->
2172 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2173 let rec loop pageno pdimno pdim x y rowh pdims =
2174 let rec fixrow m = if m = pageno then () else
2175 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2176 if h < rowh
2177 then (
2178 let y = y + (rowh - h) / 2 in
2179 a.(m) <- (pdimno, x, y, pdim);
2181 fixrow (m+1)
2183 if pageno = state.pagecount
2184 then fixrow (((pageno - 1) / columns) * columns)
2185 else
2186 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2187 match pdims with
2188 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2189 pdimno+1, pdim, rest
2190 | _ ->
2191 pdimno, pdim, pdims
2193 let x, y, rowh' =
2194 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2195 then (
2196 let x = (wadjsb state.winw - w) / 2 in
2197 let ips =
2198 if conf.presentation then calcips h else conf.interpagespace in
2199 x, y + ips + rowh, h
2201 else (
2202 if (pageno - coverA) mod columns = 0
2203 then (
2204 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2205 let y =
2206 if conf.presentation
2207 then
2208 let ips = calcips h in
2209 y + (if pageno = 0 then 0 else calcips rowh + ips)
2210 else
2211 y + (if pageno = 0 then 0 else conf.interpagespace)
2213 x, y + rowh, h
2215 else x, y, max rowh h
2218 let y =
2219 if pageno > 1 && (pageno - coverA) mod columns = 0
2220 then (
2221 let y =
2222 if pageno = columns && conf.presentation
2223 then (
2224 let ips = calcips rowh in
2225 for i = 0 to pred columns
2227 let (pdimno, x, y, pdim) = a.(i) in
2228 a.(i) <- (pdimno, x, y+ips, pdim)
2229 done;
2230 y+ips;
2232 else y
2234 fixrow (pageno - columns);
2237 else y
2239 a.(pageno) <- (pdimno, x, y, pdim);
2240 let x = x + w + xoff*2 + conf.interpagespace in
2241 loop (pageno+1) pdimno pdim x y rowh' pdims
2243 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2244 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2246 | Csplit (c, _) ->
2247 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2248 let rec loop pageno pdimno pdim y pdims =
2249 if pageno = state.pagecount
2250 then ()
2251 else
2252 let pdimno, ((_, w, h, _) as pdim), pdims =
2253 match pdims with
2254 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2255 pdimno+1, pdim, rest
2256 | _ ->
2257 pdimno, pdim, pdims
2259 let cw = w / c in
2260 let rec loop1 n x y =
2261 if n = c then y else (
2262 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2263 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2266 let y = loop1 0 0 y in
2267 loop (pageno+1) pdimno pdim y pdims
2269 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2270 conf.columns <- Csplit (c, a);
2273 let represent () =
2274 docolumns conf.columns;
2275 state.maxy <- calcheight ();
2276 if state.reprf == noreprf
2277 then (
2278 match state.mode with
2279 | Birdseye (_, _, pageno, _, _) ->
2280 let y, h = getpageyh pageno in
2281 let top = (state.winh - h) / 2 in
2282 gotoy (max 0 (y - top))
2283 | _ -> gotoanchor state.anchor
2285 else (
2286 state.reprf ();
2287 state.reprf <- noreprf;
2291 let reshape w h =
2292 GlDraw.viewport 0 0 w h;
2293 let firsttime = state.geomcmds == firstgeomcmds in
2294 if not firsttime && nogeomcmds state.geomcmds
2295 then state.anchor <- getanchor ();
2297 state.winw <- w;
2298 let w = wadjsb (truncate (float w *. conf.zoom)) in
2299 let w = max w 2 in
2300 state.winh <- h;
2301 setfontsize fstate.fontsize;
2302 GlMat.mode `modelview;
2303 GlMat.load_identity ();
2305 GlMat.mode `projection;
2306 GlMat.load_identity ();
2307 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2308 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2309 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2311 let relx =
2312 if conf.zoom <= 1.0
2313 then 0.0
2314 else float state.x /. float state.w
2316 invalidate "geometry"
2317 (fun () ->
2318 state.w <- w;
2319 if not firsttime
2320 then state.x <- truncate (relx *. float w);
2321 let w =
2322 match conf.columns with
2323 | Csingle _ -> w
2324 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2325 | Csplit (c, _) -> w * c
2327 wcmd "geometry %d %d %d"
2328 w (stateh h) (FMTE.to_int conf.fitmodel)
2332 let enttext () =
2333 let len = String.length state.text in
2334 let drawstring s =
2335 let hscrollh =
2336 match state.mode with
2337 | Textentry _ | View | LinkNav _ ->
2338 let h, _, _ = state.uioh#scrollpw in
2340 | _ -> 0
2342 let rect x w =
2343 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
2344 (x+.w) (float (state.winh - hscrollh))
2347 let w = float (wadjsb state.winw - 1) in
2348 if state.progress >= 0.0 && state.progress < 1.0
2349 then (
2350 GlDraw.color (0.3, 0.3, 0.3);
2351 let w1 = w *. state.progress in
2352 rect 0.0 w1;
2353 GlDraw.color (0.0, 0.0, 0.0);
2354 rect w1 (w-.w1)
2356 else (
2357 GlDraw.color (0.0, 0.0, 0.0);
2358 rect 0.0 w;
2361 GlDraw.color (1.0, 1.0, 1.0);
2362 drawstring fstate.fontsize
2363 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2365 let s =
2366 match state.mode with
2367 | Textentry ((prefix, text, _, _, _, _), _) ->
2368 let s =
2369 if len > 0
2370 then
2371 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2372 else
2373 Printf.sprintf "%s%s_" prefix text
2377 | _ -> state.text
2379 let s =
2380 if state.newerrmsgs
2381 then (
2382 if not (istextentry state.mode) && state.uioh#eformsgs
2383 then
2384 let s1 = "(press 'e' to review error messasges)" in
2385 if nonemptystr s then s ^ " " ^ s1 else s1
2386 else s
2388 else s
2390 if nonemptystr s
2391 then drawstring s
2394 let gctiles () =
2395 let len = Queue.length state.tilelru in
2396 let layout = lazy (
2397 match state.throttle with
2398 | None ->
2399 if conf.preload
2400 then preloadlayout state.y
2401 else state.layout
2402 | Some (layout, _, _) ->
2403 layout
2404 ) in
2405 let rec loop qpos =
2406 if state.memused <= conf.memlimit
2407 then ()
2408 else (
2409 if qpos < len
2410 then
2411 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2412 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2413 let (_, pw, ph, _) = getpagedim n in
2415 gen = state.gen
2416 && colorspace = conf.colorspace
2417 && angle = conf.angle
2418 && pagew = pw
2419 && pageh = ph
2420 && (
2421 let x = col*conf.tilew
2422 and y = row*conf.tileh in
2423 tilevisible (Lazy.force_val layout) n x y
2425 then Queue.push lruitem state.tilelru
2426 else (
2427 freepbo p;
2428 wcmd "freetile %s" p;
2429 state.memused <- state.memused - s;
2430 state.uioh#infochanged Memused;
2431 Hashtbl.remove state.tilemap k;
2433 loop (qpos+1)
2436 loop 0
2439 let logcurrently = function
2440 | Idle -> dolog "Idle"
2441 | Loading (l, gen) ->
2442 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2443 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2444 dolog
2445 "Tiling %d[%d,%d] page=%s cs=%s angle"
2446 l.pageno col row pageopaque
2447 (CSTE.to_string colorspace)
2449 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2450 angle gen conf.angle state.gen
2451 tilew tileh
2452 conf.tilew conf.tileh
2454 | Outlining _ ->
2455 dolog "outlining"
2458 let splitatspace =
2459 let r = Str.regexp " " in
2460 fun s -> Str.bounded_split r s 2;
2463 let onpagerect pageno f =
2464 let b =
2465 match conf.columns with
2466 | Cmulti (_, b) -> b
2467 | Csingle b -> b
2468 | Csplit (_, b) -> b
2470 if pageno >= 0 && pageno < Array.length b
2471 then
2472 let (_, _, _, (w, h, _, _)) = b.(pageno) in
2473 f w h
2476 let gotopagexy1 pageno x y =
2477 let _,w1,h1,leftx = getpagedim pageno in
2478 let top = y /. (float h1) in
2479 let left = x /. (float w1) in
2480 let py, w, h = getpageywh pageno in
2481 let wh = state.winh - hscrollh () in
2482 let x = left *. (float w) in
2483 let x = leftx + state.x + truncate x in
2484 let sx =
2485 if x < 0 || x >= wadjsb state.winw
2486 then state.x - x
2487 else state.x
2489 let pdy = truncate (top *. float h) in
2490 let y' = py + pdy in
2491 let dy = y' - state.y in
2492 let sy =
2493 if x != state.x || not (dy > 0 && dy < wh)
2494 then (
2495 if conf.presentation
2496 then
2497 if abs (py - y') > wh
2498 then y'
2499 else py
2500 else y';
2502 else state.y
2504 if state.x != sx || state.y != sy
2505 then (
2506 let x, y =
2507 if !wtmode
2508 then (
2509 let ww = wadjsb state.winw in
2510 let qx = sx / ww
2511 and qy = pdy / wh in
2512 let x = qx * ww
2513 and y = py + qy * wh in
2514 let x = if -x + ww > w1 then -(w1-ww) else x
2515 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2516 let y =
2517 if conf.presentation
2518 then
2519 if abs (py - y') > wh
2520 then y'
2521 else py
2522 else y';
2524 (x, y)
2526 else (sx, sy)
2528 state.x <- x;
2529 gotoy_and_clear_text y;
2531 else gotoy_and_clear_text state.y;
2534 let gotopagexy pageno x y =
2535 match state.mode with
2536 | Birdseye _ -> gotopage pageno 0.0
2537 | _ -> gotopagexy1 pageno x y
2540 let act cmds =
2541 (* dolog "%S" cmds; *)
2542 let cl = splitatspace cmds in
2543 let scan s fmt f =
2544 try Scanf.sscanf s fmt f
2545 with exn ->
2546 dolog "error processing '%S': %s" cmds (exntos exn);
2547 exit 1
2549 match cl with
2550 | "clear" :: [] ->
2551 state.uioh#infochanged Pdim;
2552 state.pdims <- [];
2554 | "clearrects" :: [] ->
2555 state.rects <- state.rects1;
2556 G.postRedisplay "clearrects";
2558 | "continue" :: args :: [] ->
2559 let n = scan args "%u" (fun n -> n) in
2560 state.pagecount <- n;
2561 begin match state.currently with
2562 | Outlining l ->
2563 state.currently <- Idle;
2564 state.outlines <- Array.of_list (List.rev l)
2565 | _ -> ()
2566 end;
2568 let cur, cmds = state.geomcmds in
2569 if emptystr cur
2570 then failwith "umpossible";
2572 begin match List.rev cmds with
2573 | [] ->
2574 state.geomcmds <- "", [];
2575 represent ();
2576 | (s, f) :: rest ->
2577 f ();
2578 state.geomcmds <- s, List.rev rest;
2579 end;
2580 if conf.maxwait = None && not !wtmode
2581 then G.postRedisplay "continue";
2583 | "title" :: args :: [] ->
2584 Wsi.settitle args
2586 | "msg" :: args :: [] ->
2587 showtext ' ' args
2589 | "vmsg" :: args :: [] ->
2590 if conf.verbose
2591 then showtext ' ' args
2593 | "emsg" :: args :: [] ->
2594 Buffer.add_string state.errmsgs args;
2595 state.newerrmsgs <- true;
2596 G.postRedisplay "error message"
2598 | "progress" :: args :: [] ->
2599 let progress, text =
2600 scan args "%f %n"
2601 (fun f pos ->
2602 f, String.sub args pos (String.length args - pos))
2604 state.text <- text;
2605 state.progress <- progress;
2606 G.postRedisplay "progress"
2608 | "firstmatch" :: args :: [] ->
2609 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2610 scan args "%u %d %f %f %f %f %f %f %f %f"
2611 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2612 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2614 let y = (getpagey pageno) + truncate y0 in
2615 addnav ();
2616 gotoy y;
2617 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2619 | "match" :: args :: [] ->
2620 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2621 scan args "%u %d %f %f %f %f %f %f %f %f"
2622 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2623 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2625 state.rects1 <-
2626 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2628 | "page" :: args :: [] ->
2629 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2630 begin match state.currently with
2631 | Loading (l, gen) ->
2632 vlog "page %d took %f sec" l.pageno t;
2633 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2634 begin match state.throttle with
2635 | None ->
2636 let preloadedpages =
2637 if conf.preload
2638 then preloadlayout state.y
2639 else state.layout
2641 let evict () =
2642 let set =
2643 List.fold_left (fun s l -> IntSet.add l.pageno s)
2644 IntSet.empty preloadedpages
2646 let evictedpages =
2647 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2648 if not (IntSet.mem pageno set)
2649 then (
2650 wcmd "freepage %s" opaque;
2651 key :: accu
2653 else accu
2654 ) state.pagemap []
2656 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2658 evict ();
2659 state.currently <- Idle;
2660 if gen = state.gen
2661 then (
2662 tilepage l.pageno pageopaque state.layout;
2663 load state.layout;
2664 load preloadedpages;
2665 if pagevisible state.layout l.pageno
2666 && layoutready state.layout
2667 then G.postRedisplay "page";
2670 | Some (layout, _, _) ->
2671 state.currently <- Idle;
2672 tilepage l.pageno pageopaque layout;
2673 load state.layout
2674 end;
2676 | _ ->
2677 dolog "Inconsistent loading state";
2678 logcurrently state.currently;
2679 exit 1
2682 | "tile" :: args :: [] ->
2683 let (x, y, opaque, size, t) =
2684 scan args "%u %u %s %u %f"
2685 (fun x y p size t -> (x, y, p, size, t))
2687 begin match state.currently with
2688 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2689 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2691 unmappbo opaque;
2692 if tilew != conf.tilew || tileh != conf.tileh
2693 then (
2694 wcmd "freetile %s" opaque;
2695 state.currently <- Idle;
2696 load state.layout;
2698 else (
2699 puttileopaque l col row gen cs angle opaque size t;
2700 state.memused <- state.memused + size;
2701 state.uioh#infochanged Memused;
2702 gctiles ();
2703 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2704 opaque, size) state.tilelru;
2706 let layout =
2707 match state.throttle with
2708 | None -> state.layout
2709 | Some (layout, _, _) -> layout
2712 state.currently <- Idle;
2713 if gen = state.gen
2714 && conf.colorspace = cs
2715 && conf.angle = angle
2716 && tilevisible layout l.pageno x y
2717 then conttiling l.pageno pageopaque;
2719 begin match state.throttle with
2720 | None ->
2721 preload state.layout;
2722 if gen = state.gen
2723 && conf.colorspace = cs
2724 && conf.angle = angle
2725 && tilevisible state.layout l.pageno x y
2726 && (not !wtmode || layoutready state.layout)
2727 then G.postRedisplay "tile nothrottle";
2729 | Some (layout, y, _) ->
2730 let ready = layoutready layout in
2731 if ready
2732 then (
2733 state.y <- y;
2734 state.layout <- layout;
2735 state.throttle <- None;
2736 G.postRedisplay "throttle";
2738 else load layout;
2739 end;
2742 | _ ->
2743 dolog "Inconsistent tiling state";
2744 logcurrently state.currently;
2745 exit 1
2748 | "pdim" :: args :: [] ->
2749 let (n, w, h, _) as pdim =
2750 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2752 let pdim =
2753 match conf.fitmodel, conf.columns with
2754 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2755 | _ -> pdim
2757 state.uioh#infochanged Pdim;
2758 state.pdims <- pdim :: state.pdims
2760 | "o" :: args :: [] ->
2761 let (l, n, t, h, pos) =
2762 scan args "%u %u %d %u %n"
2763 (fun l n t h pos -> l, n, t, h, pos)
2765 let s = String.sub args pos (String.length args - pos) in
2766 let outline = (s, l, (n, float t /. float h, 0.0)) in
2767 begin match state.currently with
2768 | Outlining outlines ->
2769 state.currently <- Outlining (outline :: outlines)
2770 | Idle ->
2771 state.currently <- Outlining [outline]
2772 | currently ->
2773 dolog "invalid outlining state";
2774 logcurrently currently
2777 | "a" :: args :: [] ->
2778 let (n, l, t) =
2779 scan args "%u %d %d" (fun n l t -> n, l, t)
2781 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2783 | "info" :: args :: [] ->
2784 state.docinfo <- (1, args) :: state.docinfo
2786 | "infoend" :: [] ->
2787 state.uioh#infochanged Docinfo;
2788 state.docinfo <- List.rev state.docinfo
2790 | _ ->
2791 error "unknown cmd `%S'" cmds
2794 let onhist cb =
2795 let rc = cb.rc in
2796 let action = function
2797 | HCprev -> cbget cb ~-1
2798 | HCnext -> cbget cb 1
2799 | HCfirst -> cbget cb ~-(cb.rc)
2800 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2801 and cancel () = cb.rc <- rc
2802 in (action, cancel)
2805 let search pattern forward =
2806 match conf.columns with
2807 | Csplit _ ->
2808 showtext '!' "searching does not work properly in split columns mode"
2809 | _ ->
2810 if nonemptystr pattern
2811 then
2812 let pn, py =
2813 match state.layout with
2814 | [] -> 0, 0
2815 | l :: _ ->
2816 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2818 wcmd "search %d %d %d %d,%s\000"
2819 (btod conf.icase) pn py (btod forward) pattern;
2822 let intentry text key =
2823 let c =
2824 if key >= 32 && key < 127
2825 then Char.chr key
2826 else '\000'
2828 match c with
2829 | '0' .. '9' ->
2830 let text = addchar text c in
2831 TEcont text
2833 | _ ->
2834 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2835 TEcont text
2838 let linknentry text key =
2839 let c =
2840 if key >= 32 && key < 127
2841 then Char.chr key
2842 else '\000'
2844 match c with
2845 | 'a' .. 'z' ->
2846 let text = addchar text c in
2847 TEcont text
2849 | _ ->
2850 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2851 TEcont text
2854 let linkndone f s =
2855 if nonemptystr s
2856 then (
2857 let n =
2858 let l = String.length s in
2859 let rec loop pos n = if pos = l then n else
2860 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2861 loop (pos+1) (n*26 + m)
2862 in loop 0 0
2864 let rec loop n = function
2865 | [] -> ()
2866 | l :: rest ->
2867 match getopaque l.pageno with
2868 | None -> loop n rest
2869 | Some opaque ->
2870 let m = getlinkcount opaque in
2871 if n < m
2872 then (
2873 let under = getlink opaque n in
2874 f under
2876 else loop (n-m) rest
2878 loop n state.layout;
2882 let textentry text key =
2883 if key land 0xff00 = 0xff00
2884 then TEcont text
2885 else TEcont (text ^ toutf8 key)
2888 let reqlayout angle fitmodel =
2889 match state.throttle with
2890 | None ->
2891 if nogeomcmds state.geomcmds
2892 then state.anchor <- getanchor ();
2893 conf.angle <- angle mod 360;
2894 if conf.angle != 0
2895 then (
2896 match state.mode with
2897 | LinkNav _ -> state.mode <- View
2898 | _ -> ()
2900 conf.fitmodel <- fitmodel;
2901 invalidate "reqlayout"
2902 (fun () ->
2903 wcmd "reqlayout %d %d %d"
2904 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2906 | _ -> ()
2909 let settrim trimmargins trimfuzz =
2910 if nogeomcmds state.geomcmds
2911 then state.anchor <- getanchor ();
2912 conf.trimmargins <- trimmargins;
2913 conf.trimfuzz <- trimfuzz;
2914 let x0, y0, x1, y1 = trimfuzz in
2915 invalidate "settrim"
2916 (fun () ->
2917 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2918 flushpages ();
2921 let setzoom zoom =
2922 match state.throttle with
2923 | None ->
2924 let zoom = max 0.0001 zoom in
2925 if zoom <> conf.zoom
2926 then (
2927 state.prevzoom <- (conf.zoom, state.x);
2928 conf.zoom <- zoom;
2929 reshape state.winw state.winh;
2930 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2933 | Some (layout, y, started) ->
2934 let time =
2935 match conf.maxwait with
2936 | None -> 0.0
2937 | Some t -> t
2939 let dt = now () -. started in
2940 if dt > time
2941 then (
2942 state.y <- y;
2943 load layout;
2947 let setcolumns mode columns coverA coverB =
2948 state.prevcolumns <- Some (conf.columns, conf.zoom);
2949 if columns < 0
2950 then (
2951 if isbirdseye mode
2952 then showtext '!' "split mode doesn't work in bird's eye"
2953 else (
2954 conf.columns <- Csplit (-columns, [||]);
2955 state.x <- 0;
2956 conf.zoom <- 1.0;
2959 else (
2960 if columns < 2
2961 then (
2962 conf.columns <- Csingle [||];
2963 state.x <- 0;
2964 setzoom 1.0;
2966 else (
2967 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2968 conf.zoom <- 1.0;
2971 reshape state.winw state.winh;
2974 let enterbirdseye () =
2975 let zoom = float conf.thumbw /. float state.winw in
2976 let birdseyepageno =
2977 let cy = state.winh / 2 in
2978 let fold = function
2979 | [] -> 0
2980 | l :: rest ->
2981 let rec fold best = function
2982 | [] -> best.pageno
2983 | l :: rest ->
2984 let d = cy - (l.pagedispy + l.pagevh/2)
2985 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2986 if abs d < abs dbest
2987 then fold l rest
2988 else best.pageno
2989 in fold l rest
2991 fold state.layout
2993 state.mode <- Birdseye (
2994 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2996 conf.zoom <- zoom;
2997 conf.presentation <- false;
2998 conf.interpagespace <- 10;
2999 conf.hlinks <- false;
3000 conf.fitmodel <- FitProportional;
3001 state.x <- 0;
3002 state.mstate <- Mnone;
3003 conf.maxwait <- None;
3004 conf.columns <- (
3005 match conf.beyecolumns with
3006 | Some c ->
3007 conf.zoom <- 1.0;
3008 Cmulti ((c, 0, 0), [||])
3009 | None -> Csingle [||]
3011 Wsi.setcursor Wsi.CURSOR_INHERIT;
3012 if conf.verbose
3013 then
3014 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
3015 (100.0*.zoom)
3016 else
3017 state.text <- ""
3019 reshape state.winw state.winh;
3022 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
3023 state.mode <- View;
3024 conf.zoom <- c.zoom;
3025 conf.presentation <- c.presentation;
3026 conf.interpagespace <- c.interpagespace;
3027 conf.maxwait <- c.maxwait;
3028 conf.hlinks <- c.hlinks;
3029 conf.fitmodel <- c.fitmodel;
3030 conf.beyecolumns <- (
3031 match conf.columns with
3032 | Cmulti ((c, _, _), _) -> Some c
3033 | Csingle _ -> None
3034 | Csplit _ -> failwith "leaving bird's eye split mode"
3036 conf.columns <- (
3037 match c.columns with
3038 | Cmulti (c, _) -> Cmulti (c, [||])
3039 | Csingle _ -> Csingle [||]
3040 | Csplit (c, _) -> Csplit (c, [||])
3042 if conf.verbose
3043 then
3044 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
3045 (100.0*.conf.zoom)
3047 reshape state.winw state.winh;
3048 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
3049 state.x <- leftx;
3052 let togglebirdseye () =
3053 match state.mode with
3054 | Birdseye vals -> leavebirdseye vals true
3055 | View -> enterbirdseye ()
3056 | _ -> ()
3059 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3060 let pageno = max 0 (pageno - incr) in
3061 let rec loop = function
3062 | [] -> gotopage1 pageno 0
3063 | l :: _ when l.pageno = pageno ->
3064 if l.pagedispy >= 0 && l.pagey = 0
3065 then G.postRedisplay "upbirdseye"
3066 else gotopage1 pageno 0
3067 | _ :: rest -> loop rest
3069 loop state.layout;
3070 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
3073 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3074 let pageno = min (state.pagecount - 1) (pageno + incr) in
3075 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3076 let rec loop = function
3077 | [] ->
3078 let y, h = getpageyh pageno in
3079 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
3080 gotoy (clamp dy)
3081 | l :: _ when l.pageno = pageno ->
3082 if l.pagevh != l.pageh
3083 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
3084 else G.postRedisplay "downbirdseye"
3085 | _ :: rest -> loop rest
3087 loop state.layout
3090 let optentry mode _ key =
3091 let btos b = if b then "on" else "off" in
3092 if key >= 32 && key < 127
3093 then
3094 let c = Char.chr key in
3095 match c with
3096 | 's' ->
3097 let ondone s =
3098 try conf.scrollstep <- int_of_string s with exc ->
3099 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3101 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3103 | 'A' ->
3104 let ondone s =
3106 conf.autoscrollstep <- int_of_string s;
3107 if state.autoscroll <> None
3108 then state.autoscroll <- Some conf.autoscrollstep
3109 with exc ->
3110 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3112 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3114 | 'C' ->
3115 let ondone s =
3117 let n, a, b = multicolumns_of_string s in
3118 setcolumns mode n a b;
3119 with exc ->
3120 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3122 TEswitch ("columns: ", "", None, textentry, ondone, true)
3124 | 'Z' ->
3125 let ondone s =
3127 let zoom = float (int_of_string s) /. 100.0 in
3128 setzoom zoom
3129 with exc ->
3130 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3132 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3134 | 't' ->
3135 let ondone s =
3137 conf.thumbw <- bound (int_of_string s) 2 4096;
3138 state.text <-
3139 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3140 begin match mode with
3141 | Birdseye beye ->
3142 leavebirdseye beye false;
3143 enterbirdseye ();
3144 | _ -> ();
3146 with exc ->
3147 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3149 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3151 | 'R' ->
3152 let ondone s =
3153 match try
3154 Some (int_of_string s)
3155 with exc ->
3156 state.text <- Printf.sprintf "bad integer `%s': %s"
3157 s (exntos exc);
3158 None
3159 with
3160 | Some angle -> reqlayout angle conf.fitmodel
3161 | None -> ()
3163 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3165 | 'i' ->
3166 conf.icase <- not conf.icase;
3167 TEdone ("case insensitive search " ^ (btos conf.icase))
3169 | 'p' ->
3170 conf.preload <- not conf.preload;
3171 gotoy state.y;
3172 TEdone ("preload " ^ (btos conf.preload))
3174 | 'v' ->
3175 conf.verbose <- not conf.verbose;
3176 TEdone ("verbose " ^ (btos conf.verbose))
3178 | 'd' ->
3179 conf.debug <- not conf.debug;
3180 TEdone ("debug " ^ (btos conf.debug))
3182 | 'h' ->
3183 conf.maxhfit <- not conf.maxhfit;
3184 state.maxy <- calcheight ();
3185 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3187 | 'c' ->
3188 conf.crophack <- not conf.crophack;
3189 TEdone ("crophack " ^ btos conf.crophack)
3191 | 'a' ->
3192 let s =
3193 match conf.maxwait with
3194 | None ->
3195 conf.maxwait <- Some infinity;
3196 "always wait for page to complete"
3197 | Some _ ->
3198 conf.maxwait <- None;
3199 "show placeholder if page is not ready"
3201 TEdone s
3203 | 'f' ->
3204 conf.underinfo <- not conf.underinfo;
3205 TEdone ("underinfo " ^ btos conf.underinfo)
3207 | 'P' ->
3208 conf.savebmarks <- not conf.savebmarks;
3209 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3211 | 'S' ->
3212 let ondone s =
3214 let pageno, py =
3215 match state.layout with
3216 | [] -> 0, 0
3217 | l :: _ ->
3218 l.pageno, l.pagey
3220 conf.interpagespace <- int_of_string s;
3221 docolumns conf.columns;
3222 state.maxy <- calcheight ();
3223 let y = getpagey pageno in
3224 gotoy (y + py)
3225 with exc ->
3226 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3228 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3230 | 'l' ->
3231 let fm =
3232 match conf.fitmodel with
3233 | FitProportional -> FitWidth
3234 | _ -> FitProportional
3236 reqlayout conf.angle fm;
3237 TEdone ("proportional display " ^ btos (fm == FitProportional))
3239 | 'T' ->
3240 settrim (not conf.trimmargins) conf.trimfuzz;
3241 TEdone ("trim margins " ^ btos conf.trimmargins)
3243 | 'I' ->
3244 conf.invert <- not conf.invert;
3245 TEdone ("invert colors " ^ btos conf.invert)
3247 | 'x' ->
3248 let ondone s =
3249 cbput state.hists.sel s;
3250 conf.selcmd <- s;
3252 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3253 textentry, ondone, true)
3255 | 'M' ->
3256 if conf.pax == None
3257 then conf.pax <- Some (ref (0.0, 0, 0))
3258 else conf.pax <- None;
3259 TEdone ("PAX " ^ btos (conf.pax != None))
3261 | _ ->
3262 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3263 TEstop
3264 else
3265 TEcont state.text
3268 class type lvsource = object
3269 method getitemcount : int
3270 method getitem : int -> (string * int)
3271 method hasaction : int -> bool
3272 method exit :
3273 uioh:uioh ->
3274 cancel:bool ->
3275 active:int ->
3276 first:int ->
3277 pan:int ->
3278 qsearch:string ->
3279 uioh option
3280 method getactive : int
3281 method getfirst : int
3282 method getqsearch : string
3283 method setqsearch : string -> unit
3284 method getpan : int
3285 end;;
3287 class virtual lvsourcebase = object
3288 val mutable m_active = 0
3289 val mutable m_first = 0
3290 val mutable m_qsearch = ""
3291 val mutable m_pan = 0
3292 method getactive = m_active
3293 method getfirst = m_first
3294 method getqsearch = m_qsearch
3295 method getpan = m_pan
3296 method setqsearch s = m_qsearch <- s
3297 end;;
3299 let withoutlastutf8 s =
3300 let len = String.length s in
3301 if len = 0
3302 then s
3303 else
3304 let rec find pos =
3305 if pos = 0
3306 then pos
3307 else
3308 let b = Char.code s.[pos] in
3309 if b land 0b11000000 = 0b11000000
3310 then pos
3311 else find (pos-1)
3313 let first =
3314 if Char.code s.[len-1] land 0x80 = 0
3315 then len-1
3316 else find (len-1)
3318 String.sub s 0 first;
3321 let textentrykeyboard
3322 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3323 let key =
3324 if key >= 0xffb0 && key <= 0xffb9
3325 then key - 0xffb0 + 48 else key
3327 let enttext te =
3328 state.mode <- Textentry (te, onleave);
3329 state.text <- "";
3330 enttext ();
3331 G.postRedisplay "textentrykeyboard enttext";
3333 let histaction cmd =
3334 match opthist with
3335 | None -> ()
3336 | Some (action, _) ->
3337 state.mode <- Textentry (
3338 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3340 G.postRedisplay "textentry histaction"
3342 match key with
3343 | 0xff08 -> (* backspace *)
3344 if emptystr text && cancelonempty
3345 then (
3346 onleave Cancel;
3347 G.postRedisplay "textentrykeyboard after cancel";
3349 else
3350 let s = withoutlastutf8 text in
3351 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3353 | 0xff0d | 0xff8d -> (* (kp) enter *)
3354 ondone text;
3355 onleave Confirm;
3356 G.postRedisplay "textentrykeyboard after confirm"
3358 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3359 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3360 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3361 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3363 | 0xff1b -> (* escape*)
3364 if emptystr text
3365 then (
3366 begin match opthist with
3367 | None -> ()
3368 | Some (_, onhistcancel) -> onhistcancel ()
3369 end;
3370 onleave Cancel;
3371 state.text <- "";
3372 G.postRedisplay "textentrykeyboard after cancel2"
3374 else (
3375 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3378 | 0xff9f | 0xffff -> () (* delete *)
3380 | _ when key != 0
3381 && key land 0xff00 != 0xff00 (* keyboard *)
3382 && key land 0xfe00 != 0xfe00 (* xkb *)
3383 && key land 0xfd00 != 0xfd00 (* 3270 *)
3385 begin match onkey text key with
3386 | TEdone text ->
3387 ondone text;
3388 onleave Confirm;
3389 G.postRedisplay "textentrykeyboard after confirm2";
3391 | TEcont text ->
3392 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3394 | TEstop ->
3395 onleave Cancel;
3396 G.postRedisplay "textentrykeyboard after cancel3"
3398 | TEswitch te ->
3399 state.mode <- Textentry (te, onleave);
3400 G.postRedisplay "textentrykeyboard switch";
3401 end;
3403 | _ ->
3404 vlog "unhandled key %s" (Wsi.keyname key)
3407 let firstof first active =
3408 if first > active || abs (first - active) > fstate.maxrows - 1
3409 then max 0 (active - (fstate.maxrows/2))
3410 else first
3413 let calcfirst first active =
3414 if active > first
3415 then
3416 let rows = active - first in
3417 if rows > fstate.maxrows then active - fstate.maxrows else first
3418 else active
3421 let scrollph y maxy =
3422 let sh = float (maxy + state.winh) /. float state.winh in
3423 let sh = float state.winh /. sh in
3424 let sh = max sh (float conf.scrollh) in
3426 let percent = float y /. float maxy in
3427 let position = (float state.winh -. sh) *. percent in
3429 let position =
3430 if position +. sh > float state.winh
3431 then float state.winh -. sh
3432 else position
3434 position, sh;
3437 let coe s = (s :> uioh);;
3439 class listview ~(source:lvsource) ~trusted ~modehash =
3440 object (self)
3441 val m_pan = source#getpan
3442 val m_first = source#getfirst
3443 val m_active = source#getactive
3444 val m_qsearch = source#getqsearch
3445 val m_prev_uioh = state.uioh
3447 method private elemunder y =
3448 let n = y / (fstate.fontsize+1) in
3449 if m_first + n < source#getitemcount
3450 then (
3451 if source#hasaction (m_first + n)
3452 then Some (m_first + n)
3453 else None
3455 else None
3457 method display =
3458 Gl.enable `blend;
3459 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3460 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3461 filledrect 0. 0. (float state.winw) (float state.winh);
3462 GlDraw.color (1., 1., 1.);
3463 Gl.enable `texture_2d;
3464 let fs = fstate.fontsize in
3465 let nfs = fs + 1 in
3466 let ww = fstate.wwidth in
3467 let tabw = 30.0*.ww in
3468 let itemcount = source#getitemcount in
3469 let rec loop row =
3470 if (row - m_first) > fstate.maxrows
3471 then ()
3472 else (
3473 if row >= 0 && row < itemcount
3474 then (
3475 let (s, level) = source#getitem row in
3476 let y = (row - m_first) * nfs in
3477 let x = 5.0 +. float (level + m_pan) *. ww in
3478 if row = m_active
3479 then (
3480 Gl.disable `texture_2d;
3481 let alpha = if source#hasaction row then 0.9 else 0.3 in
3482 GlDraw.color (1., 1., 1.) ~alpha;
3483 linerect 1. (float (y + 1))
3484 (float (state.winw - conf.scrollbw - 1)) (float (y + fs + 3));
3485 GlDraw.color (1., 1., 1.);
3486 Gl.enable `texture_2d;
3489 let drawtabularstring s =
3490 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3491 if trusted
3492 then
3493 let tabpos = try String.index s '\t' with Not_found -> -1 in
3494 if tabpos > 0
3495 then
3496 let len = String.length s - tabpos - 1 in
3497 let s1 = String.sub s 0 tabpos
3498 and s2 = String.sub s (tabpos + 1) len in
3499 let nx = drawstr x s1 in
3500 let sw = nx -. x in
3501 let x = x +. (max tabw sw) in
3502 drawstr x s2
3503 else
3504 drawstr x s
3505 else
3506 drawstr x s
3508 let _ = drawtabularstring s in
3509 loop (row+1)
3513 loop m_first;
3514 Gl.disable `blend;
3515 Gl.disable `texture_2d;
3517 method updownlevel incr =
3518 let len = source#getitemcount in
3519 let curlevel =
3520 if m_active >= 0 && m_active < len
3521 then snd (source#getitem m_active)
3522 else -1
3524 let rec flow i =
3525 if i = len then i-1 else if i = -1 then 0 else
3526 let _, l = source#getitem i in
3527 if l != curlevel then i else flow (i+incr)
3529 let active = flow m_active in
3530 let first = calcfirst m_first active in
3531 G.postRedisplay "outline updownlevel";
3532 {< m_active = active; m_first = first >}
3534 method private key1 key mask =
3535 let set1 active first qsearch =
3536 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3538 let search active pattern incr =
3539 let active = if active = -1 then m_first else active in
3540 let dosearch re =
3541 let rec loop n =
3542 if n >= 0 && n < source#getitemcount
3543 then (
3544 let s, _ = source#getitem n in
3546 (try ignore (Str.search_forward re s 0); true
3547 with Not_found -> false)
3548 then Some n
3549 else loop (n + incr)
3551 else None
3553 loop active
3556 let re = Str.regexp_case_fold pattern in
3557 dosearch re
3558 with Failure s ->
3559 state.text <- s;
3560 None
3562 let itemcount = source#getitemcount in
3563 let find start incr =
3564 let rec find i =
3565 if i = -1 || i = itemcount
3566 then -1
3567 else (
3568 if source#hasaction i
3569 then i
3570 else find (i + incr)
3573 find start
3575 let set active first =
3576 let first = bound first 0 (itemcount - fstate.maxrows) in
3577 state.text <- "";
3578 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3580 let navigate incr =
3581 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3582 let active, first =
3583 let incr1 = if incr > 0 then 1 else -1 in
3584 if isvisible m_first m_active
3585 then
3586 let next =
3587 let next = m_active + incr in
3588 let next =
3589 if next < 0 || next >= itemcount
3590 then -1
3591 else find next incr1
3593 if abs (m_active - next) > fstate.maxrows
3594 then -1
3595 else next
3597 if next = -1
3598 then
3599 let first = m_first + incr in
3600 let first = bound first 0 (itemcount - fstate.maxrows) in
3601 let next =
3602 let next = m_active + incr in
3603 let next = bound next 0 (itemcount - 1) in
3604 find next ~-incr1
3606 let active =
3607 if next = -1
3608 then m_active
3609 else (
3610 if isvisible first next
3611 then next
3612 else m_active
3615 active, first
3616 else
3617 let first = min next m_first in
3618 let first =
3619 if abs (next - first) > fstate.maxrows
3620 then first + incr
3621 else first
3623 next, first
3624 else
3625 let first = m_first + incr in
3626 let first = bound first 0 (itemcount - 1) in
3627 let active =
3628 let next = m_active + incr in
3629 let next = bound next 0 (itemcount - 1) in
3630 let next = find next incr1 in
3631 let active =
3632 if next = -1 || abs (m_active - first) > fstate.maxrows
3633 then (
3634 let active = if m_active = -1 then next else m_active in
3635 active
3637 else next
3639 if isvisible first active
3640 then active
3641 else -1
3643 active, first
3645 G.postRedisplay "listview navigate";
3646 set active first;
3648 match key with
3649 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3650 let incr = if key = 0x72 then -1 else 1 in
3651 let active, first =
3652 match search (m_active + incr) m_qsearch incr with
3653 | None ->
3654 state.text <- m_qsearch ^ " [not found]";
3655 m_active, m_first
3656 | Some active ->
3657 state.text <- m_qsearch;
3658 active, firstof m_first active
3660 G.postRedisplay "listview ctrl-r/s";
3661 set1 active first m_qsearch;
3663 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3664 if m_active >= 0 && m_active < source#getitemcount
3665 then (
3666 let s, _ = source#getitem m_active in
3667 selstring s;
3669 coe self
3671 | 0xff08 -> (* backspace *)
3672 if emptystr m_qsearch
3673 then coe self
3674 else (
3675 let qsearch = withoutlastutf8 m_qsearch in
3676 if emptystr qsearch
3677 then (
3678 state.text <- "";
3679 G.postRedisplay "listview empty qsearch";
3680 set1 m_active m_first "";
3682 else
3683 let active, first =
3684 match search m_active qsearch ~-1 with
3685 | None ->
3686 state.text <- qsearch ^ " [not found]";
3687 m_active, m_first
3688 | Some active ->
3689 state.text <- qsearch;
3690 active, firstof m_first active
3692 G.postRedisplay "listview backspace qsearch";
3693 set1 active first qsearch
3696 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3697 let pattern = m_qsearch ^ toutf8 key in
3698 let active, first =
3699 match search m_active pattern 1 with
3700 | None ->
3701 state.text <- pattern ^ " [not found]";
3702 m_active, m_first
3703 | Some active ->
3704 state.text <- pattern;
3705 active, firstof m_first active
3707 G.postRedisplay "listview qsearch add";
3708 set1 active first pattern;
3710 | 0xff1b -> (* escape *)
3711 state.text <- "";
3712 if emptystr m_qsearch
3713 then (
3714 G.postRedisplay "list view escape";
3715 begin
3716 match
3717 source#exit (coe self) true m_active m_first m_pan m_qsearch
3718 with
3719 | None -> m_prev_uioh
3720 | Some uioh -> uioh
3723 else (
3724 G.postRedisplay "list view kill qsearch";
3725 source#setqsearch "";
3726 coe {< m_qsearch = "" >}
3729 | 0xff0d | 0xff8d -> (* (kp) enter *)
3730 state.text <- "";
3731 let self = {< m_qsearch = "" >} in
3732 source#setqsearch "";
3733 let opt =
3734 G.postRedisplay "listview enter";
3735 if m_active >= 0 && m_active < source#getitemcount
3736 then (
3737 source#exit (coe self) false m_active m_first m_pan "";
3739 else (
3740 source#exit (coe self) true m_active m_first m_pan "";
3743 begin match opt with
3744 | None -> m_prev_uioh
3745 | Some uioh -> uioh
3748 | 0xff9f | 0xffff -> (* (kp) delete *)
3749 coe self
3751 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3752 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3753 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3754 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3756 | 0xff53 | 0xff98 -> (* (kp) right *)
3757 state.text <- "";
3758 G.postRedisplay "listview right";
3759 coe {< m_pan = m_pan - 1 >}
3761 | 0xff51 | 0xff96 -> (* (kp) left *)
3762 state.text <- "";
3763 G.postRedisplay "listview left";
3764 coe {< m_pan = m_pan + 1 >}
3766 | 0xff50 | 0xff95 -> (* (kp) home *)
3767 let active = find 0 1 in
3768 G.postRedisplay "listview home";
3769 set active 0;
3771 | 0xff57 | 0xff9c -> (* (kp) end *)
3772 let first = max 0 (itemcount - fstate.maxrows) in
3773 let active = find (itemcount - 1) ~-1 in
3774 G.postRedisplay "listview end";
3775 set active first;
3777 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3778 coe self
3780 | _ ->
3781 dolog "listview unknown key %#x" key; coe self
3783 method key key mask =
3784 match state.mode with
3785 | Textentry te -> textentrykeyboard key mask te; coe self
3786 | _ -> self#key1 key mask
3788 method button button down x y _ =
3789 let opt =
3790 match button with
3791 | 1 when x > state.winw - conf.scrollbw ->
3792 G.postRedisplay "listview scroll";
3793 if down
3794 then
3795 let _, position, sh = self#scrollph in
3796 if y > truncate position && y < truncate (position +. sh)
3797 then (
3798 state.mstate <- Mscrolly;
3799 Some (coe self)
3801 else
3802 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3803 let first = truncate (s *. float source#getitemcount) in
3804 let first = min source#getitemcount first in
3805 Some (coe {< m_first = first; m_active = first >})
3806 else (
3807 state.mstate <- Mnone;
3808 Some (coe self);
3810 | 1 when not down ->
3811 begin match self#elemunder y with
3812 | Some n ->
3813 G.postRedisplay "listview click";
3814 source#exit
3815 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3816 | _ ->
3817 Some (coe self)
3819 | n when (n == 4 || n == 5) && not down ->
3820 let len = source#getitemcount in
3821 let first =
3822 if n = 5 && m_first + fstate.maxrows >= len
3823 then
3824 m_first
3825 else
3826 let first = m_first + (if n == 4 then -1 else 1) in
3827 bound first 0 (len - 1)
3829 G.postRedisplay "listview wheel";
3830 Some (coe {< m_first = first >})
3831 | n when (n = 6 || n = 7) && not down ->
3832 let inc = if n = 7 then -1 else 1 in
3833 G.postRedisplay "listview hwheel";
3834 Some (coe {< m_pan = m_pan + inc >})
3835 | _ ->
3836 Some (coe self)
3838 match opt with
3839 | None -> m_prev_uioh
3840 | Some uioh -> uioh
3842 method motion _ y =
3843 match state.mstate with
3844 | Mscrolly ->
3845 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3846 let first = truncate (s *. float source#getitemcount) in
3847 let first = min source#getitemcount first in
3848 G.postRedisplay "listview motion";
3849 coe {< m_first = first; m_active = first >}
3850 | _ -> coe self
3852 method pmotion x y =
3853 if x < state.winw - conf.scrollbw
3854 then
3855 let n =
3856 match self#elemunder y with
3857 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3858 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3860 let o =
3861 if n != m_active
3862 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3863 else self
3865 coe o
3866 else (
3867 Wsi.setcursor Wsi.CURSOR_INHERIT;
3868 coe self
3871 method infochanged _ = ()
3873 method scrollpw = (0, 0.0, 0.0)
3874 method scrollph =
3875 let nfs = fstate.fontsize + 1 in
3876 let y = m_first * nfs in
3877 let itemcount = source#getitemcount in
3878 let maxi = max 0 (itemcount - fstate.maxrows) in
3879 let maxy = maxi * nfs in
3880 let p, h = scrollph y maxy in
3881 conf.scrollbw, p, h
3883 method modehash = modehash
3884 method eformsgs = false
3885 end;;
3887 class outlinelistview ~source =
3888 let settext autonarrow s =
3889 if autonarrow
3890 then state.text <- "[" ^ s ^ "]"
3891 else state.text <- s
3893 object (self)
3894 inherit listview
3895 ~source:(source :> lvsource)
3896 ~trusted:false
3897 ~modehash:(findkeyhash conf "outline")
3898 as super
3900 val m_autonarrow = false
3902 method key key mask =
3903 let maxrows =
3904 if emptystr state.text
3905 then fstate.maxrows
3906 else fstate.maxrows - 2
3908 let calcfirst first active =
3909 if active > first
3910 then
3911 let rows = active - first in
3912 if rows > maxrows then active - maxrows else first
3913 else active
3915 let navigate incr =
3916 let active = m_active + incr in
3917 let active = bound active 0 (source#getitemcount - 1) in
3918 let first = calcfirst m_first active in
3919 G.postRedisplay "outline navigate";
3920 coe {< m_active = active; m_first = first >}
3922 let navscroll first =
3923 let active =
3924 let dist = m_active - first in
3925 if dist < 0
3926 then first
3927 else (
3928 if dist < maxrows
3929 then m_active
3930 else first + maxrows
3933 G.postRedisplay "outline navscroll";
3934 coe {< m_first = first; m_active = active >}
3936 let ctrl = Wsi.withctrl mask in
3937 match key with
3938 | 97 when ctrl -> (* ctrl-a *)
3939 if m_autonarrow
3940 then source#denarrow
3941 else source#narrow m_qsearch;
3942 settext (not m_autonarrow) m_qsearch;
3943 G.postRedisplay "toggle auto narrowing";
3944 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3946 | 47 when emptystr m_qsearch && not m_autonarrow -> (* / *)
3947 settext true "";
3948 G.postRedisplay "toggle auto narrowing";
3949 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3951 | 110 when ctrl -> (* ctrl-n *)
3952 source#narrow m_qsearch;
3953 if not m_autonarrow
3954 then source#add_narrow_pattern m_qsearch;
3955 G.postRedisplay "outline ctrl-n";
3956 coe {< m_first = 0; m_active = 0 >}
3958 | 115 when ctrl -> (* ctrl-s *)
3959 let active = source#calcactive (getanchor ()) in
3960 let first = firstof m_first active in
3961 G.postRedisplay "outline ctrl-s";
3962 coe {< m_first = first; m_active = active >}
3964 | 117 when ctrl -> (* ctrl-u *)
3965 source#del_narrow_pattern;
3966 let pattern = source#renarrow in
3967 G.postRedisplay "outline ctrl-u";
3968 let text =
3969 if emptystr pattern then "" else "Narrowed to " ^ pattern
3971 settext m_autonarrow text;
3972 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
3974 | 108 when ctrl -> (* ctrl-l *)
3975 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3976 G.postRedisplay "outline ctrl-l";
3977 coe {< m_first = first >}
3979 | 0xff1b -> (* escape *)
3980 let o = super#key key mask in
3981 if m_autonarrow
3982 then (
3983 if nonemptystr m_qsearch
3984 then (
3985 source#add_narrow_pattern m_qsearch;
3986 settext true "";
3991 | 0xff0d | 0xff8d when m_autonarrow -> (* (kp) enter *)
3992 if nonemptystr m_qsearch
3993 then source#add_narrow_pattern m_qsearch;
3994 super#key key mask
3996 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3997 let pattern = m_qsearch ^ toutf8 key in
3998 G.postRedisplay "outlinelistview autonarrow add";
3999 source#narrow pattern;
4000 settext true pattern;
4001 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4003 | key when m_autonarrow && key = 0xff08 -> (* backspace *)
4004 if emptystr m_qsearch
4005 then coe self
4006 else
4007 let pattern = withoutlastutf8 m_qsearch in
4008 G.postRedisplay "outlinelistview autonarrow backspace";
4009 ignore (source#renarrow);
4010 source#narrow pattern;
4011 settext true pattern;
4012 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4014 | 0xff9f | 0xffff -> (* (kp) delete *)
4015 source#remove m_active;
4016 G.postRedisplay "outline delete";
4017 let active = max 0 (m_active-1) in
4018 coe {< m_first = firstof m_first active;
4019 m_active = active >}
4021 | 0xff52 | 0xff97 when ctrl -> (* ctrl-(kp) up *)
4022 navscroll (max 0 (m_first - 1))
4024 | 0xff54 | 0xff99 when ctrl -> (* ctrl-(kp) down *)
4025 navscroll (min (source#getitemcount - 1) (m_first + 1))
4027 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
4028 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
4029 | 0xff55 | 0xff9a -> (* (kp) prior *)
4030 navigate ~-(fstate.maxrows)
4031 | 0xff56 | 0xff9b -> (* (kp) next *)
4032 navigate fstate.maxrows
4034 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
4035 let o =
4036 if ctrl
4037 then (
4038 G.postRedisplay "outline ctrl right";
4039 {< m_pan = m_pan + 1 >}
4041 else self#updownlevel 1
4043 coe o
4045 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
4046 let o =
4047 if ctrl
4048 then (
4049 G.postRedisplay "outline ctrl left";
4050 {< m_pan = m_pan - 1 >}
4052 else self#updownlevel ~-1
4054 coe o
4056 | 0xff50 | 0xff95 -> (* (kp) home *)
4057 G.postRedisplay "outline home";
4058 coe {< m_first = 0; m_active = 0 >}
4060 | 0xff57 | 0xff9c -> (* (kp) end *)
4061 let active = source#getitemcount - 1 in
4062 let first = max 0 (active - fstate.maxrows) in
4063 G.postRedisplay "outline end";
4064 coe {< m_active = active; m_first = first >}
4066 | _ -> super#key key mask
4069 let outlinesource usebookmarks =
4070 let empty = [||] in
4071 (object (self)
4072 inherit lvsourcebase
4073 val mutable m_items = empty
4074 val mutable m_orig_items = empty
4075 val mutable m_narrow_patterns = []
4076 val mutable m_hadremovals = false
4078 method getitemcount =
4079 Array.length m_items + (if m_hadremovals then 1 else 0)
4081 method getitem n =
4082 if n == Array.length m_items && m_hadremovals
4083 then
4084 ("[Confirm removal]", 0)
4085 else
4086 let s, n, _ = m_items.(n) in
4087 (s, n)
4089 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4090 ignore (uioh, first, qsearch);
4091 let confrimremoval = m_hadremovals && active = Array.length m_items in
4092 let items =
4093 if m_narrow_patterns = []
4094 then m_orig_items
4095 else m_items
4097 if not cancel
4098 then (
4099 if not confrimremoval
4100 then (
4101 let _, _, ((pageno, y, _) as anchor) = m_items.(active) in
4102 let y = getanchory
4103 (if conf.presentation then (pageno, y, 1.0) else anchor)
4105 gotoghyll y;
4106 m_items <- items;
4108 else (
4109 state.bookmarks <- Array.to_list m_items;
4110 m_orig_items <- m_items;
4113 else m_items <- items;
4114 m_pan <- pan;
4115 None
4117 method hasaction _ = true
4119 method greetmsg =
4120 if Array.length m_items != Array.length m_orig_items
4121 then
4122 let s =
4123 match m_narrow_patterns with
4124 | one :: [] -> one
4125 | many -> String.concat " --> " (List.rev many)
4127 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4128 else ""
4130 method narrow pattern =
4131 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4132 match reopt with
4133 | None -> ()
4134 | Some re ->
4135 let rec loop accu n =
4136 if n = -1
4137 then m_items <- Array.of_list accu
4138 else
4139 let (s, _, _) as o = m_items.(n) in
4140 let accu =
4141 if (try ignore (Str.search_forward re s 0); true
4142 with Not_found -> false)
4143 then o :: accu
4144 else accu
4146 loop accu (n-1)
4148 loop [] (Array.length m_items - 1)
4150 method denarrow =
4151 m_orig_items <- (
4152 if usebookmarks
4153 then Array.of_list state.bookmarks
4154 else state.outlines
4156 m_items <- m_orig_items
4158 method remove m =
4159 if usebookmarks
4160 then
4161 if m >= 0 && m < Array.length m_items
4162 then (
4163 m_hadremovals <- true;
4164 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4165 let n = if n >= m then n+1 else n in
4166 m_items.(n)
4170 method add_narrow_pattern pattern =
4171 m_narrow_patterns <- pattern :: m_narrow_patterns
4173 method del_narrow_pattern =
4174 match m_narrow_patterns with
4175 | _ :: rest -> m_narrow_patterns <- rest
4176 | [] -> ()
4178 method renarrow =
4179 self#denarrow;
4180 match m_narrow_patterns with
4181 | pattern :: [] -> self#narrow pattern; pattern
4182 | list ->
4183 List.fold_left (fun accu pattern ->
4184 self#narrow pattern;
4185 accu ^ " --> " ^ pattern) "" list
4187 method calcactive anchor =
4188 let rely = getanchory anchor in
4189 let rec loop n best bestd =
4190 if n = Array.length m_items
4191 then best
4192 else
4193 let (_, _, anchor) = m_items.(n) in
4194 let orely = getanchory anchor in
4195 let d = abs (orely - rely) in
4196 if d < bestd
4197 then loop (n+1) n d
4198 else loop (n+1) best bestd
4200 loop 0 ~-1 max_int
4202 method reset anchor items =
4203 m_hadremovals <- false;
4204 if m_orig_items == empty
4205 then (
4206 m_orig_items <- items;
4207 if m_narrow_patterns == []
4208 then m_items <- items;
4210 let active = self#calcactive anchor in
4211 m_active <- active;
4212 m_first <- firstof m_first active
4213 end)
4216 let enterselector usebookmarks =
4217 let source = outlinesource usebookmarks in
4218 fun errmsg ->
4219 let outlines =
4220 if usebookmarks
4221 then Array.of_list state.bookmarks
4222 else state.outlines
4224 if Array.length outlines = 0
4225 then (
4226 showtext ' ' errmsg;
4228 else (
4229 state.text <- source#greetmsg;
4230 Wsi.setcursor Wsi.CURSOR_INHERIT;
4231 let anchor = getanchor () in
4232 source#reset anchor outlines;
4233 state.uioh <- coe (new outlinelistview ~source);
4234 G.postRedisplay "enter selector";
4238 let enteroutlinemode =
4239 let f = enterselector false in
4240 fun ()-> f "Document has no outline";
4243 let enterbookmarkmode =
4244 let f = enterselector true in
4245 fun () -> f "Document has no bookmarks (yet)";
4248 let color_of_string s =
4249 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4250 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4254 let color_to_string (r, g, b) =
4255 let r = truncate (r *. 256.0)
4256 and g = truncate (g *. 256.0)
4257 and b = truncate (b *. 256.0) in
4258 Printf.sprintf "%d/%d/%d" r g b
4261 let irect_of_string s =
4262 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4265 let irect_to_string (x0,y0,x1,y1) =
4266 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4269 let makecheckers () =
4270 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4271 following to say:
4272 converted by Issac Trotts. July 25, 2002 *)
4273 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4274 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4275 let id = GlTex.gen_texture () in
4276 GlTex.bind_texture `texture_2d id;
4277 GlPix.store (`unpack_alignment 1);
4278 GlTex.image2d image;
4279 List.iter (GlTex.parameter ~target:`texture_2d)
4280 [ `mag_filter `nearest; `min_filter `nearest ];
4284 let setcheckers enabled =
4285 match state.texid with
4286 | None ->
4287 if enabled then state.texid <- Some (makecheckers ())
4289 | Some texid ->
4290 if not enabled
4291 then (
4292 GlTex.delete_texture texid;
4293 state.texid <- None;
4297 let int_of_string_with_suffix s =
4298 let l = String.length s in
4299 let s1, shift =
4300 if l > 1
4301 then
4302 let suffix = Char.lowercase s.[l-1] in
4303 match suffix with
4304 | 'k' -> String.sub s 0 (l-1), 10
4305 | 'm' -> String.sub s 0 (l-1), 20
4306 | 'g' -> String.sub s 0 (l-1), 30
4307 | _ -> s, 0
4308 else s, 0
4310 let n = int_of_string s1 in
4311 let m = n lsl shift in
4312 if m < 0 || m < n
4313 then raise (Failure "value too large")
4314 else m
4317 let string_with_suffix_of_int n =
4318 if n = 0
4319 then "0"
4320 else
4321 let units = [(30, "G"); (20, "M"); (10, "K")] in
4322 let prettyint n =
4323 let rec loop s n =
4324 let h = n mod 1000 in
4325 let n = n / 1000 in
4326 if n = 0
4327 then string_of_int h ^ s
4328 else (
4329 let s = Printf.sprintf "_%03d%s" h s in
4330 loop s n
4333 loop "" n
4335 let rec find = function
4336 | [] -> prettyint n
4337 | (shift, suffix) :: rest ->
4338 if (n land ((1 lsl shift) - 1)) = 0
4339 then prettyint (n lsr shift) ^ suffix
4340 else find rest
4342 find units
4345 let defghyllscroll = (40, 8, 32);;
4346 let fastghyllscroll = (5,1,2);;
4347 let neatghyllscroll = (10,1,9);;
4348 let ghyllscroll_of_string s =
4349 match s with
4350 | "default" -> Some defghyllscroll
4351 | "fast" -> Some (5,1,2)
4352 | "neat" -> Some (10,1,9)
4353 | "" | "none" -> None
4354 | _ ->
4355 let (n,a,b) as nab =
4356 Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b) in
4357 if n <= a || n <= b || a >= b
4358 then error "invalid ghyll N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)"
4359 n a b;
4360 Some nab
4363 let ghyllscroll_to_string ((n, a, b) as nab) =
4364 (**) if nab = defghyllscroll then "default"
4365 else if nab = fastghyllscroll then "fast"
4366 else if nab = neatghyllscroll then "neat"
4367 else Printf.sprintf "%d,%d,%d" n a b;
4370 let describe_location () =
4371 let fn = page_of_y state.y in
4372 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4373 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4374 let percent =
4375 if maxy <= 0
4376 then 100.
4377 else (100. *. (float state.y /. float maxy))
4379 if fn = ln
4380 then
4381 Printf.sprintf "page %d of %d [%.2f%%]"
4382 (fn+1) state.pagecount percent
4383 else
4384 Printf.sprintf
4385 "pages %d-%d of %d [%.2f%%]"
4386 (fn+1) (ln+1) state.pagecount percent
4389 let setpresentationmode v =
4390 let n = page_of_y state.y in
4391 state.anchor <- (n, 0.0, 1.0);
4392 conf.presentation <- v;
4393 if conf.fitmodel = FitPage
4394 then reqlayout conf.angle conf.fitmodel;
4395 represent ();
4398 let enterinfomode =
4399 let btos b = if b then "\xe2\x88\x9a" else "" in
4400 let showextended = ref false in
4401 let leave mode = function
4402 | Confirm -> state.mode <- mode
4403 | Cancel -> state.mode <- mode in
4404 let src =
4405 (object
4406 val mutable m_first_time = true
4407 val mutable m_l = []
4408 val mutable m_a = [||]
4409 val mutable m_prev_uioh = nouioh
4410 val mutable m_prev_mode = View
4412 inherit lvsourcebase
4414 method reset prev_mode prev_uioh =
4415 m_a <- Array.of_list (List.rev m_l);
4416 m_l <- [];
4417 m_prev_mode <- prev_mode;
4418 m_prev_uioh <- prev_uioh;
4419 if m_first_time
4420 then (
4421 let rec loop n =
4422 if n >= Array.length m_a
4423 then ()
4424 else
4425 match m_a.(n) with
4426 | _, _, _, Action _ -> m_active <- n
4427 | _ -> loop (n+1)
4429 loop 0;
4430 m_first_time <- false;
4433 method int name get set =
4434 m_l <-
4435 (name, `int get, 1, Action (
4436 fun u ->
4437 let ondone s =
4438 try set (int_of_string s)
4439 with exn ->
4440 state.text <- Printf.sprintf "bad integer `%s': %s"
4441 s (exntos exn)
4443 state.text <- "";
4444 let te = name ^ ": ", "", None, intentry, ondone, true in
4445 state.mode <- Textentry (te, leave m_prev_mode);
4447 )) :: m_l
4449 method int_with_suffix name get set =
4450 m_l <-
4451 (name, `intws get, 1, Action (
4452 fun u ->
4453 let ondone s =
4454 try set (int_of_string_with_suffix s)
4455 with exn ->
4456 state.text <- Printf.sprintf "bad integer `%s': %s"
4457 s (exntos exn)
4459 state.text <- "";
4460 let te =
4461 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4463 state.mode <- Textentry (te, leave m_prev_mode);
4465 )) :: m_l
4467 method bool ?(offset=1) ?(btos=btos) name get set =
4468 m_l <-
4469 (name, `bool (btos, get), offset, Action (
4470 fun u ->
4471 let v = get () in
4472 set (not v);
4474 )) :: m_l
4476 method color name get set =
4477 m_l <-
4478 (name, `color get, 1, Action (
4479 fun u ->
4480 let invalid = (nan, nan, nan) in
4481 let ondone s =
4482 let c =
4483 try color_of_string s
4484 with exn ->
4485 state.text <- Printf.sprintf "bad color `%s': %s"
4486 s (exntos exn);
4487 invalid
4489 if c <> invalid
4490 then set c;
4492 let te = name ^ ": ", "", None, textentry, ondone, true in
4493 state.text <- color_to_string (get ());
4494 state.mode <- Textentry (te, leave m_prev_mode);
4496 )) :: m_l
4498 method string name get set =
4499 m_l <-
4500 (name, `string get, 1, Action (
4501 fun u ->
4502 let ondone s = set s in
4503 let te = name ^ ": ", "", None, textentry, ondone, true in
4504 state.mode <- Textentry (te, leave m_prev_mode);
4506 )) :: m_l
4508 method colorspace name get set =
4509 m_l <-
4510 (name, `string get, 1, Action (
4511 fun _ ->
4512 let source =
4513 (object
4514 inherit lvsourcebase
4516 initializer
4517 m_active <- CSTE.to_int conf.colorspace;
4518 m_first <- 0;
4520 method getitemcount =
4521 Array.length CSTE.names
4522 method getitem n =
4523 (CSTE.names.(n), 0)
4524 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4525 ignore (uioh, first, pan, qsearch);
4526 if not cancel then set active;
4527 None
4528 method hasaction _ = true
4529 end)
4531 state.text <- "";
4532 let modehash = findkeyhash conf "info" in
4533 coe (new listview ~source ~trusted:true ~modehash)
4534 )) :: m_l
4536 method paxmark name get set =
4537 m_l <-
4538 (name, `string get, 1, Action (
4539 fun _ ->
4540 let source =
4541 (object
4542 inherit lvsourcebase
4544 initializer
4545 m_active <- MTE.to_int conf.paxmark;
4546 m_first <- 0;
4548 method getitemcount = Array.length MTE.names
4549 method getitem n = (MTE.names.(n), 0)
4550 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4551 ignore (uioh, first, pan, qsearch);
4552 if not cancel then set active;
4553 None
4554 method hasaction _ = true
4555 end)
4557 state.text <- "";
4558 let modehash = findkeyhash conf "info" in
4559 coe (new listview ~source ~trusted:true ~modehash)
4560 )) :: m_l
4562 method fitmodel name get set =
4563 m_l <-
4564 (name, `string get, 1, Action (
4565 fun _ ->
4566 let source =
4567 (object
4568 inherit lvsourcebase
4570 initializer
4571 m_active <- FMTE.to_int conf.fitmodel;
4572 m_first <- 0;
4574 method getitemcount = Array.length FMTE.names
4575 method getitem n = (FMTE.names.(n), 0)
4576 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4577 ignore (uioh, first, pan, qsearch);
4578 if not cancel then set active;
4579 None
4580 method hasaction _ = true
4581 end)
4583 state.text <- "";
4584 let modehash = findkeyhash conf "info" in
4585 coe (new listview ~source ~trusted:true ~modehash)
4586 )) :: m_l
4588 method caption s offset =
4589 m_l <- (s, `empty, offset, Noaction) :: m_l
4591 method caption2 s f offset =
4592 m_l <- (s, `string f, offset, Noaction) :: m_l
4594 method getitemcount = Array.length m_a
4596 method getitem n =
4597 let tostr = function
4598 | `int f -> string_of_int (f ())
4599 | `intws f -> string_with_suffix_of_int (f ())
4600 | `string f -> f ()
4601 | `color f -> color_to_string (f ())
4602 | `bool (btos, f) -> btos (f ())
4603 | `empty -> ""
4605 let name, t, offset, _ = m_a.(n) in
4606 ((let s = tostr t in
4607 if nonemptystr s
4608 then Printf.sprintf "%s\t%s" name s
4609 else name),
4610 offset)
4612 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4613 let uiohopt =
4614 if not cancel
4615 then (
4616 m_qsearch <- qsearch;
4617 let uioh =
4618 match m_a.(active) with
4619 | _, _, _, Action f -> f uioh
4620 | _ -> uioh
4622 Some uioh
4624 else None
4626 m_active <- active;
4627 m_first <- first;
4628 m_pan <- pan;
4629 uiohopt
4631 method hasaction n =
4632 match m_a.(n) with
4633 | _, _, _, Action _ -> true
4634 | _ -> false
4635 end)
4637 let rec fillsrc prevmode prevuioh =
4638 let sep () = src#caption "" 0 in
4639 let colorp name get set =
4640 src#string name
4641 (fun () -> color_to_string (get ()))
4642 (fun v ->
4644 let c = color_of_string v in
4645 set c
4646 with exn ->
4647 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4650 let oldmode = state.mode in
4651 let birdseye = isbirdseye state.mode in
4653 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4655 src#bool "presentation mode"
4656 (fun () -> conf.presentation)
4657 (fun v -> setpresentationmode v);
4659 src#bool "ignore case in searches"
4660 (fun () -> conf.icase)
4661 (fun v -> conf.icase <- v);
4663 src#bool "preload"
4664 (fun () -> conf.preload)
4665 (fun v -> conf.preload <- v);
4667 src#bool "highlight links"
4668 (fun () -> conf.hlinks)
4669 (fun v -> conf.hlinks <- v);
4671 src#bool "under info"
4672 (fun () -> conf.underinfo)
4673 (fun v -> conf.underinfo <- v);
4675 src#bool "persistent bookmarks"
4676 (fun () -> conf.savebmarks)
4677 (fun v -> conf.savebmarks <- v);
4679 src#fitmodel "fit model"
4680 (fun () -> FMTE.to_string conf.fitmodel)
4681 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4683 src#bool "trim margins"
4684 (fun () -> conf.trimmargins)
4685 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4687 src#bool "persistent location"
4688 (fun () -> conf.jumpback)
4689 (fun v -> conf.jumpback <- v);
4691 sep ();
4692 src#int "inter-page space"
4693 (fun () -> conf.interpagespace)
4694 (fun n ->
4695 conf.interpagespace <- n;
4696 docolumns conf.columns;
4697 let pageno, py =
4698 match state.layout with
4699 | [] -> 0, 0
4700 | l :: _ ->
4701 l.pageno, l.pagey
4703 state.maxy <- calcheight ();
4704 let y = getpagey pageno in
4705 gotoy (y + py)
4708 src#int "page bias"
4709 (fun () -> conf.pagebias)
4710 (fun v -> conf.pagebias <- v);
4712 src#int "scroll step"
4713 (fun () -> conf.scrollstep)
4714 (fun n -> conf.scrollstep <- n);
4716 src#int "horizontal scroll step"
4717 (fun () -> conf.hscrollstep)
4718 (fun v -> conf.hscrollstep <- v);
4720 src#int "auto scroll step"
4721 (fun () ->
4722 match state.autoscroll with
4723 | Some step -> step
4724 | _ -> conf.autoscrollstep)
4725 (fun n ->
4726 if state.autoscroll <> None
4727 then state.autoscroll <- Some n;
4728 conf.autoscrollstep <- n);
4730 src#int "zoom"
4731 (fun () -> truncate (conf.zoom *. 100.))
4732 (fun v -> setzoom ((float v) /. 100.));
4734 src#int "rotation"
4735 (fun () -> conf.angle)
4736 (fun v -> reqlayout v conf.fitmodel);
4738 src#int "scroll bar width"
4739 (fun () -> conf.scrollbw)
4740 (fun v ->
4741 conf.scrollbw <- v;
4742 reshape state.winw state.winh;
4745 src#int "scroll handle height"
4746 (fun () -> conf.scrollh)
4747 (fun v -> conf.scrollh <- v;);
4749 src#int "thumbnail width"
4750 (fun () -> conf.thumbw)
4751 (fun v ->
4752 conf.thumbw <- min 4096 v;
4753 match oldmode with
4754 | Birdseye beye ->
4755 leavebirdseye beye false;
4756 enterbirdseye ()
4757 | _ -> ()
4760 let mode = state.mode in
4761 src#string "columns"
4762 (fun () ->
4763 match conf.columns with
4764 | Csingle _ -> "1"
4765 | Cmulti (multi, _) -> multicolumns_to_string multi
4766 | Csplit (count, _) -> "-" ^ string_of_int count
4768 (fun v ->
4769 let n, a, b = multicolumns_of_string v in
4770 setcolumns mode n a b);
4772 sep ();
4773 src#caption "Pixmap cache" 0;
4774 src#int_with_suffix "size (advisory)"
4775 (fun () -> conf.memlimit)
4776 (fun v -> conf.memlimit <- v);
4778 src#caption2 "used"
4779 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4780 (string_with_suffix_of_int state.memused)
4781 (Hashtbl.length state.tilemap)) 1;
4783 sep ();
4784 src#caption "Layout" 0;
4785 src#caption2 "Dimension"
4786 (fun () ->
4787 Printf.sprintf "%dx%d (virtual %dx%d)"
4788 state.winw state.winh
4789 state.w state.maxy)
4791 if conf.debug
4792 then
4793 src#caption2 "Position" (fun () ->
4794 Printf.sprintf "%dx%d" state.x state.y
4796 else
4797 src#caption2 "Position" (fun () -> describe_location ()) 1
4800 sep ();
4801 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4802 "Save these parameters as global defaults at exit"
4803 (fun () -> conf.bedefault)
4804 (fun v -> conf.bedefault <- v)
4807 sep ();
4808 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4809 src#bool ~offset:0 ~btos "Extended parameters"
4810 (fun () -> !showextended)
4811 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4812 if !showextended
4813 then (
4814 src#bool "checkers"
4815 (fun () -> conf.checkers)
4816 (fun v -> conf.checkers <- v; setcheckers v);
4817 src#bool "update cursor"
4818 (fun () -> conf.updatecurs)
4819 (fun v -> conf.updatecurs <- v);
4820 src#bool "verbose"
4821 (fun () -> conf.verbose)
4822 (fun v -> conf.verbose <- v);
4823 src#bool "invert colors"
4824 (fun () -> conf.invert)
4825 (fun v -> conf.invert <- v);
4826 src#bool "max fit"
4827 (fun () -> conf.maxhfit)
4828 (fun v -> conf.maxhfit <- v);
4829 src#bool "redirect stderr"
4830 (fun () -> conf.redirectstderr)
4831 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4832 src#bool "pax mode"
4833 (fun () -> conf.pax != None)
4834 (fun v ->
4835 if v
4836 then conf.pax <- Some (ref (now (), 0, 0))
4837 else conf.pax <- None);
4838 src#string "uri launcher"
4839 (fun () -> conf.urilauncher)
4840 (fun v -> conf.urilauncher <- v);
4841 src#string "path launcher"
4842 (fun () -> conf.pathlauncher)
4843 (fun v -> conf.pathlauncher <- v);
4844 src#string "tile size"
4845 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4846 (fun v ->
4848 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4849 conf.tilew <- max 64 w;
4850 conf.tileh <- max 64 h;
4851 flushtiles ();
4852 with exn ->
4853 state.text <- Printf.sprintf "bad tile size `%s': %s"
4854 v (exntos exn)
4856 src#int "texture count"
4857 (fun () -> conf.texcount)
4858 (fun v ->
4859 if realloctexts v
4860 then conf.texcount <- v
4861 else showtext '!' " Failed to set texture count please retry later"
4863 src#int "slice height"
4864 (fun () -> conf.sliceheight)
4865 (fun v ->
4866 conf.sliceheight <- v;
4867 wcmd "sliceh %d" conf.sliceheight;
4869 src#int "anti-aliasing level"
4870 (fun () -> conf.aalevel)
4871 (fun v ->
4872 conf.aalevel <- bound v 0 8;
4873 state.anchor <- getanchor ();
4874 opendoc state.path state.password;
4876 src#string "page scroll scaling factor"
4877 (fun () -> string_of_float conf.pgscale)
4878 (fun v ->
4880 let s = float_of_string v in
4881 conf.pgscale <- s
4882 with exn ->
4883 state.text <- Printf.sprintf
4884 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4887 src#int "ui font size"
4888 (fun () -> fstate.fontsize)
4889 (fun v -> setfontsize (bound v 5 100));
4890 src#int "hint font size"
4891 (fun () -> conf.hfsize)
4892 (fun v -> conf.hfsize <- bound v 5 100);
4893 colorp "background color"
4894 (fun () -> conf.bgcolor)
4895 (fun v -> conf.bgcolor <- v);
4896 src#bool "crop hack"
4897 (fun () -> conf.crophack)
4898 (fun v -> conf.crophack <- v);
4899 src#string "trim fuzz"
4900 (fun () -> irect_to_string conf.trimfuzz)
4901 (fun v ->
4903 conf.trimfuzz <- irect_of_string v;
4904 if conf.trimmargins
4905 then settrim true conf.trimfuzz;
4906 with exn ->
4907 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4909 src#string "throttle"
4910 (fun () ->
4911 match conf.maxwait with
4912 | None -> "show place holder if page is not ready"
4913 | Some time ->
4914 if time = infinity
4915 then "wait for page to fully render"
4916 else
4917 "wait " ^ string_of_float time
4918 ^ " seconds before showing placeholder"
4920 (fun v ->
4922 let f = float_of_string v in
4923 if f <= 0.0
4924 then conf.maxwait <- None
4925 else conf.maxwait <- Some f
4926 with exn ->
4927 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4929 src#string "ghyll scroll"
4930 (fun () ->
4931 match conf.ghyllscroll with
4932 | None -> ""
4933 | Some nab -> ghyllscroll_to_string nab
4935 (fun v ->
4936 try conf.ghyllscroll <- ghyllscroll_of_string v
4937 with exn ->
4938 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4940 src#string "selection command"
4941 (fun () -> conf.selcmd)
4942 (fun v -> conf.selcmd <- v);
4943 src#string "synctex command"
4944 (fun () -> conf.stcmd)
4945 (fun v -> conf.stcmd <- v);
4946 src#string "pax command"
4947 (fun () -> conf.paxcmd)
4948 (fun v -> conf.paxcmd <- v);
4949 src#colorspace "color space"
4950 (fun () -> CSTE.to_string conf.colorspace)
4951 (fun v ->
4952 conf.colorspace <- CSTE.of_int v;
4953 wcmd "cs %d" v;
4954 load state.layout;
4956 src#paxmark "pax mark method"
4957 (fun () -> MTE.to_string conf.paxmark)
4958 (fun v -> conf.paxmark <- MTE.of_int v);
4959 if pbousable ()
4960 then
4961 src#bool "use PBO"
4962 (fun () -> conf.usepbo)
4963 (fun v -> conf.usepbo <- v);
4964 src#bool "mouse wheel scrolls pages"
4965 (fun () -> conf.wheelbypage)
4966 (fun v -> conf.wheelbypage <- v);
4967 src#bool "open remote links in a new instance"
4968 (fun () -> conf.riani)
4969 (fun v -> conf.riani <- v);
4972 sep ();
4973 src#caption "Document" 0;
4974 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4975 src#caption2 "Pages"
4976 (fun () -> string_of_int state.pagecount) 1;
4977 src#caption2 "Dimensions"
4978 (fun () -> string_of_int (List.length state.pdims)) 1;
4979 if conf.trimmargins
4980 then (
4981 sep ();
4982 src#caption "Trimmed margins" 0;
4983 src#caption2 "Dimensions"
4984 (fun () -> string_of_int (List.length state.pdims)) 1;
4987 sep ();
4988 src#caption "OpenGL" 0;
4989 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4990 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4992 sep ();
4993 src#caption "Location" 0;
4994 if nonemptystr state.origin
4995 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4996 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4998 src#reset prevmode prevuioh;
5000 fun () ->
5001 state.text <- "";
5002 let prevmode = state.mode
5003 and prevuioh = state.uioh in
5004 fillsrc prevmode prevuioh;
5005 let source = (src :> lvsource) in
5006 let modehash = findkeyhash conf "info" in
5007 state.uioh <- coe (object (self)
5008 inherit listview ~source ~trusted:true ~modehash as super
5009 val mutable m_prevmemused = 0
5010 method infochanged = function
5011 | Memused ->
5012 if m_prevmemused != state.memused
5013 then (
5014 m_prevmemused <- state.memused;
5015 G.postRedisplay "memusedchanged";
5017 | Pdim -> G.postRedisplay "pdimchanged"
5018 | Docinfo -> fillsrc prevmode prevuioh
5020 method key key mask =
5021 if not (Wsi.withctrl mask)
5022 then
5023 match key with
5024 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
5025 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
5026 | _ -> super#key key mask
5027 else super#key key mask
5028 end);
5029 G.postRedisplay "info";
5032 let enterhelpmode =
5033 let source =
5034 (object
5035 inherit lvsourcebase
5036 method getitemcount = Array.length state.help
5037 method getitem n =
5038 let s, l, _ = state.help.(n) in
5039 (s, l)
5041 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5042 let optuioh =
5043 if not cancel
5044 then (
5045 m_qsearch <- qsearch;
5046 match state.help.(active) with
5047 | _, _, Action f -> Some (f uioh)
5048 | _ -> Some (uioh)
5050 else None
5052 m_active <- active;
5053 m_first <- first;
5054 m_pan <- pan;
5055 optuioh
5057 method hasaction n =
5058 match state.help.(n) with
5059 | _, _, Action _ -> true
5060 | _ -> false
5062 initializer
5063 m_active <- -1
5064 end)
5065 in fun () ->
5066 let modehash = findkeyhash conf "help" in
5067 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
5068 G.postRedisplay "help";
5071 let entermsgsmode =
5072 let msgsource =
5073 let re = Str.regexp "[\r\n]" in
5074 (object
5075 inherit lvsourcebase
5076 val mutable m_items = [||]
5078 method getitemcount = 1 + Array.length m_items
5080 method getitem n =
5081 if n = 0
5082 then "[Clear]", 0
5083 else m_items.(n-1), 0
5085 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5086 ignore uioh;
5087 if not cancel
5088 then (
5089 if active = 0
5090 then Buffer.clear state.errmsgs;
5091 m_qsearch <- qsearch;
5093 m_active <- active;
5094 m_first <- first;
5095 m_pan <- pan;
5096 None
5098 method hasaction n =
5099 n = 0
5101 method reset =
5102 state.newerrmsgs <- false;
5103 let l = Str.split re (Buffer.contents state.errmsgs) in
5104 m_items <- Array.of_list l
5106 initializer
5107 m_active <- 0
5108 end)
5109 in fun () ->
5110 state.text <- "";
5111 msgsource#reset;
5112 let source = (msgsource :> lvsource) in
5113 let modehash = findkeyhash conf "listview" in
5114 state.uioh <- coe (object
5115 inherit listview ~source ~trusted:false ~modehash as super
5116 method display =
5117 if state.newerrmsgs
5118 then msgsource#reset;
5119 super#display
5120 end);
5121 G.postRedisplay "msgs";
5124 let quickbookmark ?title () =
5125 match state.layout with
5126 | [] -> ()
5127 | l :: _ ->
5128 let title =
5129 match title with
5130 | None ->
5131 let sec = Unix.gettimeofday () in
5132 let tm = Unix.localtime sec in
5133 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
5134 (l.pageno+1)
5135 tm.Unix.tm_mday
5136 tm.Unix.tm_mon
5137 (tm.Unix.tm_year + 1900)
5138 tm.Unix.tm_hour
5139 tm.Unix.tm_min
5140 | Some title -> title
5142 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
5145 let setautoscrollspeed step goingdown =
5146 let incr = max 1 ((abs step) / 2) in
5147 let incr = if goingdown then incr else -incr in
5148 let astep = step + incr in
5149 state.autoscroll <- Some astep;
5152 let gotounder under =
5153 let getpath filename =
5154 let path =
5155 if nonemptystr filename
5156 then
5157 if Filename.is_relative filename
5158 then
5159 let dir = Filename.dirname state.path in
5160 let dir =
5161 if Filename.is_implicit dir
5162 then Filename.concat (Sys.getcwd ()) dir
5163 else dir
5165 Filename.concat dir filename
5166 else filename
5167 else ""
5169 if Sys.file_exists path
5170 then path
5171 else ""
5173 match under with
5174 | Ulinkgoto (pageno, top) ->
5175 if pageno >= 0
5176 then (
5177 addnav ();
5178 gotopage1 pageno top;
5181 | Ulinkuri s ->
5182 gotouri s
5184 | Uremote (filename, pageno) ->
5185 let path = getpath filename in
5186 if nonemptystr path
5187 then (
5188 if conf.riani
5189 then
5190 let command = !selfexec ^ " " ^ path in
5191 try popen command []
5192 with exn ->
5193 Printf.eprintf
5194 "failed to execute `%s': %s\n" command (exntos exn);
5195 flush stderr;
5196 else
5197 let anchor = getanchor () in
5198 let ranchor = state.path, state.password, anchor, state.origin in
5199 state.origin <- "";
5200 state.anchor <- (pageno, 0.0, 0.0);
5201 state.ranchors <- ranchor :: state.ranchors;
5202 opendoc path "";
5204 else showtext '!' ("Could not find " ^ filename)
5206 | Uremotedest (filename, destname) ->
5207 let path = getpath filename in
5208 if nonemptystr path
5209 then (
5210 if conf.riani
5211 then
5212 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
5213 try popen command []
5214 with exn ->
5215 Printf.eprintf
5216 "failed to execute `%s': %s\n" command (exntos exn);
5217 flush stderr;
5218 else
5219 let anchor = getanchor () in
5220 let ranchor = state.path, state.password, anchor, state.origin in
5221 state.origin <- "";
5222 state.nameddest <- destname;
5223 state.ranchors <- ranchor :: state.ranchors;
5224 opendoc path "";
5226 else showtext '!' ("Could not find " ^ filename)
5228 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
5231 let canpan () =
5232 match conf.columns with
5233 | Csplit _ -> true
5234 | _ -> state.x != 0 || conf.zoom > 1.0
5237 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5239 let existsinrow pageno (columns, coverA, coverB) p =
5240 let last = ((pageno - coverA) mod columns) + columns in
5241 let rec any = function
5242 | [] -> false
5243 | l :: rest ->
5244 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5245 then p l
5246 else (
5247 if not (p l)
5248 then (if l.pageno = last then false else any rest)
5249 else true
5252 any state.layout
5255 let nextpage () =
5256 match state.layout with
5257 | [] ->
5258 let pageno = page_of_y state.y in
5259 gotoghyll (getpagey (pageno+1))
5260 | l :: rest ->
5261 match conf.columns with
5262 | Csingle _ ->
5263 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5264 then
5265 let y = clamp (pgscale state.winh) in
5266 gotoghyll y
5267 else
5268 let pageno = min (l.pageno+1) (state.pagecount-1) in
5269 gotoghyll (getpagey pageno)
5270 | Cmulti ((c, _, _) as cl, _) ->
5271 if conf.presentation
5272 && (existsinrow l.pageno cl
5273 (fun l -> l.pageh > l.pagey + l.pagevh))
5274 then
5275 let y = clamp (pgscale state.winh) in
5276 gotoghyll y
5277 else
5278 let pageno = min (l.pageno+c) (state.pagecount-1) in
5279 gotoghyll (getpagey pageno)
5280 | Csplit (n, _) ->
5281 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5282 then
5283 let pagey, pageh = getpageyh l.pageno in
5284 let pagey = pagey + pageh * l.pagecol in
5285 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5286 gotoghyll (pagey + pageh + ips)
5289 let prevpage () =
5290 match state.layout with
5291 | [] ->
5292 let pageno = page_of_y state.y in
5293 gotoghyll (getpagey (pageno-1))
5294 | l :: _ ->
5295 match conf.columns with
5296 | Csingle _ ->
5297 if conf.presentation && l.pagey != 0
5298 then
5299 gotoghyll (clamp (pgscale ~-(state.winh)))
5300 else
5301 let pageno = max 0 (l.pageno-1) in
5302 gotoghyll (getpagey pageno)
5303 | Cmulti ((c, _, coverB) as cl, _) ->
5304 if conf.presentation &&
5305 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5306 then
5307 gotoghyll (clamp (pgscale ~-(state.winh)))
5308 else
5309 let decr =
5310 if l.pageno = state.pagecount - coverB
5311 then 1
5312 else c
5314 let pageno = max 0 (l.pageno-decr) in
5315 gotoghyll (getpagey pageno)
5316 | Csplit (n, _) ->
5317 let y =
5318 if l.pagecol = 0
5319 then
5320 if l.pageno = 0
5321 then l.pagey
5322 else
5323 let pageno = max 0 (l.pageno-1) in
5324 let pagey, pageh = getpageyh pageno in
5325 pagey + (n-1)*pageh
5326 else
5327 let pagey, pageh = getpageyh l.pageno in
5328 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5330 gotoghyll y
5333 let viewkeyboard key mask =
5334 let enttext te =
5335 let mode = state.mode in
5336 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5337 state.text <- "";
5338 enttext ();
5339 G.postRedisplay "view:enttext"
5341 let ctrl = Wsi.withctrl mask in
5342 let key =
5343 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5345 match key with
5346 | 81 -> (* Q *)
5347 exit 0
5349 | 0xff63 -> (* insert *)
5350 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5351 then (
5352 state.mode <- LinkNav (Ltgendir 0);
5353 gotoy state.y;
5355 else showtext '!' "Keyboard link navigation does not work under rotation"
5357 | 0xff1b | 113 -> (* escape / q *)
5358 begin match state.mstate with
5359 | Mzoomrect _ ->
5360 state.mstate <- Mnone;
5361 Wsi.setcursor Wsi.CURSOR_INHERIT;
5362 G.postRedisplay "kill zoom rect";
5363 | _ ->
5364 begin match state.mode with
5365 | LinkNav _ ->
5366 state.mode <- View;
5367 G.postRedisplay "esc leave linknav"
5368 | _ ->
5369 match state.ranchors with
5370 | [] -> raise Quit
5371 | (path, password, anchor, origin) :: rest ->
5372 state.ranchors <- rest;
5373 state.anchor <- anchor;
5374 state.origin <- origin;
5375 state.nameddest <- "";
5376 opendoc path password
5377 end;
5378 end;
5380 | 0xff08 -> (* backspace *)
5381 gotoghyll (getnav ~-1)
5383 | 111 -> (* o *)
5384 enteroutlinemode ()
5386 | 117 -> (* u *)
5387 state.rects <- [];
5388 state.text <- "";
5389 G.postRedisplay "dehighlight";
5391 | 47 | 63 -> (* / ? *)
5392 let ondone isforw s =
5393 cbput state.hists.pat s;
5394 state.searchpattern <- s;
5395 search s isforw
5397 let s = String.create 1 in
5398 s.[0] <- Char.chr key;
5399 enttext (s, "", Some (onhist state.hists.pat),
5400 textentry, ondone (key = 47), true)
5402 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5403 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5404 setzoom (conf.zoom +. incr)
5406 | 43 | 0xffab -> (* + *)
5407 let ondone s =
5408 let n =
5409 try int_of_string s with exc ->
5410 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5411 max_int
5413 if n != max_int
5414 then (
5415 conf.pagebias <- n;
5416 state.text <- "page bias is now " ^ string_of_int n;
5419 enttext ("page bias: ", "", None, intentry, ondone, true)
5421 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5422 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5423 setzoom (max 0.01 (conf.zoom -. decr))
5425 | 45 | 0xffad -> (* - *)
5426 let ondone msg = state.text <- msg in
5427 enttext (
5428 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5429 optentry state.mode, ondone, true
5432 | 48 when ctrl -> (* ctrl-0 *)
5433 if conf.zoom = 1.0
5434 then (
5435 state.x <- 0;
5436 gotoy state.y
5438 else setzoom 1.0
5440 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5441 let cols =
5442 match conf.columns with
5443 | Csingle _ | Cmulti _ -> 1
5444 | Csplit (n, _) -> n
5446 let h = state.winh -
5447 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5449 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5450 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5451 then setzoom zoom
5453 | 51 when ctrl -> (* ctrl-3 *)
5454 let fm =
5455 match conf.fitmodel with
5456 | FitWidth -> FitProportional
5457 | FitProportional -> FitPage
5458 | FitPage -> FitWidth
5460 state.text <- "fit model: " ^ FMTE.to_string fm;
5461 reqlayout conf.angle fm
5463 | 0xffc6 -> (* f9 *)
5464 togglebirdseye ()
5466 | 57 when ctrl -> (* ctrl-9 *)
5467 togglebirdseye ()
5469 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5470 when not ctrl -> (* 0..9 *)
5471 let ondone s =
5472 let n =
5473 try int_of_string s with exc ->
5474 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5477 if n >= 0
5478 then (
5479 addnav ();
5480 cbput state.hists.pag (string_of_int n);
5481 gotopage1 (n + conf.pagebias - 1) 0;
5484 let pageentry text key =
5485 match Char.unsafe_chr key with
5486 | 'g' -> TEdone text
5487 | _ -> intentry text key
5489 let text = "x" in text.[0] <- Char.chr key;
5490 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5492 | 98 -> (* b *)
5493 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5494 reshape state.winw state.winh;
5496 | 66 -> (* B *)
5497 state.bzoom <- not state.bzoom;
5498 state.rects <- [];
5499 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5501 | 108 -> (* l *)
5502 conf.hlinks <- not conf.hlinks;
5503 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5504 G.postRedisplay "toggle highlightlinks";
5506 | 70 -> (* F *)
5507 state.glinks <- true;
5508 let mode = state.mode in
5509 state.mode <- Textentry (
5510 (":", "", None, linknentry, linkndone gotounder, false),
5511 (fun _ ->
5512 state.glinks <- false;
5513 state.mode <- mode)
5515 state.text <- "";
5516 G.postRedisplay "view:linkent(F)"
5518 | 121 -> (* y *)
5519 state.glinks <- true;
5520 let mode = state.mode in
5521 state.mode <- Textentry (
5523 ":", "", None, linknentry, linkndone (fun under ->
5524 selstring (undertext under);
5525 ), false
5527 fun _ ->
5528 state.glinks <- false;
5529 state.mode <- mode
5531 state.text <- "";
5532 G.postRedisplay "view:linkent"
5534 | 97 -> (* a *)
5535 begin match state.autoscroll with
5536 | Some step ->
5537 conf.autoscrollstep <- step;
5538 state.autoscroll <- None
5539 | None ->
5540 if conf.autoscrollstep = 0
5541 then state.autoscroll <- Some 1
5542 else state.autoscroll <- Some conf.autoscrollstep
5545 | 112 when ctrl -> (* ctrl-p *)
5546 launchpath ()
5548 | 80 -> (* P *)
5549 setpresentationmode (not conf.presentation);
5550 showtext ' ' ("presentation mode " ^
5551 if conf.presentation then "on" else "off");
5553 | 102 -> (* f *)
5554 if List.mem Wsi.Fullscreen state.winstate
5555 then Wsi.reshape conf.cwinw conf.cwinh
5556 else Wsi.fullscreen ()
5558 | 112 | 78 -> (* p|N *)
5559 search state.searchpattern false
5561 | 110 | 0xffc0 -> (* n|F3 *)
5562 search state.searchpattern true
5564 | 116 -> (* t *)
5565 begin match state.layout with
5566 | [] -> ()
5567 | l :: _ ->
5568 gotoghyll (getpagey l.pageno)
5571 | 32 -> (* space *)
5572 nextpage ()
5574 | 0xff9f | 0xffff -> (* delete *)
5575 prevpage ()
5577 | 61 -> (* = *)
5578 showtext ' ' (describe_location ());
5580 | 119 -> (* w *)
5581 begin match state.layout with
5582 | [] -> ()
5583 | l :: _ ->
5584 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5585 G.postRedisplay "w"
5588 | 39 -> (* ' *)
5589 enterbookmarkmode ()
5591 | 104 | 0xffbe -> (* h|F1 *)
5592 enterhelpmode ()
5594 | 105 -> (* i *)
5595 enterinfomode ()
5597 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5598 entermsgsmode ()
5600 | 109 -> (* m *)
5601 let ondone s =
5602 match state.layout with
5603 | l :: _ ->
5604 if nonemptystr s
5605 then
5606 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5607 | _ -> ()
5609 enttext ("bookmark: ", "", None, textentry, ondone, true)
5611 | 126 -> (* ~ *)
5612 quickbookmark ();
5613 showtext ' ' "Quick bookmark added";
5615 | 122 -> (* z *)
5616 begin match state.layout with
5617 | l :: _ ->
5618 let rect = getpdimrect l.pagedimno in
5619 let w, h =
5620 if conf.crophack
5621 then
5622 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5623 truncate (1.2 *. (rect.(3) -. rect.(0))))
5624 else
5625 (truncate (rect.(1) -. rect.(0)),
5626 truncate (rect.(3) -. rect.(0)))
5628 let w = truncate ((float w)*.conf.zoom)
5629 and h = truncate ((float h)*.conf.zoom) in
5630 if w != 0 && h != 0
5631 then (
5632 state.anchor <- getanchor ();
5633 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5635 G.postRedisplay "z";
5637 | [] -> ()
5640 | 120 -> state.roam () (* x *)
5641 | 60 | 62 -> (* < > *)
5642 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5644 | 91 | 93 -> (* [ ] *)
5645 conf.colorscale <-
5646 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5648 G.postRedisplay "brightness";
5650 | 99 when state.mode = View -> (* [alt-]c *)
5651 if Wsi.withalt mask
5652 then (
5653 if conf.zoom > 1.0
5654 then
5655 let m = (wadjsb state.winw - state.w) / 2 in
5656 state.x <- m;
5657 gotoy_and_clear_text state.y
5659 else
5660 let (c, a, b), z =
5661 match state.prevcolumns with
5662 | None -> (1, 0, 0), 1.0
5663 | Some (columns, z) ->
5664 let cab =
5665 match columns with
5666 | Csplit (c, _) -> -c, 0, 0
5667 | Cmulti ((c, a, b), _) -> c, a, b
5668 | Csingle _ -> 1, 0, 0
5670 cab, z
5672 setcolumns View c a b;
5673 setzoom z
5675 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
5676 -> (* ctrl-shift- (kp) [up|down] *)
5677 let zoom, x = state.prevzoom in
5678 setzoom zoom;
5679 state.x <- x;
5681 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5682 begin match state.autoscroll with
5683 | None ->
5684 begin match state.mode with
5685 | Birdseye beye -> upbirdseye 1 beye
5686 | _ ->
5687 if ctrl
5688 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5689 else (
5690 if not (Wsi.withshift mask) && conf.presentation
5691 then prevpage ()
5692 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5695 | Some n ->
5696 setautoscrollspeed n false
5699 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5700 begin match state.autoscroll with
5701 | None ->
5702 begin match state.mode with
5703 | Birdseye beye -> downbirdseye 1 beye
5704 | _ ->
5705 if ctrl
5706 then gotoy_and_clear_text (clamp (state.winh/2))
5707 else (
5708 if not (Wsi.withshift mask) && conf.presentation
5709 then nextpage ()
5710 else gotoy_and_clear_text (clamp conf.scrollstep)
5713 | Some n ->
5714 setautoscrollspeed n true
5717 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5718 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5719 if canpan ()
5720 then
5721 let dx =
5722 if ctrl
5723 then state.winw / 2
5724 else conf.hscrollstep
5726 let dx = if key = 0xff51 || key = 0xff96 then dx else -dx in
5727 state.x <- panbound (state.x + dx);
5728 gotoy_and_clear_text state.y
5729 else (
5730 state.text <- "";
5731 G.postRedisplay "left/right"
5734 | 0xff55 | 0xff9a -> (* (kp) prior *)
5735 let y =
5736 if ctrl
5737 then
5738 match state.layout with
5739 | [] -> state.y
5740 | l :: _ -> state.y - l.pagey
5741 else
5742 clamp (pgscale (-state.winh))
5744 gotoghyll y
5746 | 0xff56 | 0xff9b -> (* (kp) next *)
5747 let y =
5748 if ctrl
5749 then
5750 match List.rev state.layout with
5751 | [] -> state.y
5752 | l :: _ -> getpagey l.pageno
5753 else
5754 clamp (pgscale state.winh)
5756 gotoghyll y
5758 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5759 gotoghyll 0
5760 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5761 gotoghyll (clamp state.maxy)
5763 | 0xff53 | 0xff98
5764 when Wsi.withalt mask -> (* alt-(kp) right *)
5765 gotoghyll (getnav 1)
5766 | 0xff51 | 0xff96
5767 when Wsi.withalt mask -> (* alt-(kp) left *)
5768 gotoghyll (getnav ~-1)
5770 | 114 -> (* r *)
5771 reload ()
5773 | 118 when conf.debug -> (* v *)
5774 state.rects <- [];
5775 List.iter (fun l ->
5776 match getopaque l.pageno with
5777 | None -> ()
5778 | Some opaque ->
5779 let x0, y0, x1, y1 = pagebbox opaque in
5780 let a,b = float x0, float y0 in
5781 let c,d = float x1, float y0 in
5782 let e,f = float x1, float y1 in
5783 let h,j = float x0, float y1 in
5784 let rect = (a,b,c,d,e,f,h,j) in
5785 debugrect rect;
5786 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5787 ) state.layout;
5788 G.postRedisplay "v";
5790 | _ ->
5791 vlog "huh? %s" (Wsi.keyname key)
5794 let linknavkeyboard key mask linknav =
5795 let getpage pageno =
5796 let rec loop = function
5797 | [] -> None
5798 | l :: _ when l.pageno = pageno -> Some l
5799 | _ :: rest -> loop rest
5800 in loop state.layout
5802 let doexact (pageno, n) =
5803 match getopaque pageno, getpage pageno with
5804 | Some opaque, Some l ->
5805 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5806 then
5807 let under = getlink opaque n in
5808 G.postRedisplay "link gotounder";
5809 gotounder under;
5810 state.mode <- View;
5811 else
5812 let opt, dir =
5813 match key with
5814 | 0xff50 -> (* home *)
5815 Some (findlink opaque LDfirst), -1
5817 | 0xff57 -> (* end *)
5818 Some (findlink opaque LDlast), 1
5820 | 0xff51 -> (* left *)
5821 Some (findlink opaque (LDleft n)), -1
5823 | 0xff53 -> (* right *)
5824 Some (findlink opaque (LDright n)), 1
5826 | 0xff52 -> (* up *)
5827 Some (findlink opaque (LDup n)), -1
5829 | 0xff54 -> (* down *)
5830 Some (findlink opaque (LDdown n)), 1
5832 | _ -> None, 0
5834 let pwl l dir =
5835 begin match findpwl l.pageno dir with
5836 | Pwlnotfound -> ()
5837 | Pwl pageno ->
5838 let notfound dir =
5839 state.mode <- LinkNav (Ltgendir dir);
5840 let y, h = getpageyh pageno in
5841 let y =
5842 if dir < 0
5843 then y + h - state.winh
5844 else y
5846 gotoy y
5848 begin match getopaque pageno, getpage pageno with
5849 | Some opaque, Some _ ->
5850 let link =
5851 let ld = if dir > 0 then LDfirst else LDlast in
5852 findlink opaque ld
5854 begin match link with
5855 | Lfound m ->
5856 showlinktype (getlink opaque m);
5857 state.mode <- LinkNav (Ltexact (pageno, m));
5858 G.postRedisplay "linknav jpage";
5859 | _ -> notfound dir
5860 end;
5861 | _ -> notfound dir
5862 end;
5863 end;
5865 begin match opt with
5866 | Some Lnotfound -> pwl l dir;
5867 | Some (Lfound m) ->
5868 if m = n
5869 then pwl l dir
5870 else (
5871 let _, y0, _, y1 = getlinkrect opaque m in
5872 if y0 < l.pagey
5873 then gotopage1 l.pageno y0
5874 else (
5875 let d = fstate.fontsize + 1 in
5876 if y1 - l.pagey > l.pagevh - d
5877 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5878 else G.postRedisplay "linknav";
5880 showlinktype (getlink opaque m);
5881 state.mode <- LinkNav (Ltexact (l.pageno, m));
5884 | None -> viewkeyboard key mask
5885 end;
5886 | _ -> viewkeyboard key mask
5888 if key = 0xff63
5889 then (
5890 state.mode <- View;
5891 G.postRedisplay "leave linknav"
5893 else
5894 match linknav with
5895 | Ltgendir _ -> viewkeyboard key mask
5896 | Ltexact exact -> doexact exact
5899 let keyboard key mask =
5900 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5901 then wcmd "interrupt"
5902 else state.uioh <- state.uioh#key key mask
5905 let birdseyekeyboard key mask
5906 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5907 let incr =
5908 match conf.columns with
5909 | Csingle _ -> 1
5910 | Cmulti ((c, _, _), _) -> c
5911 | Csplit _ -> failwith "bird's eye split mode"
5913 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5914 match key with
5915 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5916 let y, h = getpageyh pageno in
5917 let top = (state.winh - h) / 2 in
5918 gotoy (max 0 (y - top))
5919 | 0xff0d (* enter *)
5920 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5921 | 0xff1b -> leavebirdseye beye true (* escape *)
5922 | 0xff52 -> upbirdseye incr beye (* up *)
5923 | 0xff54 -> downbirdseye incr beye (* down *)
5924 | 0xff51 -> upbirdseye 1 beye (* left *)
5925 | 0xff53 -> downbirdseye 1 beye (* right *)
5927 | 0xff55 -> (* prior *)
5928 begin match state.layout with
5929 | l :: _ ->
5930 if l.pagey != 0
5931 then (
5932 state.mode <- Birdseye (
5933 oconf, leftx, l.pageno, hooverpageno, anchor
5935 gotopage1 l.pageno 0;
5937 else (
5938 let layout = layout (state.y-state.winh) (pgh state.layout) in
5939 match layout with
5940 | [] -> gotoy (clamp (-state.winh))
5941 | l :: _ ->
5942 state.mode <- Birdseye (
5943 oconf, leftx, l.pageno, hooverpageno, anchor
5945 gotopage1 l.pageno 0
5948 | [] -> gotoy (clamp (-state.winh))
5949 end;
5951 | 0xff56 -> (* next *)
5952 begin match List.rev state.layout with
5953 | l :: _ ->
5954 let layout = layout (state.y + (pgh state.layout)) state.winh in
5955 begin match layout with
5956 | [] ->
5957 let incr = l.pageh - l.pagevh in
5958 if incr = 0
5959 then (
5960 state.mode <-
5961 Birdseye (
5962 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5964 G.postRedisplay "birdseye pagedown";
5966 else gotoy (clamp (incr + conf.interpagespace*2));
5968 | l :: _ ->
5969 state.mode <-
5970 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5971 gotopage1 l.pageno 0;
5974 | [] -> gotoy (clamp state.winh)
5975 end;
5977 | 0xff50 -> (* home *)
5978 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5979 gotopage1 0 0
5981 | 0xff57 -> (* end *)
5982 let pageno = state.pagecount - 1 in
5983 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5984 if not (pagevisible state.layout pageno)
5985 then
5986 let h =
5987 match List.rev state.pdims with
5988 | [] -> state.winh
5989 | (_, _, h, _) :: _ -> h
5991 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5992 else G.postRedisplay "birdseye end";
5993 | _ -> viewkeyboard key mask
5996 let drawpage l =
5997 let color =
5998 match state.mode with
5999 | Textentry _ -> scalecolor 0.4
6000 | LinkNav _
6001 | View -> scalecolor 1.0
6002 | Birdseye (_, _, pageno, hooverpageno, _) ->
6003 if l.pageno = hooverpageno
6004 then scalecolor 0.9
6005 else (
6006 if l.pageno = pageno
6007 then scalecolor 1.0
6008 else scalecolor 0.8
6011 drawtiles l color;
6014 let postdrawpage l linkindexbase =
6015 match getopaque l.pageno with
6016 | Some opaque ->
6017 if tileready l l.pagex l.pagey
6018 then
6019 let x = l.pagedispx - l.pagex
6020 and y = l.pagedispy - l.pagey in
6021 let hlmask =
6022 match conf.columns with
6023 | Csingle _ | Cmulti _ ->
6024 (if conf.hlinks then 1 else 0)
6025 + (if state.glinks
6026 && not (isbirdseye state.mode) then 2 else 0)
6027 | _ -> 0
6029 let s =
6030 match state.mode with
6031 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
6032 | _ -> ""
6034 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
6035 else 0
6036 | _ -> 0
6039 let scrollindicator () =
6040 let sbw, ph, sh = state.uioh#scrollph in
6041 let sbh, pw, sw = state.uioh#scrollpw in
6043 GlDraw.color (0.64, 0.64, 0.64);
6044 filledrect
6045 (float (state.winw - sbw)) 0.
6046 (float state.winw) (float state.winh)
6048 filledrect
6049 0. (float (state.winh - sbh))
6050 (float (wadjsb state.winw - 1)) (float state.winh)
6052 GlDraw.color (0.0, 0.0, 0.0);
6054 filledrect
6055 (float (state.winw - sbw)) ph
6056 (float state.winw) (ph +. sh)
6058 filledrect
6059 pw (float (state.winh - sbh))
6060 (pw +. sw) (float state.winh)
6064 let showsel () =
6065 match state.mstate with
6066 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
6069 | Msel ((x0, y0), (x1, y1)) ->
6070 let rec loop = function
6071 | l :: ls ->
6072 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
6073 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
6074 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
6075 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
6076 then
6077 match getopaque l.pageno with
6078 | Some opaque ->
6079 let x0, y0 = pagetranslatepoint l x0 y0 in
6080 let x1, y1 = pagetranslatepoint l x1 y1 in
6081 seltext opaque (x0, y0, x1, y1);
6082 | _ -> ()
6083 else loop ls
6084 | [] -> ()
6086 loop state.layout
6089 let showrects = function [] -> () | rects ->
6090 Gl.enable `blend;
6091 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
6092 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6093 List.iter
6094 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
6095 List.iter (fun l ->
6096 if l.pageno = pageno
6097 then (
6098 let dx = float (l.pagedispx - l.pagex) in
6099 let dy = float (l.pagedispy - l.pagey) in
6100 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
6101 Raw.sets_float state.vraw ~pos:0
6102 [| x0+.dx; y0+.dy;
6103 x1+.dx; y1+.dy;
6104 x3+.dx; y3+.dy;
6105 x2+.dx; y2+.dy |];
6106 GlArray.vertex `two state.vraw;
6107 GlArray.draw_arrays `triangle_strip 0 4;
6109 ) state.layout
6110 ) rects
6112 Gl.disable `blend;
6115 let display () =
6116 GlClear.color (scalecolor2 conf.bgcolor);
6117 GlClear.clear [`color];
6118 List.iter drawpage state.layout;
6119 let rects =
6120 match state.mode with
6121 | LinkNav (Ltexact (pageno, linkno)) ->
6122 begin match getopaque pageno with
6123 | Some opaque ->
6124 let x0, y0, x1, y1 = getlinkrect opaque linkno in
6125 (pageno, 5, (
6126 float x0, float y0,
6127 float x1, float y0,
6128 float x1, float y1,
6129 float x0, float y1)
6130 ) :: state.rects
6131 | None -> state.rects
6133 | _ -> state.rects
6135 showrects rects;
6136 let rec postloop linkindexbase = function
6137 | l :: rest ->
6138 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
6139 postloop linkindexbase rest
6140 | [] -> ()
6142 showsel ();
6143 postloop 0 state.layout;
6144 state.uioh#display;
6145 begin match state.mstate with
6146 | Mzoomrect ((x0, y0), (x1, y1)) ->
6147 Gl.enable `blend;
6148 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
6149 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6150 filledrect (float x0) (float y0) (float x1) (float y1);
6151 Gl.disable `blend;
6152 | _ -> ()
6153 end;
6154 enttext ();
6155 scrollindicator ();
6156 Wsi.swapb ();
6159 let zoomrect x y x1 y1 =
6160 let x0 = min x x1
6161 and x1 = max x x1
6162 and y0 = min y y1 in
6163 gotoy (state.y + y0);
6164 state.anchor <- getanchor ();
6165 let zoom = (float state.w) /. float (x1 - x0) in
6166 let margin =
6167 match conf.fitmodel, conf.columns with
6168 | FitPage, Csplit _ ->
6169 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
6171 | _, _ ->
6172 let adjw = wadjsb state.winw in
6173 if state.w < adjw
6174 then (adjw - state.w) / 2
6175 else 0
6177 state.x <- (state.x + margin) - x0;
6178 setzoom zoom;
6179 Wsi.setcursor Wsi.CURSOR_INHERIT;
6180 state.mstate <- Mnone;
6183 let zoomblock x y =
6184 let g opaque l px py =
6185 match rectofblock opaque px py with
6186 | Some a ->
6187 let x0 = a.(0) -. 20. in
6188 let x1 = a.(1) +. 20. in
6189 let y0 = a.(2) -. 20. in
6190 let zoom = (float state.w) /. (x1 -. x0) in
6191 let pagey = getpagey l.pageno in
6192 gotoy_and_clear_text (pagey + truncate y0);
6193 state.anchor <- getanchor ();
6194 let margin = (state.w - l.pagew)/2 in
6195 state.x <- -truncate x0 - margin;
6196 setzoom zoom;
6197 None
6198 | None -> None
6200 match conf.columns with
6201 | Csplit _ ->
6202 showtext '!' "block zooming does not work properly in split columns mode"
6203 | _ -> onppundermouse g x y ()
6206 let scrollx x =
6207 let winw = wadjsb state.winw - 1 in
6208 let s = float x /. float winw in
6209 let destx = truncate (float (state.w + winw) *. s) in
6210 state.x <- winw - destx;
6211 gotoy_and_clear_text state.y;
6212 state.mstate <- Mscrollx;
6215 let scrolly y =
6216 let s = float y /. float state.winh in
6217 let desty = truncate (float (state.maxy - state.winh) *. s) in
6218 gotoy_and_clear_text desty;
6219 state.mstate <- Mscrolly;
6222 let viewmouse button down x y mask =
6223 match button with
6224 | n when (n == 4 || n == 5) && not down ->
6225 if Wsi.withctrl mask
6226 then (
6227 match state.mstate with
6228 | Mzoom (oldn, i) ->
6229 if oldn = n
6230 then (
6231 if i = 2
6232 then
6233 let incr =
6234 match n with
6235 | 5 ->
6236 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6237 | _ ->
6238 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6240 let zoom = conf.zoom -. incr in
6241 setzoom zoom;
6242 state.mstate <- Mzoom (n, 0);
6243 else
6244 state.mstate <- Mzoom (n, i+1);
6246 else state.mstate <- Mzoom (n, 0)
6248 | _ -> state.mstate <- Mzoom (n, 0)
6250 else (
6251 match state.autoscroll with
6252 | Some step -> setautoscrollspeed step (n=4)
6253 | None ->
6254 if conf.wheelbypage || conf.presentation
6255 then (
6256 if n = 4
6257 then prevpage ()
6258 else nextpage ()
6260 else
6261 let incr =
6262 if n = 4
6263 then -conf.scrollstep
6264 else conf.scrollstep
6266 let incr = incr * 2 in
6267 let y = clamp incr in
6268 gotoy_and_clear_text y
6271 | n when (n = 6 || n = 7) && not down && canpan () ->
6272 state.x <-
6273 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6274 gotoy_and_clear_text state.y
6276 | 1 when Wsi.withshift mask ->
6277 state.mstate <- Mnone;
6278 if not down
6279 then (
6280 match unproject x y with
6281 | Some (pageno, ux, uy) ->
6282 let cmd = Printf.sprintf
6283 "%s %s %d %d %d"
6284 conf.stcmd state.path pageno ux uy
6286 popen cmd []
6287 | None -> ()
6290 | 1 when Wsi.withctrl mask ->
6291 if down
6292 then (
6293 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6294 state.mstate <- Mpan (x, y)
6296 else
6297 state.mstate <- Mnone
6299 | 3 ->
6300 if down
6301 then (
6302 Wsi.setcursor Wsi.CURSOR_CYCLE;
6303 let p = (x, y) in
6304 state.mstate <- Mzoomrect (p, p)
6306 else (
6307 match state.mstate with
6308 | Mzoomrect ((x0, y0), _) ->
6309 if abs (x-x0) > 10 && abs (y - y0) > 10
6310 then zoomrect x0 y0 x y
6311 else (
6312 state.mstate <- Mnone;
6313 Wsi.setcursor Wsi.CURSOR_INHERIT;
6314 G.postRedisplay "kill accidental zoom rect";
6316 | _ ->
6317 Wsi.setcursor Wsi.CURSOR_INHERIT;
6318 state.mstate <- Mnone
6321 | 1 when x > state.winw - vscrollw () ->
6322 if down
6323 then
6324 let _, position, sh = state.uioh#scrollph in
6325 if y > truncate position && y < truncate (position +. sh)
6326 then state.mstate <- Mscrolly
6327 else scrolly y
6328 else
6329 state.mstate <- Mnone
6331 | 1 when y > state.winh - hscrollh () ->
6332 if down
6333 then
6334 let _, position, sw = state.uioh#scrollpw in
6335 if x > truncate position && x < truncate (position +. sw)
6336 then state.mstate <- Mscrollx
6337 else scrollx x
6338 else
6339 state.mstate <- Mnone
6341 | 1 when state.bzoom -> if not down then zoomblock x y
6343 | 1 ->
6344 let dest = if down then getunder x y else Unone in
6345 begin match dest with
6346 | Ulinkgoto _
6347 | Ulinkuri _
6348 | Uremote _ | Uremotedest _
6349 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6350 gotounder dest
6352 | Unone when down ->
6353 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6354 state.mstate <- Mpan (x, y);
6356 | Unone | Utext _ ->
6357 if down
6358 then (
6359 if conf.angle mod 360 = 0
6360 then (
6361 state.mstate <- Msel ((x, y), (x, y));
6362 G.postRedisplay "mouse select";
6365 else (
6366 match state.mstate with
6367 | Mnone -> ()
6369 | Mzoom _ | Mscrollx | Mscrolly ->
6370 state.mstate <- Mnone
6372 | Mzoomrect ((x0, y0), _) ->
6373 zoomrect x0 y0 x y
6375 | Mpan _ ->
6376 Wsi.setcursor Wsi.CURSOR_INHERIT;
6377 state.mstate <- Mnone
6379 | Msel ((x0, y0), (x1, y1)) ->
6380 let rec loop = function
6381 | [] -> ()
6382 | l :: rest ->
6383 let inside =
6384 let a0 = l.pagedispy in
6385 let a1 = a0 + l.pagevh in
6386 let b0 = l.pagedispx in
6387 let b1 = b0 + l.pagevw in
6388 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6389 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6391 if inside
6392 then
6393 match getopaque l.pageno with
6394 | Some opaque ->
6395 begin
6396 match Ne.pipe () with
6397 | Ne.Exn exn ->
6398 showtext '!'
6399 (Printf.sprintf
6400 "can not create sel pipe: %s"
6401 (exntos exn));
6402 | Ne.Res (r, w) ->
6403 let doclose what fd =
6404 Ne.clo fd (fun msg ->
6405 dolog "%s close failed: %s" what msg)
6408 popen conf.selcmd [r, 0; w, -1];
6409 copysel w opaque true;
6410 doclose "pipe/r" r;
6411 G.postRedisplay "copysel";
6412 with exn ->
6413 dolog "can not execute %S: %s"
6414 conf.selcmd (exntos exn);
6415 doclose "pipe/r" r;
6416 doclose "pipe/w" w;
6418 | None -> ()
6419 else loop rest
6421 loop state.layout;
6422 Wsi.setcursor Wsi.CURSOR_INHERIT;
6423 state.mstate <- Mnone;
6427 | _ -> ()
6430 let birdseyemouse button down x y mask
6431 (conf, leftx, _, hooverpageno, anchor) =
6432 match button with
6433 | 1 when down ->
6434 let rec loop = function
6435 | [] -> ()
6436 | l :: rest ->
6437 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6438 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6439 then (
6440 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6442 else loop rest
6444 loop state.layout
6445 | 3 -> ()
6446 | _ -> viewmouse button down x y mask
6449 let uioh = object
6450 method display = ()
6452 method key key mask =
6453 begin match state.mode with
6454 | Textentry textentry -> textentrykeyboard key mask textentry
6455 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6456 | View -> viewkeyboard key mask
6457 | LinkNav linknav -> linknavkeyboard key mask linknav
6458 end;
6459 state.uioh
6461 method button button bstate x y mask =
6462 begin match state.mode with
6463 | LinkNav _
6464 | View -> viewmouse button bstate x y mask
6465 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6466 | Textentry _ -> ()
6467 end;
6468 state.uioh
6470 method motion x y =
6471 begin match state.mode with
6472 | Textentry _ -> ()
6473 | View | Birdseye _ | LinkNav _ ->
6474 match state.mstate with
6475 | Mzoom _ | Mnone -> ()
6477 | Mpan (x0, y0) ->
6478 let dx = x - x0
6479 and dy = y0 - y in
6480 state.mstate <- Mpan (x, y);
6481 if canpan ()
6482 then state.x <- panbound (state.x + dx);
6483 let y = clamp dy in
6484 gotoy_and_clear_text y
6486 | Msel (a, _) ->
6487 state.mstate <- Msel (a, (x, y));
6488 G.postRedisplay "motion select";
6490 | Mscrolly ->
6491 let y = min state.winh (max 0 y) in
6492 scrolly y
6494 | Mscrollx ->
6495 let x = min state.winw (max 0 x) in
6496 scrollx x
6498 | Mzoomrect (p0, _) ->
6499 state.mstate <- Mzoomrect (p0, (x, y));
6500 G.postRedisplay "motion zoomrect";
6501 end;
6502 state.uioh
6504 method pmotion x y =
6505 begin match state.mode with
6506 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6507 let rec loop = function
6508 | [] ->
6509 if hooverpageno != -1
6510 then (
6511 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6512 G.postRedisplay "pmotion birdseye no hoover";
6514 | l :: rest ->
6515 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6516 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6517 then (
6518 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6519 G.postRedisplay "pmotion birdseye hoover";
6521 else loop rest
6523 loop state.layout
6525 | Textentry _ -> ()
6527 | LinkNav _
6528 | View ->
6529 match state.mstate with
6530 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6532 | Mnone ->
6533 updateunder x y;
6534 match conf.pax with
6535 | None -> ()
6536 | Some r ->
6537 let past, _, _ = !r in
6538 let now = now () in
6539 let delta = now -. past in
6540 if delta > 0.01
6541 then paxunder x y
6542 else r := (now, x, y)
6543 end;
6544 state.uioh
6546 method infochanged _ = ()
6548 method scrollph =
6549 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6550 let p, h =
6551 if maxy = 0
6552 then 0.0, float state.winh
6553 else scrollph state.y maxy
6555 vscrollw (), p, h
6557 method scrollpw =
6558 let winw = wadjsb state.winw in
6559 let fwinw = float winw in
6560 let sw =
6561 let sw = fwinw /. float state.w in
6562 let sw = fwinw *. sw in
6563 max sw (float conf.scrollh)
6565 let position =
6566 let maxx = state.w + winw in
6567 let x = winw - state.x in
6568 let percent = float x /. float maxx in
6569 (fwinw -. sw) *. percent
6571 hscrollh (), position, sw
6573 method modehash =
6574 let modename =
6575 match state.mode with
6576 | LinkNav _ -> "links"
6577 | Textentry _ -> "textentry"
6578 | Birdseye _ -> "birdseye"
6579 | View -> "view"
6581 findkeyhash conf modename
6583 method eformsgs = true
6584 end;;
6586 module Config =
6587 struct
6588 open Parser
6590 let fontpath = ref "";;
6592 module KeyMap =
6593 Map.Make (struct type t = (int * int) let compare = compare end);;
6595 let unent s =
6596 let l = String.length s in
6597 let b = Buffer.create l in
6598 unent b s 0 l;
6599 Buffer.contents b;
6602 let home =
6603 try Sys.getenv "HOME"
6604 with exn ->
6605 prerr_endline
6606 ("Can not determine home directory location: " ^ exntos exn);
6610 let modifier_of_string = function
6611 | "alt" -> Wsi.altmask
6612 | "shift" -> Wsi.shiftmask
6613 | "ctrl" | "control" -> Wsi.ctrlmask
6614 | "meta" -> Wsi.metamask
6615 | _ -> 0
6618 let key_of_string =
6619 let r = Str.regexp "-" in
6620 fun s ->
6621 let elems = Str.full_split r s in
6622 let f n k m =
6623 let g s =
6624 let m1 = modifier_of_string s in
6625 if m1 = 0
6626 then (Wsi.namekey s, m)
6627 else (k, m lor m1)
6628 in function
6629 | Str.Delim s when n land 1 = 0 -> g s
6630 | Str.Text s -> g s
6631 | Str.Delim _ -> (k, m)
6633 let rec loop n k m = function
6634 | [] -> (k, m)
6635 | x :: xs ->
6636 let k, m = f n k m x in
6637 loop (n+1) k m xs
6639 loop 0 0 0 elems
6642 let keys_of_string =
6643 let r = Str.regexp "[ \t]" in
6644 fun s ->
6645 let elems = Str.split r s in
6646 List.map key_of_string elems
6649 let copykeyhashes c =
6650 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6653 let config_of c attrs =
6654 let apply c k v =
6656 match k with
6657 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6658 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6659 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6660 | "preload" -> { c with preload = bool_of_string v }
6661 | "page-bias" -> { c with pagebias = int_of_string v }
6662 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6663 | "horizontal-scroll-step" ->
6664 { c with hscrollstep = max (int_of_string v) 1 }
6665 | "auto-scroll-step" ->
6666 { c with autoscrollstep = max 0 (int_of_string v) }
6667 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6668 | "crop-hack" -> { c with crophack = bool_of_string v }
6669 | "throttle" ->
6670 let mw =
6671 match String.lowercase v with
6672 | "true" -> Some infinity
6673 | "false" -> None
6674 | f -> Some (float_of_string f)
6676 { c with maxwait = mw}
6677 | "highlight-links" -> { c with hlinks = bool_of_string v }
6678 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6679 | "vertical-margin" ->
6680 { c with interpagespace = max 0 (int_of_string v) }
6681 | "zoom" ->
6682 let zoom = float_of_string v /. 100. in
6683 let zoom = max zoom 0.0 in
6684 { c with zoom = zoom }
6685 | "presentation" -> { c with presentation = bool_of_string v }
6686 | "rotation-angle" -> { c with angle = int_of_string v }
6687 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6688 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6689 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6690 | "proportional-display" ->
6691 let fm =
6692 if bool_of_string v
6693 then FitProportional
6694 else FitWidth
6696 { c with fitmodel = fm }
6697 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6698 | "pixmap-cache-size" ->
6699 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6700 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6701 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6702 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6703 | "persistent-location" -> { c with jumpback = bool_of_string v }
6704 | "background-color" -> { c with bgcolor = color_of_string v }
6705 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6706 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6707 | "mupdf-store-size" ->
6708 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6709 | "checkers" -> { c with checkers = bool_of_string v }
6710 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6711 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6712 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6713 | "uri-launcher" -> { c with urilauncher = unent v }
6714 | "path-launcher" -> { c with pathlauncher = unent v }
6715 | "color-space" -> { c with colorspace = CSTE.of_string v }
6716 | "invert-colors" -> { c with invert = bool_of_string v }
6717 | "brightness" -> { c with colorscale = float_of_string v }
6718 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6719 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
6720 | "columns" ->
6721 let (n, _, _) as nab = multicolumns_of_string v in
6722 if n < 0
6723 then { c with columns = Csplit (-n, [||]) }
6724 else { c with columns = Cmulti (nab, [||]) }
6725 | "birds-eye-columns" ->
6726 { c with beyecolumns = Some (max (int_of_string v) 2) }
6727 | "selection-command" -> { c with selcmd = unent v }
6728 | "synctex-command" -> { c with stcmd = unent v }
6729 | "pax-command" -> { c with paxcmd = unent v }
6730 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6731 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6732 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6733 | "use-pbo" -> { c with usepbo = bool_of_string v }
6734 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6735 | "horizontal-scrollbar-visible" ->
6736 let b =
6737 if bool_of_string v
6738 then c.scrollb lor scrollbhv
6739 else c.scrollb land (lnot scrollbhv)
6741 { c with scrollb = b }
6742 | "vertical-scrollbar-visible" ->
6743 let b =
6744 if bool_of_string v
6745 then c.scrollb lor scrollbvv
6746 else c.scrollb land (lnot scrollbvv)
6748 { c with scrollb = b }
6749 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6750 | "point-and-x" ->
6751 { c with pax =
6752 if bool_of_string v
6753 then Some (ref (0.0, 0, 0))
6754 else None }
6755 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
6756 | _ -> c
6757 with exn ->
6758 prerr_endline ("Error processing attribute (`" ^
6759 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6762 let rec fold c = function
6763 | [] -> c
6764 | (k, v) :: rest ->
6765 let c = apply c k v in
6766 fold c rest
6768 fold { c with keyhashes = copykeyhashes c } attrs;
6771 let fromstring f pos n v d =
6772 try f v
6773 with exn ->
6774 dolog "Error processing attribute (%S=%S) at %d\n%s"
6775 n v pos (exntos exn)
6780 let bookmark_of attrs =
6781 let rec fold title page rely visy = function
6782 | ("title", v) :: rest -> fold v page rely visy rest
6783 | ("page", v) :: rest -> fold title v rely visy rest
6784 | ("rely", v) :: rest -> fold title page v visy rest
6785 | ("visy", v) :: rest -> fold title page rely v rest
6786 | _ :: rest -> fold title page rely visy rest
6787 | [] -> title, page, rely, visy
6789 fold "invalid" "0" "0" "0" attrs
6792 let doc_of attrs =
6793 let rec fold path page rely pan visy = function
6794 | ("path", v) :: rest -> fold v page rely pan visy rest
6795 | ("page", v) :: rest -> fold path v rely pan visy rest
6796 | ("rely", v) :: rest -> fold path page v pan visy rest
6797 | ("pan", v) :: rest -> fold path page rely v visy rest
6798 | ("visy", v) :: rest -> fold path page rely pan v rest
6799 | _ :: rest -> fold path page rely pan visy rest
6800 | [] -> path, page, rely, pan, visy
6802 fold "" "0" "0" "0" "0" attrs
6805 let map_of attrs =
6806 let rec fold rs ls = function
6807 | ("out", v) :: rest -> fold v ls rest
6808 | ("in", v) :: rest -> fold rs v rest
6809 | _ :: rest -> fold ls rs rest
6810 | [] -> ls, rs
6812 fold "" "" attrs
6815 let setconf dst src =
6816 dst.scrollbw <- src.scrollbw;
6817 dst.scrollh <- src.scrollh;
6818 dst.icase <- src.icase;
6819 dst.preload <- src.preload;
6820 dst.pagebias <- src.pagebias;
6821 dst.verbose <- src.verbose;
6822 dst.scrollstep <- src.scrollstep;
6823 dst.maxhfit <- src.maxhfit;
6824 dst.crophack <- src.crophack;
6825 dst.autoscrollstep <- src.autoscrollstep;
6826 dst.maxwait <- src.maxwait;
6827 dst.hlinks <- src.hlinks;
6828 dst.underinfo <- src.underinfo;
6829 dst.interpagespace <- src.interpagespace;
6830 dst.zoom <- src.zoom;
6831 dst.presentation <- src.presentation;
6832 dst.angle <- src.angle;
6833 dst.cwinw <- src.cwinw;
6834 dst.cwinh <- src.cwinh;
6835 dst.savebmarks <- src.savebmarks;
6836 dst.memlimit <- src.memlimit;
6837 dst.fitmodel <- src.fitmodel;
6838 dst.texcount <- src.texcount;
6839 dst.sliceheight <- src.sliceheight;
6840 dst.thumbw <- src.thumbw;
6841 dst.jumpback <- src.jumpback;
6842 dst.bgcolor <- src.bgcolor;
6843 dst.tilew <- src.tilew;
6844 dst.tileh <- src.tileh;
6845 dst.mustoresize <- src.mustoresize;
6846 dst.checkers <- src.checkers;
6847 dst.aalevel <- src.aalevel;
6848 dst.trimmargins <- src.trimmargins;
6849 dst.trimfuzz <- src.trimfuzz;
6850 dst.urilauncher <- src.urilauncher;
6851 dst.colorspace <- src.colorspace;
6852 dst.invert <- src.invert;
6853 dst.colorscale <- src.colorscale;
6854 dst.redirectstderr <- src.redirectstderr;
6855 dst.ghyllscroll <- src.ghyllscroll;
6856 dst.columns <- src.columns;
6857 dst.beyecolumns <- src.beyecolumns;
6858 dst.selcmd <- src.selcmd;
6859 dst.updatecurs <- src.updatecurs;
6860 dst.pathlauncher <- src.pathlauncher;
6861 dst.keyhashes <- copykeyhashes src;
6862 dst.hfsize <- src.hfsize;
6863 dst.hscrollstep <- src.hscrollstep;
6864 dst.pgscale <- src.pgscale;
6865 dst.usepbo <- src.usepbo;
6866 dst.wheelbypage <- src.wheelbypage;
6867 dst.stcmd <- src.stcmd;
6868 dst.paxcmd <- src.paxcmd;
6869 dst.scrollb <- src.scrollb;
6870 dst.riani <- src.riani;
6871 dst.paxmark <- src.paxmark;
6872 dst.pax <-
6873 if src.pax = None
6874 then None
6875 else Some ((ref (0.0, 0, 0)));
6878 let get s =
6879 let h = Hashtbl.create 10 in
6880 let dc = { defconf with angle = defconf.angle } in
6881 let rec toplevel v t spos _ =
6882 match t with
6883 | Vdata | Vcdata | Vend -> v
6884 | Vopen ("llppconfig", _, closed) ->
6885 if closed
6886 then v
6887 else { v with f = llppconfig }
6888 | Vopen _ ->
6889 error "unexpected subelement at top level" s spos
6890 | Vclose _ -> error "unexpected close at top level" s spos
6892 and llppconfig v t spos _ =
6893 match t with
6894 | Vdata | Vcdata -> v
6895 | Vend -> error "unexpected end of input in llppconfig" s spos
6896 | Vopen ("defaults", attrs, closed) ->
6897 let c = config_of dc attrs in
6898 setconf dc c;
6899 if closed
6900 then v
6901 else { v with f = defaults }
6903 | Vopen ("ui-font", attrs, closed) ->
6904 let rec getsize size = function
6905 | [] -> size
6906 | ("size", v) :: rest ->
6907 let size =
6908 fromstring int_of_string spos "size" v fstate.fontsize in
6909 getsize size rest
6910 | l -> getsize size l
6912 fstate.fontsize <- getsize fstate.fontsize attrs;
6913 if closed
6914 then v
6915 else { v with f = uifont (Buffer.create 10) }
6917 | Vopen ("doc", attrs, closed) ->
6918 let pathent, spage, srely, span, svisy = doc_of attrs in
6919 let path = unent pathent
6920 and pageno = fromstring int_of_string spos "page" spage 0
6921 and rely = fromstring float_of_string spos "rely" srely 0.0
6922 and pan = fromstring int_of_string spos "pan" span 0
6923 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6924 let c = config_of dc attrs in
6925 let anchor = (pageno, rely, visy) in
6926 if closed
6927 then (Hashtbl.add h path (c, [], pan, anchor); v)
6928 else { v with f = doc path pan anchor c [] }
6930 | Vopen _ ->
6931 error "unexpected subelement in llppconfig" s spos
6933 | Vclose "llppconfig" -> { v with f = toplevel }
6934 | Vclose _ -> error "unexpected close in llppconfig" s spos
6936 and defaults v t spos _ =
6937 match t with
6938 | Vdata | Vcdata -> v
6939 | Vend -> error "unexpected end of input in defaults" s spos
6940 | Vopen ("keymap", attrs, closed) ->
6941 let modename =
6942 try List.assoc "mode" attrs
6943 with Not_found -> "global" in
6944 if closed
6945 then v
6946 else
6947 let ret keymap =
6948 let h = findkeyhash dc modename in
6949 KeyMap.iter (Hashtbl.replace h) keymap;
6950 defaults
6952 { v with f = pkeymap ret KeyMap.empty }
6954 | Vopen (_, _, _) ->
6955 error "unexpected subelement in defaults" s spos
6957 | Vclose "defaults" ->
6958 { v with f = llppconfig }
6960 | Vclose _ -> error "unexpected close in defaults" s spos
6962 and uifont b v t spos epos =
6963 match t with
6964 | Vdata | Vcdata ->
6965 Buffer.add_substring b s spos (epos - spos);
6967 | Vopen (_, _, _) ->
6968 error "unexpected subelement in ui-font" s spos
6969 | Vclose "ui-font" ->
6970 if emptystr !fontpath
6971 then fontpath := Buffer.contents b;
6972 { v with f = llppconfig }
6973 | Vclose _ -> error "unexpected close in ui-font" s spos
6974 | Vend -> error "unexpected end of input in ui-font" s spos
6976 and doc path pan anchor c bookmarks v t spos _ =
6977 match t with
6978 | Vdata | Vcdata -> v
6979 | Vend -> error "unexpected end of input in doc" s spos
6980 | Vopen ("bookmarks", _, closed) ->
6981 if closed
6982 then v
6983 else { v with f = pbookmarks path pan anchor c bookmarks }
6985 | Vopen ("keymap", attrs, closed) ->
6986 let modename =
6987 try List.assoc "mode" attrs
6988 with Not_found -> "global"
6990 if closed
6991 then v
6992 else
6993 let ret keymap =
6994 let h = findkeyhash c modename in
6995 KeyMap.iter (Hashtbl.replace h) keymap;
6996 doc path pan anchor c bookmarks
6998 { v with f = pkeymap ret KeyMap.empty }
7000 | Vopen (_, _, _) ->
7001 error "unexpected subelement in doc" s spos
7003 | Vclose "doc" ->
7004 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
7005 { v with f = llppconfig }
7007 | Vclose _ -> error "unexpected close in doc" s spos
7009 and pkeymap ret keymap v t spos _ =
7010 match t with
7011 | Vdata | Vcdata -> v
7012 | Vend -> error "unexpected end of input in keymap" s spos
7013 | Vopen ("map", attrs, closed) ->
7014 let r, l = map_of attrs in
7015 let kss = fromstring keys_of_string spos "in" r [] in
7016 let lss = fromstring keys_of_string spos "out" l [] in
7017 let keymap =
7018 match kss with
7019 | [] -> keymap
7020 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
7021 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
7023 if closed
7024 then { v with f = pkeymap ret keymap }
7025 else
7026 let f () = v in
7027 { v with f = skip "map" f }
7029 | Vopen _ ->
7030 error "unexpected subelement in keymap" s spos
7032 | Vclose "keymap" ->
7033 { v with f = ret keymap }
7035 | Vclose _ -> error "unexpected close in keymap" s spos
7037 and pbookmarks path pan anchor c bookmarks v t spos _ =
7038 match t with
7039 | Vdata | Vcdata -> v
7040 | Vend -> error "unexpected end of input in bookmarks" s spos
7041 | Vopen ("item", attrs, closed) ->
7042 let titleent, spage, srely, svisy = bookmark_of attrs in
7043 let page = fromstring int_of_string spos "page" spage 0
7044 and rely = fromstring float_of_string spos "rely" srely 0.0
7045 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7046 let bookmarks =
7047 (unent titleent, 0, (page, rely, visy)) :: bookmarks
7049 if closed
7050 then { v with f = pbookmarks path pan anchor c bookmarks }
7051 else
7052 let f () = v in
7053 { v with f = skip "item" f }
7055 | Vopen _ ->
7056 error "unexpected subelement in bookmarks" s spos
7058 | Vclose "bookmarks" ->
7059 { v with f = doc path pan anchor c bookmarks }
7061 | Vclose _ -> error "unexpected close in bookmarks" s spos
7063 and skip tag f v t spos _ =
7064 match t with
7065 | Vdata | Vcdata -> v
7066 | Vend ->
7067 error ("unexpected end of input in skipped " ^ tag) s spos
7068 | Vopen (tag', _, closed) ->
7069 if closed
7070 then v
7071 else
7072 let f' () = { v with f = skip tag f } in
7073 { v with f = skip tag' f' }
7074 | Vclose ctag ->
7075 if tag = ctag
7076 then f ()
7077 else error ("unexpected close in skipped " ^ tag) s spos
7080 parse { f = toplevel; accu = () } s;
7081 h, dc;
7084 let do_load f ic =
7086 let len = in_channel_length ic in
7087 let s = String.create len in
7088 really_input ic s 0 len;
7089 f s;
7090 with
7091 | Parse_error (msg, s, pos) ->
7092 let subs = subs s pos in
7093 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
7095 | exn ->
7096 failwith ("config load error: " ^ exntos exn)
7099 let defconfpath =
7100 let dir =
7102 let dir = Filename.concat home ".config" in
7103 if Sys.is_directory dir then dir else home
7104 with _ -> home
7106 Filename.concat dir "llpp.conf"
7109 let confpath = ref defconfpath;;
7111 let load1 f =
7112 if Sys.file_exists !confpath
7113 then
7114 match
7115 (try Some (open_in_bin !confpath)
7116 with exn ->
7117 prerr_endline
7118 ("Error opening configuration file `" ^ !confpath ^ "': " ^
7119 exntos exn);
7120 None
7122 with
7123 | Some ic ->
7124 let success =
7126 f (do_load get ic)
7127 with exn ->
7128 prerr_endline
7129 ("Error loading configuration from `" ^ !confpath ^ "': " ^
7130 exntos exn);
7131 false
7133 close_in ic;
7134 success
7136 | None -> false
7137 else
7138 f (Hashtbl.create 0, defconf)
7141 let load () =
7142 let f (h, dc) =
7143 let pc, pb, px, pa =
7145 let key =
7146 if emptystr state.origin
7147 then state.path
7148 else state.origin
7150 Hashtbl.find h (Filename.basename key)
7151 with Not_found -> dc, [], 0, emptyanchor
7153 setconf defconf dc;
7154 setconf conf pc;
7155 state.bookmarks <- pb;
7156 state.x <- px;
7157 if conf.jumpback
7158 then state.anchor <- pa;
7159 cbput state.hists.nav pa;
7160 true
7162 load1 f
7165 let add_attrs bb always dc c =
7166 let ob s a b =
7167 if always || a != b
7168 then Printf.bprintf bb "\n %s='%b'" s a
7169 and op s a b =
7170 if always || a <> b
7171 then Printf.bprintf bb "\n %s='%b'" s (a != None)
7172 and oi s a b =
7173 if always || a != b
7174 then Printf.bprintf bb "\n %s='%d'" s a
7175 and oI s a b =
7176 if always || a != b
7177 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
7178 and oz s a b =
7179 if always || a <> b
7180 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
7181 and oF s a b =
7182 if always || a <> b
7183 then Printf.bprintf bb "\n %s='%f'" s a
7184 and oc s a b =
7185 if always || a <> b
7186 then
7187 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
7188 and oC s a b =
7189 if always || a <> b
7190 then
7191 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
7192 and oR s a b =
7193 if always || a <> b
7194 then
7195 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
7196 and os s a b =
7197 if always || a <> b
7198 then
7199 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7200 and og s a b =
7201 if always || a <> b
7202 then
7203 match a with
7204 | Some (_N, _A, _B) ->
7205 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7206 | None ->
7207 match b with
7208 | None -> ()
7209 | _ ->
7210 Printf.bprintf bb "\n %s='none'" s
7211 and oW s a b =
7212 if always || a <> b
7213 then
7214 let v =
7215 match a with
7216 | None -> "false"
7217 | Some f ->
7218 if f = infinity
7219 then "true"
7220 else string_of_float f
7222 Printf.bprintf bb "\n %s='%s'" s v
7223 and oco s a b =
7224 if always || a <> b
7225 then
7226 match a with
7227 | Cmulti ((n, a, b), _) when n > 1 ->
7228 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7229 | Csplit (n, _) when n > 1 ->
7230 Printf.bprintf bb "\n %s='%d'" s ~-n
7231 | _ -> ()
7232 and obeco s a b =
7233 if always || a <> b
7234 then
7235 match a with
7236 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7237 | _ -> ()
7238 and oFm s a b =
7239 if always || a <> b
7240 then
7241 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7242 and oSv s a b m =
7243 if always || a <> b
7244 then
7245 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7246 and oPm s a b =
7247 if always || a <> b
7248 then
7249 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
7251 oi "width" c.cwinw dc.cwinw;
7252 oi "height" c.cwinh dc.cwinh;
7253 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7254 oi "scroll-handle-height" c.scrollh dc.scrollh;
7255 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7256 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7257 ob "case-insensitive-search" c.icase dc.icase;
7258 ob "preload" c.preload dc.preload;
7259 oi "page-bias" c.pagebias dc.pagebias;
7260 oi "scroll-step" c.scrollstep dc.scrollstep;
7261 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7262 ob "max-height-fit" c.maxhfit dc.maxhfit;
7263 ob "crop-hack" c.crophack dc.crophack;
7264 oW "throttle" c.maxwait dc.maxwait;
7265 ob "highlight-links" c.hlinks dc.hlinks;
7266 ob "under-cursor-info" c.underinfo dc.underinfo;
7267 oi "vertical-margin" c.interpagespace dc.interpagespace;
7268 oz "zoom" c.zoom dc.zoom;
7269 ob "presentation" c.presentation dc.presentation;
7270 oi "rotation-angle" c.angle dc.angle;
7271 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7272 oFm "fit-model" c.fitmodel dc.fitmodel;
7273 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7274 oi "tex-count" c.texcount dc.texcount;
7275 oi "slice-height" c.sliceheight dc.sliceheight;
7276 oi "thumbnail-width" c.thumbw dc.thumbw;
7277 ob "persistent-location" c.jumpback dc.jumpback;
7278 oc "background-color" c.bgcolor dc.bgcolor;
7279 oi "tile-width" c.tilew dc.tilew;
7280 oi "tile-height" c.tileh dc.tileh;
7281 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7282 ob "checkers" c.checkers dc.checkers;
7283 oi "aalevel" c.aalevel dc.aalevel;
7284 ob "trim-margins" c.trimmargins dc.trimmargins;
7285 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7286 os "uri-launcher" c.urilauncher dc.urilauncher;
7287 os "path-launcher" c.pathlauncher dc.pathlauncher;
7288 oC "color-space" c.colorspace dc.colorspace;
7289 ob "invert-colors" c.invert dc.invert;
7290 oF "brightness" c.colorscale dc.colorscale;
7291 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7292 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7293 oco "columns" c.columns dc.columns;
7294 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7295 os "selection-command" c.selcmd dc.selcmd;
7296 os "synctex-command" c.stcmd dc.stcmd;
7297 os "pax-command" c.paxcmd dc.paxcmd;
7298 ob "update-cursor" c.updatecurs dc.updatecurs;
7299 oi "hint-font-size" c.hfsize dc.hfsize;
7300 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7301 oF "page-scroll-scale" c.pgscale dc.pgscale;
7302 ob "use-pbo" c.usepbo dc.usepbo;
7303 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7304 ob "remote-in-a-new-instance" c.riani dc.riani;
7305 op "point-and-x" c.pax dc.pax;
7306 oPm "point-and-x-mark" c.paxmark dc.paxmark;
7309 let keymapsbuf always dc c =
7310 let bb = Buffer.create 16 in
7311 let rec loop = function
7312 | [] -> ()
7313 | (modename, h) :: rest ->
7314 let dh = findkeyhash dc modename in
7315 if always || h <> dh
7316 then (
7317 if Hashtbl.length h > 0
7318 then (
7319 if Buffer.length bb > 0
7320 then Buffer.add_char bb '\n';
7321 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7322 Hashtbl.iter (fun i o ->
7323 let isdifferent = always ||
7325 let dO = Hashtbl.find dh i in
7326 dO <> o
7327 with Not_found -> true
7329 if isdifferent
7330 then
7331 let addkm (k, m) =
7332 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7333 if Wsi.withalt m then Buffer.add_string bb "alt-";
7334 if Wsi.withshift m then Buffer.add_string bb "shift-";
7335 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7336 Buffer.add_string bb (Wsi.keyname k);
7338 let addkms l =
7339 let rec loop = function
7340 | [] -> ()
7341 | km :: [] -> addkm km
7342 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7344 loop l
7346 Buffer.add_string bb "<map in='";
7347 addkm i;
7348 match o with
7349 | KMinsrt km ->
7350 Buffer.add_string bb "' out='";
7351 addkm km;
7352 Buffer.add_string bb "'/>\n"
7354 | KMinsrl kms ->
7355 Buffer.add_string bb "' out='";
7356 addkms kms;
7357 Buffer.add_string bb "'/>\n"
7359 | KMmulti (ins, kms) ->
7360 Buffer.add_char bb ' ';
7361 addkms ins;
7362 Buffer.add_string bb "' out='";
7363 addkms kms;
7364 Buffer.add_string bb "'/>\n"
7365 ) h;
7366 Buffer.add_string bb "</keymap>";
7369 loop rest
7371 loop c.keyhashes;
7375 let save () =
7376 let uifontsize = fstate.fontsize in
7377 let bb = Buffer.create 32768 in
7378 let relx = float state.x /. float state.winw in
7379 let w, h, x =
7380 let cx w = truncate (relx *. float w) in
7381 List.fold_left
7382 (fun (w, h, x) ws ->
7383 match ws with
7384 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7385 | Wsi.MaxVert -> (w, conf.cwinh, x)
7386 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7388 (state.winw, state.winh, state.x) state.winstate
7390 conf.cwinw <- w;
7391 conf.cwinh <- h;
7392 let f (h, dc) =
7393 let dc = if conf.bedefault then conf else dc in
7394 Buffer.add_string bb "<llppconfig>\n";
7396 if nonemptystr !fontpath
7397 then
7398 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7399 uifontsize
7400 !fontpath
7401 else (
7402 if uifontsize <> 14
7403 then
7404 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7407 Buffer.add_string bb "<defaults";
7408 add_attrs bb true dc dc;
7409 let kb = keymapsbuf true dc dc in
7410 if Buffer.length kb > 0
7411 then (
7412 Buffer.add_string bb ">\n";
7413 Buffer.add_buffer bb kb;
7414 Buffer.add_string bb "\n</defaults>\n";
7416 else Buffer.add_string bb "/>\n";
7418 let adddoc path pan anchor c bookmarks =
7419 if bookmarks == [] && c = dc && anchor = emptyanchor
7420 then ()
7421 else (
7422 Printf.bprintf bb "<doc path='%s'"
7423 (enent path 0 (String.length path));
7425 if anchor <> emptyanchor
7426 then (
7427 let n, rely, visy = anchor in
7428 Printf.bprintf bb " page='%d'" n;
7429 if rely > 1e-6
7430 then
7431 Printf.bprintf bb " rely='%f'" rely
7433 if abs_float visy > 1e-6
7434 then
7435 Printf.bprintf bb " visy='%f'" visy
7439 if pan != 0
7440 then Printf.bprintf bb " pan='%d'" pan;
7442 add_attrs bb false dc c;
7443 let kb = keymapsbuf false dc c in
7445 begin match bookmarks with
7446 | [] ->
7447 if Buffer.length kb > 0
7448 then (
7449 Buffer.add_string bb ">\n";
7450 Buffer.add_buffer bb kb;
7451 Buffer.add_string bb "\n</doc>\n";
7453 else Buffer.add_string bb "/>\n"
7454 | _ ->
7455 Buffer.add_string bb ">\n<bookmarks>\n";
7456 List.iter (fun (title, _level, (page, rely, visy)) ->
7457 Printf.bprintf bb
7458 "<item title='%s' page='%d'"
7459 (enent title 0 (String.length title))
7460 page
7462 if rely > 1e-6
7463 then
7464 Printf.bprintf bb " rely='%f'" rely
7466 if abs_float visy > 1e-6
7467 then
7468 Printf.bprintf bb " visy='%f'" visy
7470 Buffer.add_string bb "/>\n";
7471 ) bookmarks;
7472 Buffer.add_string bb "</bookmarks>";
7473 if Buffer.length kb > 0
7474 then (
7475 Buffer.add_string bb "\n";
7476 Buffer.add_buffer bb kb;
7478 Buffer.add_string bb "\n</doc>\n";
7479 end;
7483 let pan, conf =
7484 match state.mode with
7485 | Birdseye (c, pan, _, _, _) ->
7486 let beyecolumns =
7487 match conf.columns with
7488 | Cmulti ((c, _, _), _) -> Some c
7489 | Csingle _ -> None
7490 | Csplit _ -> None
7491 and columns =
7492 match c.columns with
7493 | Cmulti (c, _) -> Cmulti (c, [||])
7494 | Csingle _ -> Csingle [||]
7495 | Csplit _ -> failwith "quit from bird's eye while split"
7497 pan, { c with beyecolumns = beyecolumns; columns = columns }
7498 | _ -> x, conf
7500 let basename = Filename.basename
7501 (if emptystr state.origin then state.path else state.origin)
7503 adddoc basename pan (getanchor ())
7504 (let conf =
7505 let autoscrollstep =
7506 match state.autoscroll with
7507 | Some step -> step
7508 | None -> conf.autoscrollstep
7510 match state.mode with
7511 | Birdseye (bc, _, _, _, _) ->
7512 { conf with
7513 zoom = bc.zoom;
7514 presentation = bc.presentation;
7515 interpagespace = bc.interpagespace;
7516 maxwait = bc.maxwait;
7517 autoscrollstep = autoscrollstep }
7518 | _ -> { conf with autoscrollstep = autoscrollstep }
7519 in conf)
7520 (if conf.savebmarks then state.bookmarks else []);
7522 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7523 if basename <> path
7524 then adddoc path x anchor c bookmarks
7525 ) h;
7526 Buffer.add_string bb "</llppconfig>\n";
7527 true;
7529 if load1 f && Buffer.length bb > 0
7530 then
7532 let tmp = !confpath ^ ".tmp" in
7533 let oc = open_out_bin tmp in
7534 Buffer.output_buffer oc bb;
7535 close_out oc;
7536 Unix.rename tmp !confpath;
7537 with exn ->
7538 prerr_endline
7539 ("error while saving configuration: " ^ exntos exn)
7541 end;;
7543 let adderrmsg src msg =
7544 Buffer.add_string state.errmsgs msg;
7545 state.newerrmsgs <- true;
7546 G.postRedisplay src
7549 let adderrfmt src fmt =
7550 Format.kprintf (fun s -> adderrmsg src s) fmt;
7553 let ract cmds =
7554 let cl = splitatspace cmds in
7555 let scan s fmt f =
7556 try Scanf.sscanf s fmt f
7557 with exn ->
7558 adderrfmt "remote exec"
7559 "error processing '%S': %s\n" cmds (exntos exn)
7561 match cl with
7562 | "reload" :: [] -> reload ()
7563 | "goto" :: args :: [] ->
7564 scan args "%u %f %f"
7565 (fun pageno x y ->
7566 let cmd, _ = state.geomcmds in
7567 if emptystr cmd
7568 then gotopagexy pageno x y
7569 else
7570 let f prevf () =
7571 gotopagexy pageno x y;
7572 prevf ()
7574 state.reprf <- f state.reprf
7576 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7577 | "rect" :: args :: [] ->
7578 scan args "%u %u %f %f %f %f"
7579 (fun pageno color x0 y0 x1 y1 ->
7580 onpagerect pageno (fun w h ->
7581 let _,w1,h1,_ = getpagedim pageno in
7582 let sw = float w1 /. float w
7583 and sh = float h1 /. float h in
7584 let x0s = x0 *. sw
7585 and x1s = x1 *. sw
7586 and y0s = y0 *. sh
7587 and y1s = y1 *. sh in
7588 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7589 debugrect rect;
7590 state.rects <- (pageno, color, rect) :: state.rects;
7591 G.postRedisplay "rect";
7594 | "activatewin" :: [] -> Wsi.activatewin ()
7595 | "quit" :: [] -> raise Quit
7596 | _ ->
7597 adderrfmt "remote command"
7598 "error processing remote command: %S\n" cmds;
7601 let remote =
7602 let scratch = String.create 80 in
7603 let buf = Buffer.create 80 in
7604 fun fd ->
7605 let rec tempfr () =
7606 try Some (Unix.read fd scratch 0 80)
7607 with
7608 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7609 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7610 | exn -> raise exn
7612 match tempfr () with
7613 | None -> Some fd
7614 | Some n ->
7615 if n = 0
7616 then (
7617 Unix.close fd;
7618 if Buffer.length buf > 0
7619 then (
7620 let s = Buffer.contents buf in
7621 Buffer.clear buf;
7622 ract s;
7624 None
7626 else
7627 let rec eat ppos =
7628 let nlpos =
7630 let pos = String.index_from scratch ppos '\n' in
7631 if pos >= n then -1 else pos
7632 with Not_found -> -1
7634 if nlpos >= 0
7635 then (
7636 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7637 let s = Buffer.contents buf in
7638 Buffer.clear buf;
7639 ract s;
7640 eat (nlpos+1);
7642 else (
7643 Buffer.add_substring buf scratch ppos (n-ppos);
7644 Some fd
7646 in eat 0
7649 let remoteopen path =
7650 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7651 with exn ->
7652 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7653 None
7656 let () =
7657 let trimcachepath = ref "" in
7658 let rcmdpath = ref "" in
7659 selfexec := Sys.executable_name;
7660 Arg.parse
7661 (Arg.align
7662 [("-p", Arg.String (fun s -> state.password <- s),
7663 "<password> Set password");
7665 ("-f", Arg.String
7666 (fun s ->
7667 Config.fontpath := s;
7668 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7670 "<path> Set path to the user interface font");
7672 ("-c", Arg.String
7673 (fun s ->
7674 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7675 Config.confpath := s),
7676 "<path> Set path to the configuration file");
7678 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7679 "<path> Set path to the trim cache file");
7681 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7682 "<named-destination> Set named destination");
7684 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7685 ("-cxack", Arg.Set cxack, " Cut corners");
7687 ("-remote", Arg.String (fun s -> rcmdpath := s),
7688 "<path> Set path to the remote commands source");
7690 ("-origin", Arg.String (fun s -> state.origin <- s),
7691 "<original-path> Set original path");
7693 ("-v", Arg.Unit (fun () ->
7694 Printf.printf
7695 "%s\nconfiguration path: %s\n"
7696 (version ())
7697 Config.defconfpath
7699 exit 0), " Print version and exit");
7702 (fun s -> state.path <- s)
7703 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7705 if !wtmode
7706 then selfexec := !selfexec ^ " -wtmode";
7708 if emptystr state.path
7709 then (prerr_endline "file name missing"; exit 1);
7711 if not (Config.load ())
7712 then prerr_endline "failed to load configuration";
7714 let wsfd, winw, winh = Wsi.init (object
7715 val mutable m_hack = false
7716 method expose = if not m_hack then G.postRedisplay "expose"
7717 method visible = G.postRedisplay "visible"
7718 method display = m_hack <- false; display ()
7719 method reshape w h =
7720 m_hack <- w < state.winw && h < state.winh;
7721 reshape w h
7722 method mouse b d x y m = state.uioh <- state.uioh#button b d x y m
7723 method motion x y =
7724 state.mpos <- (x, y);
7725 state.uioh <- state.uioh#motion x y
7726 method pmotion x y =
7727 state.mpos <- (x, y);
7728 state.uioh <- state.uioh#pmotion x y
7729 method key k m =
7730 let mascm = m land (
7731 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7732 ) in
7733 match state.keystate with
7734 | KSnone ->
7735 let km = k, mascm in
7736 begin
7737 match
7738 let modehash = state.uioh#modehash in
7739 try Hashtbl.find modehash km
7740 with Not_found ->
7741 try Hashtbl.find (findkeyhash conf "global") km
7742 with Not_found -> KMinsrt (k, m)
7743 with
7744 | KMinsrt (k, m) -> keyboard k m
7745 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7746 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7748 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7749 List.iter (fun (k, m) -> keyboard k m) insrt;
7750 state.keystate <- KSnone
7751 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7752 state.keystate <- KSinto (keys, insrt)
7753 | _ ->
7754 state.keystate <- KSnone
7756 method enter x y =
7757 state.mpos <- (x, y);
7758 state.uioh <- state.uioh#pmotion x y
7759 method leave = state.mpos <- (-1, -1)
7760 method winstate wsl = state.winstate <- wsl; m_hack <- false
7761 method quit = raise Quit
7762 end) conf.cwinw conf.cwinh (platform = Posx) in
7764 state.wsfd <- wsfd;
7766 if not (
7767 List.exists GlMisc.check_extension
7768 [ "GL_ARB_texture_rectangle"
7769 ; "GL_EXT_texture_recangle"
7770 ; "GL_NV_texture_rectangle" ]
7772 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7774 if (
7775 let r = GlMisc.get_string `renderer in
7776 let p = "Mesa DRI Intel(" in
7777 let l = String.length p in
7778 String.length r > l && String.sub r 0 l = p
7780 then (
7781 defconf.sliceheight <- 1024;
7782 defconf.texcount <- 32;
7783 defconf.usepbo <- true;
7786 let cr, sw =
7787 match Ne.pipe () with
7788 | Ne.Exn exn ->
7789 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7790 exit 1
7791 | Ne.Res rw -> rw
7792 and sr, cw =
7793 match Ne.pipe () with
7794 | Ne.Exn exn ->
7795 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7796 exit 1
7797 | Ne.Res rw -> rw
7800 cloexec cr;
7801 cloexec sw;
7802 cloexec sr;
7803 cloexec cw;
7805 setcheckers conf.checkers;
7806 redirectstderr ();
7807 if conf.redirectstderr
7808 then
7809 at_exit (fun () ->
7810 let s = Buffer.contents state.errmsgs ^
7811 (match state.errfd with
7812 | Some fd ->
7813 let s = String.create (80*24) in
7814 let n =
7816 let r, _, _ = Unix.select [fd] [] [] 0.0 in
7817 if List.mem fd r
7818 then Unix.read fd s 0 (String.length s)
7819 else 0
7820 with _ -> 0
7822 if n = 0
7823 then ""
7824 else String.sub s 0 n
7825 | None -> ""
7828 try ignore (Unix.write state.stderr s 0 (String.length s))
7829 with exn -> print_endline (exntos exn)
7833 init (cr, cw) (
7834 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7835 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7836 !Config.fontpath, !trimcachepath,
7837 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7839 List.iter GlArray.enable [`texture_coord; `vertex];
7840 state.sr <- sr;
7841 state.sw <- sw;
7842 state.text <- "Opening " ^ (mbtoutf8 state.path);
7843 reshape winw winh;
7844 opendoc state.path state.password;
7845 state.uioh <- uioh;
7846 display ();
7847 Wsi.mapwin ();
7848 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7849 let optrfd =
7850 ref (
7851 if nonemptystr !rcmdpath
7852 then remoteopen !rcmdpath
7853 else None
7857 let rec loop deadline =
7858 let r =
7859 match state.errfd with
7860 | None -> [state.sr; state.wsfd]
7861 | Some fd -> [state.sr; state.wsfd; fd]
7863 let r =
7864 match !optrfd with
7865 | None -> r
7866 | Some fd -> fd :: r
7868 if state.redisplay
7869 then (
7870 state.redisplay <- false;
7871 display ();
7873 let timeout =
7874 let now = now () in
7875 if deadline > now
7876 then (
7877 if deadline = infinity
7878 then ~-.1.0
7879 else max 0.0 (deadline -. now)
7881 else 0.0
7883 let r, _, _ =
7884 try Unix.select r [] [] timeout
7885 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7887 begin match r with
7888 | [] ->
7889 state.ghyll None;
7890 let newdeadline =
7891 if state.ghyll == noghyll
7892 then
7893 match state.autoscroll with
7894 | Some step when step != 0 ->
7895 let y = state.y + step in
7896 let y =
7897 if y < 0
7898 then state.maxy
7899 else if y >= state.maxy then 0 else y
7901 gotoy y;
7902 if state.mode = View
7903 then state.text <- "";
7904 deadline +. 0.01
7905 | _ -> infinity
7906 else deadline +. 0.01
7908 loop newdeadline
7910 | l ->
7911 let rec checkfds = function
7912 | [] -> ()
7913 | fd :: rest when fd = state.sr ->
7914 let cmd = readcmd state.sr in
7915 act cmd;
7916 checkfds rest
7918 | fd :: rest when fd = state.wsfd ->
7919 Wsi.readresp fd;
7920 checkfds rest
7922 | fd :: rest when Some fd = !optrfd ->
7923 begin match remote fd with
7924 | None -> optrfd := remoteopen !rcmdpath;
7925 | opt -> optrfd := opt
7926 end;
7927 checkfds rest
7929 | fd :: rest ->
7930 let s = String.create 80 in
7931 let n = tempfailureretry (Unix.read fd s 0) 80 in
7932 if conf.redirectstderr
7933 then (
7934 Buffer.add_substring state.errmsgs s 0 n;
7935 state.newerrmsgs <- true;
7936 state.redisplay <- true;
7938 else (
7939 prerr_string (String.sub s 0 n);
7940 flush stderr;
7942 checkfds rest
7944 checkfds l;
7945 let newdeadline =
7946 let deadline1 =
7947 if deadline = infinity
7948 then now () +. 0.01
7949 else deadline
7951 match state.autoscroll with
7952 | Some step when step != 0 -> deadline1
7953 | _ -> if state.ghyll == noghyll then infinity else deadline1
7955 loop newdeadline
7956 end;
7959 loop infinity;
7960 with Quit ->
7961 Config.save ();