Neater backspace behavior during text entry
[llpp.git] / main.ml
blob752110b3a7435aea62c297354b55e126be6bbc27
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";;
128 let platform_to_string = function
129 | Punknown -> "unknown"
130 | Plinux -> "Linux"
131 | Posx -> "OSX"
132 | Psun -> "Sun"
133 | Pfreebsd -> "FreeBSD"
134 | Pdragonflybsd -> "DragonflyBSD"
135 | Popenbsd -> "OpenBSD"
136 | Pnetbsd -> "NetBSD"
137 | Pcygwin -> "Cygwin"
140 let platform = platform ();;
142 let now = Unix.gettimeofday;;
144 let selfexec = ref "";;
146 let popen cmd fda =
147 if platform = Pcygwin
148 then (
149 let sh = "/bin/sh" in
150 let args = [|sh; "-c"; cmd|] in
151 let rec std si so se = function
152 | [] -> si, so, se
153 | (fd, 0) :: rest -> std fd so se rest
154 | (fd, -1) :: rest ->
155 Unix.set_close_on_exec fd;
156 std si so se rest
157 | (_, n) :: _ ->
158 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
160 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
161 ignore (Unix.create_process sh args si so se)
163 else popen cmd fda;
166 type mpos = int * int
167 and mstate =
168 | Msel of (mpos * mpos)
169 | Mpan of mpos
170 | Mscrolly | Mscrollx
171 | Mzoom of (int * int)
172 | Mzoomrect of (mpos * mpos)
173 | Mnone
176 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
177 and onkey = string -> int -> te
178 and ondone = string -> unit
179 and histcancel = unit -> unit
180 and onhist = ((histcmd -> string) * histcancel)
181 and histcmd = HCnext | HCprev | HCfirst | HClast
182 and cancelonempty = bool
183 and te =
184 | TEstop
185 | TEdone of string
186 | TEcont of string
187 | TEswitch of textentry
190 type 'a circbuf =
191 { store : 'a array
192 ; mutable rc : int
193 ; mutable wc : int
194 ; mutable len : int
198 let bound v minv maxv =
199 max minv (min maxv v);
202 let cbnew n v =
203 { store = Array.create n v
204 ; rc = 0
205 ; wc = 0
206 ; len = 0
210 let cbcap b = Array.length b.store;;
212 let cbput b v =
213 let cap = cbcap b in
214 b.store.(b.wc) <- v;
215 b.wc <- (b.wc + 1) mod cap;
216 b.rc <- b.wc;
217 b.len <- min (b.len + 1) cap;
220 let cbempty b = b.len = 0;;
222 let cbgetg b circular dir =
223 if cbempty b
224 then b.store.(0)
225 else
226 let rc = b.rc + dir in
227 let rc =
228 if circular
229 then (
230 if rc = -1
231 then b.len-1
232 else (
233 if rc >= b.len
234 then 0
235 else rc
238 else bound rc 0 (b.len-1)
240 b.rc <- rc;
241 b.store.(rc);
244 let cbget b = cbgetg b false;;
245 let cbgetc b = cbgetg b true;;
247 let drawstring size x y s =
248 Gl.enable `blend;
249 Gl.enable `texture_2d;
250 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
251 ignore (drawstr size x y s);
252 Gl.disable `blend;
253 Gl.disable `texture_2d;
256 let drawstring1 size x y s =
257 drawstr size x y s;
260 let drawstring2 size x y fmt =
261 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
264 type page =
265 { pageno : int
266 ; pagedimno : int
267 ; pagew : int
268 ; pageh : int
269 ; pagex : int
270 ; pagey : int
271 ; pagevw : int
272 ; pagevh : int
273 ; pagedispx : int
274 ; pagedispy : int
275 ; pagecol : int
279 let debugl l =
280 dolog "l %d dim=%d {" l.pageno l.pagedimno;
281 dolog " WxH %dx%d" l.pagew l.pageh;
282 dolog " vWxH %dx%d" l.pagevw l.pagevh;
283 dolog " pagex,y %d,%d" l.pagex l.pagey;
284 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
285 dolog " column %d" l.pagecol;
286 dolog "}";
289 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
290 dolog "rect {";
291 dolog " x0,y0=(% f, % f)" x0 y0;
292 dolog " x1,y1=(% f, % f)" x1 y1;
293 dolog " x2,y2=(% f, % f)" x2 y2;
294 dolog " x3,y3=(% f, % f)" x3 y3;
295 dolog "}";
298 type multicolumns = multicol * pagegeom
299 and singlecolumn = pagegeom
300 and splitcolumns = columncount * pagegeom
301 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
302 and multicol = columncount * covercount * covercount
303 and pdimno = int
304 and columncount = int
305 and covercount = int;;
307 type scrollb = int;;
308 let scrollbvv = 1;;
309 let scrollbhv = 2;;
311 type conf =
312 { mutable scrollbw : int
313 ; mutable scrollh : int
314 ; mutable scrollb : scrollb
315 ; mutable icase : bool
316 ; mutable preload : bool
317 ; mutable pagebias : int
318 ; mutable verbose : bool
319 ; mutable debug : bool
320 ; mutable scrollstep : int
321 ; mutable hscrollstep : int
322 ; mutable maxhfit : bool
323 ; mutable crophack : bool
324 ; mutable autoscrollstep : int
325 ; mutable maxwait : float option
326 ; mutable hlinks : bool
327 ; mutable underinfo : bool
328 ; mutable interpagespace : interpagespace
329 ; mutable zoom : float
330 ; mutable presentation : bool
331 ; mutable angle : angle
332 ; mutable cwinw : int
333 ; mutable cwinh : int
334 ; mutable savebmarks : bool
335 ; mutable fitmodel : fitmodel
336 ; mutable trimmargins : trimmargins
337 ; mutable trimfuzz : irect
338 ; mutable memlimit : memsize
339 ; mutable texcount : texcount
340 ; mutable sliceheight : sliceheight
341 ; mutable thumbw : width
342 ; mutable jumpback : bool
343 ; mutable bgcolor : (float * float * float)
344 ; mutable bedefault : bool
345 ; mutable tilew : int
346 ; mutable tileh : int
347 ; mutable mustoresize : memsize
348 ; mutable checkers : bool
349 ; mutable aalevel : int
350 ; mutable urilauncher : string
351 ; mutable pathlauncher : string
352 ; mutable colorspace : colorspace
353 ; mutable invert : bool
354 ; mutable colorscale : float
355 ; mutable redirectstderr : bool
356 ; mutable ghyllscroll : (int * int * int) option
357 ; mutable columns : columns
358 ; mutable beyecolumns : columncount option
359 ; mutable selcmd : string
360 ; mutable paxcmd : string
361 ; mutable updatecurs : bool
362 ; mutable keyhashes : (string * keyhash) list
363 ; mutable hfsize : int
364 ; mutable pgscale : float
365 ; mutable usepbo : bool
366 ; mutable wheelbypage : bool
367 ; mutable stcmd : string
368 ; mutable riani : bool
369 ; mutable pax : (float * int * int) ref option
370 ; mutable paxmark : mark
372 and columns =
373 | Csingle of singlecolumn
374 | Cmulti of multicolumns
375 | Csplit of splitcolumns
378 type anchor = pageno * top * dtop;;
380 type outline = string * int * anchor;;
382 type rect = float * float * float * float * float * float * float * float;;
384 type tile = opaque * pixmapsize * elapsed
385 and elapsed = float;;
386 type pagemapkey = pageno * gen;;
387 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
388 and row = int
389 and col = int;;
391 let emptyanchor = (0, 0.0, 0.0);;
393 type infochange = | Memused | Docinfo | Pdim;;
395 class type uioh = object
396 method display : unit
397 method key : int -> int -> uioh
398 method button : int -> bool -> int -> int -> int -> uioh
399 method motion : int -> int -> uioh
400 method pmotion : int -> int -> uioh
401 method infochanged : infochange -> unit
402 method scrollpw : (int * float * float)
403 method scrollph : (int * float * float)
404 method modehash : keyhash
405 method eformsgs : bool
406 end;;
408 type mode =
409 | Birdseye of (conf * leftx * pageno * pageno * anchor)
410 | Textentry of (textentry * onleave)
411 | View
412 | LinkNav of linktarget
413 and onleave = leavetextentrystatus -> unit
414 and leavetextentrystatus = | Cancel | Confirm
415 and helpitem = string * int * action
416 and action =
417 | Noaction
418 | Action of (uioh -> uioh)
419 and linktarget =
420 | Ltexact of (pageno * int)
421 | Ltgendir of int
424 let isbirdseye = function Birdseye _ -> true | _ -> false;;
425 let istextentry = function Textentry _ -> true | _ -> false;;
427 type currently =
428 | Idle
429 | Loading of (page * gen)
430 | Tiling of (
431 page * opaque * colorspace * angle * gen * col * row * width * height
433 | Outlining of outline list
436 let emptykeyhash = Hashtbl.create 0;;
437 let nouioh : uioh = object (self)
438 method display = ()
439 method key _ _ = self
440 method button _ _ _ _ _ = self
441 method motion _ _ = self
442 method pmotion _ _ = self
443 method infochanged _ = ()
444 method scrollpw = (0, nan, nan)
445 method scrollph = (0, nan, nan)
446 method modehash = emptykeyhash
447 method eformsgs = false
448 end;;
450 type state =
451 { mutable sr : Unix.file_descr
452 ; mutable sw : Unix.file_descr
453 ; mutable wsfd : Unix.file_descr
454 ; mutable errfd : Unix.file_descr option
455 ; mutable stderr : Unix.file_descr
456 ; mutable errmsgs : Buffer.t
457 ; mutable newerrmsgs : bool
458 ; mutable w : int
459 ; mutable x : int
460 ; mutable y : int
461 ; mutable anchor : anchor
462 ; mutable ranchors : (string * string * anchor * string) list
463 ; mutable maxy : int
464 ; mutable layout : page list
465 ; pagemap : (pagemapkey, opaque) Hashtbl.t
466 ; tilemap : (tilemapkey, tile) Hashtbl.t
467 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
468 ; mutable pdims : (pageno * width * height * leftx) list
469 ; mutable pagecount : int
470 ; mutable currently : currently
471 ; mutable mstate : mstate
472 ; mutable searchpattern : string
473 ; mutable rects : (pageno * recttype * rect) list
474 ; mutable rects1 : (pageno * recttype * rect) list
475 ; mutable text : string
476 ; mutable winstate : Wsi.winstate list
477 ; mutable mode : mode
478 ; mutable uioh : uioh
479 ; mutable outlines : outline array
480 ; mutable bookmarks : outline list
481 ; mutable path : string
482 ; mutable password : string
483 ; mutable nameddest : string
484 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
485 ; mutable memused : memsize
486 ; mutable gen : gen
487 ; mutable throttle : (page list * int * float) option
488 ; mutable autoscroll : int option
489 ; mutable ghyll : (int option -> unit)
490 ; mutable help : helpitem array
491 ; mutable docinfo : (int * string) list
492 ; mutable texid : GlTex.texture_id option
493 ; hists : hists
494 ; mutable prevzoom : (float * int)
495 ; mutable progress : float
496 ; mutable redisplay : bool
497 ; mutable mpos : mpos
498 ; mutable keystate : keystate
499 ; mutable glinks : bool
500 ; mutable prevcolumns : (columns * float) option
501 ; mutable winw : int
502 ; mutable winh : int
503 ; mutable reprf : (unit -> unit)
504 ; mutable origin : string
505 ; mutable roam : (unit -> unit)
506 ; mutable bzoom : bool
508 and hists =
509 { pat : string circbuf
510 ; pag : string circbuf
511 ; nav : anchor circbuf
512 ; sel : string circbuf
516 let defconf =
517 { scrollbw = 7
518 ; scrollh = 12
519 ; scrollb = scrollbhv lor scrollbvv
520 ; icase = true
521 ; preload = true
522 ; pagebias = 0
523 ; verbose = false
524 ; debug = false
525 ; scrollstep = 24
526 ; hscrollstep = 24
527 ; maxhfit = true
528 ; crophack = false
529 ; autoscrollstep = 2
530 ; maxwait = None
531 ; hlinks = false
532 ; underinfo = false
533 ; interpagespace = 2
534 ; zoom = 1.0
535 ; presentation = false
536 ; angle = 0
537 ; cwinw = 900
538 ; cwinh = 900
539 ; savebmarks = true
540 ; fitmodel = FitProportional
541 ; trimmargins = false
542 ; trimfuzz = (0,0,0,0)
543 ; memlimit = 32 lsl 20
544 ; texcount = 256
545 ; sliceheight = 24
546 ; thumbw = 76
547 ; jumpback = true
548 ; bgcolor = (0.5, 0.5, 0.5)
549 ; bedefault = false
550 ; tilew = 2048
551 ; tileh = 2048
552 ; mustoresize = 256 lsl 20
553 ; checkers = true
554 ; aalevel = 8
555 ; urilauncher =
556 (match platform with
557 | Plinux | Pfreebsd | Pdragonflybsd
558 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
559 | Posx -> "open \"%s\""
560 | Pcygwin -> "cygstart \"%s\""
561 | Punknown -> "echo %s")
562 ; pathlauncher = "lp \"%s\""
563 ; selcmd =
564 (match platform with
565 | Plinux | Pfreebsd | Pdragonflybsd
566 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
567 | Posx -> "pbcopy"
568 | Pcygwin -> "wsel"
569 | Punknown -> "cat")
570 ; paxcmd = "cat"
571 ; colorspace = Rgb
572 ; invert = false
573 ; colorscale = 1.0
574 ; redirectstderr = false
575 ; ghyllscroll = None
576 ; columns = Csingle [||]
577 ; beyecolumns = None
578 ; updatecurs = false
579 ; hfsize = 12
580 ; pgscale = 1.0
581 ; usepbo = false
582 ; wheelbypage = false
583 ; stcmd = "echo SyncTex"
584 ; riani = false
585 ; pax = None
586 ; paxmark = Mark_word
587 ; keyhashes =
588 let mk n = (n, Hashtbl.create 1) in
589 [ mk "global"
590 ; mk "info"
591 ; mk "help"
592 ; mk "outline"
593 ; mk "listview"
594 ; mk "birdseye"
595 ; mk "textentry"
596 ; mk "links"
597 ; mk "view"
602 let wtmode = ref false;;
603 let cxack = ref false;;
605 let findkeyhash c name =
606 try List.assoc name c.keyhashes
607 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
610 let conf = { defconf with angle = defconf.angle };;
612 let pgscale h = truncate (float h *. conf.pgscale);;
614 type fontstate =
615 { mutable fontsize : int
616 ; mutable wwidth : float
617 ; mutable maxrows : int
621 let fstate =
622 { fontsize = 14
623 ; wwidth = nan
624 ; maxrows = -1
628 let geturl s =
629 let colonpos = try String.index s ':' with Not_found -> -1 in
630 let len = String.length s in
631 if colonpos >= 0 && colonpos + 3 < len
632 then (
633 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
634 then
635 let schemestartpos =
636 try String.rindex_from s colonpos ' '
637 with Not_found -> -1
639 let scheme =
640 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
642 match scheme with
643 | "http" | "ftp" | "mailto" ->
644 let epos =
645 try String.index_from s colonpos ' '
646 with Not_found -> len
648 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
649 | _ -> ""
650 else ""
652 else ""
655 let gotouri uri =
656 if emptystr conf.urilauncher
657 then print_endline uri
658 else (
659 let url = geturl uri in
660 if emptystr url
661 then Printf.eprintf "obtained empty url from uri %S" uri
662 else
663 let re = Str.regexp "%s" in
664 let command = Str.global_replace re url conf.urilauncher in
665 try popen command []
666 with exn ->
667 Printf.eprintf
668 "failed to execute `%s': %s\n" command (exntos exn);
669 flush stderr;
673 let version () =
674 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
675 (platform_to_string platform) Sys.word_size Sys.ocaml_version
678 let makehelp () =
679 let strings = version () :: "" :: Help.keys in
680 Array.of_list (
681 List.map (fun s ->
682 let url = geturl s in
683 if nonemptystr url
684 then (s, 0, Action (fun u -> gotouri url; u))
685 else (s, 0, Noaction)
686 ) strings);
689 let noghyll _ = ();;
690 let firstgeomcmds = "", [];;
691 let noreprf () = ();;
693 let state =
694 { sr = Unix.stdin
695 ; sw = Unix.stdin
696 ; wsfd = Unix.stdin
697 ; errfd = None
698 ; stderr = Unix.stderr
699 ; errmsgs = Buffer.create 0
700 ; newerrmsgs = false
701 ; x = 0
702 ; y = 0
703 ; w = 0
704 ; anchor = emptyanchor
705 ; ranchors = []
706 ; layout = []
707 ; maxy = max_int
708 ; tilelru = Queue.create ()
709 ; pagemap = Hashtbl.create 10
710 ; tilemap = Hashtbl.create 10
711 ; pdims = []
712 ; pagecount = 0
713 ; currently = Idle
714 ; mstate = Mnone
715 ; rects = []
716 ; rects1 = []
717 ; text = ""
718 ; mode = View
719 ; winstate = []
720 ; searchpattern = ""
721 ; outlines = [||]
722 ; bookmarks = []
723 ; path = ""
724 ; password = ""
725 ; nameddest = ""
726 ; geomcmds = firstgeomcmds
727 ; hists =
728 { nav = cbnew 10 emptyanchor
729 ; pat = cbnew 10 ""
730 ; pag = cbnew 10 ""
731 ; sel = cbnew 10 ""
733 ; memused = 0
734 ; gen = 0
735 ; throttle = None
736 ; autoscroll = None
737 ; ghyll = noghyll
738 ; help = makehelp ()
739 ; docinfo = []
740 ; texid = None
741 ; prevzoom = (1.0, 0)
742 ; progress = -1.0
743 ; uioh = nouioh
744 ; redisplay = true
745 ; mpos = (-1, -1)
746 ; keystate = KSnone
747 ; glinks = false
748 ; prevcolumns = None
749 ; winw = -1
750 ; winh = -1
751 ; reprf = noreprf
752 ; origin = ""
753 ; roam = (fun () -> ())
754 ; bzoom = false
758 let hscrollh () =
759 if (conf.scrollb land scrollbhv = 0)
760 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
761 then 0
762 else conf.scrollbw
765 let vscrollw () =
766 if (conf.scrollb land scrollbvv = 0)
767 then 0
768 else conf.scrollbw
771 let wadjsb w = w - vscrollw ();;
773 let setfontsize n =
774 fstate.fontsize <- n;
775 fstate.wwidth <- measurestr fstate.fontsize "w";
776 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
779 let vlog fmt =
780 if conf.verbose
781 then
782 Printf.kprintf prerr_endline fmt
783 else
784 Printf.kprintf ignore fmt
787 let launchpath () =
788 if emptystr conf.pathlauncher
789 then print_endline state.path
790 else (
791 let re = Str.regexp "%s" in
792 let command = Str.global_replace re state.path conf.pathlauncher in
793 try popen command []
794 with exn ->
795 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
796 flush stderr;
800 module Ne = struct
801 type 'a t = | Res of 'a | Exn of exn;;
803 let pipe () =
804 try Res (Unix.pipe ())
805 with exn -> Exn exn
808 let clo fd f =
809 try tempfailureretry Unix.close fd
810 with exn -> f (exntos exn)
813 let dup fd =
814 try Res (tempfailureretry Unix.dup fd)
815 with exn -> Exn exn
818 let dup2 fd1 fd2 =
819 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
820 with exn -> Exn exn
822 end;;
824 let redirectstderr () =
825 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
826 if conf.redirectstderr
827 then
828 match Ne.pipe () with
829 | Ne.Exn exn ->
830 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
832 | Ne.Res (r, w) ->
833 begin match Ne.dup Unix.stderr with
834 | Ne.Exn exn ->
835 dolog "failed to dup stderr: %s" (exntos exn);
836 Ne.clo r (clofail "pipe/r");
837 Ne.clo w (clofail "pipe/w");
839 | Ne.Res dupstderr ->
840 begin match Ne.dup2 w Unix.stderr with
841 | Ne.Exn exn ->
842 dolog "failed to dup2 to stderr: %s" (exntos exn);
843 Ne.clo dupstderr (clofail "stderr duplicate");
844 Ne.clo r (clofail "redir pipe/r");
845 Ne.clo w (clofail "redir pipe/w");
847 | Ne.Res () ->
848 state.stderr <- dupstderr;
849 state.errfd <- Some r;
850 end;
852 else (
853 state.newerrmsgs <- false;
854 begin match state.errfd with
855 | Some fd ->
856 begin match Ne.dup2 state.stderr Unix.stderr with
857 | Ne.Exn exn ->
858 dolog "failed to dup2 original stderr: %s" (exntos exn)
859 | Ne.Res () ->
860 Ne.clo fd (clofail "dup of stderr");
861 state.errfd <- None;
862 end;
863 | None -> ()
864 end;
865 prerr_string (Buffer.contents state.errmsgs);
866 flush stderr;
867 Buffer.clear state.errmsgs;
871 module G =
872 struct
873 let postRedisplay who =
874 if conf.verbose
875 then prerr_endline ("redisplay for " ^ who);
876 state.redisplay <- true;
878 end;;
880 let getopaque pageno =
881 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
882 with Not_found -> None
885 let putopaque pageno opaque =
886 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
889 let pagetranslatepoint l x y =
890 let dy = y - l.pagedispy in
891 let y = dy + l.pagey in
892 let dx = x - l.pagedispx in
893 let x = dx + l.pagex in
894 (x, y);
897 let onppundermouse g x y d =
898 let rec f = function
899 | l :: rest ->
900 begin match getopaque l.pageno with
901 | Some opaque ->
902 let x0 = l.pagedispx in
903 let x1 = x0 + l.pagevw in
904 let y0 = l.pagedispy in
905 let y1 = y0 + l.pagevh in
906 if y >= y0 && y <= y1 && x >= x0 && x <= x1
907 then
908 let px, py = pagetranslatepoint l x y in
909 match g opaque l px py with
910 | Some res -> res
911 | None -> f rest
912 else f rest
913 | _ ->
914 f rest
916 | [] -> d
918 f state.layout
921 let getunder x y =
922 let g opaque l px py =
923 if state.bzoom
924 then (
925 match rectofblock opaque px py with
926 | Some a ->
927 let rect = (a.(0),a.(2),a.(1),a.(2),a.(1),a.(3),a.(0),a.(3)) in
928 state.rects <- [l.pageno, l.pageno mod 3, rect];
929 G.postRedisplay "getunder";
930 | None -> ()
932 match whatsunder opaque px py with
933 | Unone -> None
934 | under -> Some under
936 onppundermouse g x y Unone
939 let unproject x y =
940 let g opaque l x y =
941 match unproject opaque x y with
942 | Some (x, y) -> Some (Some (l.pageno, x, y))
943 | None -> None
945 onppundermouse g x y None;
948 let showtext c s =
949 state.text <- Printf.sprintf "%c%s" c s;
950 G.postRedisplay "showtext";
953 let paxunder x y =
954 let g opaque l px py =
955 if markunder opaque px py conf.paxmark
956 then (
957 Some (fun () ->
958 match getopaque l.pageno with
959 | None -> ()
960 | Some opaque ->
961 match Ne.pipe () with
962 | Ne.Exn exn ->
963 showtext '!'
964 (Printf.sprintf
965 "can not create mark pipe: %s"
966 (exntos exn));
967 | Ne.Res (r, w) ->
968 let doclose what fd =
969 Ne.clo fd (fun msg ->
970 dolog "%s close failed: %s" what msg)
973 popen conf.paxcmd [r, 0; w, -1];
974 copysel w opaque false;
975 doclose "pipe/r" r;
976 G.postRedisplay "paxunder";
977 with exn ->
978 dolog "can not execute %S: %s"
979 conf.paxcmd (exntos exn);
980 doclose "pipe/r" r;
981 doclose "pipe/w" w;
984 else None
986 G.postRedisplay "paxunder";
987 if conf.paxmark = Mark_page
988 then
989 List.iter (fun l ->
990 match getopaque l.pageno with
991 | None -> ()
992 | Some opaque -> clearmark opaque) state.layout;
993 state.roam <-
994 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
997 let selstring s =
998 match Ne.pipe () with
999 | Ne.Exn exn ->
1000 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
1001 | Ne.Res (r, w) ->
1002 let popened =
1003 try popen conf.selcmd [r, 0; w, -1]; true
1004 with exn ->
1005 showtext '!'
1006 (Printf.sprintf "failed to execute %s: %s"
1007 conf.selcmd (exntos exn));
1008 false
1010 let clo cap fd =
1011 Ne.clo fd (fun msg ->
1012 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
1015 if popened
1016 then (
1018 let l = String.length s in
1019 let n = tempfailureretry (Unix.write w s 0) l in
1020 if n != l
1021 then
1022 showtext '!'
1023 (Printf.sprintf
1024 "failed to write %d characters to sel pipe, wrote %d"
1027 with exn ->
1028 showtext '!'
1029 (Printf.sprintf "failed to write to sel pipe: %s"
1030 (exntos exn)
1033 else dolog "%s" s;
1034 clo "pipe/r" r;
1035 clo "pipe/w" w;
1038 let undertext = function
1039 | Unone -> "none"
1040 | Ulinkuri s -> s
1041 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
1042 | Utext s -> "font: " ^ s
1043 | Uunexpected s -> "unexpected: " ^ s
1044 | Ulaunch s -> "launch: " ^ s
1045 | Unamed s -> "named: " ^ s
1046 | Uremote (filename, pageno) ->
1047 Printf.sprintf "%s: page %d" filename (pageno+1)
1048 | Uremotedest (filename, destname) ->
1049 Printf.sprintf "%s: destination %S" filename destname
1052 let updateunder x y =
1053 match getunder x y with
1054 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
1055 | Ulinkuri uri ->
1056 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1057 Wsi.setcursor Wsi.CURSOR_INFO
1058 | Ulinkgoto (pageno, _) ->
1059 if conf.underinfo
1060 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
1061 Wsi.setcursor Wsi.CURSOR_INFO
1062 | Utext s ->
1063 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1064 Wsi.setcursor Wsi.CURSOR_TEXT
1065 | Uunexpected s ->
1066 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
1067 Wsi.setcursor Wsi.CURSOR_INHERIT
1068 | Ulaunch s ->
1069 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
1070 Wsi.setcursor Wsi.CURSOR_INHERIT
1071 | Unamed s ->
1072 if conf.underinfo then showtext 'n' ("amed: " ^ s);
1073 Wsi.setcursor Wsi.CURSOR_INHERIT
1074 | Uremote (filename, pageno) ->
1075 if conf.underinfo then showtext 'r'
1076 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
1077 Wsi.setcursor Wsi.CURSOR_INFO
1078 | Uremotedest (filename, destname) ->
1079 if conf.underinfo then showtext 'r'
1080 (Printf.sprintf "emote destination: %s (%S)" filename destname);
1081 Wsi.setcursor Wsi.CURSOR_INFO
1084 let showlinktype under =
1085 if conf.underinfo
1086 then
1087 match under with
1088 | Unone -> ()
1089 | under ->
1090 let s = undertext under in
1091 showtext ' ' s
1094 let addchar s c =
1095 let b = Buffer.create (String.length s + 1) in
1096 Buffer.add_string b s;
1097 Buffer.add_char b c;
1098 Buffer.contents b;
1101 module type TextEnumType =
1103 type t
1104 val name : string
1105 val names : string array
1106 end;;
1108 module TextEnumMake (Ten : TextEnumType) =
1109 struct
1110 let names = Ten.names;;
1111 let to_int (t : Ten.t) = Obj.magic t;;
1112 let to_string t = names.(to_int t);;
1113 let of_int n : Ten.t = Obj.magic n;;
1114 let of_string s =
1115 let rec find i =
1116 if i = Array.length names
1117 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
1118 else (
1119 if Ten.names.(i) = s
1120 then of_int i
1121 else find (i+1)
1123 in find 0;;
1124 end;;
1126 module CSTE = TextEnumMake (struct
1127 type t = colorspace;;
1128 let name = "colorspace";;
1129 let names = [|"rgb"; "bgr"; "gray"|];;
1130 end);;
1132 module MTE = TextEnumMake (struct
1133 type t = mark;;
1134 let name = "mark";;
1135 let names = [|"page"; "block"; "line"; "word"|];;
1136 end);;
1138 module FMTE = TextEnumMake (struct
1139 type t= fitmodel;;
1140 let name = "fitmodel";;
1141 let names = [|"width"; "proportional"; "page"|];;
1142 end);;
1144 let intentry_with_suffix text key =
1145 let c =
1146 if key >= 32 && key < 127
1147 then Char.chr key
1148 else '\000'
1150 match Char.lowercase c with
1151 | '0' .. '9' ->
1152 let text = addchar text c in
1153 TEcont text
1155 | 'k' | 'm' | 'g' ->
1156 let text = addchar text c in
1157 TEcont text
1159 | _ ->
1160 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1161 TEcont text
1164 let multicolumns_to_string (n, a, b) =
1165 if a = 0 && b = 0
1166 then Printf.sprintf "%d" n
1167 else Printf.sprintf "%d,%d,%d" n a b;
1170 let multicolumns_of_string s =
1172 (int_of_string s, 0, 0)
1173 with _ ->
1174 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1175 if a > 1 || b > 1
1176 then failwith "subtly broken"; (n, a, b)
1180 let readcmd fd =
1181 let s = "xxxx" in
1182 let n = tempfailureretry (Unix.read fd s 0) 4 in
1183 if n != 4 then error "incomplete read(len) = %d" n;
1184 let len = 0
1185 lor (Char.code s.[0] lsl 24)
1186 lor (Char.code s.[1] lsl 16)
1187 lor (Char.code s.[2] lsl 8)
1188 lor (Char.code s.[3] lsl 0)
1190 let s = String.create len in
1191 let n = tempfailureretry (Unix.read fd s 0) len in
1192 if n != len then error "incomplete read(data) %d vs %d" n len;
1196 let btod b = if b then 1 else 0;;
1198 let wcmd fmt =
1199 let b = Buffer.create 16 in
1200 Buffer.add_string b "llll";
1201 Printf.kbprintf
1202 (fun b ->
1203 let s = Buffer.contents b in
1204 let n = String.length s in
1205 let len = n - 4 in
1206 (* dolog "wcmd %S" (String.sub s 4 len); *)
1207 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1208 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1209 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1210 s.[3] <- Char.chr (len land 0xff);
1211 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1212 if n' != n then error "write failed %d vs %d" n' n;
1213 ) b fmt;
1216 let calcips h =
1217 let d = state.winh - h in
1218 max conf.interpagespace ((d + 1) / 2)
1221 let rowyh (c, coverA, coverB) b n =
1222 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1223 then
1224 let _, _, vy, (_, _, h, _) = b.(n) in
1225 (vy, h)
1226 else
1227 let n' = n - coverA in
1228 let d = n' mod c in
1229 let s = n - d in
1230 let e = min state.pagecount (s + c) in
1231 let rec find m miny maxh = if m = e then miny, maxh else
1232 let _, _, y, (_, _, h, _) = b.(m) in
1233 let miny = min miny y in
1234 let maxh = max maxh h in
1235 find (m+1) miny maxh
1236 in find s max_int 0
1239 let calcheight () =
1240 match conf.columns with
1241 | Cmulti ((_, _, _) as cl, b) ->
1242 if Array.length b > 0
1243 then
1244 let y, h = rowyh cl b (Array.length b - 1) in
1245 y + h + (if conf.presentation then calcips h else 0)
1246 else 0
1247 | Csingle b ->
1248 if Array.length b > 0
1249 then
1250 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1251 y + h + (if conf.presentation then calcips h else 0)
1252 else 0
1253 | Csplit (_, b) ->
1254 if Array.length b > 0
1255 then
1256 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1257 y + h
1258 else 0
1261 let getpageywh pageno =
1262 let pageno = bound pageno 0 (state.pagecount-1) in
1263 match conf.columns with
1264 | Csingle b ->
1265 if Array.length b = 0
1266 then 0, 0, 0
1267 else
1268 let (_, _, y, (_, w, h, _)) = b.(pageno) in
1269 let y =
1270 if conf.presentation
1271 then y - calcips h
1272 else y
1274 y, w, h
1275 | Cmulti (cl, b) ->
1276 if Array.length b = 0
1277 then 0, 0, 0
1278 else
1279 let y, h = rowyh cl b pageno in
1280 let (_, _, _, (_, w, _, _)) = b.(pageno) in
1281 let y =
1282 if conf.presentation
1283 then y - calcips h
1284 else y
1286 y, w, h
1287 | Csplit (c, b) ->
1288 if Array.length b = 0
1289 then 0, 0, 0
1290 else
1291 let n = pageno*c in
1292 let (_, _, y, (_, w, h, _)) = b.(n) in
1293 y, w / c, h
1296 let getpageyh pageno =
1297 let y,_,h = getpageywh pageno in
1298 y, h;
1301 let getpagedim pageno =
1302 let rec f ppdim l =
1303 match l with
1304 | (n, _, _, _) as pdim :: rest ->
1305 if n >= pageno
1306 then (if n = pageno then pdim else ppdim)
1307 else f pdim rest
1309 | [] -> ppdim
1311 f (-1, -1, -1, -1) state.pdims
1314 let getpagey pageno = fst (getpageyh pageno);;
1316 let nogeomcmds cmds =
1317 match cmds with
1318 | s, [] -> emptystr s
1319 | _ -> false
1322 let page_of_y y =
1323 let ((c, coverA, coverB) as cl), b =
1324 match conf.columns with
1325 | Csingle b -> (1, 0, 0), b
1326 | Cmulti (c, b) -> c, b
1327 | Csplit (_, b) -> (1, 0, 0), b
1329 if Array.length b = 0
1330 then -1
1331 else
1332 let rec bsearch nmin nmax =
1333 if nmin > nmax
1334 then bound nmin 0 (state.pagecount-1)
1335 else
1336 let n = (nmax + nmin) / 2 in
1337 let vy, h = rowyh cl b n in
1338 let y0, y1 =
1339 if conf.presentation
1340 then
1341 let ips = calcips h in
1342 let y0 = vy - ips in
1343 let y1 = vy + h + ips in
1344 y0, y1
1345 else (
1346 if n = 0
1347 then 0, vy + h + conf.interpagespace
1348 else
1349 let y0 = vy - conf.interpagespace in
1350 y0, y0 + h + conf.interpagespace
1353 if y >= y0 && y < y1
1354 then (
1355 if c = 1
1356 then n
1357 else (
1358 if n > coverA
1359 then
1360 if n < state.pagecount - coverB
1361 then ((n-coverA)/c)*c + coverA
1362 else n
1363 else n
1366 else (
1367 if y > y0
1368 then bsearch (n+1) nmax
1369 else bsearch nmin (n-1)
1372 bsearch 0 (state.pagecount-1);
1375 let layoutN ((columns, coverA, coverB), b) y sh =
1376 let sh = sh - (hscrollh ()) in
1377 let rec fold accu n =
1378 if n = Array.length b
1379 then accu
1380 else
1381 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1382 if (vy - y) > sh &&
1383 (n = coverA - 1
1384 || n = state.pagecount - coverB
1385 || (n - coverA) mod columns = columns - 1)
1386 then accu
1387 else
1388 let accu =
1389 if vy + h > y
1390 then
1391 let pagey = max 0 (y - vy) in
1392 let pagedispy = if pagey > 0 then 0 else vy - y in
1393 let pagedispx, pagex =
1394 let pdx =
1395 if n = coverA - 1 || n = state.pagecount - coverB
1396 then state.x + (wadjsb state.winw - w) / 2
1397 else dx + xoff + state.x
1399 if pdx < 0
1400 then 0, -pdx
1401 else pdx, 0
1403 let pagevw =
1404 let vw = wadjsb state.winw - pagedispx in
1405 let pw = w - pagex in
1406 min vw pw
1408 let pagevh = min (h - pagey) (sh - pagedispy) in
1409 if pagevw > 0 && pagevh > 0
1410 then
1411 let e =
1412 { pageno = n
1413 ; pagedimno = pdimno
1414 ; pagew = w
1415 ; pageh = h
1416 ; pagex = pagex
1417 ; pagey = pagey
1418 ; pagevw = pagevw
1419 ; pagevh = pagevh
1420 ; pagedispx = pagedispx
1421 ; pagedispy = pagedispy
1422 ; pagecol = 0
1425 e :: accu
1426 else
1427 accu
1428 else
1429 accu
1431 fold accu (n+1)
1433 if Array.length b = 0
1434 then []
1435 else List.rev (fold [] (page_of_y y))
1438 let layoutS (columns, b) y sh =
1439 let sh = sh - hscrollh () in
1440 let rec fold accu n =
1441 if n = Array.length b
1442 then accu
1443 else
1444 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1445 if (vy - y) > sh
1446 then accu
1447 else
1448 let accu =
1449 if vy + pageh > y
1450 then
1451 let x = xoff + state.x in
1452 let pagey = max 0 (y - vy) in
1453 let pagedispy = if pagey > 0 then 0 else vy - y in
1454 let pagedispx, pagex =
1455 if px = 0
1456 then (
1457 if x < 0
1458 then 0, -x
1459 else x, 0
1461 else (
1462 let px = px - x in
1463 if px < 0
1464 then -px, 0
1465 else 0, px
1468 let pagecolw = pagew/columns in
1469 let pagedispx =
1470 if pagecolw < state.winw
1471 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
1472 else pagedispx
1474 let pagevw =
1475 let vw = wadjsb state.winw - pagedispx in
1476 let pw = pagew - pagex in
1477 min vw pw
1479 let pagevw = min pagevw pagecolw in
1480 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1481 if pagevw > 0 && pagevh > 0
1482 then
1483 let e =
1484 { pageno = n/columns
1485 ; pagedimno = pdimno
1486 ; pagew = pagew
1487 ; pageh = pageh
1488 ; pagex = pagex
1489 ; pagey = pagey
1490 ; pagevw = pagevw
1491 ; pagevh = pagevh
1492 ; pagedispx = pagedispx
1493 ; pagedispy = pagedispy
1494 ; pagecol = n mod columns
1497 e :: accu
1498 else
1499 accu
1500 else
1501 accu
1503 fold accu (n+1)
1505 List.rev (fold [] 0)
1508 let layout y sh =
1509 if nogeomcmds state.geomcmds
1510 then
1511 match conf.columns with
1512 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1513 | Cmulti c -> layoutN c y sh
1514 | Csplit s -> layoutS s y sh
1515 else []
1518 let clamp incr =
1519 let y = state.y + incr in
1520 let y = max 0 y in
1521 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1525 let itertiles l f =
1526 let tilex = l.pagex mod conf.tilew in
1527 let tiley = l.pagey mod conf.tileh in
1529 let col = l.pagex / conf.tilew in
1530 let row = l.pagey / conf.tileh in
1532 let rec rowloop row y0 dispy h =
1533 if h = 0
1534 then ()
1535 else (
1536 let dh = conf.tileh - y0 in
1537 let dh = min h dh in
1538 let rec colloop col x0 dispx w =
1539 if w = 0
1540 then ()
1541 else (
1542 let dw = conf.tilew - x0 in
1543 let dw = min w dw in
1545 f col row dispx dispy x0 y0 dw dh;
1546 colloop (col+1) 0 (dispx+dw) (w-dw)
1549 colloop col tilex l.pagedispx l.pagevw;
1550 rowloop (row+1) 0 (dispy+dh) (h-dh)
1553 if l.pagevw > 0 && l.pagevh > 0
1554 then rowloop row tiley l.pagedispy l.pagevh;
1557 let gettileopaque l col row =
1558 let key =
1559 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1561 try Some (Hashtbl.find state.tilemap key)
1562 with Not_found -> None
1565 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1566 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1567 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1570 let drawtiles l color =
1571 GlDraw.color color;
1572 let f col row x y tilex tiley w h =
1573 match gettileopaque l col row with
1574 | Some (opaque, _, t) ->
1575 let params = x, y, w, h, tilex, tiley in
1576 if conf.invert
1577 then (
1578 Gl.enable `blend;
1579 GlFunc.blend_func `zero `one_minus_src_color;
1581 drawtile params opaque;
1582 if conf.invert
1583 then Gl.disable `blend;
1584 if conf.debug
1585 then (
1586 let s = Printf.sprintf
1587 "%d[%d,%d] %f sec"
1588 l.pageno col row t
1590 let w = measurestr fstate.fontsize s in
1591 GlMisc.push_attrib [`current];
1592 GlDraw.color (0.0, 0.0, 0.0);
1593 GlDraw.rect
1594 (float (x-2), float (y-2))
1595 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1596 GlDraw.color (1.0, 1.0, 1.0);
1597 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1598 GlMisc.pop_attrib ();
1601 | _ ->
1602 let w =
1603 let lw = wadjsb state.winw - x in
1604 min lw w
1605 and h =
1606 let lh = state.winh - y in
1607 min lh h
1609 begin match state.texid with
1610 | Some id ->
1611 Gl.enable `texture_2d;
1612 GlTex.bind_texture `texture_2d id;
1613 let x0 = float x
1614 and y0 = float y
1615 and x1 = float (x+w)
1616 and y1 = float (y+h) in
1618 let tw = float w /. 16.0
1619 and th = float h /. 16.0 in
1620 let tx0 = float tilex /. 16.0
1621 and ty0 = float tiley /. 16.0 in
1622 let tx1 = tx0 +. tw
1623 and ty1 = ty0 +. th in
1624 GlDraw.begins `quads;
1625 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1626 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1627 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1628 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1629 GlDraw.ends ();
1631 Gl.disable `texture_2d;
1632 | None ->
1633 GlDraw.color (1.0, 1.0, 1.0);
1634 GlDraw.rect
1635 (float x, float y)
1636 (float (x+w), float (y+h));
1637 end;
1638 if w > 128 && h > fstate.fontsize + 10
1639 then (
1640 GlDraw.color (0.0, 0.0, 0.0);
1641 let c, r =
1642 if conf.verbose
1643 then (col*conf.tilew, row*conf.tileh)
1644 else col, row
1646 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1648 GlDraw.color color;
1650 itertiles l f
1653 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1655 let tilevisible1 l x y =
1656 let ax0 = l.pagex
1657 and ax1 = l.pagex + l.pagevw
1658 and ay0 = l.pagey
1659 and ay1 = l.pagey + l.pagevh in
1661 let bx0 = x
1662 and by0 = y in
1663 let bx1 = min (bx0 + conf.tilew) l.pagew
1664 and by1 = min (by0 + conf.tileh) l.pageh in
1666 let rx0 = max ax0 bx0
1667 and ry0 = max ay0 by0
1668 and rx1 = min ax1 bx1
1669 and ry1 = min ay1 by1 in
1671 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1672 nonemptyintersection
1675 let tilevisible layout n x y =
1676 let rec findpageinlayout m = function
1677 | l :: rest when l.pageno = n ->
1678 tilevisible1 l x y || (
1679 match conf.columns with
1680 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1681 | _ -> false
1683 | _ :: rest -> findpageinlayout 0 rest
1684 | [] -> false
1686 findpageinlayout 0 layout;
1689 let tileready l x y =
1690 tilevisible1 l x y &&
1691 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1694 let tilepage n p layout =
1695 let rec loop = function
1696 | l :: rest ->
1697 if l.pageno = n
1698 then
1699 let f col row _ _ _ _ _ _ =
1700 if state.currently = Idle
1701 then
1702 match gettileopaque l col row with
1703 | Some _ -> ()
1704 | None ->
1705 let x = col*conf.tilew
1706 and y = row*conf.tileh in
1707 let w =
1708 let w = l.pagew - x in
1709 min w conf.tilew
1711 let h =
1712 let h = l.pageh - y in
1713 min h conf.tileh
1715 let pbo =
1716 if conf.usepbo
1717 then getpbo w h conf.colorspace
1718 else "0"
1720 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1721 state.currently <-
1722 Tiling (
1723 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1724 conf.tilew, conf.tileh
1727 itertiles l f;
1728 else
1729 loop rest
1731 | [] -> ()
1733 if nogeomcmds state.geomcmds
1734 then loop layout;
1737 let preloadlayout y =
1738 let y = if y < state.winh then 0 else y - state.winh in
1739 let h = state.winh*3 in
1740 layout y h;
1743 let load pages =
1744 let rec loop pages =
1745 if state.currently != Idle
1746 then ()
1747 else
1748 match pages with
1749 | l :: rest ->
1750 begin match getopaque l.pageno with
1751 | None ->
1752 wcmd "page %d %d" l.pageno l.pagedimno;
1753 state.currently <- Loading (l, state.gen);
1754 | Some opaque ->
1755 tilepage l.pageno opaque pages;
1756 loop rest
1757 end;
1758 | _ -> ()
1760 if nogeomcmds state.geomcmds
1761 then loop pages
1764 let preload pages =
1765 load pages;
1766 if conf.preload && state.currently = Idle
1767 then load (preloadlayout state.y);
1770 let layoutready layout =
1771 let rec fold all ls =
1772 all && match ls with
1773 | l :: rest ->
1774 let seen = ref false in
1775 let allvisible = ref true in
1776 let foo col row _ _ _ _ _ _ =
1777 seen := true;
1778 allvisible := !allvisible &&
1779 begin match gettileopaque l col row with
1780 | Some _ -> true
1781 | None -> false
1784 itertiles l foo;
1785 fold (!seen && !allvisible) rest
1786 | [] -> true
1788 let alltilesvisible = fold true layout in
1789 alltilesvisible;
1792 let gotoy y =
1793 let y = bound y 0 state.maxy in
1794 let y, layout, proceed =
1795 match conf.maxwait with
1796 | Some time when state.ghyll == noghyll ->
1797 begin match state.throttle with
1798 | None ->
1799 let layout = layout y state.winh in
1800 let ready = layoutready layout in
1801 if not ready
1802 then (
1803 load layout;
1804 state.throttle <- Some (layout, y, now ());
1806 else G.postRedisplay "gotoy showall (None)";
1807 y, layout, ready
1808 | Some (_, _, started) ->
1809 let dt = now () -. started in
1810 if dt > time
1811 then (
1812 state.throttle <- None;
1813 let layout = layout y state.winh in
1814 load layout;
1815 G.postRedisplay "maxwait";
1816 y, layout, true
1818 else -1, [], false
1821 | _ ->
1822 let layout = layout y state.winh in
1823 if not !wtmode || layoutready layout
1824 then G.postRedisplay "gotoy ready";
1825 y, layout, true
1827 if proceed
1828 then (
1829 state.y <- y;
1830 state.layout <- layout;
1831 begin match state.mode with
1832 | LinkNav (Ltexact (pageno, linkno)) ->
1833 let rec loop = function
1834 | [] ->
1835 state.mode <- LinkNav (Ltgendir 0)
1836 | l :: _ when l.pageno = pageno ->
1837 begin match getopaque pageno with
1838 | None ->
1839 state.mode <- LinkNav (Ltgendir 0)
1840 | Some opaque ->
1841 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1842 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1843 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1844 then state.mode <- LinkNav (Ltgendir 0)
1846 | _ :: rest -> loop rest
1848 loop layout
1849 | _ -> ()
1850 end;
1851 begin match state.mode with
1852 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1853 if not (pagevisible layout pageno)
1854 then (
1855 match state.layout with
1856 | [] -> ()
1857 | l :: _ ->
1858 state.mode <- Birdseye (
1859 conf, leftx, l.pageno, hooverpageno, anchor
1862 | LinkNav (Ltgendir dir as lt) ->
1863 let linknav =
1864 let rec loop = function
1865 | [] -> lt
1866 | l :: rest ->
1867 match getopaque l.pageno with
1868 | None -> loop rest
1869 | Some opaque ->
1870 let link =
1871 let ld =
1872 if dir = 0
1873 then LDfirstvisible (l.pagex, l.pagey, dir)
1874 else (
1875 if dir > 0 then LDfirst else LDlast
1878 findlink opaque ld
1880 match link with
1881 | Lnotfound -> loop rest
1882 | Lfound n ->
1883 showlinktype (getlink opaque n);
1884 Ltexact (l.pageno, n)
1886 loop state.layout
1888 state.mode <- LinkNav linknav
1889 | _ -> ()
1890 end;
1891 preload layout;
1893 state.ghyll <- noghyll;
1894 if conf.updatecurs
1895 then (
1896 let mx, my = state.mpos in
1897 updateunder mx my;
1901 let conttiling pageno opaque =
1902 tilepage pageno opaque
1903 (if conf.preload then preloadlayout state.y else state.layout)
1906 let gotoy_and_clear_text y =
1907 if not conf.verbose then state.text <- "";
1908 gotoy y;
1911 let getanchor1 l =
1912 let top =
1913 let coloff = l.pagecol * l.pageh in
1914 float (l.pagey + coloff) /. float l.pageh
1916 let dtop =
1917 if l.pagedispy = 0
1918 then
1920 else (
1921 if conf.presentation
1922 then float l.pagedispy /. float (calcips l.pageh)
1923 else float l.pagedispy /. float conf.interpagespace
1926 (l.pageno, top, dtop)
1929 let getanchor () =
1930 match state.layout with
1931 | l :: _ -> getanchor1 l
1932 | [] ->
1933 let n = page_of_y state.y in
1934 if n = -1
1935 then state.anchor
1936 else
1937 let y, h = getpageyh n in
1938 let dy = y - state.y in
1939 let dtop =
1940 if conf.presentation
1941 then
1942 let ips = calcips h in
1943 float (dy + ips) /. float ips
1944 else
1945 float dy /. float conf.interpagespace
1947 (n, 0.0, dtop)
1950 let getanchory (n, top, dtop) =
1951 let y, h = getpageyh n in
1952 if conf.presentation
1953 then
1954 let ips = calcips h in
1955 y + truncate (top*.float h -. dtop*.float ips) + ips;
1956 else
1957 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1960 let gotoanchor anchor =
1961 gotoy (getanchory anchor);
1964 let addnav () =
1965 cbput state.hists.nav (getanchor ());
1968 let getnav dir =
1969 let anchor = cbgetc state.hists.nav dir in
1970 getanchory anchor;
1973 let gotoghyll y =
1974 let scroll f n a b =
1975 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1976 let snake f a b =
1977 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1978 if f < a
1979 then s (float f /. float a)
1980 else (
1981 if f > b
1982 then 1.0 -. s ((float (f-b) /. float (n-b)))
1983 else 1.0
1986 snake f a b
1987 and summa n a b =
1988 let ins = float a *. 0.5
1989 and outs = float (n-b) *. 0.5 in
1990 let ones = b - a in
1991 ins +. outs +. float ones
1993 let rec set (_N, _A, _B) y sy =
1994 let sum = summa _N _A _B in
1995 let dy = float (y - sy) in
1996 state.ghyll <- (
1997 let rec gf n y1 o =
1998 if n >= _N
1999 then state.ghyll <- noghyll
2000 else
2001 let go n =
2002 let s = scroll n _N _A _B in
2003 let y1 = y1 +. ((s *. dy) /. sum) in
2004 gotoy_and_clear_text (truncate y1);
2005 state.ghyll <- gf (n+1) y1;
2007 match o with
2008 | None -> go n
2009 | Some y' -> set (_N/2, 1, 1) y' state.y
2011 gf 0 (float state.y)
2014 match conf.ghyllscroll with
2015 | None ->
2016 gotoy_and_clear_text y
2017 | Some nab ->
2018 if state.ghyll == noghyll
2019 then set nab y state.y
2020 else state.ghyll (Some y)
2023 let gotopage n top =
2024 let y, h = getpageyh n in
2025 let y = y + (truncate (top *. float h)) in
2026 gotoghyll y
2029 let gotopage1 n top =
2030 let y = getpagey n in
2031 let y = y + top in
2032 gotoghyll y
2035 let invalidate s f =
2036 state.layout <- [];
2037 state.pdims <- [];
2038 state.rects <- [];
2039 state.rects1 <- [];
2040 match state.geomcmds with
2041 | ps, [] when emptystr ps ->
2042 f ();
2043 state.geomcmds <- s, [];
2045 | ps, [] ->
2046 state.geomcmds <- ps, [s, f];
2048 | ps, (s', _) :: rest when s' = s ->
2049 state.geomcmds <- ps, ((s, f) :: rest);
2051 | ps, cmds ->
2052 state.geomcmds <- ps, ((s, f) :: cmds);
2055 let flushpages () =
2056 Hashtbl.iter (fun _ opaque ->
2057 wcmd "freepage %s" opaque;
2058 ) state.pagemap;
2059 Hashtbl.clear state.pagemap;
2062 let flushtiles () =
2063 if not (Queue.is_empty state.tilelru)
2064 then (
2065 Queue.iter (fun (k, p, s) ->
2066 wcmd "freetile %s" p;
2067 state.memused <- state.memused - s;
2068 Hashtbl.remove state.tilemap k;
2069 ) state.tilelru;
2070 state.uioh#infochanged Memused;
2071 Queue.clear state.tilelru;
2073 load state.layout;
2076 let stateh h =
2077 let h = truncate (float h*.conf.zoom) in
2078 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2079 h - d
2082 let opendoc path password =
2083 state.path <- path;
2084 state.password <- password;
2085 state.gen <- state.gen + 1;
2086 state.docinfo <- [];
2088 flushpages ();
2089 setaalevel conf.aalevel;
2090 let titlepath =
2091 if emptystr state.origin
2092 then path
2093 else state.origin
2095 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2096 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
2097 invalidate "reqlayout"
2098 (fun () ->
2099 wcmd "reqlayout %d %d %d %s\000"
2100 conf.angle (FMTE.to_int conf.fitmodel)
2101 (stateh state.winh) state.nameddest
2105 let reload () =
2106 state.anchor <- getanchor ();
2107 opendoc state.path state.password;
2110 let scalecolor c =
2111 let c = c *. conf.colorscale in
2112 (c, c, c);
2115 let scalecolor2 (r, g, b) =
2116 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2119 let docolumns = function
2120 | Csingle _ ->
2121 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2122 let rec loop pageno pdimno pdim y ph pdims =
2123 if pageno = state.pagecount
2124 then ()
2125 else
2126 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2127 match pdims with
2128 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2129 pdimno+1, pdim, rest
2130 | _ ->
2131 pdimno, pdim, pdims
2133 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2134 let y = y +
2135 (if conf.presentation
2136 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2137 else (if pageno = 0 then 0 else conf.interpagespace)
2140 a.(pageno) <- (pdimno, x, y, pdim);
2141 loop (pageno+1) pdimno pdim (y + h) h pdims
2143 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2144 conf.columns <- Csingle a;
2146 | Cmulti ((columns, coverA, coverB), _) ->
2147 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2148 let rec loop pageno pdimno pdim x y rowh pdims =
2149 let rec fixrow m = if m = pageno then () else
2150 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2151 if h < rowh
2152 then (
2153 let y = y + (rowh - h) / 2 in
2154 a.(m) <- (pdimno, x, y, pdim);
2156 fixrow (m+1)
2158 if pageno = state.pagecount
2159 then fixrow (((pageno - 1) / columns) * columns)
2160 else
2161 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2162 match pdims with
2163 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2164 pdimno+1, pdim, rest
2165 | _ ->
2166 pdimno, pdim, pdims
2168 let x, y, rowh' =
2169 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2170 then (
2171 let x = (wadjsb state.winw - w) / 2 in
2172 let ips =
2173 if conf.presentation then calcips h else conf.interpagespace in
2174 x, y + ips + rowh, h
2176 else (
2177 if (pageno - coverA) mod columns = 0
2178 then (
2179 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2180 let y =
2181 if conf.presentation
2182 then
2183 let ips = calcips h in
2184 y + (if pageno = 0 then 0 else calcips rowh + ips)
2185 else
2186 y + (if pageno = 0 then 0 else conf.interpagespace)
2188 x, y + rowh, h
2190 else x, y, max rowh h
2193 let y =
2194 if pageno > 1 && (pageno - coverA) mod columns = 0
2195 then (
2196 let y =
2197 if pageno = columns && conf.presentation
2198 then (
2199 let ips = calcips rowh in
2200 for i = 0 to pred columns
2202 let (pdimno, x, y, pdim) = a.(i) in
2203 a.(i) <- (pdimno, x, y+ips, pdim)
2204 done;
2205 y+ips;
2207 else y
2209 fixrow (pageno - columns);
2212 else y
2214 a.(pageno) <- (pdimno, x, y, pdim);
2215 let x = x + w + xoff*2 + conf.interpagespace in
2216 loop (pageno+1) pdimno pdim x y rowh' pdims
2218 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2219 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2221 | Csplit (c, _) ->
2222 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2223 let rec loop pageno pdimno pdim y pdims =
2224 if pageno = state.pagecount
2225 then ()
2226 else
2227 let pdimno, ((_, w, h, _) as pdim), pdims =
2228 match pdims with
2229 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2230 pdimno+1, pdim, rest
2231 | _ ->
2232 pdimno, pdim, pdims
2234 let cw = w / c in
2235 let rec loop1 n x y =
2236 if n = c then y else (
2237 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2238 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2241 let y = loop1 0 0 y in
2242 loop (pageno+1) pdimno pdim y pdims
2244 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2245 conf.columns <- Csplit (c, a);
2248 let represent () =
2249 docolumns conf.columns;
2250 state.maxy <- calcheight ();
2251 if state.reprf == noreprf
2252 then (
2253 match state.mode with
2254 | Birdseye (_, _, pageno, _, _) ->
2255 let y, h = getpageyh pageno in
2256 let top = (state.winh - h) / 2 in
2257 gotoy (max 0 (y - top))
2258 | _ -> gotoanchor state.anchor
2260 else (
2261 state.reprf ();
2262 state.reprf <- noreprf;
2266 let reshape w h =
2267 GlDraw.viewport 0 0 w h;
2268 let firsttime = state.geomcmds == firstgeomcmds in
2269 if not firsttime && nogeomcmds state.geomcmds
2270 then state.anchor <- getanchor ();
2272 state.winw <- w;
2273 let w = wadjsb (truncate (float w *. conf.zoom)) in
2274 let w = max w 2 in
2275 state.winh <- h;
2276 setfontsize fstate.fontsize;
2277 GlMat.mode `modelview;
2278 GlMat.load_identity ();
2280 GlMat.mode `projection;
2281 GlMat.load_identity ();
2282 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2283 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2284 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2286 let relx =
2287 if conf.zoom <= 1.0
2288 then 0.0
2289 else float state.x /. float state.w
2291 invalidate "geometry"
2292 (fun () ->
2293 state.w <- w;
2294 if not firsttime
2295 then state.x <- truncate (relx *. float w);
2296 let w =
2297 match conf.columns with
2298 | Csingle _ -> w
2299 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2300 | Csplit (c, _) -> w * c
2302 wcmd "geometry %d %d %d"
2303 w (stateh h) (FMTE.to_int conf.fitmodel)
2307 let enttext () =
2308 let len = String.length state.text in
2309 let drawstring s =
2310 let hscrollh =
2311 match state.mode with
2312 | Textentry _ | View | LinkNav _ ->
2313 let h, _, _ = state.uioh#scrollpw in
2315 | _ -> 0
2317 let rect x w =
2318 GlDraw.rect
2319 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2320 (x+.w, float (state.winh - hscrollh))
2323 let w = float (wadjsb state.winw - 1) in
2324 if state.progress >= 0.0 && state.progress < 1.0
2325 then (
2326 GlDraw.color (0.3, 0.3, 0.3);
2327 let w1 = w *. state.progress in
2328 rect 0.0 w1;
2329 GlDraw.color (0.0, 0.0, 0.0);
2330 rect w1 (w-.w1)
2332 else (
2333 GlDraw.color (0.0, 0.0, 0.0);
2334 rect 0.0 w;
2337 GlDraw.color (1.0, 1.0, 1.0);
2338 drawstring fstate.fontsize
2339 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2341 let s =
2342 match state.mode with
2343 | Textentry ((prefix, text, _, _, _, _), _) ->
2344 let s =
2345 if len > 0
2346 then
2347 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2348 else
2349 Printf.sprintf "%s%s_" prefix text
2353 | _ -> state.text
2355 let s =
2356 if state.newerrmsgs
2357 then (
2358 if not (istextentry state.mode) && state.uioh#eformsgs
2359 then
2360 let s1 = "(press 'e' to review error messasges)" in
2361 if nonemptystr s then s ^ " " ^ s1 else s1
2362 else s
2364 else s
2366 if nonemptystr s
2367 then drawstring s
2370 let gctiles () =
2371 let len = Queue.length state.tilelru in
2372 let layout = lazy (
2373 match state.throttle with
2374 | None ->
2375 if conf.preload
2376 then preloadlayout state.y
2377 else state.layout
2378 | Some (layout, _, _) ->
2379 layout
2380 ) in
2381 let rec loop qpos =
2382 if state.memused <= conf.memlimit
2383 then ()
2384 else (
2385 if qpos < len
2386 then
2387 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2388 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2389 let (_, pw, ph, _) = getpagedim n in
2391 gen = state.gen
2392 && colorspace = conf.colorspace
2393 && angle = conf.angle
2394 && pagew = pw
2395 && pageh = ph
2396 && (
2397 let x = col*conf.tilew
2398 and y = row*conf.tileh in
2399 tilevisible (Lazy.force_val layout) n x y
2401 then Queue.push lruitem state.tilelru
2402 else (
2403 freepbo p;
2404 wcmd "freetile %s" p;
2405 state.memused <- state.memused - s;
2406 state.uioh#infochanged Memused;
2407 Hashtbl.remove state.tilemap k;
2409 loop (qpos+1)
2412 loop 0
2415 let logcurrently = function
2416 | Idle -> dolog "Idle"
2417 | Loading (l, gen) ->
2418 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2419 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2420 dolog
2421 "Tiling %d[%d,%d] page=%s cs=%s angle"
2422 l.pageno col row pageopaque
2423 (CSTE.to_string colorspace)
2425 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2426 angle gen conf.angle state.gen
2427 tilew tileh
2428 conf.tilew conf.tileh
2430 | Outlining _ ->
2431 dolog "outlining"
2434 let splitatspace =
2435 let r = Str.regexp " " in
2436 fun s -> Str.bounded_split r s 2;
2439 let onpagerect pageno f =
2440 let b =
2441 match conf.columns with
2442 | Cmulti (_, b) -> b
2443 | Csingle b -> b
2444 | Csplit (_, b) -> b
2446 if pageno >= 0 && pageno < Array.length b
2447 then
2448 let (_, _, _, (w, h, _, _)) = b.(pageno) in
2449 f w h
2452 let gotopagexy1 pageno x y =
2453 let _,w1,h1,leftx = getpagedim pageno in
2454 let top = y /. (float h1) in
2455 let left = x /. (float w1) in
2456 let py, w, h = getpageywh pageno in
2457 let wh = state.winh - hscrollh () in
2458 let x = left *. (float w) in
2459 let x = leftx + state.x + truncate x in
2460 let sx =
2461 if x < 0 || x >= wadjsb state.winw
2462 then state.x - x
2463 else state.x
2465 let pdy = truncate (top *. float h) in
2466 let y' = py + pdy in
2467 let dy = y' - state.y in
2468 let sy =
2469 if x != state.x || not (dy > 0 && dy < wh)
2470 then (
2471 if conf.presentation
2472 then
2473 if abs (py - y') > wh
2474 then y'
2475 else py
2476 else y';
2478 else state.y
2480 if state.x != sx || state.y != sy
2481 then (
2482 let x, y =
2483 if !wtmode
2484 then (
2485 let ww = wadjsb state.winw in
2486 let qx = sx / ww
2487 and qy = pdy / wh in
2488 let x = qx * ww
2489 and y = py + qy * wh in
2490 let x = if -x + ww > w1 then -(w1-ww) else x
2491 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2492 let y =
2493 if conf.presentation
2494 then
2495 if abs (py - y') > wh
2496 then y'
2497 else py
2498 else y';
2500 (x, y)
2502 else (sx, sy)
2504 state.x <- x;
2505 gotoy_and_clear_text y;
2507 else gotoy_and_clear_text state.y;
2510 let gotopagexy pageno x y =
2511 match state.mode with
2512 | Birdseye _ -> gotopage pageno 0.0
2513 | _ -> gotopagexy1 pageno x y
2516 let act cmds =
2517 (* dolog "%S" cmds; *)
2518 let cl = splitatspace cmds in
2519 let scan s fmt f =
2520 try Scanf.sscanf s fmt f
2521 with exn ->
2522 dolog "error processing '%S': %s" cmds (exntos exn);
2523 exit 1
2525 match cl with
2526 | "clear" :: [] ->
2527 state.uioh#infochanged Pdim;
2528 state.pdims <- [];
2530 | "clearrects" :: [] ->
2531 state.rects <- state.rects1;
2532 G.postRedisplay "clearrects";
2534 | "continue" :: args :: [] ->
2535 let n = scan args "%u" (fun n -> n) in
2536 state.pagecount <- n;
2537 begin match state.currently with
2538 | Outlining l ->
2539 state.currently <- Idle;
2540 state.outlines <- Array.of_list (List.rev l)
2541 | _ -> ()
2542 end;
2544 let cur, cmds = state.geomcmds in
2545 if emptystr cur
2546 then failwith "umpossible";
2548 begin match List.rev cmds with
2549 | [] ->
2550 state.geomcmds <- "", [];
2551 represent ();
2552 | (s, f) :: rest ->
2553 f ();
2554 state.geomcmds <- s, List.rev rest;
2555 end;
2556 if conf.maxwait = None && not !wtmode
2557 then G.postRedisplay "continue";
2559 | "title" :: args :: [] ->
2560 Wsi.settitle args
2562 | "msg" :: args :: [] ->
2563 showtext ' ' args
2565 | "vmsg" :: args :: [] ->
2566 if conf.verbose
2567 then showtext ' ' args
2569 | "emsg" :: args :: [] ->
2570 Buffer.add_string state.errmsgs args;
2571 state.newerrmsgs <- true;
2572 G.postRedisplay "error message"
2574 | "progress" :: args :: [] ->
2575 let progress, text =
2576 scan args "%f %n"
2577 (fun f pos ->
2578 f, String.sub args pos (String.length args - pos))
2580 state.text <- text;
2581 state.progress <- progress;
2582 G.postRedisplay "progress"
2584 | "firstmatch" :: args :: [] ->
2585 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2586 scan args "%u %d %f %f %f %f %f %f %f %f"
2587 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2588 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2590 let y = (getpagey pageno) + truncate y0 in
2591 addnav ();
2592 gotoy y;
2593 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2595 | "match" :: args :: [] ->
2596 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2597 scan args "%u %d %f %f %f %f %f %f %f %f"
2598 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2599 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2601 state.rects1 <-
2602 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2604 | "page" :: args :: [] ->
2605 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2606 begin match state.currently with
2607 | Loading (l, gen) ->
2608 vlog "page %d took %f sec" l.pageno t;
2609 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2610 begin match state.throttle with
2611 | None ->
2612 let preloadedpages =
2613 if conf.preload
2614 then preloadlayout state.y
2615 else state.layout
2617 let evict () =
2618 let set =
2619 List.fold_left (fun s l -> IntSet.add l.pageno s)
2620 IntSet.empty preloadedpages
2622 let evictedpages =
2623 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2624 if not (IntSet.mem pageno set)
2625 then (
2626 wcmd "freepage %s" opaque;
2627 key :: accu
2629 else accu
2630 ) state.pagemap []
2632 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2634 evict ();
2635 state.currently <- Idle;
2636 if gen = state.gen
2637 then (
2638 tilepage l.pageno pageopaque state.layout;
2639 load state.layout;
2640 load preloadedpages;
2641 if pagevisible state.layout l.pageno
2642 && layoutready state.layout
2643 then G.postRedisplay "page";
2646 | Some (layout, _, _) ->
2647 state.currently <- Idle;
2648 tilepage l.pageno pageopaque layout;
2649 load state.layout
2650 end;
2652 | _ ->
2653 dolog "Inconsistent loading state";
2654 logcurrently state.currently;
2655 exit 1
2658 | "tile" :: args :: [] ->
2659 let (x, y, opaque, size, t) =
2660 scan args "%u %u %s %u %f"
2661 (fun x y p size t -> (x, y, p, size, t))
2663 begin match state.currently with
2664 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2665 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2667 unmappbo opaque;
2668 if tilew != conf.tilew || tileh != conf.tileh
2669 then (
2670 wcmd "freetile %s" opaque;
2671 state.currently <- Idle;
2672 load state.layout;
2674 else (
2675 puttileopaque l col row gen cs angle opaque size t;
2676 state.memused <- state.memused + size;
2677 state.uioh#infochanged Memused;
2678 gctiles ();
2679 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2680 opaque, size) state.tilelru;
2682 let layout =
2683 match state.throttle with
2684 | None -> state.layout
2685 | Some (layout, _, _) -> layout
2688 state.currently <- Idle;
2689 if gen = state.gen
2690 && conf.colorspace = cs
2691 && conf.angle = angle
2692 && tilevisible layout l.pageno x y
2693 then conttiling l.pageno pageopaque;
2695 begin match state.throttle with
2696 | None ->
2697 preload state.layout;
2698 if gen = state.gen
2699 && conf.colorspace = cs
2700 && conf.angle = angle
2701 && tilevisible state.layout l.pageno x y
2702 && (not !wtmode || layoutready state.layout)
2703 then G.postRedisplay "tile nothrottle";
2705 | Some (layout, y, _) ->
2706 let ready = layoutready layout in
2707 if ready
2708 then (
2709 state.y <- y;
2710 state.layout <- layout;
2711 state.throttle <- None;
2712 G.postRedisplay "throttle";
2714 else load layout;
2715 end;
2718 | _ ->
2719 dolog "Inconsistent tiling state";
2720 logcurrently state.currently;
2721 exit 1
2724 | "pdim" :: args :: [] ->
2725 let (n, w, h, _) as pdim =
2726 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2728 let pdim =
2729 match conf.fitmodel, conf.columns with
2730 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2731 | _ -> pdim
2733 state.uioh#infochanged Pdim;
2734 state.pdims <- pdim :: state.pdims
2736 | "o" :: args :: [] ->
2737 let (l, n, t, h, pos) =
2738 scan args "%u %u %d %u %n"
2739 (fun l n t h pos -> l, n, t, h, pos)
2741 let s = String.sub args pos (String.length args - pos) in
2742 let outline = (s, l, (n, float t /. float h, 0.0)) in
2743 begin match state.currently with
2744 | Outlining outlines ->
2745 state.currently <- Outlining (outline :: outlines)
2746 | Idle ->
2747 state.currently <- Outlining [outline]
2748 | currently ->
2749 dolog "invalid outlining state";
2750 logcurrently currently
2753 | "a" :: args :: [] ->
2754 let (n, l, t) =
2755 scan args "%u %d %d" (fun n l t -> n, l, t)
2757 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2759 | "info" :: args :: [] ->
2760 state.docinfo <- (1, args) :: state.docinfo
2762 | "infoend" :: [] ->
2763 state.uioh#infochanged Docinfo;
2764 state.docinfo <- List.rev state.docinfo
2766 | _ ->
2767 error "unknown cmd `%S'" cmds
2770 let onhist cb =
2771 let rc = cb.rc in
2772 let action = function
2773 | HCprev -> cbget cb ~-1
2774 | HCnext -> cbget cb 1
2775 | HCfirst -> cbget cb ~-(cb.rc)
2776 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2777 and cancel () = cb.rc <- rc
2778 in (action, cancel)
2781 let search pattern forward =
2782 match conf.columns with
2783 | Csplit _ ->
2784 showtext '!' "searching does not work properly in split columns mode"
2785 | _ ->
2786 if nonemptystr pattern
2787 then
2788 let pn, py =
2789 match state.layout with
2790 | [] -> 0, 0
2791 | l :: _ ->
2792 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2794 wcmd "search %d %d %d %d,%s\000"
2795 (btod conf.icase) pn py (btod forward) pattern;
2798 let intentry text key =
2799 let c =
2800 if key >= 32 && key < 127
2801 then Char.chr key
2802 else '\000'
2804 match c with
2805 | '0' .. '9' ->
2806 let text = addchar text c in
2807 TEcont text
2809 | _ ->
2810 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2811 TEcont text
2814 let linknentry text key =
2815 let c =
2816 if key >= 32 && key < 127
2817 then Char.chr key
2818 else '\000'
2820 match c with
2821 | 'a' .. 'z' ->
2822 let text = addchar text c in
2823 TEcont text
2825 | _ ->
2826 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2827 TEcont text
2830 let linkndone f s =
2831 if nonemptystr s
2832 then (
2833 let n =
2834 let l = String.length s in
2835 let rec loop pos n = if pos = l then n else
2836 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2837 loop (pos+1) (n*26 + m)
2838 in loop 0 0
2840 let rec loop n = function
2841 | [] -> ()
2842 | l :: rest ->
2843 match getopaque l.pageno with
2844 | None -> loop n rest
2845 | Some opaque ->
2846 let m = getlinkcount opaque in
2847 if n < m
2848 then (
2849 let under = getlink opaque n in
2850 f under
2852 else loop (n-m) rest
2854 loop n state.layout;
2858 let textentry text key =
2859 if key land 0xff00 = 0xff00
2860 then TEcont text
2861 else TEcont (text ^ toutf8 key)
2864 let reqlayout angle fitmodel =
2865 match state.throttle with
2866 | None ->
2867 if nogeomcmds state.geomcmds
2868 then state.anchor <- getanchor ();
2869 conf.angle <- angle mod 360;
2870 if conf.angle != 0
2871 then (
2872 match state.mode with
2873 | LinkNav _ -> state.mode <- View
2874 | _ -> ()
2876 conf.fitmodel <- fitmodel;
2877 invalidate "reqlayout"
2878 (fun () ->
2879 wcmd "reqlayout %d %d %d"
2880 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2882 | _ -> ()
2885 let settrim trimmargins trimfuzz =
2886 if nogeomcmds state.geomcmds
2887 then state.anchor <- getanchor ();
2888 conf.trimmargins <- trimmargins;
2889 conf.trimfuzz <- trimfuzz;
2890 let x0, y0, x1, y1 = trimfuzz in
2891 invalidate "settrim"
2892 (fun () ->
2893 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2894 flushpages ();
2897 let setzoom zoom =
2898 match state.throttle with
2899 | None ->
2900 let zoom = max 0.0001 zoom in
2901 if zoom <> conf.zoom
2902 then (
2903 state.prevzoom <- (conf.zoom, state.x);
2904 conf.zoom <- zoom;
2905 reshape state.winw state.winh;
2906 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2909 | Some (layout, y, started) ->
2910 let time =
2911 match conf.maxwait with
2912 | None -> 0.0
2913 | Some t -> t
2915 let dt = now () -. started in
2916 if dt > time
2917 then (
2918 state.y <- y;
2919 load layout;
2923 let setcolumns mode columns coverA coverB =
2924 state.prevcolumns <- Some (conf.columns, conf.zoom);
2925 if columns < 0
2926 then (
2927 if isbirdseye mode
2928 then showtext '!' "split mode doesn't work in bird's eye"
2929 else (
2930 conf.columns <- Csplit (-columns, [||]);
2931 state.x <- 0;
2932 conf.zoom <- 1.0;
2935 else (
2936 if columns < 2
2937 then (
2938 conf.columns <- Csingle [||];
2939 state.x <- 0;
2940 setzoom 1.0;
2942 else (
2943 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2944 conf.zoom <- 1.0;
2947 reshape state.winw state.winh;
2950 let enterbirdseye () =
2951 let zoom = float conf.thumbw /. float state.winw in
2952 let birdseyepageno =
2953 let cy = state.winh / 2 in
2954 let fold = function
2955 | [] -> 0
2956 | l :: rest ->
2957 let rec fold best = function
2958 | [] -> best.pageno
2959 | l :: rest ->
2960 let d = cy - (l.pagedispy + l.pagevh/2)
2961 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2962 if abs d < abs dbest
2963 then fold l rest
2964 else best.pageno
2965 in fold l rest
2967 fold state.layout
2969 state.mode <- Birdseye (
2970 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2972 conf.zoom <- zoom;
2973 conf.presentation <- false;
2974 conf.interpagespace <- 10;
2975 conf.hlinks <- false;
2976 conf.fitmodel <- FitProportional;
2977 state.x <- 0;
2978 state.mstate <- Mnone;
2979 conf.maxwait <- None;
2980 conf.columns <- (
2981 match conf.beyecolumns with
2982 | Some c ->
2983 conf.zoom <- 1.0;
2984 Cmulti ((c, 0, 0), [||])
2985 | None -> Csingle [||]
2987 Wsi.setcursor Wsi.CURSOR_INHERIT;
2988 if conf.verbose
2989 then
2990 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2991 (100.0*.zoom)
2992 else
2993 state.text <- ""
2995 reshape state.winw state.winh;
2998 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2999 state.mode <- View;
3000 conf.zoom <- c.zoom;
3001 conf.presentation <- c.presentation;
3002 conf.interpagespace <- c.interpagespace;
3003 conf.maxwait <- c.maxwait;
3004 conf.hlinks <- c.hlinks;
3005 conf.fitmodel <- c.fitmodel;
3006 conf.beyecolumns <- (
3007 match conf.columns with
3008 | Cmulti ((c, _, _), _) -> Some c
3009 | Csingle _ -> None
3010 | Csplit _ -> failwith "leaving bird's eye split mode"
3012 conf.columns <- (
3013 match c.columns with
3014 | Cmulti (c, _) -> Cmulti (c, [||])
3015 | Csingle _ -> Csingle [||]
3016 | Csplit (c, _) -> Csplit (c, [||])
3018 if conf.verbose
3019 then
3020 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
3021 (100.0*.conf.zoom)
3023 reshape state.winw state.winh;
3024 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
3025 state.x <- leftx;
3028 let togglebirdseye () =
3029 match state.mode with
3030 | Birdseye vals -> leavebirdseye vals true
3031 | View -> enterbirdseye ()
3032 | _ -> ()
3035 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3036 let pageno = max 0 (pageno - incr) in
3037 let rec loop = function
3038 | [] -> gotopage1 pageno 0
3039 | l :: _ when l.pageno = pageno ->
3040 if l.pagedispy >= 0 && l.pagey = 0
3041 then G.postRedisplay "upbirdseye"
3042 else gotopage1 pageno 0
3043 | _ :: rest -> loop rest
3045 loop state.layout;
3046 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
3049 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3050 let pageno = min (state.pagecount - 1) (pageno + incr) in
3051 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3052 let rec loop = function
3053 | [] ->
3054 let y, h = getpageyh pageno in
3055 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
3056 gotoy (clamp dy)
3057 | l :: _ when l.pageno = pageno ->
3058 if l.pagevh != l.pageh
3059 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
3060 else G.postRedisplay "downbirdseye"
3061 | _ :: rest -> loop rest
3063 loop state.layout
3066 let optentry mode _ key =
3067 let btos b = if b then "on" else "off" in
3068 if key >= 32 && key < 127
3069 then
3070 let c = Char.chr key in
3071 match c with
3072 | 's' ->
3073 let ondone s =
3074 try conf.scrollstep <- int_of_string s with exc ->
3075 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3077 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3079 | 'A' ->
3080 let ondone s =
3082 conf.autoscrollstep <- int_of_string s;
3083 if state.autoscroll <> None
3084 then state.autoscroll <- Some conf.autoscrollstep
3085 with exc ->
3086 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3088 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3090 | 'C' ->
3091 let ondone s =
3093 let n, a, b = multicolumns_of_string s in
3094 setcolumns mode n a b;
3095 with exc ->
3096 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3098 TEswitch ("columns: ", "", None, textentry, ondone, true)
3100 | 'Z' ->
3101 let ondone s =
3103 let zoom = float (int_of_string s) /. 100.0 in
3104 setzoom zoom
3105 with exc ->
3106 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3108 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3110 | 't' ->
3111 let ondone s =
3113 conf.thumbw <- bound (int_of_string s) 2 4096;
3114 state.text <-
3115 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3116 begin match mode with
3117 | Birdseye beye ->
3118 leavebirdseye beye false;
3119 enterbirdseye ();
3120 | _ -> ();
3122 with exc ->
3123 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3125 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3127 | 'R' ->
3128 let ondone s =
3129 match try
3130 Some (int_of_string s)
3131 with exc ->
3132 state.text <- Printf.sprintf "bad integer `%s': %s"
3133 s (exntos exc);
3134 None
3135 with
3136 | Some angle -> reqlayout angle conf.fitmodel
3137 | None -> ()
3139 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3141 | 'i' ->
3142 conf.icase <- not conf.icase;
3143 TEdone ("case insensitive search " ^ (btos conf.icase))
3145 | 'p' ->
3146 conf.preload <- not conf.preload;
3147 gotoy state.y;
3148 TEdone ("preload " ^ (btos conf.preload))
3150 | 'v' ->
3151 conf.verbose <- not conf.verbose;
3152 TEdone ("verbose " ^ (btos conf.verbose))
3154 | 'd' ->
3155 conf.debug <- not conf.debug;
3156 TEdone ("debug " ^ (btos conf.debug))
3158 | 'h' ->
3159 conf.maxhfit <- not conf.maxhfit;
3160 state.maxy <- calcheight ();
3161 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3163 | 'c' ->
3164 conf.crophack <- not conf.crophack;
3165 TEdone ("crophack " ^ btos conf.crophack)
3167 | 'a' ->
3168 let s =
3169 match conf.maxwait with
3170 | None ->
3171 conf.maxwait <- Some infinity;
3172 "always wait for page to complete"
3173 | Some _ ->
3174 conf.maxwait <- None;
3175 "show placeholder if page is not ready"
3177 TEdone s
3179 | 'f' ->
3180 conf.underinfo <- not conf.underinfo;
3181 TEdone ("underinfo " ^ btos conf.underinfo)
3183 | 'P' ->
3184 conf.savebmarks <- not conf.savebmarks;
3185 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3187 | 'S' ->
3188 let ondone s =
3190 let pageno, py =
3191 match state.layout with
3192 | [] -> 0, 0
3193 | l :: _ ->
3194 l.pageno, l.pagey
3196 conf.interpagespace <- int_of_string s;
3197 docolumns conf.columns;
3198 state.maxy <- calcheight ();
3199 let y = getpagey pageno in
3200 gotoy (y + py)
3201 with exc ->
3202 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3204 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3206 | 'l' ->
3207 let fm =
3208 match conf.fitmodel with
3209 | FitProportional -> FitWidth
3210 | _ -> FitProportional
3212 reqlayout conf.angle fm;
3213 TEdone ("proportional display " ^ btos (fm == FitProportional))
3215 | 'T' ->
3216 settrim (not conf.trimmargins) conf.trimfuzz;
3217 TEdone ("trim margins " ^ btos conf.trimmargins)
3219 | 'I' ->
3220 conf.invert <- not conf.invert;
3221 TEdone ("invert colors " ^ btos conf.invert)
3223 | 'x' ->
3224 let ondone s =
3225 cbput state.hists.sel s;
3226 conf.selcmd <- s;
3228 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3229 textentry, ondone, true)
3231 | 'M' ->
3232 if conf.pax == None
3233 then conf.pax <- Some (ref (0.0, 0, 0))
3234 else conf.pax <- None;
3235 TEdone ("PAX " ^ btos (conf.pax != None))
3237 | _ ->
3238 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3239 TEstop
3240 else
3241 TEcont state.text
3244 class type lvsource = object
3245 method getitemcount : int
3246 method getitem : int -> (string * int)
3247 method hasaction : int -> bool
3248 method exit :
3249 uioh:uioh ->
3250 cancel:bool ->
3251 active:int ->
3252 first:int ->
3253 pan:int ->
3254 qsearch:string ->
3255 uioh option
3256 method getactive : int
3257 method getfirst : int
3258 method getqsearch : string
3259 method setqsearch : string -> unit
3260 method getpan : int
3261 end;;
3263 class virtual lvsourcebase = object
3264 val mutable m_active = 0
3265 val mutable m_first = 0
3266 val mutable m_qsearch = ""
3267 val mutable m_pan = 0
3268 method getactive = m_active
3269 method getfirst = m_first
3270 method getqsearch = m_qsearch
3271 method getpan = m_pan
3272 method setqsearch s = m_qsearch <- s
3273 end;;
3275 let withoutlastutf8 s =
3276 let len = String.length s in
3277 if len = 0
3278 then s
3279 else
3280 let rec find pos =
3281 if pos = 0
3282 then pos
3283 else
3284 let b = Char.code s.[pos] in
3285 if b land 0b11000000 = 0b11000000
3286 then pos
3287 else find (pos-1)
3289 let first =
3290 if Char.code s.[len-1] land 0x80 = 0
3291 then len-1
3292 else find (len-1)
3294 String.sub s 0 first;
3297 let textentrykeyboard
3298 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3299 let key =
3300 if key >= 0xffb0 && key <= 0xffb9
3301 then key - 0xffb0 + 48 else key
3303 let enttext te =
3304 state.mode <- Textentry (te, onleave);
3305 state.text <- "";
3306 enttext ();
3307 G.postRedisplay "textentrykeyboard enttext";
3309 let histaction cmd =
3310 match opthist with
3311 | None -> ()
3312 | Some (action, _) ->
3313 state.mode <- Textentry (
3314 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3316 G.postRedisplay "textentry histaction"
3318 match key with
3319 | 0xff08 -> (* backspace *)
3320 if emptystr text && cancelonempty
3321 then (
3322 onleave Cancel;
3323 G.postRedisplay "textentrykeyboard after cancel";
3325 else
3326 let s = withoutlastutf8 text in
3327 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3329 | 0xff0d | 0xff8d -> (* (kp) enter *)
3330 ondone text;
3331 onleave Confirm;
3332 G.postRedisplay "textentrykeyboard after confirm"
3334 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3335 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3336 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3337 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3339 | 0xff1b -> (* escape*)
3340 if emptystr text
3341 then (
3342 begin match opthist with
3343 | None -> ()
3344 | Some (_, onhistcancel) -> onhistcancel ()
3345 end;
3346 onleave Cancel;
3347 state.text <- "";
3348 G.postRedisplay "textentrykeyboard after cancel2"
3350 else (
3351 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3354 | 0xff9f | 0xffff -> () (* delete *)
3356 | _ when key != 0
3357 && key land 0xff00 != 0xff00 (* keyboard *)
3358 && key land 0xfe00 != 0xfe00 (* xkb *)
3359 && key land 0xfd00 != 0xfd00 (* 3270 *)
3361 begin match onkey text key with
3362 | TEdone text ->
3363 ondone text;
3364 onleave Confirm;
3365 G.postRedisplay "textentrykeyboard after confirm2";
3367 | TEcont text ->
3368 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3370 | TEstop ->
3371 onleave Cancel;
3372 G.postRedisplay "textentrykeyboard after cancel3"
3374 | TEswitch te ->
3375 state.mode <- Textentry (te, onleave);
3376 G.postRedisplay "textentrykeyboard switch";
3377 end;
3379 | _ ->
3380 vlog "unhandled key %s" (Wsi.keyname key)
3383 let firstof first active =
3384 if first > active || abs (first - active) > fstate.maxrows - 1
3385 then max 0 (active - (fstate.maxrows/2))
3386 else first
3389 let calcfirst first active =
3390 if active > first
3391 then
3392 let rows = active - first in
3393 if rows > fstate.maxrows then active - fstate.maxrows else first
3394 else active
3397 let scrollph y maxy =
3398 let sh = float (maxy + state.winh) /. float state.winh in
3399 let sh = float state.winh /. sh in
3400 let sh = max sh (float conf.scrollh) in
3402 let percent = float y /. float maxy in
3403 let position = (float state.winh -. sh) *. percent in
3405 let position =
3406 if position +. sh > float state.winh
3407 then float state.winh -. sh
3408 else position
3410 position, sh;
3413 let coe s = (s :> uioh);;
3415 class listview ~(source:lvsource) ~trusted ~modehash =
3416 object (self)
3417 val m_pan = source#getpan
3418 val m_first = source#getfirst
3419 val m_active = source#getactive
3420 val m_qsearch = source#getqsearch
3421 val m_prev_uioh = state.uioh
3423 method private elemunder y =
3424 let n = y / (fstate.fontsize+1) in
3425 if m_first + n < source#getitemcount
3426 then (
3427 if source#hasaction (m_first + n)
3428 then Some (m_first + n)
3429 else None
3431 else None
3433 method display =
3434 Gl.enable `blend;
3435 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3436 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3437 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3438 GlDraw.color (1., 1., 1.);
3439 Gl.enable `texture_2d;
3440 let fs = fstate.fontsize in
3441 let nfs = fs + 1 in
3442 let ww = fstate.wwidth in
3443 let tabw = 30.0*.ww in
3444 let itemcount = source#getitemcount in
3445 let rec loop row =
3446 if (row - m_first) > fstate.maxrows
3447 then ()
3448 else (
3449 if row >= 0 && row < itemcount
3450 then (
3451 let (s, level) = source#getitem row in
3452 let y = (row - m_first) * nfs in
3453 let x = 5.0 +. float (level + m_pan) *. ww in
3454 if row = m_active
3455 then (
3456 Gl.disable `texture_2d;
3457 GlDraw.polygon_mode `both `line;
3458 let alpha = if source#hasaction row then 0.9 else 0.3 in
3459 GlDraw.color (1., 1., 1.) ~alpha;
3460 GlDraw.rect (1., float (y + 1))
3461 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3462 GlDraw.polygon_mode `both `fill;
3463 GlDraw.color (1., 1., 1.);
3464 Gl.enable `texture_2d;
3467 let drawtabularstring s =
3468 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3469 if trusted
3470 then
3471 let tabpos = try String.index s '\t' with Not_found -> -1 in
3472 if tabpos > 0
3473 then
3474 let len = String.length s - tabpos - 1 in
3475 let s1 = String.sub s 0 tabpos
3476 and s2 = String.sub s (tabpos + 1) len in
3477 let nx = drawstr x s1 in
3478 let sw = nx -. x in
3479 let x = x +. (max tabw sw) in
3480 drawstr x s2
3481 else
3482 drawstr x s
3483 else
3484 drawstr x s
3486 let _ = drawtabularstring s in
3487 loop (row+1)
3491 loop m_first;
3492 Gl.disable `blend;
3493 Gl.disable `texture_2d;
3495 method updownlevel incr =
3496 let len = source#getitemcount in
3497 let curlevel =
3498 if m_active >= 0 && m_active < len
3499 then snd (source#getitem m_active)
3500 else -1
3502 let rec flow i =
3503 if i = len then i-1 else if i = -1 then 0 else
3504 let _, l = source#getitem i in
3505 if l != curlevel then i else flow (i+incr)
3507 let active = flow m_active in
3508 let first = calcfirst m_first active in
3509 G.postRedisplay "outline updownlevel";
3510 {< m_active = active; m_first = first >}
3512 method private key1 key mask =
3513 let set1 active first qsearch =
3514 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3516 let search active pattern incr =
3517 let active = if active = -1 then m_first else active in
3518 let dosearch re =
3519 let rec loop n =
3520 if n >= 0 && n < source#getitemcount
3521 then (
3522 let s, _ = source#getitem n in
3524 (try ignore (Str.search_forward re s 0); true
3525 with Not_found -> false)
3526 then Some n
3527 else loop (n + incr)
3529 else None
3531 loop active
3534 let re = Str.regexp_case_fold pattern in
3535 dosearch re
3536 with Failure s ->
3537 state.text <- s;
3538 None
3540 let itemcount = source#getitemcount in
3541 let find start incr =
3542 let rec find i =
3543 if i = -1 || i = itemcount
3544 then -1
3545 else (
3546 if source#hasaction i
3547 then i
3548 else find (i + incr)
3551 find start
3553 let set active first =
3554 let first = bound first 0 (itemcount - fstate.maxrows) in
3555 state.text <- "";
3556 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3558 let navigate incr =
3559 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3560 let active, first =
3561 let incr1 = if incr > 0 then 1 else -1 in
3562 if isvisible m_first m_active
3563 then
3564 let next =
3565 let next = m_active + incr in
3566 let next =
3567 if next < 0 || next >= itemcount
3568 then -1
3569 else find next incr1
3571 if abs (m_active - next) > fstate.maxrows
3572 then -1
3573 else next
3575 if next = -1
3576 then
3577 let first = m_first + incr in
3578 let first = bound first 0 (itemcount - fstate.maxrows) in
3579 let next =
3580 let next = m_active + incr in
3581 let next = bound next 0 (itemcount - 1) in
3582 find next ~-incr1
3584 let active =
3585 if next = -1
3586 then m_active
3587 else (
3588 if isvisible first next
3589 then next
3590 else m_active
3593 active, first
3594 else
3595 let first = min next m_first in
3596 let first =
3597 if abs (next - first) > fstate.maxrows
3598 then first + incr
3599 else first
3601 next, first
3602 else
3603 let first = m_first + incr in
3604 let first = bound first 0 (itemcount - 1) in
3605 let active =
3606 let next = m_active + incr in
3607 let next = bound next 0 (itemcount - 1) in
3608 let next = find next incr1 in
3609 let active =
3610 if next = -1 || abs (m_active - first) > fstate.maxrows
3611 then (
3612 let active = if m_active = -1 then next else m_active in
3613 active
3615 else next
3617 if isvisible first active
3618 then active
3619 else -1
3621 active, first
3623 G.postRedisplay "listview navigate";
3624 set active first;
3626 match key with
3627 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3628 let incr = if key = 0x72 then -1 else 1 in
3629 let active, first =
3630 match search (m_active + incr) m_qsearch incr with
3631 | None ->
3632 state.text <- m_qsearch ^ " [not found]";
3633 m_active, m_first
3634 | Some active ->
3635 state.text <- m_qsearch;
3636 active, firstof m_first active
3638 G.postRedisplay "listview ctrl-r/s";
3639 set1 active first m_qsearch;
3641 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3642 if m_active >= 0 && m_active < source#getitemcount
3643 then (
3644 let s, _ = source#getitem m_active in
3645 selstring s;
3647 coe self
3649 | 0xff08 -> (* backspace *)
3650 if emptystr m_qsearch
3651 then coe self
3652 else (
3653 let qsearch = withoutlastutf8 m_qsearch in
3654 if emptystr qsearch
3655 then (
3656 state.text <- "";
3657 G.postRedisplay "listview empty qsearch";
3658 set1 m_active m_first "";
3660 else
3661 let active, first =
3662 match search m_active qsearch ~-1 with
3663 | None ->
3664 state.text <- qsearch ^ " [not found]";
3665 m_active, m_first
3666 | Some active ->
3667 state.text <- qsearch;
3668 active, firstof m_first active
3670 G.postRedisplay "listview backspace qsearch";
3671 set1 active first qsearch
3674 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3675 let pattern = m_qsearch ^ toutf8 key in
3676 let active, first =
3677 match search m_active pattern 1 with
3678 | None ->
3679 state.text <- pattern ^ " [not found]";
3680 m_active, m_first
3681 | Some active ->
3682 state.text <- pattern;
3683 active, firstof m_first active
3685 G.postRedisplay "listview qsearch add";
3686 set1 active first pattern;
3688 | 0xff1b -> (* escape *)
3689 state.text <- "";
3690 if emptystr m_qsearch
3691 then (
3692 G.postRedisplay "list view escape";
3693 begin
3694 match
3695 source#exit (coe self) true m_active m_first m_pan m_qsearch
3696 with
3697 | None -> m_prev_uioh
3698 | Some uioh -> uioh
3701 else (
3702 G.postRedisplay "list view kill qsearch";
3703 source#setqsearch "";
3704 coe {< m_qsearch = "" >}
3707 | 0xff0d | 0xff8d -> (* (kp) enter *)
3708 state.text <- "";
3709 let self = {< m_qsearch = "" >} in
3710 source#setqsearch "";
3711 let opt =
3712 G.postRedisplay "listview enter";
3713 if m_active >= 0 && m_active < source#getitemcount
3714 then (
3715 source#exit (coe self) false m_active m_first m_pan "";
3717 else (
3718 source#exit (coe self) true m_active m_first m_pan "";
3721 begin match opt with
3722 | None -> m_prev_uioh
3723 | Some uioh -> uioh
3726 | 0xff9f | 0xffff -> (* (kp) delete *)
3727 coe self
3729 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3730 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3731 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3732 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3734 | 0xff53 | 0xff98 -> (* (kp) right *)
3735 state.text <- "";
3736 G.postRedisplay "listview right";
3737 coe {< m_pan = m_pan - 1 >}
3739 | 0xff51 | 0xff96 -> (* (kp) left *)
3740 state.text <- "";
3741 G.postRedisplay "listview left";
3742 coe {< m_pan = m_pan + 1 >}
3744 | 0xff50 | 0xff95 -> (* (kp) home *)
3745 let active = find 0 1 in
3746 G.postRedisplay "listview home";
3747 set active 0;
3749 | 0xff57 | 0xff9c -> (* (kp) end *)
3750 let first = max 0 (itemcount - fstate.maxrows) in
3751 let active = find (itemcount - 1) ~-1 in
3752 G.postRedisplay "listview end";
3753 set active first;
3755 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3756 coe self
3758 | _ ->
3759 dolog "listview unknown key %#x" key; coe self
3761 method key key mask =
3762 match state.mode with
3763 | Textentry te -> textentrykeyboard key mask te; coe self
3764 | _ -> self#key1 key mask
3766 method button button down x y _ =
3767 let opt =
3768 match button with
3769 | 1 when x > state.winw - conf.scrollbw ->
3770 G.postRedisplay "listview scroll";
3771 if down
3772 then
3773 let _, position, sh = self#scrollph in
3774 if y > truncate position && y < truncate (position +. sh)
3775 then (
3776 state.mstate <- Mscrolly;
3777 Some (coe self)
3779 else
3780 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3781 let first = truncate (s *. float source#getitemcount) in
3782 let first = min source#getitemcount first in
3783 Some (coe {< m_first = first; m_active = first >})
3784 else (
3785 state.mstate <- Mnone;
3786 Some (coe self);
3788 | 1 when not down ->
3789 begin match self#elemunder y with
3790 | Some n ->
3791 G.postRedisplay "listview click";
3792 source#exit
3793 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3794 | _ ->
3795 Some (coe self)
3797 | n when (n == 4 || n == 5) && not down ->
3798 let len = source#getitemcount in
3799 let first =
3800 if n = 5 && m_first + fstate.maxrows >= len
3801 then
3802 m_first
3803 else
3804 let first = m_first + (if n == 4 then -1 else 1) in
3805 bound first 0 (len - 1)
3807 G.postRedisplay "listview wheel";
3808 Some (coe {< m_first = first >})
3809 | n when (n = 6 || n = 7) && not down ->
3810 let inc = if n = 7 then -1 else 1 in
3811 G.postRedisplay "listview hwheel";
3812 Some (coe {< m_pan = m_pan + inc >})
3813 | _ ->
3814 Some (coe self)
3816 match opt with
3817 | None -> m_prev_uioh
3818 | Some uioh -> uioh
3820 method motion _ y =
3821 match state.mstate with
3822 | Mscrolly ->
3823 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3824 let first = truncate (s *. float source#getitemcount) in
3825 let first = min source#getitemcount first in
3826 G.postRedisplay "listview motion";
3827 coe {< m_first = first; m_active = first >}
3828 | _ -> coe self
3830 method pmotion x y =
3831 if x < state.winw - conf.scrollbw
3832 then
3833 let n =
3834 match self#elemunder y with
3835 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3836 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3838 let o =
3839 if n != m_active
3840 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3841 else self
3843 coe o
3844 else (
3845 Wsi.setcursor Wsi.CURSOR_INHERIT;
3846 coe self
3849 method infochanged _ = ()
3851 method scrollpw = (0, 0.0, 0.0)
3852 method scrollph =
3853 let nfs = fstate.fontsize + 1 in
3854 let y = m_first * nfs in
3855 let itemcount = source#getitemcount in
3856 let maxi = max 0 (itemcount - fstate.maxrows) in
3857 let maxy = maxi * nfs in
3858 let p, h = scrollph y maxy in
3859 conf.scrollbw, p, h
3861 method modehash = modehash
3862 method eformsgs = false
3863 end;;
3865 class outlinelistview ~source =
3866 let settext autonarrow s =
3867 if autonarrow
3868 then state.text <- "[" ^ s ^ "]"
3869 else state.text <- s
3871 object (self)
3872 inherit listview
3873 ~source:(source :> lvsource)
3874 ~trusted:false
3875 ~modehash:(findkeyhash conf "outline")
3876 as super
3878 val m_autonarrow = false
3880 method key key mask =
3881 let maxrows =
3882 if emptystr state.text
3883 then fstate.maxrows
3884 else fstate.maxrows - 2
3886 let calcfirst first active =
3887 if active > first
3888 then
3889 let rows = active - first in
3890 if rows > maxrows then active - maxrows else first
3891 else active
3893 let navigate incr =
3894 let active = m_active + incr in
3895 let active = bound active 0 (source#getitemcount - 1) in
3896 let first = calcfirst m_first active in
3897 G.postRedisplay "outline navigate";
3898 coe {< m_active = active; m_first = first >}
3900 let navscroll first =
3901 let active =
3902 let dist = m_active - first in
3903 if dist < 0
3904 then first
3905 else (
3906 if dist < maxrows
3907 then m_active
3908 else first + maxrows
3911 G.postRedisplay "outline navscroll";
3912 coe {< m_first = first; m_active = active >}
3914 let ctrl = Wsi.withctrl mask in
3915 match key with
3916 | 97 when ctrl -> (* ctrl-a *)
3917 if m_autonarrow
3918 then source#denarrow
3919 else source#narrow m_qsearch;
3920 settext (not m_autonarrow) m_qsearch;
3921 G.postRedisplay "toggle auto narrowing";
3922 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3924 | 110 when ctrl -> (* ctrl-n *)
3925 source#narrow m_qsearch;
3926 if not m_autonarrow
3927 then source#add_narrow_pattern m_qsearch;
3928 G.postRedisplay "outline ctrl-n";
3929 coe {< m_first = 0; m_active = 0 >}
3931 | 117 when ctrl -> (* ctrl-u *)
3932 source#del_narrow_pattern;
3933 let pattern = source#renarrow in
3934 G.postRedisplay "outline ctrl-u";
3935 let text =
3936 if emptystr pattern then "" else "Narrowed to " ^ pattern
3938 settext m_autonarrow text;
3939 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
3941 | 108 when ctrl -> (* ctrl-l *)
3942 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3943 G.postRedisplay "outline ctrl-l";
3944 coe {< m_first = first >}
3946 | 0xff1b -> (* escape *)
3947 let o = super#key key mask in
3948 if m_autonarrow
3949 then (
3950 if nonemptystr m_qsearch
3951 then (
3952 source#add_narrow_pattern m_qsearch;
3953 settext true "";
3958 | 0xff0d | 0xff8d when m_autonarrow -> (* (kp) enter *)
3959 if nonemptystr m_qsearch
3960 then source#add_narrow_pattern m_qsearch;
3961 super#key key mask
3963 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3964 let pattern = m_qsearch ^ toutf8 key in
3965 G.postRedisplay "outlinelistview autonarrow add";
3966 source#narrow pattern;
3967 settext true pattern;
3968 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3970 | key when m_autonarrow && key = 0xff08 -> (* backspace *)
3971 if emptystr m_qsearch
3972 then coe self
3973 else
3974 let pattern = withoutlastutf8 m_qsearch in
3975 G.postRedisplay "outlinelistview autonarrow backspace";
3976 ignore (source#renarrow);
3977 source#narrow pattern;
3978 settext true pattern;
3979 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3981 | 0xff9f | 0xffff -> (* (kp) delete *)
3982 source#remove m_active;
3983 G.postRedisplay "outline delete";
3984 let active = max 0 (m_active-1) in
3985 coe {< m_first = firstof m_first active;
3986 m_active = active >}
3988 | 0xff52 | 0xff97 when ctrl -> (* ctrl-(kp) up *)
3989 navscroll (max 0 (m_first - 1))
3991 | 0xff54 | 0xff99 when ctrl -> (* ctrl-(kp) down *)
3992 navscroll (min (source#getitemcount - 1) (m_first + 1))
3994 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3995 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3996 | 0xff55 | 0xff9a -> (* (kp) prior *)
3997 navigate ~-(fstate.maxrows)
3998 | 0xff56 | 0xff9b -> (* (kp) next *)
3999 navigate fstate.maxrows
4001 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
4002 let o =
4003 if ctrl
4004 then (
4005 G.postRedisplay "outline ctrl right";
4006 {< m_pan = m_pan + 1 >}
4008 else self#updownlevel 1
4010 coe o
4012 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
4013 let o =
4014 if ctrl
4015 then (
4016 G.postRedisplay "outline ctrl left";
4017 {< m_pan = m_pan - 1 >}
4019 else self#updownlevel ~-1
4021 coe o
4023 | 0xff50 | 0xff95 -> (* (kp) home *)
4024 G.postRedisplay "outline home";
4025 coe {< m_first = 0; m_active = 0 >}
4027 | 0xff57 | 0xff9c -> (* (kp) end *)
4028 let active = source#getitemcount - 1 in
4029 let first = max 0 (active - fstate.maxrows) in
4030 G.postRedisplay "outline end";
4031 coe {< m_active = active; m_first = first >}
4033 | _ -> super#key key mask
4036 let outlinesource usebookmarks =
4037 let empty = [||] in
4038 (object (self)
4039 inherit lvsourcebase
4040 val mutable m_items = empty
4041 val mutable m_orig_items = empty
4042 val mutable m_narrow_patterns = []
4043 val mutable m_hadremovals = false
4045 method getitemcount =
4046 Array.length m_items + (if m_hadremovals then 1 else 0)
4048 method getitem n =
4049 if n == Array.length m_items && m_hadremovals
4050 then
4051 ("[Confirm removal]", 0)
4052 else
4053 let s, n, _ = m_items.(n) in
4054 (s, n)
4056 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4057 ignore (uioh, first, qsearch);
4058 let confrimremoval = m_hadremovals && active = Array.length m_items in
4059 let items =
4060 if m_narrow_patterns = []
4061 then m_orig_items
4062 else m_items
4064 if not cancel
4065 then (
4066 if not confrimremoval
4067 then (
4068 let _, _, ((pageno, y, _) as anchor) = m_items.(active) in
4069 let y = getanchory
4070 (if conf.presentation then (pageno, y, 1.0) else anchor)
4072 gotoghyll y;
4073 m_items <- items;
4075 else (
4076 state.bookmarks <- Array.to_list m_items;
4077 m_orig_items <- m_items;
4080 else m_items <- items;
4081 m_pan <- pan;
4082 None
4084 method hasaction _ = true
4086 method greetmsg =
4087 if Array.length m_items != Array.length m_orig_items
4088 then
4089 let s =
4090 match m_narrow_patterns with
4091 | one :: [] -> one
4092 | many -> String.concat " --> " (List.rev many)
4094 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4095 else ""
4097 method narrow pattern =
4098 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4099 match reopt with
4100 | None -> ()
4101 | Some re ->
4102 let rec loop accu n =
4103 if n = -1
4104 then m_items <- Array.of_list accu
4105 else
4106 let (s, _, _) as o = m_items.(n) in
4107 let accu =
4108 if (try ignore (Str.search_forward re s 0); true
4109 with Not_found -> false)
4110 then o :: accu
4111 else accu
4113 loop accu (n-1)
4115 loop [] (Array.length m_items - 1)
4117 method denarrow =
4118 m_orig_items <- (
4119 if usebookmarks
4120 then Array.of_list state.bookmarks
4121 else state.outlines
4123 m_items <- m_orig_items
4125 method remove m =
4126 if usebookmarks
4127 then
4128 if m >= 0 && m < Array.length m_items
4129 then (
4130 m_hadremovals <- true;
4131 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4132 let n = if n >= m then n+1 else n in
4133 m_items.(n)
4137 method add_narrow_pattern pattern =
4138 m_narrow_patterns <- pattern :: m_narrow_patterns
4140 method del_narrow_pattern =
4141 match m_narrow_patterns with
4142 | _ :: rest -> m_narrow_patterns <- rest
4143 | [] -> ()
4145 method renarrow =
4146 self#denarrow;
4147 match m_narrow_patterns with
4148 | pattern :: [] -> self#narrow pattern; pattern
4149 | list ->
4150 List.fold_left (fun accu pattern ->
4151 self#narrow pattern;
4152 accu ^ " --> " ^ pattern) "" list
4154 method reset anchor items =
4155 m_hadremovals <- false;
4156 if m_orig_items == empty
4157 then (
4158 m_orig_items <- items;
4159 if m_narrow_patterns == []
4160 then m_items <- items;
4162 let rely = getanchory anchor in
4163 let active =
4164 let rec loop n best bestd =
4165 if n = Array.length m_items
4166 then best
4167 else
4168 let (_, _, anchor) = m_items.(n) in
4169 let orely = getanchory anchor in
4170 let d = abs (orely - rely) in
4171 if d < bestd
4172 then loop (n+1) n d
4173 else loop (n+1) best bestd
4175 loop 0 ~-1 max_int
4177 m_active <- active;
4178 m_first <- firstof m_first active
4179 end)
4182 let enterselector usebookmarks =
4183 let source = outlinesource usebookmarks in
4184 fun errmsg ->
4185 let outlines =
4186 if usebookmarks
4187 then Array.of_list state.bookmarks
4188 else state.outlines
4190 if Array.length outlines = 0
4191 then (
4192 showtext ' ' errmsg;
4194 else (
4195 state.text <- source#greetmsg;
4196 Wsi.setcursor Wsi.CURSOR_INHERIT;
4197 let anchor = getanchor () in
4198 source#reset anchor outlines;
4199 state.uioh <- coe (new outlinelistview ~source);
4200 G.postRedisplay "enter selector";
4204 let enteroutlinemode =
4205 let f = enterselector false in
4206 fun ()-> f "Document has no outline";
4209 let enterbookmarkmode =
4210 let f = enterselector true in
4211 fun () -> f "Document has no bookmarks (yet)";
4214 let color_of_string s =
4215 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4216 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4220 let color_to_string (r, g, b) =
4221 let r = truncate (r *. 256.0)
4222 and g = truncate (g *. 256.0)
4223 and b = truncate (b *. 256.0) in
4224 Printf.sprintf "%d/%d/%d" r g b
4227 let irect_of_string s =
4228 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4231 let irect_to_string (x0,y0,x1,y1) =
4232 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4235 let makecheckers () =
4236 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4237 following to say:
4238 converted by Issac Trotts. July 25, 2002 *)
4239 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4240 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4241 let id = GlTex.gen_texture () in
4242 GlTex.bind_texture `texture_2d id;
4243 GlPix.store (`unpack_alignment 1);
4244 GlTex.image2d image;
4245 List.iter (GlTex.parameter ~target:`texture_2d)
4246 [ `mag_filter `nearest; `min_filter `nearest ];
4250 let setcheckers enabled =
4251 match state.texid with
4252 | None ->
4253 if enabled then state.texid <- Some (makecheckers ())
4255 | Some texid ->
4256 if not enabled
4257 then (
4258 GlTex.delete_texture texid;
4259 state.texid <- None;
4263 let int_of_string_with_suffix s =
4264 let l = String.length s in
4265 let s1, shift =
4266 if l > 1
4267 then
4268 let suffix = Char.lowercase s.[l-1] in
4269 match suffix with
4270 | 'k' -> String.sub s 0 (l-1), 10
4271 | 'm' -> String.sub s 0 (l-1), 20
4272 | 'g' -> String.sub s 0 (l-1), 30
4273 | _ -> s, 0
4274 else s, 0
4276 let n = int_of_string s1 in
4277 let m = n lsl shift in
4278 if m < 0 || m < n
4279 then raise (Failure "value too large")
4280 else m
4283 let string_with_suffix_of_int n =
4284 if n = 0
4285 then "0"
4286 else
4287 let units = [(30, "G"); (20, "M"); (10, "K")] in
4288 let prettyint n =
4289 let rec loop s n =
4290 let h = n mod 1000 in
4291 let n = n / 1000 in
4292 if n = 0
4293 then string_of_int h ^ s
4294 else (
4295 let s = Printf.sprintf "_%03d%s" h s in
4296 loop s n
4299 loop "" n
4301 let rec find = function
4302 | [] -> prettyint n
4303 | (shift, suffix) :: rest ->
4304 if (n land ((1 lsl shift) - 1)) = 0
4305 then prettyint (n lsr shift) ^ suffix
4306 else find rest
4308 find units
4311 let defghyllscroll = (40, 8, 32);;
4312 let ghyllscroll_of_string s =
4313 let (n, a, b) as nab =
4314 if s = "default"
4315 then defghyllscroll
4316 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4318 if n <= a || n <= b || a >= b
4319 then error "invalid ghyll N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)"
4320 n a b;
4321 nab;
4324 let ghyllscroll_to_string ((n, a, b) as nab) =
4325 if nab = defghyllscroll
4326 then "default"
4327 else Printf.sprintf "%d,%d,%d" n a b;
4330 let describe_location () =
4331 let fn = page_of_y state.y in
4332 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4333 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4334 let percent =
4335 if maxy <= 0
4336 then 100.
4337 else (100. *. (float state.y /. float maxy))
4339 if fn = ln
4340 then
4341 Printf.sprintf "page %d of %d [%.2f%%]"
4342 (fn+1) state.pagecount percent
4343 else
4344 Printf.sprintf
4345 "pages %d-%d of %d [%.2f%%]"
4346 (fn+1) (ln+1) state.pagecount percent
4349 let setpresentationmode v =
4350 let n = page_of_y state.y in
4351 state.anchor <- (n, 0.0, 1.0);
4352 conf.presentation <- v;
4353 if conf.fitmodel = FitPage
4354 then reqlayout conf.angle conf.fitmodel;
4355 represent ();
4358 let enterinfomode =
4359 let btos b = if b then "\xe2\x88\x9a" else "" in
4360 let showextended = ref false in
4361 let leave mode = function
4362 | Confirm -> state.mode <- mode
4363 | Cancel -> state.mode <- mode in
4364 let src =
4365 (object
4366 val mutable m_first_time = true
4367 val mutable m_l = []
4368 val mutable m_a = [||]
4369 val mutable m_prev_uioh = nouioh
4370 val mutable m_prev_mode = View
4372 inherit lvsourcebase
4374 method reset prev_mode prev_uioh =
4375 m_a <- Array.of_list (List.rev m_l);
4376 m_l <- [];
4377 m_prev_mode <- prev_mode;
4378 m_prev_uioh <- prev_uioh;
4379 if m_first_time
4380 then (
4381 let rec loop n =
4382 if n >= Array.length m_a
4383 then ()
4384 else
4385 match m_a.(n) with
4386 | _, _, _, Action _ -> m_active <- n
4387 | _ -> loop (n+1)
4389 loop 0;
4390 m_first_time <- false;
4393 method int name get set =
4394 m_l <-
4395 (name, `int get, 1, Action (
4396 fun u ->
4397 let ondone s =
4398 try set (int_of_string s)
4399 with exn ->
4400 state.text <- Printf.sprintf "bad integer `%s': %s"
4401 s (exntos exn)
4403 state.text <- "";
4404 let te = name ^ ": ", "", None, intentry, ondone, true in
4405 state.mode <- Textentry (te, leave m_prev_mode);
4407 )) :: m_l
4409 method int_with_suffix name get set =
4410 m_l <-
4411 (name, `intws get, 1, Action (
4412 fun u ->
4413 let ondone s =
4414 try set (int_of_string_with_suffix s)
4415 with exn ->
4416 state.text <- Printf.sprintf "bad integer `%s': %s"
4417 s (exntos exn)
4419 state.text <- "";
4420 let te =
4421 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4423 state.mode <- Textentry (te, leave m_prev_mode);
4425 )) :: m_l
4427 method bool ?(offset=1) ?(btos=btos) name get set =
4428 m_l <-
4429 (name, `bool (btos, get), offset, Action (
4430 fun u ->
4431 let v = get () in
4432 set (not v);
4434 )) :: m_l
4436 method color name get set =
4437 m_l <-
4438 (name, `color get, 1, Action (
4439 fun u ->
4440 let invalid = (nan, nan, nan) in
4441 let ondone s =
4442 let c =
4443 try color_of_string s
4444 with exn ->
4445 state.text <- Printf.sprintf "bad color `%s': %s"
4446 s (exntos exn);
4447 invalid
4449 if c <> invalid
4450 then set c;
4452 let te = name ^ ": ", "", None, textentry, ondone, true in
4453 state.text <- color_to_string (get ());
4454 state.mode <- Textentry (te, leave m_prev_mode);
4456 )) :: m_l
4458 method string name get set =
4459 m_l <-
4460 (name, `string get, 1, Action (
4461 fun u ->
4462 let ondone s = set s in
4463 let te = name ^ ": ", "", None, textentry, ondone, true in
4464 state.mode <- Textentry (te, leave m_prev_mode);
4466 )) :: m_l
4468 method colorspace name get set =
4469 m_l <-
4470 (name, `string get, 1, Action (
4471 fun _ ->
4472 let source =
4473 (object
4474 inherit lvsourcebase
4476 initializer
4477 m_active <- CSTE.to_int conf.colorspace;
4478 m_first <- 0;
4480 method getitemcount =
4481 Array.length CSTE.names
4482 method getitem n =
4483 (CSTE.names.(n), 0)
4484 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4485 ignore (uioh, first, pan, qsearch);
4486 if not cancel then set active;
4487 None
4488 method hasaction _ = true
4489 end)
4491 state.text <- "";
4492 let modehash = findkeyhash conf "info" in
4493 coe (new listview ~source ~trusted:true ~modehash)
4494 )) :: m_l
4496 method paxmark name get set =
4497 m_l <-
4498 (name, `string get, 1, Action (
4499 fun _ ->
4500 let source =
4501 (object
4502 inherit lvsourcebase
4504 initializer
4505 m_active <- MTE.to_int conf.paxmark;
4506 m_first <- 0;
4508 method getitemcount = Array.length MTE.names
4509 method getitem n = (MTE.names.(n), 0)
4510 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4511 ignore (uioh, first, pan, qsearch);
4512 if not cancel then set active;
4513 None
4514 method hasaction _ = true
4515 end)
4517 state.text <- "";
4518 let modehash = findkeyhash conf "info" in
4519 coe (new listview ~source ~trusted:true ~modehash)
4520 )) :: m_l
4522 method fitmodel name get set =
4523 m_l <-
4524 (name, `string get, 1, Action (
4525 fun _ ->
4526 let source =
4527 (object
4528 inherit lvsourcebase
4530 initializer
4531 m_active <- FMTE.to_int conf.fitmodel;
4532 m_first <- 0;
4534 method getitemcount = Array.length FMTE.names
4535 method getitem n = (FMTE.names.(n), 0)
4536 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4537 ignore (uioh, first, pan, qsearch);
4538 if not cancel then set active;
4539 None
4540 method hasaction _ = true
4541 end)
4543 state.text <- "";
4544 let modehash = findkeyhash conf "info" in
4545 coe (new listview ~source ~trusted:true ~modehash)
4546 )) :: m_l
4548 method caption s offset =
4549 m_l <- (s, `empty, offset, Noaction) :: m_l
4551 method caption2 s f offset =
4552 m_l <- (s, `string f, offset, Noaction) :: m_l
4554 method getitemcount = Array.length m_a
4556 method getitem n =
4557 let tostr = function
4558 | `int f -> string_of_int (f ())
4559 | `intws f -> string_with_suffix_of_int (f ())
4560 | `string f -> f ()
4561 | `color f -> color_to_string (f ())
4562 | `bool (btos, f) -> btos (f ())
4563 | `empty -> ""
4565 let name, t, offset, _ = m_a.(n) in
4566 ((let s = tostr t in
4567 if nonemptystr s
4568 then Printf.sprintf "%s\t%s" name s
4569 else name),
4570 offset)
4572 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4573 let uiohopt =
4574 if not cancel
4575 then (
4576 m_qsearch <- qsearch;
4577 let uioh =
4578 match m_a.(active) with
4579 | _, _, _, Action f -> f uioh
4580 | _ -> uioh
4582 Some uioh
4584 else None
4586 m_active <- active;
4587 m_first <- first;
4588 m_pan <- pan;
4589 uiohopt
4591 method hasaction n =
4592 match m_a.(n) with
4593 | _, _, _, Action _ -> true
4594 | _ -> false
4595 end)
4597 let rec fillsrc prevmode prevuioh =
4598 let sep () = src#caption "" 0 in
4599 let colorp name get set =
4600 src#string name
4601 (fun () -> color_to_string (get ()))
4602 (fun v ->
4604 let c = color_of_string v in
4605 set c
4606 with exn ->
4607 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4610 let oldmode = state.mode in
4611 let birdseye = isbirdseye state.mode in
4613 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4615 src#bool "presentation mode"
4616 (fun () -> conf.presentation)
4617 (fun v -> setpresentationmode v);
4619 src#bool "ignore case in searches"
4620 (fun () -> conf.icase)
4621 (fun v -> conf.icase <- v);
4623 src#bool "preload"
4624 (fun () -> conf.preload)
4625 (fun v -> conf.preload <- v);
4627 src#bool "highlight links"
4628 (fun () -> conf.hlinks)
4629 (fun v -> conf.hlinks <- v);
4631 src#bool "under info"
4632 (fun () -> conf.underinfo)
4633 (fun v -> conf.underinfo <- v);
4635 src#bool "persistent bookmarks"
4636 (fun () -> conf.savebmarks)
4637 (fun v -> conf.savebmarks <- v);
4639 src#fitmodel "fit model"
4640 (fun () -> FMTE.to_string conf.fitmodel)
4641 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4643 src#bool "trim margins"
4644 (fun () -> conf.trimmargins)
4645 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4647 src#bool "persistent location"
4648 (fun () -> conf.jumpback)
4649 (fun v -> conf.jumpback <- v);
4651 sep ();
4652 src#int "inter-page space"
4653 (fun () -> conf.interpagespace)
4654 (fun n ->
4655 conf.interpagespace <- n;
4656 docolumns conf.columns;
4657 let pageno, py =
4658 match state.layout with
4659 | [] -> 0, 0
4660 | l :: _ ->
4661 l.pageno, l.pagey
4663 state.maxy <- calcheight ();
4664 let y = getpagey pageno in
4665 gotoy (y + py)
4668 src#int "page bias"
4669 (fun () -> conf.pagebias)
4670 (fun v -> conf.pagebias <- v);
4672 src#int "scroll step"
4673 (fun () -> conf.scrollstep)
4674 (fun n -> conf.scrollstep <- n);
4676 src#int "horizontal scroll step"
4677 (fun () -> conf.hscrollstep)
4678 (fun v -> conf.hscrollstep <- v);
4680 src#int "auto scroll step"
4681 (fun () ->
4682 match state.autoscroll with
4683 | Some step -> step
4684 | _ -> conf.autoscrollstep)
4685 (fun n ->
4686 if state.autoscroll <> None
4687 then state.autoscroll <- Some n;
4688 conf.autoscrollstep <- n);
4690 src#int "zoom"
4691 (fun () -> truncate (conf.zoom *. 100.))
4692 (fun v -> setzoom ((float v) /. 100.));
4694 src#int "rotation"
4695 (fun () -> conf.angle)
4696 (fun v -> reqlayout v conf.fitmodel);
4698 src#int "scroll bar width"
4699 (fun () -> conf.scrollbw)
4700 (fun v ->
4701 conf.scrollbw <- v;
4702 reshape state.winw state.winh;
4705 src#int "scroll handle height"
4706 (fun () -> conf.scrollh)
4707 (fun v -> conf.scrollh <- v;);
4709 src#int "thumbnail width"
4710 (fun () -> conf.thumbw)
4711 (fun v ->
4712 conf.thumbw <- min 4096 v;
4713 match oldmode with
4714 | Birdseye beye ->
4715 leavebirdseye beye false;
4716 enterbirdseye ()
4717 | _ -> ()
4720 let mode = state.mode in
4721 src#string "columns"
4722 (fun () ->
4723 match conf.columns with
4724 | Csingle _ -> "1"
4725 | Cmulti (multi, _) -> multicolumns_to_string multi
4726 | Csplit (count, _) -> "-" ^ string_of_int count
4728 (fun v ->
4729 let n, a, b = multicolumns_of_string v in
4730 setcolumns mode n a b);
4732 sep ();
4733 src#caption "Pixmap cache" 0;
4734 src#int_with_suffix "size (advisory)"
4735 (fun () -> conf.memlimit)
4736 (fun v -> conf.memlimit <- v);
4738 src#caption2 "used"
4739 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4740 (string_with_suffix_of_int state.memused)
4741 (Hashtbl.length state.tilemap)) 1;
4743 sep ();
4744 src#caption "Layout" 0;
4745 src#caption2 "Dimension"
4746 (fun () ->
4747 Printf.sprintf "%dx%d (virtual %dx%d)"
4748 state.winw state.winh
4749 state.w state.maxy)
4751 if conf.debug
4752 then
4753 src#caption2 "Position" (fun () ->
4754 Printf.sprintf "%dx%d" state.x state.y
4756 else
4757 src#caption2 "Position" (fun () -> describe_location ()) 1
4760 sep ();
4761 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4762 "Save these parameters as global defaults at exit"
4763 (fun () -> conf.bedefault)
4764 (fun v -> conf.bedefault <- v)
4767 sep ();
4768 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4769 src#bool ~offset:0 ~btos "Extended parameters"
4770 (fun () -> !showextended)
4771 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4772 if !showextended
4773 then (
4774 src#bool "checkers"
4775 (fun () -> conf.checkers)
4776 (fun v -> conf.checkers <- v; setcheckers v);
4777 src#bool "update cursor"
4778 (fun () -> conf.updatecurs)
4779 (fun v -> conf.updatecurs <- v);
4780 src#bool "verbose"
4781 (fun () -> conf.verbose)
4782 (fun v -> conf.verbose <- v);
4783 src#bool "invert colors"
4784 (fun () -> conf.invert)
4785 (fun v -> conf.invert <- v);
4786 src#bool "max fit"
4787 (fun () -> conf.maxhfit)
4788 (fun v -> conf.maxhfit <- v);
4789 src#bool "redirect stderr"
4790 (fun () -> conf.redirectstderr)
4791 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4792 src#bool "pax mode"
4793 (fun () -> conf.pax != None)
4794 (fun v ->
4795 if v
4796 then conf.pax <- Some (ref (now (), 0, 0))
4797 else conf.pax <- None);
4798 src#string "uri launcher"
4799 (fun () -> conf.urilauncher)
4800 (fun v -> conf.urilauncher <- v);
4801 src#string "path launcher"
4802 (fun () -> conf.pathlauncher)
4803 (fun v -> conf.pathlauncher <- v);
4804 src#string "tile size"
4805 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4806 (fun v ->
4808 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4809 conf.tilew <- max 64 w;
4810 conf.tileh <- max 64 h;
4811 flushtiles ();
4812 with exn ->
4813 state.text <- Printf.sprintf "bad tile size `%s': %s"
4814 v (exntos exn)
4816 src#int "texture count"
4817 (fun () -> conf.texcount)
4818 (fun v ->
4819 if realloctexts v
4820 then conf.texcount <- v
4821 else showtext '!' " Failed to set texture count please retry later"
4823 src#int "slice height"
4824 (fun () -> conf.sliceheight)
4825 (fun v ->
4826 conf.sliceheight <- v;
4827 wcmd "sliceh %d" conf.sliceheight;
4829 src#int "anti-aliasing level"
4830 (fun () -> conf.aalevel)
4831 (fun v ->
4832 conf.aalevel <- bound v 0 8;
4833 state.anchor <- getanchor ();
4834 opendoc state.path state.password;
4836 src#string "page scroll scaling factor"
4837 (fun () -> string_of_float conf.pgscale)
4838 (fun v ->
4840 let s = float_of_string v in
4841 conf.pgscale <- s
4842 with exn ->
4843 state.text <- Printf.sprintf
4844 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4847 src#int "ui font size"
4848 (fun () -> fstate.fontsize)
4849 (fun v -> setfontsize (bound v 5 100));
4850 src#int "hint font size"
4851 (fun () -> conf.hfsize)
4852 (fun v -> conf.hfsize <- bound v 5 100);
4853 colorp "background color"
4854 (fun () -> conf.bgcolor)
4855 (fun v -> conf.bgcolor <- v);
4856 src#bool "crop hack"
4857 (fun () -> conf.crophack)
4858 (fun v -> conf.crophack <- v);
4859 src#string "trim fuzz"
4860 (fun () -> irect_to_string conf.trimfuzz)
4861 (fun v ->
4863 conf.trimfuzz <- irect_of_string v;
4864 if conf.trimmargins
4865 then settrim true conf.trimfuzz;
4866 with exn ->
4867 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4869 src#string "throttle"
4870 (fun () ->
4871 match conf.maxwait with
4872 | None -> "show place holder if page is not ready"
4873 | Some time ->
4874 if time = infinity
4875 then "wait for page to fully render"
4876 else
4877 "wait " ^ string_of_float time
4878 ^ " seconds before showing placeholder"
4880 (fun v ->
4882 let f = float_of_string v in
4883 if f <= 0.0
4884 then conf.maxwait <- None
4885 else conf.maxwait <- Some f
4886 with exn ->
4887 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4889 src#string "ghyll scroll"
4890 (fun () ->
4891 match conf.ghyllscroll with
4892 | None -> ""
4893 | Some nab -> ghyllscroll_to_string nab
4895 (fun v ->
4897 let gs =
4898 if emptystr v
4899 then None
4900 else Some (ghyllscroll_of_string v)
4902 conf.ghyllscroll <- gs
4903 with exn ->
4904 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4906 src#string "selection command"
4907 (fun () -> conf.selcmd)
4908 (fun v -> conf.selcmd <- v);
4909 src#string "synctex command"
4910 (fun () -> conf.stcmd)
4911 (fun v -> conf.stcmd <- v);
4912 src#string "pax command"
4913 (fun () -> conf.paxcmd)
4914 (fun v -> conf.paxcmd <- v);
4915 src#colorspace "color space"
4916 (fun () -> CSTE.to_string conf.colorspace)
4917 (fun v ->
4918 conf.colorspace <- CSTE.of_int v;
4919 wcmd "cs %d" v;
4920 load state.layout;
4922 src#paxmark "pax mark method"
4923 (fun () -> MTE.to_string conf.paxmark)
4924 (fun v -> conf.paxmark <- MTE.of_int v);
4925 if pbousable ()
4926 then
4927 src#bool "use PBO"
4928 (fun () -> conf.usepbo)
4929 (fun v -> conf.usepbo <- v);
4930 src#bool "mouse wheel scrolls pages"
4931 (fun () -> conf.wheelbypage)
4932 (fun v -> conf.wheelbypage <- v);
4933 src#bool "open remote links in a new instance"
4934 (fun () -> conf.riani)
4935 (fun v -> conf.riani <- v);
4938 sep ();
4939 src#caption "Document" 0;
4940 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4941 src#caption2 "Pages"
4942 (fun () -> string_of_int state.pagecount) 1;
4943 src#caption2 "Dimensions"
4944 (fun () -> string_of_int (List.length state.pdims)) 1;
4945 if conf.trimmargins
4946 then (
4947 sep ();
4948 src#caption "Trimmed margins" 0;
4949 src#caption2 "Dimensions"
4950 (fun () -> string_of_int (List.length state.pdims)) 1;
4953 sep ();
4954 src#caption "OpenGL" 0;
4955 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4956 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4958 sep ();
4959 src#caption "Location" 0;
4960 if nonemptystr state.origin
4961 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4962 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4964 src#reset prevmode prevuioh;
4966 fun () ->
4967 state.text <- "";
4968 let prevmode = state.mode
4969 and prevuioh = state.uioh in
4970 fillsrc prevmode prevuioh;
4971 let source = (src :> lvsource) in
4972 let modehash = findkeyhash conf "info" in
4973 state.uioh <- coe (object (self)
4974 inherit listview ~source ~trusted:true ~modehash as super
4975 val mutable m_prevmemused = 0
4976 method infochanged = function
4977 | Memused ->
4978 if m_prevmemused != state.memused
4979 then (
4980 m_prevmemused <- state.memused;
4981 G.postRedisplay "memusedchanged";
4983 | Pdim -> G.postRedisplay "pdimchanged"
4984 | Docinfo -> fillsrc prevmode prevuioh
4986 method key key mask =
4987 if not (Wsi.withctrl mask)
4988 then
4989 match key with
4990 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4991 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4992 | _ -> super#key key mask
4993 else super#key key mask
4994 end);
4995 G.postRedisplay "info";
4998 let enterhelpmode =
4999 let source =
5000 (object
5001 inherit lvsourcebase
5002 method getitemcount = Array.length state.help
5003 method getitem n =
5004 let s, l, _ = state.help.(n) in
5005 (s, l)
5007 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5008 let optuioh =
5009 if not cancel
5010 then (
5011 m_qsearch <- qsearch;
5012 match state.help.(active) with
5013 | _, _, Action f -> Some (f uioh)
5014 | _ -> Some (uioh)
5016 else None
5018 m_active <- active;
5019 m_first <- first;
5020 m_pan <- pan;
5021 optuioh
5023 method hasaction n =
5024 match state.help.(n) with
5025 | _, _, Action _ -> true
5026 | _ -> false
5028 initializer
5029 m_active <- -1
5030 end)
5031 in fun () ->
5032 let modehash = findkeyhash conf "help" in
5033 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
5034 G.postRedisplay "help";
5037 let entermsgsmode =
5038 let msgsource =
5039 let re = Str.regexp "[\r\n]" in
5040 (object
5041 inherit lvsourcebase
5042 val mutable m_items = [||]
5044 method getitemcount = 1 + Array.length m_items
5046 method getitem n =
5047 if n = 0
5048 then "[Clear]", 0
5049 else m_items.(n-1), 0
5051 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5052 ignore uioh;
5053 if not cancel
5054 then (
5055 if active = 0
5056 then Buffer.clear state.errmsgs;
5057 m_qsearch <- qsearch;
5059 m_active <- active;
5060 m_first <- first;
5061 m_pan <- pan;
5062 None
5064 method hasaction n =
5065 n = 0
5067 method reset =
5068 state.newerrmsgs <- false;
5069 let l = Str.split re (Buffer.contents state.errmsgs) in
5070 m_items <- Array.of_list l
5072 initializer
5073 m_active <- 0
5074 end)
5075 in fun () ->
5076 state.text <- "";
5077 msgsource#reset;
5078 let source = (msgsource :> lvsource) in
5079 let modehash = findkeyhash conf "listview" in
5080 state.uioh <- coe (object
5081 inherit listview ~source ~trusted:false ~modehash as super
5082 method display =
5083 if state.newerrmsgs
5084 then msgsource#reset;
5085 super#display
5086 end);
5087 G.postRedisplay "msgs";
5090 let quickbookmark ?title () =
5091 match state.layout with
5092 | [] -> ()
5093 | l :: _ ->
5094 let title =
5095 match title with
5096 | None ->
5097 let sec = Unix.gettimeofday () in
5098 let tm = Unix.localtime sec in
5099 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
5100 (l.pageno+1)
5101 tm.Unix.tm_mday
5102 tm.Unix.tm_mon
5103 (tm.Unix.tm_year + 1900)
5104 tm.Unix.tm_hour
5105 tm.Unix.tm_min
5106 | Some title -> title
5108 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
5111 let setautoscrollspeed step goingdown =
5112 let incr = max 1 ((abs step) / 2) in
5113 let incr = if goingdown then incr else -incr in
5114 let astep = step + incr in
5115 state.autoscroll <- Some astep;
5118 let gotounder under =
5119 let getpath filename =
5120 let path =
5121 if nonemptystr filename
5122 then
5123 if Filename.is_relative filename
5124 then
5125 let dir = Filename.dirname state.path in
5126 let dir =
5127 if Filename.is_implicit dir
5128 then Filename.concat (Sys.getcwd ()) dir
5129 else dir
5131 Filename.concat dir filename
5132 else filename
5133 else ""
5135 if Sys.file_exists path
5136 then path
5137 else ""
5139 match under with
5140 | Ulinkgoto (pageno, top) ->
5141 if pageno >= 0
5142 then (
5143 addnav ();
5144 gotopage1 pageno top;
5147 | Ulinkuri s ->
5148 gotouri s
5150 | Uremote (filename, pageno) ->
5151 let path = getpath filename in
5152 if nonemptystr path
5153 then (
5154 if conf.riani
5155 then
5156 let command = !selfexec ^ " " ^ path in
5157 try popen command []
5158 with exn ->
5159 Printf.eprintf
5160 "failed to execute `%s': %s\n" command (exntos exn);
5161 flush stderr;
5162 else
5163 let anchor = getanchor () in
5164 let ranchor = state.path, state.password, anchor, state.origin in
5165 state.origin <- "";
5166 state.anchor <- (pageno, 0.0, 0.0);
5167 state.ranchors <- ranchor :: state.ranchors;
5168 opendoc path "";
5170 else showtext '!' ("Could not find " ^ filename)
5172 | Uremotedest (filename, destname) ->
5173 let path = getpath filename in
5174 if nonemptystr path
5175 then (
5176 if conf.riani
5177 then
5178 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
5179 try popen command []
5180 with exn ->
5181 Printf.eprintf
5182 "failed to execute `%s': %s\n" command (exntos exn);
5183 flush stderr;
5184 else
5185 let anchor = getanchor () in
5186 let ranchor = state.path, state.password, anchor, state.origin in
5187 state.origin <- "";
5188 state.nameddest <- destname;
5189 state.ranchors <- ranchor :: state.ranchors;
5190 opendoc path "";
5192 else showtext '!' ("Could not find " ^ filename)
5194 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
5197 let canpan () =
5198 match conf.columns with
5199 | Csplit _ -> true
5200 | _ -> state.x != 0 || conf.zoom > 1.0
5203 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5205 let existsinrow pageno (columns, coverA, coverB) p =
5206 let last = ((pageno - coverA) mod columns) + columns in
5207 let rec any = function
5208 | [] -> false
5209 | l :: rest ->
5210 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5211 then p l
5212 else (
5213 if not (p l)
5214 then (if l.pageno = last then false else any rest)
5215 else true
5218 any state.layout
5221 let nextpage () =
5222 match state.layout with
5223 | [] ->
5224 let pageno = page_of_y state.y in
5225 gotoghyll (getpagey (pageno+1))
5226 | l :: rest ->
5227 match conf.columns with
5228 | Csingle _ ->
5229 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5230 then
5231 let y = clamp (pgscale state.winh) in
5232 gotoghyll y
5233 else
5234 let pageno = min (l.pageno+1) (state.pagecount-1) in
5235 gotoghyll (getpagey pageno)
5236 | Cmulti ((c, _, _) as cl, _) ->
5237 if conf.presentation
5238 && (existsinrow l.pageno cl
5239 (fun l -> l.pageh > l.pagey + l.pagevh))
5240 then
5241 let y = clamp (pgscale state.winh) in
5242 gotoghyll y
5243 else
5244 let pageno = min (l.pageno+c) (state.pagecount-1) in
5245 gotoghyll (getpagey pageno)
5246 | Csplit (n, _) ->
5247 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5248 then
5249 let pagey, pageh = getpageyh l.pageno in
5250 let pagey = pagey + pageh * l.pagecol in
5251 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5252 gotoghyll (pagey + pageh + ips)
5255 let prevpage () =
5256 match state.layout with
5257 | [] ->
5258 let pageno = page_of_y state.y in
5259 gotoghyll (getpagey (pageno-1))
5260 | l :: _ ->
5261 match conf.columns with
5262 | Csingle _ ->
5263 if conf.presentation && l.pagey != 0
5264 then
5265 gotoghyll (clamp (pgscale ~-(state.winh)))
5266 else
5267 let pageno = max 0 (l.pageno-1) in
5268 gotoghyll (getpagey pageno)
5269 | Cmulti ((c, _, coverB) as cl, _) ->
5270 if conf.presentation &&
5271 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5272 then
5273 gotoghyll (clamp (pgscale ~-(state.winh)))
5274 else
5275 let decr =
5276 if l.pageno = state.pagecount - coverB
5277 then 1
5278 else c
5280 let pageno = max 0 (l.pageno-decr) in
5281 gotoghyll (getpagey pageno)
5282 | Csplit (n, _) ->
5283 let y =
5284 if l.pagecol = 0
5285 then
5286 if l.pageno = 0
5287 then l.pagey
5288 else
5289 let pageno = max 0 (l.pageno-1) in
5290 let pagey, pageh = getpageyh pageno in
5291 pagey + (n-1)*pageh
5292 else
5293 let pagey, pageh = getpageyh l.pageno in
5294 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5296 gotoghyll y
5299 let viewkeyboard key mask =
5300 let enttext te =
5301 let mode = state.mode in
5302 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5303 state.text <- "";
5304 enttext ();
5305 G.postRedisplay "view:enttext"
5307 let ctrl = Wsi.withctrl mask in
5308 let key =
5309 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5311 match key with
5312 | 81 -> (* Q *)
5313 exit 0
5315 | 0xff63 -> (* insert *)
5316 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5317 then (
5318 state.mode <- LinkNav (Ltgendir 0);
5319 gotoy state.y;
5321 else showtext '!' "Keyboard link navigation does not work under rotation"
5323 | 0xff1b | 113 -> (* escape / q *)
5324 begin match state.mstate with
5325 | Mzoomrect _ ->
5326 state.mstate <- Mnone;
5327 Wsi.setcursor Wsi.CURSOR_INHERIT;
5328 G.postRedisplay "kill zoom rect";
5329 | _ ->
5330 begin match state.mode with
5331 | LinkNav _ ->
5332 state.mode <- View;
5333 G.postRedisplay "esc leave linknav"
5334 | _ ->
5335 match state.ranchors with
5336 | [] -> raise Quit
5337 | (path, password, anchor, origin) :: rest ->
5338 state.ranchors <- rest;
5339 state.anchor <- anchor;
5340 state.origin <- origin;
5341 state.nameddest <- "";
5342 opendoc path password
5343 end;
5344 end;
5346 | 0xff08 -> (* backspace *)
5347 gotoghyll (getnav ~-1)
5349 | 111 -> (* o *)
5350 enteroutlinemode ()
5352 | 117 -> (* u *)
5353 state.rects <- [];
5354 state.text <- "";
5355 G.postRedisplay "dehighlight";
5357 | 47 | 63 -> (* / ? *)
5358 let ondone isforw s =
5359 cbput state.hists.pat s;
5360 state.searchpattern <- s;
5361 search s isforw
5363 let s = String.create 1 in
5364 s.[0] <- Char.chr key;
5365 enttext (s, "", Some (onhist state.hists.pat),
5366 textentry, ondone (key = 47), true)
5368 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5369 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5370 setzoom (conf.zoom +. incr)
5372 | 43 | 0xffab -> (* + *)
5373 let ondone s =
5374 let n =
5375 try int_of_string s with exc ->
5376 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5377 max_int
5379 if n != max_int
5380 then (
5381 conf.pagebias <- n;
5382 state.text <- "page bias is now " ^ string_of_int n;
5385 enttext ("page bias: ", "", None, intentry, ondone, true)
5387 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5388 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5389 setzoom (max 0.01 (conf.zoom -. decr))
5391 | 45 | 0xffad -> (* - *)
5392 let ondone msg = state.text <- msg in
5393 enttext (
5394 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5395 optentry state.mode, ondone, true
5398 | 48 when ctrl -> (* ctrl-0 *)
5399 if conf.zoom = 1.0
5400 then (
5401 state.x <- 0;
5402 gotoy state.y
5404 else setzoom 1.0
5406 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5407 let cols =
5408 match conf.columns with
5409 | Csingle _ | Cmulti _ -> 1
5410 | Csplit (n, _) -> n
5412 let h = state.winh -
5413 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5415 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5416 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5417 then setzoom zoom
5419 | 51 when ctrl -> (* ctrl-3 *)
5420 let fm =
5421 match conf.fitmodel with
5422 | FitWidth -> FitProportional
5423 | FitProportional -> FitPage
5424 | FitPage -> FitWidth
5426 state.text <- "fit model: " ^ FMTE.to_string fm;
5427 reqlayout conf.angle fm
5429 | 0xffc6 -> (* f9 *)
5430 togglebirdseye ()
5432 | 57 when ctrl -> (* ctrl-9 *)
5433 togglebirdseye ()
5435 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5436 when not ctrl -> (* 0..9 *)
5437 let ondone s =
5438 let n =
5439 try int_of_string s with exc ->
5440 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5443 if n >= 0
5444 then (
5445 addnav ();
5446 cbput state.hists.pag (string_of_int n);
5447 gotopage1 (n + conf.pagebias - 1) 0;
5450 let pageentry text key =
5451 match Char.unsafe_chr key with
5452 | 'g' -> TEdone text
5453 | _ -> intentry text key
5455 let text = "x" in text.[0] <- Char.chr key;
5456 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5458 | 98 -> (* b *)
5459 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5460 reshape state.winw state.winh;
5462 | 66 -> (* B *)
5463 state.bzoom <- not state.bzoom;
5464 state.rects <- [];
5465 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5467 | 108 -> (* l *)
5468 conf.hlinks <- not conf.hlinks;
5469 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5470 G.postRedisplay "toggle highlightlinks";
5472 | 70 -> (* F *)
5473 state.glinks <- true;
5474 let mode = state.mode in
5475 state.mode <- Textentry (
5476 (":", "", None, linknentry, linkndone gotounder, false),
5477 (fun _ ->
5478 state.glinks <- false;
5479 state.mode <- mode)
5481 state.text <- "";
5482 G.postRedisplay "view:linkent(F)"
5484 | 121 -> (* y *)
5485 state.glinks <- true;
5486 let mode = state.mode in
5487 state.mode <- Textentry (
5489 ":", "", None, linknentry, linkndone (fun under ->
5490 selstring (undertext under);
5491 ), false
5493 fun _ ->
5494 state.glinks <- false;
5495 state.mode <- mode
5497 state.text <- "";
5498 G.postRedisplay "view:linkent"
5500 | 97 -> (* a *)
5501 begin match state.autoscroll with
5502 | Some step ->
5503 conf.autoscrollstep <- step;
5504 state.autoscroll <- None
5505 | None ->
5506 if conf.autoscrollstep = 0
5507 then state.autoscroll <- Some 1
5508 else state.autoscroll <- Some conf.autoscrollstep
5511 | 112 when ctrl -> (* ctrl-p *)
5512 launchpath ()
5514 | 80 -> (* P *)
5515 setpresentationmode (not conf.presentation);
5516 showtext ' ' ("presentation mode " ^
5517 if conf.presentation then "on" else "off");
5519 | 102 -> (* f *)
5520 if List.mem Wsi.Fullscreen state.winstate
5521 then Wsi.reshape conf.cwinw conf.cwinh
5522 else Wsi.fullscreen ()
5524 | 112 | 78 -> (* p|N *)
5525 search state.searchpattern false
5527 | 110 | 0xffc0 -> (* n|F3 *)
5528 search state.searchpattern true
5530 | 116 -> (* t *)
5531 begin match state.layout with
5532 | [] -> ()
5533 | l :: _ ->
5534 gotoghyll (getpagey l.pageno)
5537 | 32 -> (* space *)
5538 nextpage ()
5540 | 0xff9f | 0xffff -> (* delete *)
5541 prevpage ()
5543 | 61 -> (* = *)
5544 showtext ' ' (describe_location ());
5546 | 119 -> (* w *)
5547 begin match state.layout with
5548 | [] -> ()
5549 | l :: _ ->
5550 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5551 G.postRedisplay "w"
5554 | 39 -> (* ' *)
5555 enterbookmarkmode ()
5557 | 104 | 0xffbe -> (* h|F1 *)
5558 enterhelpmode ()
5560 | 105 -> (* i *)
5561 enterinfomode ()
5563 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5564 entermsgsmode ()
5566 | 109 -> (* m *)
5567 let ondone s =
5568 match state.layout with
5569 | l :: _ ->
5570 if nonemptystr s
5571 then
5572 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5573 | _ -> ()
5575 enttext ("bookmark: ", "", None, textentry, ondone, true)
5577 | 126 -> (* ~ *)
5578 quickbookmark ();
5579 showtext ' ' "Quick bookmark added";
5581 | 122 -> (* z *)
5582 begin match state.layout with
5583 | l :: _ ->
5584 let rect = getpdimrect l.pagedimno in
5585 let w, h =
5586 if conf.crophack
5587 then
5588 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5589 truncate (1.2 *. (rect.(3) -. rect.(0))))
5590 else
5591 (truncate (rect.(1) -. rect.(0)),
5592 truncate (rect.(3) -. rect.(0)))
5594 let w = truncate ((float w)*.conf.zoom)
5595 and h = truncate ((float h)*.conf.zoom) in
5596 if w != 0 && h != 0
5597 then (
5598 state.anchor <- getanchor ();
5599 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5601 G.postRedisplay "z";
5603 | [] -> ()
5606 | 120 -> state.roam () (* x *)
5607 | 60 | 62 -> (* < > *)
5608 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5610 | 91 | 93 -> (* [ ] *)
5611 conf.colorscale <-
5612 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5614 G.postRedisplay "brightness";
5616 | 99 when state.mode = View -> (* [alt-]c *)
5617 if Wsi.withalt mask
5618 then (
5619 if conf.zoom > 1.0
5620 then
5621 let m = (wadjsb state.winw - state.w) / 2 in
5622 state.x <- m;
5623 gotoy_and_clear_text state.y
5625 else
5626 let (c, a, b), z =
5627 match state.prevcolumns with
5628 | None -> (1, 0, 0), 1.0
5629 | Some (columns, z) ->
5630 let cab =
5631 match columns with
5632 | Csplit (c, _) -> -c, 0, 0
5633 | Cmulti ((c, a, b), _) -> c, a, b
5634 | Csingle _ -> 1, 0, 0
5636 cab, z
5638 setcolumns View c a b;
5639 setzoom z
5641 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
5642 -> (* ctrl-shift- (kp) [up|down] *)
5643 let zoom, x = state.prevzoom in
5644 setzoom zoom;
5645 state.x <- x;
5647 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5648 begin match state.autoscroll with
5649 | None ->
5650 begin match state.mode with
5651 | Birdseye beye -> upbirdseye 1 beye
5652 | _ ->
5653 if ctrl
5654 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5655 else (
5656 if not (Wsi.withshift mask) && conf.presentation
5657 then prevpage ()
5658 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5661 | Some n ->
5662 setautoscrollspeed n false
5665 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5666 begin match state.autoscroll with
5667 | None ->
5668 begin match state.mode with
5669 | Birdseye beye -> downbirdseye 1 beye
5670 | _ ->
5671 if ctrl
5672 then gotoy_and_clear_text (clamp (state.winh/2))
5673 else (
5674 if not (Wsi.withshift mask) && conf.presentation
5675 then nextpage ()
5676 else gotoy_and_clear_text (clamp conf.scrollstep)
5679 | Some n ->
5680 setautoscrollspeed n true
5683 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5684 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5685 if canpan ()
5686 then
5687 let dx =
5688 if ctrl
5689 then state.winw / 2
5690 else conf.hscrollstep
5692 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5693 state.x <- panbound (state.x + dx);
5694 gotoy_and_clear_text state.y
5695 else (
5696 state.text <- "";
5697 G.postRedisplay "left/right"
5700 | 0xff55 | 0xff9a -> (* (kp) prior *)
5701 let y =
5702 if ctrl
5703 then
5704 match state.layout with
5705 | [] -> state.y
5706 | l :: _ -> state.y - l.pagey
5707 else
5708 clamp (pgscale (-state.winh))
5710 gotoghyll y
5712 | 0xff56 | 0xff9b -> (* (kp) next *)
5713 let y =
5714 if ctrl
5715 then
5716 match List.rev state.layout with
5717 | [] -> state.y
5718 | l :: _ -> getpagey l.pageno
5719 else
5720 clamp (pgscale state.winh)
5722 gotoghyll y
5724 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5725 gotoghyll 0
5726 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5727 gotoghyll (clamp state.maxy)
5729 | 0xff53 | 0xff98
5730 when Wsi.withalt mask -> (* alt-(kp) right *)
5731 gotoghyll (getnav 1)
5732 | 0xff51 | 0xff96
5733 when Wsi.withalt mask -> (* alt-(kp) left *)
5734 gotoghyll (getnav ~-1)
5736 | 114 -> (* r *)
5737 reload ()
5739 | 118 when conf.debug -> (* v *)
5740 state.rects <- [];
5741 List.iter (fun l ->
5742 match getopaque l.pageno with
5743 | None -> ()
5744 | Some opaque ->
5745 let x0, y0, x1, y1 = pagebbox opaque in
5746 let a,b = float x0, float y0 in
5747 let c,d = float x1, float y0 in
5748 let e,f = float x1, float y1 in
5749 let h,j = float x0, float y1 in
5750 let rect = (a,b,c,d,e,f,h,j) in
5751 debugrect rect;
5752 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5753 ) state.layout;
5754 G.postRedisplay "v";
5756 | _ ->
5757 vlog "huh? %s" (Wsi.keyname key)
5760 let linknavkeyboard key mask linknav =
5761 let getpage pageno =
5762 let rec loop = function
5763 | [] -> None
5764 | l :: _ when l.pageno = pageno -> Some l
5765 | _ :: rest -> loop rest
5766 in loop state.layout
5768 let doexact (pageno, n) =
5769 match getopaque pageno, getpage pageno with
5770 | Some opaque, Some l ->
5771 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5772 then
5773 let under = getlink opaque n in
5774 G.postRedisplay "link gotounder";
5775 gotounder under;
5776 state.mode <- View;
5777 else
5778 let opt, dir =
5779 match key with
5780 | 0xff50 -> (* home *)
5781 Some (findlink opaque LDfirst), -1
5783 | 0xff57 -> (* end *)
5784 Some (findlink opaque LDlast), 1
5786 | 0xff51 -> (* left *)
5787 Some (findlink opaque (LDleft n)), -1
5789 | 0xff53 -> (* right *)
5790 Some (findlink opaque (LDright n)), 1
5792 | 0xff52 -> (* up *)
5793 Some (findlink opaque (LDup n)), -1
5795 | 0xff54 -> (* down *)
5796 Some (findlink opaque (LDdown n)), 1
5798 | _ -> None, 0
5800 let pwl l dir =
5801 begin match findpwl l.pageno dir with
5802 | Pwlnotfound -> ()
5803 | Pwl pageno ->
5804 let notfound dir =
5805 state.mode <- LinkNav (Ltgendir dir);
5806 let y, h = getpageyh pageno in
5807 let y =
5808 if dir < 0
5809 then y + h - state.winh
5810 else y
5812 gotoy y
5814 begin match getopaque pageno, getpage pageno with
5815 | Some opaque, Some _ ->
5816 let link =
5817 let ld = if dir > 0 then LDfirst else LDlast in
5818 findlink opaque ld
5820 begin match link with
5821 | Lfound m ->
5822 showlinktype (getlink opaque m);
5823 state.mode <- LinkNav (Ltexact (pageno, m));
5824 G.postRedisplay "linknav jpage";
5825 | _ -> notfound dir
5826 end;
5827 | _ -> notfound dir
5828 end;
5829 end;
5831 begin match opt with
5832 | Some Lnotfound -> pwl l dir;
5833 | Some (Lfound m) ->
5834 if m = n
5835 then pwl l dir
5836 else (
5837 let _, y0, _, y1 = getlinkrect opaque m in
5838 if y0 < l.pagey
5839 then gotopage1 l.pageno y0
5840 else (
5841 let d = fstate.fontsize + 1 in
5842 if y1 - l.pagey > l.pagevh - d
5843 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5844 else G.postRedisplay "linknav";
5846 showlinktype (getlink opaque m);
5847 state.mode <- LinkNav (Ltexact (l.pageno, m));
5850 | None -> viewkeyboard key mask
5851 end;
5852 | _ -> viewkeyboard key mask
5854 if key = 0xff63
5855 then (
5856 state.mode <- View;
5857 G.postRedisplay "leave linknav"
5859 else
5860 match linknav with
5861 | Ltgendir _ -> viewkeyboard key mask
5862 | Ltexact exact -> doexact exact
5865 let keyboard key mask =
5866 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5867 then wcmd "interrupt"
5868 else state.uioh <- state.uioh#key key mask
5871 let birdseyekeyboard key mask
5872 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5873 let incr =
5874 match conf.columns with
5875 | Csingle _ -> 1
5876 | Cmulti ((c, _, _), _) -> c
5877 | Csplit _ -> failwith "bird's eye split mode"
5879 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5880 match key with
5881 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5882 let y, h = getpageyh pageno in
5883 let top = (state.winh - h) / 2 in
5884 gotoy (max 0 (y - top))
5885 | 0xff0d (* enter *)
5886 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5887 | 0xff1b -> leavebirdseye beye true (* escape *)
5888 | 0xff52 -> upbirdseye incr beye (* up *)
5889 | 0xff54 -> downbirdseye incr beye (* down *)
5890 | 0xff51 -> upbirdseye 1 beye (* left *)
5891 | 0xff53 -> downbirdseye 1 beye (* right *)
5893 | 0xff55 -> (* prior *)
5894 begin match state.layout with
5895 | l :: _ ->
5896 if l.pagey != 0
5897 then (
5898 state.mode <- Birdseye (
5899 oconf, leftx, l.pageno, hooverpageno, anchor
5901 gotopage1 l.pageno 0;
5903 else (
5904 let layout = layout (state.y-state.winh) (pgh state.layout) in
5905 match layout with
5906 | [] -> gotoy (clamp (-state.winh))
5907 | l :: _ ->
5908 state.mode <- Birdseye (
5909 oconf, leftx, l.pageno, hooverpageno, anchor
5911 gotopage1 l.pageno 0
5914 | [] -> gotoy (clamp (-state.winh))
5915 end;
5917 | 0xff56 -> (* next *)
5918 begin match List.rev state.layout with
5919 | l :: _ ->
5920 let layout = layout (state.y + (pgh state.layout)) state.winh in
5921 begin match layout with
5922 | [] ->
5923 let incr = l.pageh - l.pagevh in
5924 if incr = 0
5925 then (
5926 state.mode <-
5927 Birdseye (
5928 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5930 G.postRedisplay "birdseye pagedown";
5932 else gotoy (clamp (incr + conf.interpagespace*2));
5934 | l :: _ ->
5935 state.mode <-
5936 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5937 gotopage1 l.pageno 0;
5940 | [] -> gotoy (clamp state.winh)
5941 end;
5943 | 0xff50 -> (* home *)
5944 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5945 gotopage1 0 0
5947 | 0xff57 -> (* end *)
5948 let pageno = state.pagecount - 1 in
5949 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5950 if not (pagevisible state.layout pageno)
5951 then
5952 let h =
5953 match List.rev state.pdims with
5954 | [] -> state.winh
5955 | (_, _, h, _) :: _ -> h
5957 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5958 else G.postRedisplay "birdseye end";
5959 | _ -> viewkeyboard key mask
5962 let drawpage l =
5963 let color =
5964 match state.mode with
5965 | Textentry _ -> scalecolor 0.4
5966 | LinkNav _
5967 | View -> scalecolor 1.0
5968 | Birdseye (_, _, pageno, hooverpageno, _) ->
5969 if l.pageno = hooverpageno
5970 then scalecolor 0.9
5971 else (
5972 if l.pageno = pageno
5973 then scalecolor 1.0
5974 else scalecolor 0.8
5977 drawtiles l color;
5980 let postdrawpage l linkindexbase =
5981 match getopaque l.pageno with
5982 | Some opaque ->
5983 if tileready l l.pagex l.pagey
5984 then
5985 let x = l.pagedispx - l.pagex
5986 and y = l.pagedispy - l.pagey in
5987 let hlmask =
5988 match conf.columns with
5989 | Csingle _ | Cmulti _ ->
5990 (if conf.hlinks then 1 else 0)
5991 + (if state.glinks
5992 && not (isbirdseye state.mode) then 2 else 0)
5993 | _ -> 0
5995 let s =
5996 match state.mode with
5997 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
5998 | _ -> ""
6000 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
6001 else 0
6002 | _ -> 0
6005 let scrollindicator () =
6006 let sbw, ph, sh = state.uioh#scrollph in
6007 let sbh, pw, sw = state.uioh#scrollpw in
6009 GlDraw.color (0.64, 0.64, 0.64);
6010 GlDraw.rect
6011 (float (state.winw - sbw), 0.)
6012 (float state.winw, float state.winh)
6014 GlDraw.rect
6015 (0., float (state.winh - sbh))
6016 (float (wadjsb state.winw - 1), float state.winh)
6018 GlDraw.color (0.0, 0.0, 0.0);
6020 GlDraw.rect
6021 (float (state.winw - sbw), ph)
6022 (float state.winw, ph +. sh)
6024 GlDraw.rect
6025 (pw, float (state.winh - sbh))
6026 (pw +. sw, float state.winh)
6030 let showsel () =
6031 match state.mstate with
6032 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
6035 | Msel ((x0, y0), (x1, y1)) ->
6036 let rec loop = function
6037 | l :: ls ->
6038 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
6039 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
6040 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
6041 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
6042 then
6043 match getopaque l.pageno with
6044 | Some opaque ->
6045 let x0, y0 = pagetranslatepoint l x0 y0 in
6046 let x1, y1 = pagetranslatepoint l x1 y1 in
6047 seltext opaque (x0, y0, x1, y1);
6048 | _ -> ()
6049 else loop ls
6050 | [] -> ()
6052 loop state.layout
6055 let showrects = function [] -> () | rects ->
6056 Gl.enable `blend;
6057 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
6058 GlDraw.polygon_mode `both `fill;
6059 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6060 List.iter
6061 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
6062 List.iter (fun l ->
6063 if l.pageno = pageno
6064 then (
6065 let dx = float (l.pagedispx - l.pagex) in
6066 let dy = float (l.pagedispy - l.pagey) in
6067 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
6068 GlDraw.begins `quads;
6070 GlDraw.vertex2 (x0+.dx, y0+.dy);
6071 GlDraw.vertex2 (x1+.dx, y1+.dy);
6072 GlDraw.vertex2 (x2+.dx, y2+.dy);
6073 GlDraw.vertex2 (x3+.dx, y3+.dy);
6075 GlDraw.ends ();
6077 ) state.layout
6078 ) rects
6080 Gl.disable `blend;
6083 let display () =
6084 GlClear.color (scalecolor2 conf.bgcolor);
6085 GlClear.clear [`color];
6086 List.iter drawpage state.layout;
6087 let rects =
6088 match state.mode with
6089 | LinkNav (Ltexact (pageno, linkno)) ->
6090 begin match getopaque pageno with
6091 | Some opaque ->
6092 let x0, y0, x1, y1 = getlinkrect opaque linkno in
6093 (pageno, 5, (
6094 float x0, float y0,
6095 float x1, float y0,
6096 float x1, float y1,
6097 float x0, float y1)
6098 ) :: state.rects
6099 | None -> state.rects
6101 | _ -> state.rects
6103 showrects rects;
6104 let rec postloop linkindexbase = function
6105 | l :: rest ->
6106 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
6107 postloop linkindexbase rest
6108 | [] -> ()
6110 showsel ();
6111 postloop 0 state.layout;
6112 state.uioh#display;
6113 begin match state.mstate with
6114 | Mzoomrect ((x0, y0), (x1, y1)) ->
6115 Gl.enable `blend;
6116 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
6117 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6118 GlDraw.rect (float x0, float y0)
6119 (float x1, float y1);
6120 Gl.disable `blend;
6121 | _ -> ()
6122 end;
6123 enttext ();
6124 scrollindicator ();
6125 Wsi.swapb ();
6128 let zoomrect x y x1 y1 =
6129 let x0 = min x x1
6130 and x1 = max x x1
6131 and y0 = min y y1 in
6132 gotoy (state.y + y0);
6133 state.anchor <- getanchor ();
6134 let zoom = (float state.w) /. float (x1 - x0) in
6135 let margin =
6136 match conf.fitmodel, conf.columns with
6137 | FitPage, Csplit _ ->
6138 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
6140 | _, _ ->
6141 let adjw = wadjsb state.winw in
6142 if state.w < adjw
6143 then (adjw - state.w) / 2
6144 else 0
6146 state.x <- (state.x + margin) - x0;
6147 setzoom zoom;
6148 Wsi.setcursor Wsi.CURSOR_INHERIT;
6149 state.mstate <- Mnone;
6152 let zoomblock x y =
6153 let g opaque l px py =
6154 match rectofblock opaque px py with
6155 | Some a ->
6156 let x0 = a.(0) -. 20. in
6157 let x1 = a.(1) +. 20. in
6158 let y0 = a.(2) -. 20. in
6159 let zoom = (float state.w) /. (x1 -. x0) in
6160 let pagey = getpagey l.pageno in
6161 gotoy_and_clear_text (pagey + truncate y0);
6162 state.anchor <- getanchor ();
6163 let margin = (state.w - l.pagew)/2 in
6164 state.x <- -truncate x0 - margin;
6165 setzoom zoom;
6166 None
6167 | None -> None
6169 match conf.columns with
6170 | Csplit _ ->
6171 showtext '!' "block zooming does not work properly in split columns mode"
6172 | _ -> onppundermouse g x y ()
6175 let scrollx x =
6176 let winw = wadjsb state.winw - 1 in
6177 let s = float x /. float winw in
6178 let destx = truncate (float (state.w + winw) *. s) in
6179 state.x <- winw - destx;
6180 gotoy_and_clear_text state.y;
6181 state.mstate <- Mscrollx;
6184 let scrolly y =
6185 let s = float y /. float state.winh in
6186 let desty = truncate (float (state.maxy - state.winh) *. s) in
6187 gotoy_and_clear_text desty;
6188 state.mstate <- Mscrolly;
6191 let viewmouse button down x y mask =
6192 match button with
6193 | n when (n == 4 || n == 5) && not down ->
6194 if Wsi.withctrl mask
6195 then (
6196 match state.mstate with
6197 | Mzoom (oldn, i) ->
6198 if oldn = n
6199 then (
6200 if i = 2
6201 then
6202 let incr =
6203 match n with
6204 | 5 ->
6205 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6206 | _ ->
6207 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6209 let zoom = conf.zoom -. incr in
6210 setzoom zoom;
6211 state.mstate <- Mzoom (n, 0);
6212 else
6213 state.mstate <- Mzoom (n, i+1);
6215 else state.mstate <- Mzoom (n, 0)
6217 | _ -> state.mstate <- Mzoom (n, 0)
6219 else (
6220 match state.autoscroll with
6221 | Some step -> setautoscrollspeed step (n=4)
6222 | None ->
6223 if conf.wheelbypage || conf.presentation
6224 then (
6225 if n = 4
6226 then prevpage ()
6227 else nextpage ()
6229 else
6230 let incr =
6231 if n = 4
6232 then -conf.scrollstep
6233 else conf.scrollstep
6235 let incr = incr * 2 in
6236 let y = clamp incr in
6237 gotoy_and_clear_text y
6240 | n when (n = 6 || n = 7) && not down && canpan () ->
6241 state.x <-
6242 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6243 gotoy_and_clear_text state.y
6245 | 1 when Wsi.withshift mask ->
6246 state.mstate <- Mnone;
6247 if not down
6248 then (
6249 match unproject x y with
6250 | Some (pageno, ux, uy) ->
6251 let cmd = Printf.sprintf
6252 "%s %s %d %d %d"
6253 conf.stcmd state.path pageno ux uy
6255 popen cmd []
6256 | None -> ()
6259 | 1 when Wsi.withctrl mask ->
6260 if down
6261 then (
6262 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6263 state.mstate <- Mpan (x, y)
6265 else
6266 state.mstate <- Mnone
6268 | 3 ->
6269 if down
6270 then (
6271 Wsi.setcursor Wsi.CURSOR_CYCLE;
6272 let p = (x, y) in
6273 state.mstate <- Mzoomrect (p, p)
6275 else (
6276 match state.mstate with
6277 | Mzoomrect ((x0, y0), _) ->
6278 if abs (x-x0) > 10 && abs (y - y0) > 10
6279 then zoomrect x0 y0 x y
6280 else (
6281 state.mstate <- Mnone;
6282 Wsi.setcursor Wsi.CURSOR_INHERIT;
6283 G.postRedisplay "kill accidental zoom rect";
6285 | _ ->
6286 Wsi.setcursor Wsi.CURSOR_INHERIT;
6287 state.mstate <- Mnone
6290 | 1 when x > state.winw - vscrollw () ->
6291 if down
6292 then
6293 let _, position, sh = state.uioh#scrollph in
6294 if y > truncate position && y < truncate (position +. sh)
6295 then state.mstate <- Mscrolly
6296 else scrolly y
6297 else
6298 state.mstate <- Mnone
6300 | 1 when y > state.winh - hscrollh () ->
6301 if down
6302 then
6303 let _, position, sw = state.uioh#scrollpw in
6304 if x > truncate position && x < truncate (position +. sw)
6305 then state.mstate <- Mscrollx
6306 else scrollx x
6307 else
6308 state.mstate <- Mnone
6310 | 1 when state.bzoom -> if not down then zoomblock x y
6312 | 1 ->
6313 let dest = if down then getunder x y else Unone in
6314 begin match dest with
6315 | Ulinkgoto _
6316 | Ulinkuri _
6317 | Uremote _ | Uremotedest _
6318 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6319 gotounder dest
6321 | Unone when down ->
6322 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6323 state.mstate <- Mpan (x, y);
6325 | Unone | Utext _ ->
6326 if down
6327 then (
6328 if conf.angle mod 360 = 0
6329 then (
6330 state.mstate <- Msel ((x, y), (x, y));
6331 G.postRedisplay "mouse select";
6334 else (
6335 match state.mstate with
6336 | Mnone -> ()
6338 | Mzoom _ | Mscrollx | Mscrolly ->
6339 state.mstate <- Mnone
6341 | Mzoomrect ((x0, y0), _) ->
6342 zoomrect x0 y0 x y
6344 | Mpan _ ->
6345 Wsi.setcursor Wsi.CURSOR_INHERIT;
6346 state.mstate <- Mnone
6348 | Msel ((x0, y0), (x1, y1)) ->
6349 let rec loop = function
6350 | [] -> ()
6351 | l :: rest ->
6352 let inside =
6353 let a0 = l.pagedispy in
6354 let a1 = a0 + l.pagevh in
6355 let b0 = l.pagedispx in
6356 let b1 = b0 + l.pagevw in
6357 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6358 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6360 if inside
6361 then
6362 match getopaque l.pageno with
6363 | Some opaque ->
6364 begin
6365 match Ne.pipe () with
6366 | Ne.Exn exn ->
6367 showtext '!'
6368 (Printf.sprintf
6369 "can not create sel pipe: %s"
6370 (exntos exn));
6371 | Ne.Res (r, w) ->
6372 let doclose what fd =
6373 Ne.clo fd (fun msg ->
6374 dolog "%s close failed: %s" what msg)
6377 popen conf.selcmd [r, 0; w, -1];
6378 copysel w opaque true;
6379 doclose "pipe/r" r;
6380 G.postRedisplay "copysel";
6381 with exn ->
6382 dolog "can not execute %S: %s"
6383 conf.selcmd (exntos exn);
6384 doclose "pipe/r" r;
6385 doclose "pipe/w" w;
6387 | None -> ()
6388 else loop rest
6390 loop state.layout;
6391 Wsi.setcursor Wsi.CURSOR_INHERIT;
6392 state.mstate <- Mnone;
6396 | _ -> ()
6399 let birdseyemouse button down x y mask
6400 (conf, leftx, _, hooverpageno, anchor) =
6401 match button with
6402 | 1 when down ->
6403 let rec loop = function
6404 | [] -> ()
6405 | l :: rest ->
6406 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6407 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6408 then (
6409 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6411 else loop rest
6413 loop state.layout
6414 | 3 -> ()
6415 | _ -> viewmouse button down x y mask
6418 let uioh = object
6419 method display = ()
6421 method key key mask =
6422 begin match state.mode with
6423 | Textentry textentry -> textentrykeyboard key mask textentry
6424 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6425 | View -> viewkeyboard key mask
6426 | LinkNav linknav -> linknavkeyboard key mask linknav
6427 end;
6428 state.uioh
6430 method button button bstate x y mask =
6431 begin match state.mode with
6432 | LinkNav _
6433 | View -> viewmouse button bstate x y mask
6434 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6435 | Textentry _ -> ()
6436 end;
6437 state.uioh
6439 method motion x y =
6440 begin match state.mode with
6441 | Textentry _ -> ()
6442 | View | Birdseye _ | LinkNav _ ->
6443 match state.mstate with
6444 | Mzoom _ | Mnone -> ()
6446 | Mpan (x0, y0) ->
6447 let dx = x - x0
6448 and dy = y0 - y in
6449 state.mstate <- Mpan (x, y);
6450 if canpan ()
6451 then state.x <- panbound (state.x + dx);
6452 let y = clamp dy in
6453 gotoy_and_clear_text y
6455 | Msel (a, _) ->
6456 state.mstate <- Msel (a, (x, y));
6457 G.postRedisplay "motion select";
6459 | Mscrolly ->
6460 let y = min state.winh (max 0 y) in
6461 scrolly y
6463 | Mscrollx ->
6464 let x = min state.winw (max 0 x) in
6465 scrollx x
6467 | Mzoomrect (p0, _) ->
6468 state.mstate <- Mzoomrect (p0, (x, y));
6469 G.postRedisplay "motion zoomrect";
6470 end;
6471 state.uioh
6473 method pmotion x y =
6474 begin match state.mode with
6475 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6476 let rec loop = function
6477 | [] ->
6478 if hooverpageno != -1
6479 then (
6480 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6481 G.postRedisplay "pmotion birdseye no hoover";
6483 | l :: rest ->
6484 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6485 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6486 then (
6487 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6488 G.postRedisplay "pmotion birdseye hoover";
6490 else loop rest
6492 loop state.layout
6494 | Textentry _ -> ()
6496 | LinkNav _
6497 | View ->
6498 match state.mstate with
6499 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6501 | Mnone ->
6502 updateunder x y;
6503 match conf.pax with
6504 | None -> ()
6505 | Some r ->
6506 let past, _, _ = !r in
6507 let now = now () in
6508 let delta = now -. past in
6509 if delta > 0.01
6510 then paxunder x y
6511 else r := (now, x, y)
6512 end;
6513 state.uioh
6515 method infochanged _ = ()
6517 method scrollph =
6518 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6519 let p, h =
6520 if maxy = 0
6521 then 0.0, float state.winh
6522 else scrollph state.y maxy
6524 vscrollw (), p, h
6526 method scrollpw =
6527 let winw = wadjsb state.winw in
6528 let fwinw = float winw in
6529 let sw =
6530 let sw = fwinw /. float state.w in
6531 let sw = fwinw *. sw in
6532 max sw (float conf.scrollh)
6534 let position =
6535 let maxx = state.w + winw in
6536 let x = winw - state.x in
6537 let percent = float x /. float maxx in
6538 (fwinw -. sw) *. percent
6540 hscrollh (), position, sw
6542 method modehash =
6543 let modename =
6544 match state.mode with
6545 | LinkNav _ -> "links"
6546 | Textentry _ -> "textentry"
6547 | Birdseye _ -> "birdseye"
6548 | View -> "view"
6550 findkeyhash conf modename
6552 method eformsgs = true
6553 end;;
6555 module Config =
6556 struct
6557 open Parser
6559 let fontpath = ref "";;
6561 module KeyMap =
6562 Map.Make (struct type t = (int * int) let compare = compare end);;
6564 let unent s =
6565 let l = String.length s in
6566 let b = Buffer.create l in
6567 unent b s 0 l;
6568 Buffer.contents b;
6571 let home =
6572 try Sys.getenv "HOME"
6573 with exn ->
6574 prerr_endline
6575 ("Can not determine home directory location: " ^ exntos exn);
6579 let modifier_of_string = function
6580 | "alt" -> Wsi.altmask
6581 | "shift" -> Wsi.shiftmask
6582 | "ctrl" | "control" -> Wsi.ctrlmask
6583 | "meta" -> Wsi.metamask
6584 | _ -> 0
6587 let key_of_string =
6588 let r = Str.regexp "-" in
6589 fun s ->
6590 let elems = Str.full_split r s in
6591 let f n k m =
6592 let g s =
6593 let m1 = modifier_of_string s in
6594 if m1 = 0
6595 then (Wsi.namekey s, m)
6596 else (k, m lor m1)
6597 in function
6598 | Str.Delim s when n land 1 = 0 -> g s
6599 | Str.Text s -> g s
6600 | Str.Delim _ -> (k, m)
6602 let rec loop n k m = function
6603 | [] -> (k, m)
6604 | x :: xs ->
6605 let k, m = f n k m x in
6606 loop (n+1) k m xs
6608 loop 0 0 0 elems
6611 let keys_of_string =
6612 let r = Str.regexp "[ \t]" in
6613 fun s ->
6614 let elems = Str.split r s in
6615 List.map key_of_string elems
6618 let copykeyhashes c =
6619 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6622 let config_of c attrs =
6623 let apply c k v =
6625 match k with
6626 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6627 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6628 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6629 | "preload" -> { c with preload = bool_of_string v }
6630 | "page-bias" -> { c with pagebias = int_of_string v }
6631 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6632 | "horizontal-scroll-step" ->
6633 { c with hscrollstep = max (int_of_string v) 1 }
6634 | "auto-scroll-step" ->
6635 { c with autoscrollstep = max 0 (int_of_string v) }
6636 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6637 | "crop-hack" -> { c with crophack = bool_of_string v }
6638 | "throttle" ->
6639 let mw =
6640 match String.lowercase v with
6641 | "true" -> Some infinity
6642 | "false" -> None
6643 | f -> Some (float_of_string f)
6645 { c with maxwait = mw}
6646 | "highlight-links" -> { c with hlinks = bool_of_string v }
6647 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6648 | "vertical-margin" ->
6649 { c with interpagespace = max 0 (int_of_string v) }
6650 | "zoom" ->
6651 let zoom = float_of_string v /. 100. in
6652 let zoom = max zoom 0.0 in
6653 { c with zoom = zoom }
6654 | "presentation" -> { c with presentation = bool_of_string v }
6655 | "rotation-angle" -> { c with angle = int_of_string v }
6656 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6657 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6658 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6659 | "proportional-display" ->
6660 let fm =
6661 if bool_of_string v
6662 then FitProportional
6663 else FitWidth
6665 { c with fitmodel = fm }
6666 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6667 | "pixmap-cache-size" ->
6668 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6669 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6670 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6671 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6672 | "persistent-location" -> { c with jumpback = bool_of_string v }
6673 | "background-color" -> { c with bgcolor = color_of_string v }
6674 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6675 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6676 | "mupdf-store-size" ->
6677 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6678 | "checkers" -> { c with checkers = bool_of_string v }
6679 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6680 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6681 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6682 | "uri-launcher" -> { c with urilauncher = unent v }
6683 | "path-launcher" -> { c with pathlauncher = unent v }
6684 | "color-space" -> { c with colorspace = CSTE.of_string v }
6685 | "invert-colors" -> { c with invert = bool_of_string v }
6686 | "brightness" -> { c with colorscale = float_of_string v }
6687 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6688 | "ghyllscroll" ->
6689 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6690 | "columns" ->
6691 let (n, _, _) as nab = multicolumns_of_string v in
6692 if n < 0
6693 then { c with columns = Csplit (-n, [||]) }
6694 else { c with columns = Cmulti (nab, [||]) }
6695 | "birds-eye-columns" ->
6696 { c with beyecolumns = Some (max (int_of_string v) 2) }
6697 | "selection-command" -> { c with selcmd = unent v }
6698 | "synctex-command" -> { c with stcmd = unent v }
6699 | "pax-command" -> { c with paxcmd = unent v }
6700 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6701 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6702 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6703 | "use-pbo" -> { c with usepbo = bool_of_string v }
6704 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6705 | "horizontal-scrollbar-visible" ->
6706 let b =
6707 if bool_of_string v
6708 then c.scrollb lor scrollbhv
6709 else c.scrollb land (lnot scrollbhv)
6711 { c with scrollb = b }
6712 | "vertical-scrollbar-visible" ->
6713 let b =
6714 if bool_of_string v
6715 then c.scrollb lor scrollbvv
6716 else c.scrollb land (lnot scrollbvv)
6718 { c with scrollb = b }
6719 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6720 | "point-and-x" ->
6721 { c with pax =
6722 if bool_of_string v
6723 then Some (ref (0.0, 0, 0))
6724 else None }
6725 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
6726 | _ -> c
6727 with exn ->
6728 prerr_endline ("Error processing attribute (`" ^
6729 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6732 let rec fold c = function
6733 | [] -> c
6734 | (k, v) :: rest ->
6735 let c = apply c k v in
6736 fold c rest
6738 fold { c with keyhashes = copykeyhashes c } attrs;
6741 let fromstring f pos n v d =
6742 try f v
6743 with exn ->
6744 dolog "Error processing attribute (%S=%S) at %d\n%s"
6745 n v pos (exntos exn)
6750 let bookmark_of attrs =
6751 let rec fold title page rely visy = function
6752 | ("title", v) :: rest -> fold v page rely visy rest
6753 | ("page", v) :: rest -> fold title v rely visy rest
6754 | ("rely", v) :: rest -> fold title page v visy rest
6755 | ("visy", v) :: rest -> fold title page rely v rest
6756 | _ :: rest -> fold title page rely visy rest
6757 | [] -> title, page, rely, visy
6759 fold "invalid" "0" "0" "0" attrs
6762 let doc_of attrs =
6763 let rec fold path page rely pan visy = function
6764 | ("path", v) :: rest -> fold v page rely pan visy rest
6765 | ("page", v) :: rest -> fold path v rely pan visy rest
6766 | ("rely", v) :: rest -> fold path page v pan visy rest
6767 | ("pan", v) :: rest -> fold path page rely v visy rest
6768 | ("visy", v) :: rest -> fold path page rely pan v rest
6769 | _ :: rest -> fold path page rely pan visy rest
6770 | [] -> path, page, rely, pan, visy
6772 fold "" "0" "0" "0" "0" attrs
6775 let map_of attrs =
6776 let rec fold rs ls = function
6777 | ("out", v) :: rest -> fold v ls rest
6778 | ("in", v) :: rest -> fold rs v rest
6779 | _ :: rest -> fold ls rs rest
6780 | [] -> ls, rs
6782 fold "" "" attrs
6785 let setconf dst src =
6786 dst.scrollbw <- src.scrollbw;
6787 dst.scrollh <- src.scrollh;
6788 dst.icase <- src.icase;
6789 dst.preload <- src.preload;
6790 dst.pagebias <- src.pagebias;
6791 dst.verbose <- src.verbose;
6792 dst.scrollstep <- src.scrollstep;
6793 dst.maxhfit <- src.maxhfit;
6794 dst.crophack <- src.crophack;
6795 dst.autoscrollstep <- src.autoscrollstep;
6796 dst.maxwait <- src.maxwait;
6797 dst.hlinks <- src.hlinks;
6798 dst.underinfo <- src.underinfo;
6799 dst.interpagespace <- src.interpagespace;
6800 dst.zoom <- src.zoom;
6801 dst.presentation <- src.presentation;
6802 dst.angle <- src.angle;
6803 dst.cwinw <- src.cwinw;
6804 dst.cwinh <- src.cwinh;
6805 dst.savebmarks <- src.savebmarks;
6806 dst.memlimit <- src.memlimit;
6807 dst.fitmodel <- src.fitmodel;
6808 dst.texcount <- src.texcount;
6809 dst.sliceheight <- src.sliceheight;
6810 dst.thumbw <- src.thumbw;
6811 dst.jumpback <- src.jumpback;
6812 dst.bgcolor <- src.bgcolor;
6813 dst.tilew <- src.tilew;
6814 dst.tileh <- src.tileh;
6815 dst.mustoresize <- src.mustoresize;
6816 dst.checkers <- src.checkers;
6817 dst.aalevel <- src.aalevel;
6818 dst.trimmargins <- src.trimmargins;
6819 dst.trimfuzz <- src.trimfuzz;
6820 dst.urilauncher <- src.urilauncher;
6821 dst.colorspace <- src.colorspace;
6822 dst.invert <- src.invert;
6823 dst.colorscale <- src.colorscale;
6824 dst.redirectstderr <- src.redirectstderr;
6825 dst.ghyllscroll <- src.ghyllscroll;
6826 dst.columns <- src.columns;
6827 dst.beyecolumns <- src.beyecolumns;
6828 dst.selcmd <- src.selcmd;
6829 dst.updatecurs <- src.updatecurs;
6830 dst.pathlauncher <- src.pathlauncher;
6831 dst.keyhashes <- copykeyhashes src;
6832 dst.hfsize <- src.hfsize;
6833 dst.hscrollstep <- src.hscrollstep;
6834 dst.pgscale <- src.pgscale;
6835 dst.usepbo <- src.usepbo;
6836 dst.wheelbypage <- src.wheelbypage;
6837 dst.stcmd <- src.stcmd;
6838 dst.paxcmd <- src.paxcmd;
6839 dst.scrollb <- src.scrollb;
6840 dst.riani <- src.riani;
6841 dst.paxmark <- src.paxmark;
6842 dst.pax <-
6843 if src.pax = None
6844 then None
6845 else Some ((ref (0.0, 0, 0)));
6848 let get s =
6849 let h = Hashtbl.create 10 in
6850 let dc = { defconf with angle = defconf.angle } in
6851 let rec toplevel v t spos _ =
6852 match t with
6853 | Vdata | Vcdata | Vend -> v
6854 | Vopen ("llppconfig", _, closed) ->
6855 if closed
6856 then v
6857 else { v with f = llppconfig }
6858 | Vopen _ ->
6859 error "unexpected subelement at top level" s spos
6860 | Vclose _ -> error "unexpected close at top level" s spos
6862 and llppconfig v t spos _ =
6863 match t with
6864 | Vdata | Vcdata -> v
6865 | Vend -> error "unexpected end of input in llppconfig" s spos
6866 | Vopen ("defaults", attrs, closed) ->
6867 let c = config_of dc attrs in
6868 setconf dc c;
6869 if closed
6870 then v
6871 else { v with f = defaults }
6873 | Vopen ("ui-font", attrs, closed) ->
6874 let rec getsize size = function
6875 | [] -> size
6876 | ("size", v) :: rest ->
6877 let size =
6878 fromstring int_of_string spos "size" v fstate.fontsize in
6879 getsize size rest
6880 | l -> getsize size l
6882 fstate.fontsize <- getsize fstate.fontsize attrs;
6883 if closed
6884 then v
6885 else { v with f = uifont (Buffer.create 10) }
6887 | Vopen ("doc", attrs, closed) ->
6888 let pathent, spage, srely, span, svisy = doc_of attrs in
6889 let path = unent pathent
6890 and pageno = fromstring int_of_string spos "page" spage 0
6891 and rely = fromstring float_of_string spos "rely" srely 0.0
6892 and pan = fromstring int_of_string spos "pan" span 0
6893 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6894 let c = config_of dc attrs in
6895 let anchor = (pageno, rely, visy) in
6896 if closed
6897 then (Hashtbl.add h path (c, [], pan, anchor); v)
6898 else { v with f = doc path pan anchor c [] }
6900 | Vopen _ ->
6901 error "unexpected subelement in llppconfig" s spos
6903 | Vclose "llppconfig" -> { v with f = toplevel }
6904 | Vclose _ -> error "unexpected close in llppconfig" s spos
6906 and defaults v t spos _ =
6907 match t with
6908 | Vdata | Vcdata -> v
6909 | Vend -> error "unexpected end of input in defaults" s spos
6910 | Vopen ("keymap", attrs, closed) ->
6911 let modename =
6912 try List.assoc "mode" attrs
6913 with Not_found -> "global" in
6914 if closed
6915 then v
6916 else
6917 let ret keymap =
6918 let h = findkeyhash dc modename in
6919 KeyMap.iter (Hashtbl.replace h) keymap;
6920 defaults
6922 { v with f = pkeymap ret KeyMap.empty }
6924 | Vopen (_, _, _) ->
6925 error "unexpected subelement in defaults" s spos
6927 | Vclose "defaults" ->
6928 { v with f = llppconfig }
6930 | Vclose _ -> error "unexpected close in defaults" s spos
6932 and uifont b v t spos epos =
6933 match t with
6934 | Vdata | Vcdata ->
6935 Buffer.add_substring b s spos (epos - spos);
6937 | Vopen (_, _, _) ->
6938 error "unexpected subelement in ui-font" s spos
6939 | Vclose "ui-font" ->
6940 if emptystr !fontpath
6941 then fontpath := Buffer.contents b;
6942 { v with f = llppconfig }
6943 | Vclose _ -> error "unexpected close in ui-font" s spos
6944 | Vend -> error "unexpected end of input in ui-font" s spos
6946 and doc path pan anchor c bookmarks v t spos _ =
6947 match t with
6948 | Vdata | Vcdata -> v
6949 | Vend -> error "unexpected end of input in doc" s spos
6950 | Vopen ("bookmarks", _, closed) ->
6951 if closed
6952 then v
6953 else { v with f = pbookmarks path pan anchor c bookmarks }
6955 | Vopen ("keymap", attrs, closed) ->
6956 let modename =
6957 try List.assoc "mode" attrs
6958 with Not_found -> "global"
6960 if closed
6961 then v
6962 else
6963 let ret keymap =
6964 let h = findkeyhash c modename in
6965 KeyMap.iter (Hashtbl.replace h) keymap;
6966 doc path pan anchor c bookmarks
6968 { v with f = pkeymap ret KeyMap.empty }
6970 | Vopen (_, _, _) ->
6971 error "unexpected subelement in doc" s spos
6973 | Vclose "doc" ->
6974 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6975 { v with f = llppconfig }
6977 | Vclose _ -> error "unexpected close in doc" s spos
6979 and pkeymap ret keymap v t spos _ =
6980 match t with
6981 | Vdata | Vcdata -> v
6982 | Vend -> error "unexpected end of input in keymap" s spos
6983 | Vopen ("map", attrs, closed) ->
6984 let r, l = map_of attrs in
6985 let kss = fromstring keys_of_string spos "in" r [] in
6986 let lss = fromstring keys_of_string spos "out" l [] in
6987 let keymap =
6988 match kss with
6989 | [] -> keymap
6990 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
6991 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
6993 if closed
6994 then { v with f = pkeymap ret keymap }
6995 else
6996 let f () = v in
6997 { v with f = skip "map" f }
6999 | Vopen _ ->
7000 error "unexpected subelement in keymap" s spos
7002 | Vclose "keymap" ->
7003 { v with f = ret keymap }
7005 | Vclose _ -> error "unexpected close in keymap" s spos
7007 and pbookmarks path pan anchor c bookmarks v t spos _ =
7008 match t with
7009 | Vdata | Vcdata -> v
7010 | Vend -> error "unexpected end of input in bookmarks" s spos
7011 | Vopen ("item", attrs, closed) ->
7012 let titleent, spage, srely, svisy = bookmark_of attrs in
7013 let page = fromstring int_of_string spos "page" spage 0
7014 and rely = fromstring float_of_string spos "rely" srely 0.0
7015 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7016 let bookmarks =
7017 (unent titleent, 0, (page, rely, visy)) :: bookmarks
7019 if closed
7020 then { v with f = pbookmarks path pan anchor c bookmarks }
7021 else
7022 let f () = v in
7023 { v with f = skip "item" f }
7025 | Vopen _ ->
7026 error "unexpected subelement in bookmarks" s spos
7028 | Vclose "bookmarks" ->
7029 { v with f = doc path pan anchor c bookmarks }
7031 | Vclose _ -> error "unexpected close in bookmarks" s spos
7033 and skip tag f v t spos _ =
7034 match t with
7035 | Vdata | Vcdata -> v
7036 | Vend ->
7037 error ("unexpected end of input in skipped " ^ tag) s spos
7038 | Vopen (tag', _, closed) ->
7039 if closed
7040 then v
7041 else
7042 let f' () = { v with f = skip tag f } in
7043 { v with f = skip tag' f' }
7044 | Vclose ctag ->
7045 if tag = ctag
7046 then f ()
7047 else error ("unexpected close in skipped " ^ tag) s spos
7050 parse { f = toplevel; accu = () } s;
7051 h, dc;
7054 let do_load f ic =
7056 let len = in_channel_length ic in
7057 let s = String.create len in
7058 really_input ic s 0 len;
7059 f s;
7060 with
7061 | Parse_error (msg, s, pos) ->
7062 let subs = subs s pos in
7063 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
7065 | exn ->
7066 failwith ("config load error: " ^ exntos exn)
7069 let defconfpath =
7070 let dir =
7072 let dir = Filename.concat home ".config" in
7073 if Sys.is_directory dir then dir else home
7074 with _ -> home
7076 Filename.concat dir "llpp.conf"
7079 let confpath = ref defconfpath;;
7081 let load1 f =
7082 if Sys.file_exists !confpath
7083 then
7084 match
7085 (try Some (open_in_bin !confpath)
7086 with exn ->
7087 prerr_endline
7088 ("Error opening configuration file `" ^ !confpath ^ "': " ^
7089 exntos exn);
7090 None
7092 with
7093 | Some ic ->
7094 let success =
7096 f (do_load get ic)
7097 with exn ->
7098 prerr_endline
7099 ("Error loading configuration from `" ^ !confpath ^ "': " ^
7100 exntos exn);
7101 false
7103 close_in ic;
7104 success
7106 | None -> false
7107 else
7108 f (Hashtbl.create 0, defconf)
7111 let load () =
7112 let f (h, dc) =
7113 let pc, pb, px, pa =
7115 let key =
7116 if emptystr state.origin
7117 then state.path
7118 else state.origin
7120 Hashtbl.find h (Filename.basename key)
7121 with Not_found -> dc, [], 0, emptyanchor
7123 setconf defconf dc;
7124 setconf conf pc;
7125 state.bookmarks <- pb;
7126 state.x <- px;
7127 if conf.jumpback
7128 then state.anchor <- pa;
7129 cbput state.hists.nav pa;
7130 true
7132 load1 f
7135 let add_attrs bb always dc c =
7136 let ob s a b =
7137 if always || a != b
7138 then Printf.bprintf bb "\n %s='%b'" s a
7139 and op s a b =
7140 if always || a <> b
7141 then Printf.bprintf bb "\n %s='%b'" s (a != None)
7142 and oi s a b =
7143 if always || a != b
7144 then Printf.bprintf bb "\n %s='%d'" s a
7145 and oI s a b =
7146 if always || a != b
7147 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
7148 and oz s a b =
7149 if always || a <> b
7150 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
7151 and oF s a b =
7152 if always || a <> b
7153 then Printf.bprintf bb "\n %s='%f'" s a
7154 and oc s a b =
7155 if always || a <> b
7156 then
7157 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
7158 and oC s a b =
7159 if always || a <> b
7160 then
7161 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
7162 and oR s a b =
7163 if always || a <> b
7164 then
7165 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
7166 and os s a b =
7167 if always || a <> b
7168 then
7169 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7170 and og s a b =
7171 if always || a <> b
7172 then
7173 match a with
7174 | None -> ()
7175 | Some (_N, _A, _B) ->
7176 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7177 and oW s a b =
7178 if always || a <> b
7179 then
7180 let v =
7181 match a with
7182 | None -> "false"
7183 | Some f ->
7184 if f = infinity
7185 then "true"
7186 else string_of_float f
7188 Printf.bprintf bb "\n %s='%s'" s v
7189 and oco s a b =
7190 if always || a <> b
7191 then
7192 match a with
7193 | Cmulti ((n, a, b), _) when n > 1 ->
7194 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7195 | Csplit (n, _) when n > 1 ->
7196 Printf.bprintf bb "\n %s='%d'" s ~-n
7197 | _ -> ()
7198 and obeco s a b =
7199 if always || a <> b
7200 then
7201 match a with
7202 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7203 | _ -> ()
7204 and oFm s a b =
7205 if always || a <> b
7206 then
7207 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7208 and oSv s a b m =
7209 if always || a <> b
7210 then
7211 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7212 and oPm s a b =
7213 if always || a <> b
7214 then
7215 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
7217 oi "width" c.cwinw dc.cwinw;
7218 oi "height" c.cwinh dc.cwinh;
7219 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7220 oi "scroll-handle-height" c.scrollh dc.scrollh;
7221 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7222 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7223 ob "case-insensitive-search" c.icase dc.icase;
7224 ob "preload" c.preload dc.preload;
7225 oi "page-bias" c.pagebias dc.pagebias;
7226 oi "scroll-step" c.scrollstep dc.scrollstep;
7227 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7228 ob "max-height-fit" c.maxhfit dc.maxhfit;
7229 ob "crop-hack" c.crophack dc.crophack;
7230 oW "throttle" c.maxwait dc.maxwait;
7231 ob "highlight-links" c.hlinks dc.hlinks;
7232 ob "under-cursor-info" c.underinfo dc.underinfo;
7233 oi "vertical-margin" c.interpagespace dc.interpagespace;
7234 oz "zoom" c.zoom dc.zoom;
7235 ob "presentation" c.presentation dc.presentation;
7236 oi "rotation-angle" c.angle dc.angle;
7237 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7238 oFm "fit-model" c.fitmodel dc.fitmodel;
7239 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7240 oi "tex-count" c.texcount dc.texcount;
7241 oi "slice-height" c.sliceheight dc.sliceheight;
7242 oi "thumbnail-width" c.thumbw dc.thumbw;
7243 ob "persistent-location" c.jumpback dc.jumpback;
7244 oc "background-color" c.bgcolor dc.bgcolor;
7245 oi "tile-width" c.tilew dc.tilew;
7246 oi "tile-height" c.tileh dc.tileh;
7247 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7248 ob "checkers" c.checkers dc.checkers;
7249 oi "aalevel" c.aalevel dc.aalevel;
7250 ob "trim-margins" c.trimmargins dc.trimmargins;
7251 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7252 os "uri-launcher" c.urilauncher dc.urilauncher;
7253 os "path-launcher" c.pathlauncher dc.pathlauncher;
7254 oC "color-space" c.colorspace dc.colorspace;
7255 ob "invert-colors" c.invert dc.invert;
7256 oF "brightness" c.colorscale dc.colorscale;
7257 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7258 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7259 oco "columns" c.columns dc.columns;
7260 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7261 os "selection-command" c.selcmd dc.selcmd;
7262 os "synctex-command" c.stcmd dc.stcmd;
7263 os "pax-command" c.paxcmd dc.paxcmd;
7264 ob "update-cursor" c.updatecurs dc.updatecurs;
7265 oi "hint-font-size" c.hfsize dc.hfsize;
7266 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7267 oF "page-scroll-scale" c.pgscale dc.pgscale;
7268 ob "use-pbo" c.usepbo dc.usepbo;
7269 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7270 ob "remote-in-a-new-instance" c.riani dc.riani;
7271 op "point-and-x" c.pax dc.pax;
7272 oPm "point-and-x-mark" c.paxmark dc.paxmark;
7275 let keymapsbuf always dc c =
7276 let bb = Buffer.create 16 in
7277 let rec loop = function
7278 | [] -> ()
7279 | (modename, h) :: rest ->
7280 let dh = findkeyhash dc modename in
7281 if always || h <> dh
7282 then (
7283 if Hashtbl.length h > 0
7284 then (
7285 if Buffer.length bb > 0
7286 then Buffer.add_char bb '\n';
7287 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7288 Hashtbl.iter (fun i o ->
7289 let isdifferent = always ||
7291 let dO = Hashtbl.find dh i in
7292 dO <> o
7293 with Not_found -> true
7295 if isdifferent
7296 then
7297 let addkm (k, m) =
7298 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7299 if Wsi.withalt m then Buffer.add_string bb "alt-";
7300 if Wsi.withshift m then Buffer.add_string bb "shift-";
7301 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7302 Buffer.add_string bb (Wsi.keyname k);
7304 let addkms l =
7305 let rec loop = function
7306 | [] -> ()
7307 | km :: [] -> addkm km
7308 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7310 loop l
7312 Buffer.add_string bb "<map in='";
7313 addkm i;
7314 match o with
7315 | KMinsrt km ->
7316 Buffer.add_string bb "' out='";
7317 addkm km;
7318 Buffer.add_string bb "'/>\n"
7320 | KMinsrl kms ->
7321 Buffer.add_string bb "' out='";
7322 addkms kms;
7323 Buffer.add_string bb "'/>\n"
7325 | KMmulti (ins, kms) ->
7326 Buffer.add_char bb ' ';
7327 addkms ins;
7328 Buffer.add_string bb "' out='";
7329 addkms kms;
7330 Buffer.add_string bb "'/>\n"
7331 ) h;
7332 Buffer.add_string bb "</keymap>";
7335 loop rest
7337 loop c.keyhashes;
7341 let save () =
7342 let uifontsize = fstate.fontsize in
7343 let bb = Buffer.create 32768 in
7344 let relx = float state.x /. float state.winw in
7345 let w, h, x =
7346 let cx w = truncate (relx *. float w) in
7347 List.fold_left
7348 (fun (w, h, x) ws ->
7349 match ws with
7350 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7351 | Wsi.MaxVert -> (w, conf.cwinh, x)
7352 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7354 (state.winw, state.winh, state.x) state.winstate
7356 conf.cwinw <- w;
7357 conf.cwinh <- h;
7358 let f (h, dc) =
7359 let dc = if conf.bedefault then conf else dc in
7360 Buffer.add_string bb "<llppconfig>\n";
7362 if nonemptystr !fontpath
7363 then
7364 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7365 uifontsize
7366 !fontpath
7367 else (
7368 if uifontsize <> 14
7369 then
7370 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7373 Buffer.add_string bb "<defaults";
7374 add_attrs bb true dc dc;
7375 let kb = keymapsbuf true dc dc in
7376 if Buffer.length kb > 0
7377 then (
7378 Buffer.add_string bb ">\n";
7379 Buffer.add_buffer bb kb;
7380 Buffer.add_string bb "\n</defaults>\n";
7382 else Buffer.add_string bb "/>\n";
7384 let adddoc path pan anchor c bookmarks =
7385 if bookmarks == [] && c = dc && anchor = emptyanchor
7386 then ()
7387 else (
7388 Printf.bprintf bb "<doc path='%s'"
7389 (enent path 0 (String.length path));
7391 if anchor <> emptyanchor
7392 then (
7393 let n, rely, visy = anchor in
7394 Printf.bprintf bb " page='%d'" n;
7395 if rely > 1e-6
7396 then
7397 Printf.bprintf bb " rely='%f'" rely
7399 if abs_float visy > 1e-6
7400 then
7401 Printf.bprintf bb " visy='%f'" visy
7405 if pan != 0
7406 then Printf.bprintf bb " pan='%d'" pan;
7408 add_attrs bb false dc c;
7409 let kb = keymapsbuf false dc c in
7411 begin match bookmarks with
7412 | [] ->
7413 if Buffer.length kb > 0
7414 then (
7415 Buffer.add_string bb ">\n";
7416 Buffer.add_buffer bb kb;
7417 Buffer.add_string bb "\n</doc>\n";
7419 else Buffer.add_string bb "/>\n"
7420 | _ ->
7421 Buffer.add_string bb ">\n<bookmarks>\n";
7422 List.iter (fun (title, _level, (page, rely, visy)) ->
7423 Printf.bprintf bb
7424 "<item title='%s' page='%d'"
7425 (enent title 0 (String.length title))
7426 page
7428 if rely > 1e-6
7429 then
7430 Printf.bprintf bb " rely='%f'" rely
7432 if abs_float visy > 1e-6
7433 then
7434 Printf.bprintf bb " visy='%f'" visy
7436 Buffer.add_string bb "/>\n";
7437 ) bookmarks;
7438 Buffer.add_string bb "</bookmarks>";
7439 if Buffer.length kb > 0
7440 then (
7441 Buffer.add_string bb "\n";
7442 Buffer.add_buffer bb kb;
7444 Buffer.add_string bb "\n</doc>\n";
7445 end;
7449 let pan, conf =
7450 match state.mode with
7451 | Birdseye (c, pan, _, _, _) ->
7452 let beyecolumns =
7453 match conf.columns with
7454 | Cmulti ((c, _, _), _) -> Some c
7455 | Csingle _ -> None
7456 | Csplit _ -> None
7457 and columns =
7458 match c.columns with
7459 | Cmulti (c, _) -> Cmulti (c, [||])
7460 | Csingle _ -> Csingle [||]
7461 | Csplit _ -> failwith "quit from bird's eye while split"
7463 pan, { c with beyecolumns = beyecolumns; columns = columns }
7464 | _ -> x, conf
7466 let basename = Filename.basename
7467 (if emptystr state.origin then state.path else state.origin)
7469 adddoc basename pan (getanchor ())
7470 (let conf =
7471 let autoscrollstep =
7472 match state.autoscroll with
7473 | Some step -> step
7474 | None -> conf.autoscrollstep
7476 match state.mode with
7477 | Birdseye (bc, _, _, _, _) ->
7478 { conf with
7479 zoom = bc.zoom;
7480 presentation = bc.presentation;
7481 interpagespace = bc.interpagespace;
7482 maxwait = bc.maxwait;
7483 autoscrollstep = autoscrollstep }
7484 | _ -> { conf with autoscrollstep = autoscrollstep }
7485 in conf)
7486 (if conf.savebmarks then state.bookmarks else []);
7488 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7489 if basename <> path
7490 then adddoc path x anchor c bookmarks
7491 ) h;
7492 Buffer.add_string bb "</llppconfig>\n";
7493 true;
7495 if load1 f && Buffer.length bb > 0
7496 then
7498 let tmp = !confpath ^ ".tmp" in
7499 let oc = open_out_bin tmp in
7500 Buffer.output_buffer oc bb;
7501 close_out oc;
7502 Unix.rename tmp !confpath;
7503 with exn ->
7504 prerr_endline
7505 ("error while saving configuration: " ^ exntos exn)
7507 end;;
7509 let adderrmsg src msg =
7510 Buffer.add_string state.errmsgs msg;
7511 state.newerrmsgs <- true;
7512 G.postRedisplay src
7515 let adderrfmt src fmt =
7516 Format.kprintf (fun s -> adderrmsg src s) fmt;
7519 let ract cmds =
7520 let cl = splitatspace cmds in
7521 let scan s fmt f =
7522 try Scanf.sscanf s fmt f
7523 with exn ->
7524 adderrfmt "remote exec"
7525 "error processing '%S': %s\n" cmds (exntos exn)
7527 match cl with
7528 | "reload" :: [] -> reload ()
7529 | "goto" :: args :: [] ->
7530 scan args "%u %f %f"
7531 (fun pageno x y ->
7532 let cmd, _ = state.geomcmds in
7533 if emptystr cmd
7534 then gotopagexy pageno x y
7535 else
7536 let f prevf () =
7537 gotopagexy pageno x y;
7538 prevf ()
7540 state.reprf <- f state.reprf
7542 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7543 | "rect" :: args :: [] ->
7544 scan args "%u %u %f %f %f %f"
7545 (fun pageno color x0 y0 x1 y1 ->
7546 onpagerect pageno (fun w h ->
7547 let _,w1,h1,_ = getpagedim pageno in
7548 let sw = float w1 /. float w
7549 and sh = float h1 /. float h in
7550 let x0s = x0 *. sw
7551 and x1s = x1 *. sw
7552 and y0s = y0 *. sh
7553 and y1s = y1 *. sh in
7554 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7555 debugrect rect;
7556 state.rects <- (pageno, color, rect) :: state.rects;
7557 G.postRedisplay "rect";
7560 | "activatewin" :: [] -> Wsi.activatewin ()
7561 | "quit" :: [] -> raise Quit
7562 | _ ->
7563 adderrfmt "remote command"
7564 "error processing remote command: %S\n" cmds;
7567 let remote =
7568 let scratch = String.create 80 in
7569 let buf = Buffer.create 80 in
7570 fun fd ->
7571 let rec tempfr () =
7572 try Some (Unix.read fd scratch 0 80)
7573 with
7574 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7575 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7576 | exn -> raise exn
7578 match tempfr () with
7579 | None -> Some fd
7580 | Some n ->
7581 if n = 0
7582 then (
7583 Unix.close fd;
7584 if Buffer.length buf > 0
7585 then (
7586 let s = Buffer.contents buf in
7587 Buffer.clear buf;
7588 ract s;
7590 None
7592 else
7593 let rec eat ppos =
7594 let nlpos =
7596 let pos = String.index_from scratch ppos '\n' in
7597 if pos >= n then -1 else pos
7598 with Not_found -> -1
7600 if nlpos >= 0
7601 then (
7602 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7603 let s = Buffer.contents buf in
7604 Buffer.clear buf;
7605 ract s;
7606 eat (nlpos+1);
7608 else (
7609 Buffer.add_substring buf scratch ppos (n-ppos);
7610 Some fd
7612 in eat 0
7615 let remoteopen path =
7616 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7617 with exn ->
7618 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7619 None
7622 let () =
7623 let trimcachepath = ref "" in
7624 let rcmdpath = ref "" in
7625 selfexec := Sys.executable_name;
7626 Arg.parse
7627 (Arg.align
7628 [("-p", Arg.String (fun s -> state.password <- s),
7629 "<password> Set password");
7631 ("-f", Arg.String
7632 (fun s ->
7633 Config.fontpath := s;
7634 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7636 "<path> Set path to the user interface font");
7638 ("-c", Arg.String
7639 (fun s ->
7640 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7641 Config.confpath := s),
7642 "<path> Set path to the configuration file");
7644 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7645 "<path> Set path to the trim cache file");
7647 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7648 "<named-destination> Set named destination");
7650 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7651 ("-cxack", Arg.Set cxack, " Cut corners");
7653 ("-remote", Arg.String (fun s -> rcmdpath := s),
7654 "<path> Set path to the remote commands source");
7656 ("-origin", Arg.String (fun s -> state.origin <- s),
7657 "<original-path> Set original path");
7659 ("-v", Arg.Unit (fun () ->
7660 Printf.printf
7661 "%s\nconfiguration path: %s\n"
7662 (version ())
7663 Config.defconfpath
7665 exit 0), " Print version and exit");
7668 (fun s -> state.path <- s)
7669 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7671 if !wtmode
7672 then selfexec := !selfexec ^ " -wtmode";
7674 if emptystr state.path
7675 then (prerr_endline "file name missing"; exit 1);
7677 if not (Config.load ())
7678 then prerr_endline "failed to load configuration";
7680 let wsfd, winw, winh = Wsi.init (object
7681 val mutable m_hack = false
7682 method expose = if not m_hack then G.postRedisplay "expose"
7683 method visible = G.postRedisplay "visible"
7684 method display = m_hack <- false; display ()
7685 method reshape w h =
7686 m_hack <- w < state.winw && h < state.winh;
7687 reshape w h
7688 method mouse b d x y m = state.uioh <- state.uioh#button b d x y m
7689 method motion x y =
7690 state.mpos <- (x, y);
7691 state.uioh <- state.uioh#motion x y
7692 method pmotion x y =
7693 state.mpos <- (x, y);
7694 state.uioh <- state.uioh#pmotion x y
7695 method key k m =
7696 let mascm = m land (
7697 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7698 ) in
7699 match state.keystate with
7700 | KSnone ->
7701 let km = k, mascm in
7702 begin
7703 match
7704 let modehash = state.uioh#modehash in
7705 try Hashtbl.find modehash km
7706 with Not_found ->
7707 try Hashtbl.find (findkeyhash conf "global") km
7708 with Not_found -> KMinsrt (k, m)
7709 with
7710 | KMinsrt (k, m) -> keyboard k m
7711 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7712 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7714 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7715 List.iter (fun (k, m) -> keyboard k m) insrt;
7716 state.keystate <- KSnone
7717 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7718 state.keystate <- KSinto (keys, insrt)
7719 | _ ->
7720 state.keystate <- KSnone
7722 method enter x y =
7723 state.mpos <- (x, y);
7724 state.uioh <- state.uioh#pmotion x y
7725 method leave = state.mpos <- (-1, -1)
7726 method winstate wsl = state.winstate <- wsl; m_hack <- false
7727 method quit = raise Quit
7728 end) conf.cwinw conf.cwinh (platform = Posx) in
7730 state.wsfd <- wsfd;
7732 if not (
7733 List.exists GlMisc.check_extension
7734 [ "GL_ARB_texture_rectangle"
7735 ; "GL_EXT_texture_recangle"
7736 ; "GL_NV_texture_rectangle" ]
7738 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7740 if (
7741 let r = GlMisc.get_string `renderer in
7742 let p = "Mesa DRI Intel(" in
7743 let l = String.length p in
7744 String.length r > l && String.sub r 0 l = p
7746 then (
7747 defconf.sliceheight <- 1024;
7748 defconf.texcount <- 32;
7749 defconf.usepbo <- true;
7752 let cr, sw =
7753 match Ne.pipe () with
7754 | Ne.Exn exn ->
7755 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7756 exit 1
7757 | Ne.Res rw -> rw
7758 and sr, cw =
7759 match Ne.pipe () with
7760 | Ne.Exn exn ->
7761 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7762 exit 1
7763 | Ne.Res rw -> rw
7766 cloexec cr;
7767 cloexec sw;
7768 cloexec sr;
7769 cloexec cw;
7771 setcheckers conf.checkers;
7772 redirectstderr ();
7773 if conf.redirectstderr
7774 then
7775 at_exit (fun () ->
7776 let s = Buffer.contents state.errmsgs ^
7777 (match state.errfd with
7778 | Some fd ->
7779 let s = String.create (80*24) in
7780 let n =
7782 let r, _, _ = Unix.select [fd] [] [] 0.0 in
7783 if List.mem fd r
7784 then Unix.read fd s 0 (String.length s)
7785 else 0
7786 with _ -> 0
7788 if n = 0
7789 then ""
7790 else String.sub s 0 n
7791 | None -> ""
7794 try ignore (Unix.write state.stderr s 0 (String.length s))
7795 with exn -> print_endline (exntos exn)
7799 init (cr, cw) (
7800 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7801 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7802 !Config.fontpath, !trimcachepath,
7803 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7805 state.sr <- sr;
7806 state.sw <- sw;
7807 state.text <- "Opening " ^ (mbtoutf8 state.path);
7808 reshape winw winh;
7809 opendoc state.path state.password;
7810 state.uioh <- uioh;
7811 display ();
7812 Wsi.mapwin ();
7813 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7814 let optrfd =
7815 ref (
7816 if nonemptystr !rcmdpath
7817 then remoteopen !rcmdpath
7818 else None
7822 let rec loop deadline =
7823 let r =
7824 match state.errfd with
7825 | None -> [state.sr; state.wsfd]
7826 | Some fd -> [state.sr; state.wsfd; fd]
7828 let r =
7829 match !optrfd with
7830 | None -> r
7831 | Some fd -> fd :: r
7833 if state.redisplay
7834 then (
7835 state.redisplay <- false;
7836 display ();
7838 let timeout =
7839 let now = now () in
7840 if deadline > now
7841 then (
7842 if deadline = infinity
7843 then ~-.1.0
7844 else max 0.0 (deadline -. now)
7846 else 0.0
7848 let r, _, _ =
7849 try Unix.select r [] [] timeout
7850 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7852 begin match r with
7853 | [] ->
7854 state.ghyll None;
7855 let newdeadline =
7856 if state.ghyll == noghyll
7857 then
7858 match state.autoscroll with
7859 | Some step when step != 0 ->
7860 let y = state.y + step in
7861 let y =
7862 if y < 0
7863 then state.maxy
7864 else if y >= state.maxy then 0 else y
7866 gotoy y;
7867 if state.mode = View
7868 then state.text <- "";
7869 deadline +. 0.01
7870 | _ -> infinity
7871 else deadline +. 0.01
7873 loop newdeadline
7875 | l ->
7876 let rec checkfds = function
7877 | [] -> ()
7878 | fd :: rest when fd = state.sr ->
7879 let cmd = readcmd state.sr in
7880 act cmd;
7881 checkfds rest
7883 | fd :: rest when fd = state.wsfd ->
7884 Wsi.readresp fd;
7885 checkfds rest
7887 | fd :: rest when Some fd = !optrfd ->
7888 begin match remote fd with
7889 | None -> optrfd := remoteopen !rcmdpath;
7890 | opt -> optrfd := opt
7891 end;
7892 checkfds rest
7894 | fd :: rest ->
7895 let s = String.create 80 in
7896 let n = tempfailureretry (Unix.read fd s 0) 80 in
7897 if conf.redirectstderr
7898 then (
7899 Buffer.add_substring state.errmsgs s 0 n;
7900 state.newerrmsgs <- true;
7901 state.redisplay <- true;
7903 else (
7904 prerr_string (String.sub s 0 n);
7905 flush stderr;
7907 checkfds rest
7909 checkfds l;
7910 let newdeadline =
7911 let deadline1 =
7912 if deadline = infinity
7913 then now () +. 0.01
7914 else deadline
7916 match state.autoscroll with
7917 | Some step when step != 0 -> deadline1
7918 | _ -> if state.ghyll == noghyll then infinity else deadline1
7920 loop newdeadline
7921 end;
7924 loop infinity;
7925 with Quit ->
7926 Config.save ();