Fix page marking
[llpp.git] / main.ml
blob13e2576d217891cead61686e2066dc67dda825d1
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
1017 (try
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 let r = bsearch 0 (state.pagecount-1) in
1376 let layoutN ((columns, coverA, coverB), b) y sh =
1377 let sh = sh - (hscrollh ()) in
1378 let rec fold accu n =
1379 if n = Array.length b
1380 then accu
1381 else
1382 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1383 if (vy - y) > sh &&
1384 (n = coverA - 1
1385 || n = state.pagecount - coverB
1386 || (n - coverA) mod columns = columns - 1)
1387 then accu
1388 else
1389 let accu =
1390 if vy + h > y
1391 then
1392 let pagey = max 0 (y - vy) in
1393 let pagedispy = if pagey > 0 then 0 else vy - y in
1394 let pagedispx, pagex =
1395 let pdx =
1396 if n = coverA - 1 || n = state.pagecount - coverB
1397 then state.x + (wadjsb state.winw - w) / 2
1398 else dx + xoff + state.x
1400 if pdx < 0
1401 then 0, -pdx
1402 else pdx, 0
1404 let pagevw =
1405 let vw = wadjsb state.winw - pagedispx in
1406 let pw = w - pagex in
1407 min vw pw
1409 let pagevh = min (h - pagey) (sh - pagedispy) in
1410 if pagevw > 0 && pagevh > 0
1411 then
1412 let e =
1413 { pageno = n
1414 ; pagedimno = pdimno
1415 ; pagew = w
1416 ; pageh = h
1417 ; pagex = pagex
1418 ; pagey = pagey
1419 ; pagevw = pagevw
1420 ; pagevh = pagevh
1421 ; pagedispx = pagedispx
1422 ; pagedispy = pagedispy
1423 ; pagecol = 0
1426 e :: accu
1427 else
1428 accu
1429 else
1430 accu
1432 fold accu (n+1)
1434 if Array.length b = 0
1435 then []
1436 else List.rev (fold [] (page_of_y y))
1439 let layoutS (columns, b) y sh =
1440 let sh = sh - hscrollh () in
1441 let rec fold accu n =
1442 if n = Array.length b
1443 then accu
1444 else
1445 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1446 if (vy - y) > sh
1447 then accu
1448 else
1449 let accu =
1450 if vy + pageh > y
1451 then
1452 let x = xoff + state.x in
1453 let pagey = max 0 (y - vy) in
1454 let pagedispy = if pagey > 0 then 0 else vy - y in
1455 let pagedispx, pagex =
1456 if px = 0
1457 then (
1458 if x < 0
1459 then 0, -x
1460 else x, 0
1462 else (
1463 let px = px - x in
1464 if px < 0
1465 then -px, 0
1466 else 0, px
1469 let pagecolw = pagew/columns in
1470 let pagedispx =
1471 if pagecolw < state.winw
1472 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
1473 else pagedispx
1475 let pagevw =
1476 let vw = wadjsb state.winw - pagedispx in
1477 let pw = pagew - pagex in
1478 min vw pw
1480 let pagevw = min pagevw pagecolw in
1481 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1482 if pagevw > 0 && pagevh > 0
1483 then
1484 let e =
1485 { pageno = n/columns
1486 ; pagedimno = pdimno
1487 ; pagew = pagew
1488 ; pageh = pageh
1489 ; pagex = pagex
1490 ; pagey = pagey
1491 ; pagevw = pagevw
1492 ; pagevh = pagevh
1493 ; pagedispx = pagedispx
1494 ; pagedispy = pagedispy
1495 ; pagecol = n mod columns
1498 e :: accu
1499 else
1500 accu
1501 else
1502 accu
1504 fold accu (n+1)
1506 List.rev (fold [] 0)
1509 let layout y sh =
1510 if nogeomcmds state.geomcmds
1511 then
1512 match conf.columns with
1513 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1514 | Cmulti c -> layoutN c y sh
1515 | Csplit s -> layoutS s y sh
1516 else []
1519 let clamp incr =
1520 let y = state.y + incr in
1521 let y = max 0 y in
1522 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1526 let itertiles l f =
1527 let tilex = l.pagex mod conf.tilew in
1528 let tiley = l.pagey mod conf.tileh in
1530 let col = l.pagex / conf.tilew in
1531 let row = l.pagey / conf.tileh in
1533 let rec rowloop row y0 dispy h =
1534 if h = 0
1535 then ()
1536 else (
1537 let dh = conf.tileh - y0 in
1538 let dh = min h dh in
1539 let rec colloop col x0 dispx w =
1540 if w = 0
1541 then ()
1542 else (
1543 let dw = conf.tilew - x0 in
1544 let dw = min w dw in
1546 f col row dispx dispy x0 y0 dw dh;
1547 colloop (col+1) 0 (dispx+dw) (w-dw)
1550 colloop col tilex l.pagedispx l.pagevw;
1551 rowloop (row+1) 0 (dispy+dh) (h-dh)
1554 if l.pagevw > 0 && l.pagevh > 0
1555 then rowloop row tiley l.pagedispy l.pagevh;
1558 let gettileopaque l col row =
1559 let key =
1560 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1562 try Some (Hashtbl.find state.tilemap key)
1563 with Not_found -> None
1566 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1567 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1568 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1571 let drawtiles l color =
1572 GlDraw.color color;
1573 let f col row x y tilex tiley w h =
1574 match gettileopaque l col row with
1575 | Some (opaque, _, t) ->
1576 let params = x, y, w, h, tilex, tiley in
1577 if conf.invert
1578 then (
1579 Gl.enable `blend;
1580 GlFunc.blend_func `zero `one_minus_src_color;
1582 drawtile params opaque;
1583 if conf.invert
1584 then Gl.disable `blend;
1585 if conf.debug
1586 then (
1587 let s = Printf.sprintf
1588 "%d[%d,%d] %f sec"
1589 l.pageno col row t
1591 let w = measurestr fstate.fontsize s in
1592 GlMisc.push_attrib [`current];
1593 GlDraw.color (0.0, 0.0, 0.0);
1594 GlDraw.rect
1595 (float (x-2), float (y-2))
1596 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1597 GlDraw.color (1.0, 1.0, 1.0);
1598 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1599 GlMisc.pop_attrib ();
1602 | _ ->
1603 let w =
1604 let lw = wadjsb state.winw - x in
1605 min lw w
1606 and h =
1607 let lh = state.winh - y in
1608 min lh h
1610 begin match state.texid with
1611 | Some id ->
1612 Gl.enable `texture_2d;
1613 GlTex.bind_texture `texture_2d id;
1614 let x0 = float x
1615 and y0 = float y
1616 and x1 = float (x+w)
1617 and y1 = float (y+h) in
1619 let tw = float w /. 16.0
1620 and th = float h /. 16.0 in
1621 let tx0 = float tilex /. 16.0
1622 and ty0 = float tiley /. 16.0 in
1623 let tx1 = tx0 +. tw
1624 and ty1 = ty0 +. th in
1625 GlDraw.begins `quads;
1626 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1627 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1628 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1629 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1630 GlDraw.ends ();
1632 Gl.disable `texture_2d;
1633 | None ->
1634 GlDraw.color (1.0, 1.0, 1.0);
1635 GlDraw.rect
1636 (float x, float y)
1637 (float (x+w), float (y+h));
1638 end;
1639 if w > 128 && h > fstate.fontsize + 10
1640 then (
1641 GlDraw.color (0.0, 0.0, 0.0);
1642 let c, r =
1643 if conf.verbose
1644 then (col*conf.tilew, row*conf.tileh)
1645 else col, row
1647 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1649 GlDraw.color color;
1651 itertiles l f
1654 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1656 let tilevisible1 l x y =
1657 let ax0 = l.pagex
1658 and ax1 = l.pagex + l.pagevw
1659 and ay0 = l.pagey
1660 and ay1 = l.pagey + l.pagevh in
1662 let bx0 = x
1663 and by0 = y in
1664 let bx1 = min (bx0 + conf.tilew) l.pagew
1665 and by1 = min (by0 + conf.tileh) l.pageh in
1667 let rx0 = max ax0 bx0
1668 and ry0 = max ay0 by0
1669 and rx1 = min ax1 bx1
1670 and ry1 = min ay1 by1 in
1672 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1673 nonemptyintersection
1676 let tilevisible layout n x y =
1677 let rec findpageinlayout m = function
1678 | l :: rest when l.pageno = n ->
1679 tilevisible1 l x y || (
1680 match conf.columns with
1681 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1682 | _ -> false
1684 | _ :: rest -> findpageinlayout 0 rest
1685 | [] -> false
1687 findpageinlayout 0 layout;
1690 let tileready l x y =
1691 tilevisible1 l x y &&
1692 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1695 let tilepage n p layout =
1696 let rec loop = function
1697 | l :: rest ->
1698 if l.pageno = n
1699 then
1700 let f col row _ _ _ _ _ _ =
1701 if state.currently = Idle
1702 then
1703 match gettileopaque l col row with
1704 | Some _ -> ()
1705 | None ->
1706 let x = col*conf.tilew
1707 and y = row*conf.tileh in
1708 let w =
1709 let w = l.pagew - x in
1710 min w conf.tilew
1712 let h =
1713 let h = l.pageh - y in
1714 min h conf.tileh
1716 let pbo =
1717 if conf.usepbo
1718 then getpbo w h conf.colorspace
1719 else "0"
1721 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1722 state.currently <-
1723 Tiling (
1724 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1725 conf.tilew, conf.tileh
1728 itertiles l f;
1729 else
1730 loop rest
1732 | [] -> ()
1734 if nogeomcmds state.geomcmds
1735 then loop layout;
1738 let preloadlayout y =
1739 let y = if y < state.winh then 0 else y - state.winh in
1740 let h = state.winh*3 in
1741 layout y h;
1744 let load pages =
1745 let rec loop pages =
1746 if state.currently != Idle
1747 then ()
1748 else
1749 match pages with
1750 | l :: rest ->
1751 begin match getopaque l.pageno with
1752 | None ->
1753 wcmd "page %d %d" l.pageno l.pagedimno;
1754 state.currently <- Loading (l, state.gen);
1755 | Some opaque ->
1756 tilepage l.pageno opaque pages;
1757 loop rest
1758 end;
1759 | _ -> ()
1761 if nogeomcmds state.geomcmds
1762 then loop pages
1765 let preload pages =
1766 load pages;
1767 if conf.preload && state.currently = Idle
1768 then load (preloadlayout state.y);
1771 let layoutready layout =
1772 let rec fold all ls =
1773 all && match ls with
1774 | l :: rest ->
1775 let seen = ref false in
1776 let allvisible = ref true in
1777 let foo col row _ _ _ _ _ _ =
1778 seen := true;
1779 allvisible := !allvisible &&
1780 begin match gettileopaque l col row with
1781 | Some _ -> true
1782 | None -> false
1785 itertiles l foo;
1786 fold (!seen && !allvisible) rest
1787 | [] -> true
1789 let alltilesvisible = fold true layout in
1790 alltilesvisible;
1793 let gotoy y =
1794 let y = bound y 0 state.maxy in
1795 let y, layout, proceed =
1796 match conf.maxwait with
1797 | Some time when state.ghyll == noghyll ->
1798 begin match state.throttle with
1799 | None ->
1800 let layout = layout y state.winh in
1801 let ready = layoutready layout in
1802 if not ready
1803 then (
1804 load layout;
1805 state.throttle <- Some (layout, y, now ());
1807 else G.postRedisplay "gotoy showall (None)";
1808 y, layout, ready
1809 | Some (_, _, started) ->
1810 let dt = now () -. started in
1811 if dt > time
1812 then (
1813 state.throttle <- None;
1814 let layout = layout y state.winh in
1815 load layout;
1816 G.postRedisplay "maxwait";
1817 y, layout, true
1819 else -1, [], false
1822 | _ ->
1823 let layout = layout y state.winh in
1824 if not !wtmode || layoutready layout
1825 then G.postRedisplay "gotoy ready";
1826 y, layout, true
1828 if proceed
1829 then (
1830 state.y <- y;
1831 state.layout <- layout;
1832 begin match state.mode with
1833 | LinkNav (Ltexact (pageno, linkno)) ->
1834 let rec loop = function
1835 | [] ->
1836 state.mode <- LinkNav (Ltgendir 0)
1837 | l :: _ when l.pageno = pageno ->
1838 begin match getopaque pageno with
1839 | None ->
1840 state.mode <- LinkNav (Ltgendir 0)
1841 | Some opaque ->
1842 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1843 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1844 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1845 then state.mode <- LinkNav (Ltgendir 0)
1847 | _ :: rest -> loop rest
1849 loop layout
1850 | _ -> ()
1851 end;
1852 begin match state.mode with
1853 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1854 if not (pagevisible layout pageno)
1855 then (
1856 match state.layout with
1857 | [] -> ()
1858 | l :: _ ->
1859 state.mode <- Birdseye (
1860 conf, leftx, l.pageno, hooverpageno, anchor
1863 | LinkNav (Ltgendir dir as lt) ->
1864 let linknav =
1865 let rec loop = function
1866 | [] -> lt
1867 | l :: rest ->
1868 match getopaque l.pageno with
1869 | None -> loop rest
1870 | Some opaque ->
1871 let link =
1872 let ld =
1873 if dir = 0
1874 then LDfirstvisible (l.pagex, l.pagey, dir)
1875 else (
1876 if dir > 0 then LDfirst else LDlast
1879 findlink opaque ld
1881 match link with
1882 | Lnotfound -> loop rest
1883 | Lfound n ->
1884 showlinktype (getlink opaque n);
1885 Ltexact (l.pageno, n)
1887 loop state.layout
1889 state.mode <- LinkNav linknav
1890 | _ -> ()
1891 end;
1892 preload layout;
1894 state.ghyll <- noghyll;
1895 if conf.updatecurs
1896 then (
1897 let mx, my = state.mpos in
1898 updateunder mx my;
1902 let conttiling pageno opaque =
1903 tilepage pageno opaque
1904 (if conf.preload then preloadlayout state.y else state.layout)
1907 let gotoy_and_clear_text y =
1908 if not conf.verbose then state.text <- "";
1909 gotoy y;
1912 let getanchor1 l =
1913 let top =
1914 let coloff = l.pagecol * l.pageh in
1915 float (l.pagey + coloff) /. float l.pageh
1917 let dtop =
1918 if l.pagedispy = 0
1919 then
1921 else (
1922 if conf.presentation
1923 then float l.pagedispy /. float (calcips l.pageh)
1924 else float l.pagedispy /. float conf.interpagespace
1927 (l.pageno, top, dtop)
1930 let getanchor () =
1931 match state.layout with
1932 | l :: _ -> getanchor1 l
1933 | [] ->
1934 let n = page_of_y state.y in
1935 if n = -1
1936 then state.anchor
1937 else
1938 let y, h = getpageyh n in
1939 let dy = y - state.y in
1940 let dtop =
1941 if conf.presentation
1942 then
1943 let ips = calcips h in
1944 float (dy + ips) /. float ips
1945 else
1946 float dy /. float conf.interpagespace
1948 (n, 0.0, dtop)
1951 let getanchory (n, top, dtop) =
1952 let y, h = getpageyh n in
1953 if conf.presentation
1954 then
1955 let ips = calcips h in
1956 y + truncate (top*.float h -. dtop*.float ips) + ips;
1957 else
1958 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
1961 let gotoanchor anchor =
1962 gotoy (getanchory anchor);
1965 let addnav () =
1966 cbput state.hists.nav (getanchor ());
1969 let getnav dir =
1970 let anchor = cbgetc state.hists.nav dir in
1971 getanchory anchor;
1974 let gotoghyll y =
1975 let scroll f n a b =
1976 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1977 let snake f a b =
1978 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1979 if f < a
1980 then s (float f /. float a)
1981 else (
1982 if f > b
1983 then 1.0 -. s ((float (f-b) /. float (n-b)))
1984 else 1.0
1987 snake f a b
1988 and summa n a b =
1989 let ins = float a *. 0.5
1990 and outs = float (n-b) *. 0.5 in
1991 let ones = b - a in
1992 ins +. outs +. float ones
1994 let rec set (_N, _A, _B) y sy =
1995 let sum = summa _N _A _B in
1996 let dy = float (y - sy) in
1997 state.ghyll <- (
1998 let rec gf n y1 o =
1999 if n >= _N
2000 then state.ghyll <- noghyll
2001 else
2002 let go n =
2003 let s = scroll n _N _A _B in
2004 let y1 = y1 +. ((s *. dy) /. sum) in
2005 gotoy_and_clear_text (truncate y1);
2006 state.ghyll <- gf (n+1) y1;
2008 match o with
2009 | None -> go n
2010 | Some y' -> set (_N/2, 1, 1) y' state.y
2012 gf 0 (float state.y)
2015 match conf.ghyllscroll with
2016 | None ->
2017 gotoy_and_clear_text y
2018 | Some nab ->
2019 if state.ghyll == noghyll
2020 then set nab y state.y
2021 else state.ghyll (Some y)
2024 let gotopage n top =
2025 let y, h = getpageyh n in
2026 let y = y + (truncate (top *. float h)) in
2027 gotoghyll y
2030 let gotopage1 n top =
2031 let y = getpagey n in
2032 let y = y + top in
2033 gotoghyll y
2036 let invalidate s f =
2037 state.layout <- [];
2038 state.pdims <- [];
2039 state.rects <- [];
2040 state.rects1 <- [];
2041 match state.geomcmds with
2042 | ps, [] when emptystr ps ->
2043 f ();
2044 state.geomcmds <- s, [];
2046 | ps, [] ->
2047 state.geomcmds <- ps, [s, f];
2049 | ps, (s', _) :: rest when s' = s ->
2050 state.geomcmds <- ps, ((s, f) :: rest);
2052 | ps, cmds ->
2053 state.geomcmds <- ps, ((s, f) :: cmds);
2056 let flushpages () =
2057 Hashtbl.iter (fun _ opaque ->
2058 wcmd "freepage %s" opaque;
2059 ) state.pagemap;
2060 Hashtbl.clear state.pagemap;
2063 let flushtiles () =
2064 if not (Queue.is_empty state.tilelru)
2065 then (
2066 Queue.iter (fun (k, p, s) ->
2067 wcmd "freetile %s" p;
2068 state.memused <- state.memused - s;
2069 Hashtbl.remove state.tilemap k;
2070 ) state.tilelru;
2071 state.uioh#infochanged Memused;
2072 Queue.clear state.tilelru;
2074 load state.layout;
2077 let stateh h =
2078 let h = truncate (float h*.conf.zoom) in
2079 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2080 h - d
2083 let opendoc path password =
2084 state.path <- path;
2085 state.password <- password;
2086 state.gen <- state.gen + 1;
2087 state.docinfo <- [];
2089 flushpages ();
2090 setaalevel conf.aalevel;
2091 let titlepath =
2092 if emptystr state.origin
2093 then path
2094 else state.origin
2096 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2097 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
2098 invalidate "reqlayout"
2099 (fun () ->
2100 wcmd "reqlayout %d %d %d %s\000"
2101 conf.angle (FMTE.to_int conf.fitmodel)
2102 (stateh state.winh) state.nameddest
2106 let reload () =
2107 state.anchor <- getanchor ();
2108 opendoc state.path state.password;
2111 let scalecolor c =
2112 let c = c *. conf.colorscale in
2113 (c, c, c);
2116 let scalecolor2 (r, g, b) =
2117 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2120 let docolumns = function
2121 | Csingle _ ->
2122 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2123 let rec loop pageno pdimno pdim y ph pdims =
2124 if pageno = state.pagecount
2125 then ()
2126 else
2127 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2128 match pdims with
2129 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2130 pdimno+1, pdim, rest
2131 | _ ->
2132 pdimno, pdim, pdims
2134 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2135 let y = y +
2136 (if conf.presentation
2137 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2138 else (if pageno = 0 then 0 else conf.interpagespace)
2141 a.(pageno) <- (pdimno, x, y, pdim);
2142 loop (pageno+1) pdimno pdim (y + h) h pdims
2144 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2145 conf.columns <- Csingle a;
2147 | Cmulti ((columns, coverA, coverB), _) ->
2148 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2149 let rec loop pageno pdimno pdim x y rowh pdims =
2150 let rec fixrow m = if m = pageno then () else
2151 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2152 if h < rowh
2153 then (
2154 let y = y + (rowh - h) / 2 in
2155 a.(m) <- (pdimno, x, y, pdim);
2157 fixrow (m+1)
2159 if pageno = state.pagecount
2160 then fixrow (((pageno - 1) / columns) * columns)
2161 else
2162 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2163 match pdims with
2164 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2165 pdimno+1, pdim, rest
2166 | _ ->
2167 pdimno, pdim, pdims
2169 let x, y, rowh' =
2170 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2171 then (
2172 let x = (wadjsb state.winw - w) / 2 in
2173 let ips =
2174 if conf.presentation then calcips h else conf.interpagespace in
2175 x, y + ips + rowh, h
2177 else (
2178 if (pageno - coverA) mod columns = 0
2179 then (
2180 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2181 let y =
2182 if conf.presentation
2183 then
2184 let ips = calcips h in
2185 y + (if pageno = 0 then 0 else calcips rowh + ips)
2186 else
2187 y + (if pageno = 0 then 0 else conf.interpagespace)
2189 x, y + rowh, h
2191 else x, y, max rowh h
2194 let y =
2195 if pageno > 1 && (pageno - coverA) mod columns = 0
2196 then (
2197 let y =
2198 if pageno = columns && conf.presentation
2199 then (
2200 let ips = calcips rowh in
2201 for i = 0 to pred columns
2203 let (pdimno, x, y, pdim) = a.(i) in
2204 a.(i) <- (pdimno, x, y+ips, pdim)
2205 done;
2206 y+ips;
2208 else y
2210 fixrow (pageno - columns);
2213 else y
2215 a.(pageno) <- (pdimno, x, y, pdim);
2216 let x = x + w + xoff*2 + conf.interpagespace in
2217 loop (pageno+1) pdimno pdim x y rowh' pdims
2219 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2220 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2222 | Csplit (c, _) ->
2223 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2224 let rec loop pageno pdimno pdim y pdims =
2225 if pageno = state.pagecount
2226 then ()
2227 else
2228 let pdimno, ((_, w, h, _) as pdim), pdims =
2229 match pdims with
2230 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2231 pdimno+1, pdim, rest
2232 | _ ->
2233 pdimno, pdim, pdims
2235 let cw = w / c in
2236 let rec loop1 n x y =
2237 if n = c then y else (
2238 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2239 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2242 let y = loop1 0 0 y in
2243 loop (pageno+1) pdimno pdim y pdims
2245 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2246 conf.columns <- Csplit (c, a);
2249 let represent () =
2250 docolumns conf.columns;
2251 state.maxy <- calcheight ();
2252 if state.reprf == noreprf
2253 then (
2254 match state.mode with
2255 | Birdseye (_, _, pageno, _, _) ->
2256 let y, h = getpageyh pageno in
2257 let top = (state.winh - h) / 2 in
2258 gotoy (max 0 (y - top))
2259 | _ -> gotoanchor state.anchor
2261 else (
2262 state.reprf ();
2263 state.reprf <- noreprf;
2267 let reshape w h =
2268 GlDraw.viewport 0 0 w h;
2269 let firsttime = state.geomcmds == firstgeomcmds in
2270 if not firsttime && nogeomcmds state.geomcmds
2271 then state.anchor <- getanchor ();
2273 state.winw <- w;
2274 let w = wadjsb (truncate (float w *. conf.zoom)) in
2275 let w = max w 2 in
2276 state.winh <- h;
2277 setfontsize fstate.fontsize;
2278 GlMat.mode `modelview;
2279 GlMat.load_identity ();
2281 GlMat.mode `projection;
2282 GlMat.load_identity ();
2283 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2284 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2285 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2287 let relx =
2288 if conf.zoom <= 1.0
2289 then 0.0
2290 else float state.x /. float state.w
2292 invalidate "geometry"
2293 (fun () ->
2294 state.w <- w;
2295 if not firsttime
2296 then state.x <- truncate (relx *. float w);
2297 let w =
2298 match conf.columns with
2299 | Csingle _ -> w
2300 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2301 | Csplit (c, _) -> w * c
2303 wcmd "geometry %d %d %d"
2304 w (stateh h) (FMTE.to_int conf.fitmodel)
2308 let enttext () =
2309 let len = String.length state.text in
2310 let drawstring s =
2311 let hscrollh =
2312 match state.mode with
2313 | Textentry _ | View | LinkNav _ ->
2314 let h, _, _ = state.uioh#scrollpw in
2316 | _ -> 0
2318 let rect x w =
2319 GlDraw.rect
2320 (x, float (state.winh - (fstate.fontsize + 4) - hscrollh))
2321 (x+.w, float (state.winh - hscrollh))
2324 let w = float (wadjsb state.winw - 1) in
2325 if state.progress >= 0.0 && state.progress < 1.0
2326 then (
2327 GlDraw.color (0.3, 0.3, 0.3);
2328 let w1 = w *. state.progress in
2329 rect 0.0 w1;
2330 GlDraw.color (0.0, 0.0, 0.0);
2331 rect w1 (w-.w1)
2333 else (
2334 GlDraw.color (0.0, 0.0, 0.0);
2335 rect 0.0 w;
2338 GlDraw.color (1.0, 1.0, 1.0);
2339 drawstring fstate.fontsize
2340 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2342 let s =
2343 match state.mode with
2344 | Textentry ((prefix, text, _, _, _, _), _) ->
2345 let s =
2346 if len > 0
2347 then
2348 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2349 else
2350 Printf.sprintf "%s%s_" prefix text
2354 | _ -> state.text
2356 let s =
2357 if state.newerrmsgs
2358 then (
2359 if not (istextentry state.mode) && state.uioh#eformsgs
2360 then
2361 let s1 = "(press 'e' to review error messasges)" in
2362 if nonemptystr s then s ^ " " ^ s1 else s1
2363 else s
2365 else s
2367 if nonemptystr s
2368 then drawstring s
2371 let gctiles () =
2372 let len = Queue.length state.tilelru in
2373 let layout = lazy (
2374 match state.throttle with
2375 | None ->
2376 if conf.preload
2377 then preloadlayout state.y
2378 else state.layout
2379 | Some (layout, _, _) ->
2380 layout
2381 ) in
2382 let rec loop qpos =
2383 if state.memused <= conf.memlimit
2384 then ()
2385 else (
2386 if qpos < len
2387 then
2388 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2389 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2390 let (_, pw, ph, _) = getpagedim n in
2392 gen = state.gen
2393 && colorspace = conf.colorspace
2394 && angle = conf.angle
2395 && pagew = pw
2396 && pageh = ph
2397 && (
2398 let x = col*conf.tilew
2399 and y = row*conf.tileh in
2400 tilevisible (Lazy.force_val layout) n x y
2402 then Queue.push lruitem state.tilelru
2403 else (
2404 freepbo p;
2405 wcmd "freetile %s" p;
2406 state.memused <- state.memused - s;
2407 state.uioh#infochanged Memused;
2408 Hashtbl.remove state.tilemap k;
2410 loop (qpos+1)
2413 loop 0
2416 let logcurrently = function
2417 | Idle -> dolog "Idle"
2418 | Loading (l, gen) ->
2419 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2420 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2421 dolog
2422 "Tiling %d[%d,%d] page=%s cs=%s angle"
2423 l.pageno col row pageopaque
2424 (CSTE.to_string colorspace)
2426 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2427 angle gen conf.angle state.gen
2428 tilew tileh
2429 conf.tilew conf.tileh
2431 | Outlining _ ->
2432 dolog "outlining"
2435 let splitatspace =
2436 let r = Str.regexp " " in
2437 fun s -> Str.bounded_split r s 2;
2440 let onpagerect pageno f =
2441 let b =
2442 match conf.columns with
2443 | Cmulti (_, b) -> b
2444 | Csingle b -> b
2445 | Csplit (_, b) -> b
2447 if pageno >= 0 && pageno < Array.length b
2448 then
2449 let (_, _, _, (w, h, _, _)) = b.(pageno) in
2450 f w h
2453 let gotopagexy1 pageno x y =
2454 let _,w1,h1,leftx = getpagedim pageno in
2455 let top = y /. (float h1) in
2456 let left = x /. (float w1) in
2457 let py, w, h = getpageywh pageno in
2458 let wh = state.winh - hscrollh () in
2459 let x = left *. (float w) in
2460 let x = leftx + state.x + truncate x in
2461 let sx =
2462 if x < 0 || x >= wadjsb state.winw
2463 then state.x - x
2464 else state.x
2466 let pdy = truncate (top *. float h) in
2467 let y' = py + pdy in
2468 let dy = y' - state.y in
2469 let sy =
2470 if x != state.x || not (dy > 0 && dy < wh)
2471 then (
2472 if conf.presentation
2473 then
2474 if abs (py - y') > wh
2475 then y'
2476 else py
2477 else y';
2479 else state.y
2481 if state.x != sx || state.y != sy
2482 then (
2483 let x, y =
2484 if !wtmode
2485 then (
2486 let ww = wadjsb state.winw in
2487 let qx = sx / ww
2488 and qy = pdy / wh in
2489 let x = qx * ww
2490 and y = py + qy * wh in
2491 let x = if -x + ww > w1 then -(w1-ww) else x
2492 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2493 let y =
2494 if conf.presentation
2495 then
2496 if abs (py - y') > wh
2497 then y'
2498 else py
2499 else y';
2501 (x, y)
2503 else (sx, sy)
2505 state.x <- x;
2506 gotoy_and_clear_text y;
2508 else gotoy_and_clear_text state.y;
2511 let gotopagexy pageno x y =
2512 match state.mode with
2513 | Birdseye _ -> gotopage pageno 0.0
2514 | _ -> gotopagexy1 pageno x y
2517 let act cmds =
2518 (* dolog "%S" cmds; *)
2519 let cl = splitatspace cmds in
2520 let scan s fmt f =
2521 try Scanf.sscanf s fmt f
2522 with exn ->
2523 dolog "error processing '%S': %s" cmds (exntos exn);
2524 exit 1
2526 match cl with
2527 | "clear" :: [] ->
2528 state.uioh#infochanged Pdim;
2529 state.pdims <- [];
2531 | "clearrects" :: [] ->
2532 state.rects <- state.rects1;
2533 G.postRedisplay "clearrects";
2535 | "continue" :: args :: [] ->
2536 let n = scan args "%u" (fun n -> n) in
2537 state.pagecount <- n;
2538 begin match state.currently with
2539 | Outlining l ->
2540 state.currently <- Idle;
2541 state.outlines <- Array.of_list (List.rev l)
2542 | _ -> ()
2543 end;
2545 let cur, cmds = state.geomcmds in
2546 if emptystr cur
2547 then failwith "umpossible";
2549 begin match List.rev cmds with
2550 | [] ->
2551 state.geomcmds <- "", [];
2552 represent ();
2553 | (s, f) :: rest ->
2554 f ();
2555 state.geomcmds <- s, List.rev rest;
2556 end;
2557 if conf.maxwait = None && not !wtmode
2558 then G.postRedisplay "continue";
2560 | "title" :: args :: [] ->
2561 Wsi.settitle args
2563 | "msg" :: args :: [] ->
2564 showtext ' ' args
2566 | "vmsg" :: args :: [] ->
2567 if conf.verbose
2568 then showtext ' ' args
2570 | "emsg" :: args :: [] ->
2571 Buffer.add_string state.errmsgs args;
2572 state.newerrmsgs <- true;
2573 G.postRedisplay "error message"
2575 | "progress" :: args :: [] ->
2576 let progress, text =
2577 scan args "%f %n"
2578 (fun f pos ->
2579 f, String.sub args pos (String.length args - pos))
2581 state.text <- text;
2582 state.progress <- progress;
2583 G.postRedisplay "progress"
2585 | "firstmatch" :: args :: [] ->
2586 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2587 scan args "%u %d %f %f %f %f %f %f %f %f"
2588 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2589 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2591 let y = (getpagey pageno) + truncate y0 in
2592 addnav ();
2593 gotoy y;
2594 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2596 | "match" :: args :: [] ->
2597 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2598 scan args "%u %d %f %f %f %f %f %f %f %f"
2599 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2600 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2602 state.rects1 <-
2603 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2605 | "page" :: args :: [] ->
2606 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2607 begin match state.currently with
2608 | Loading (l, gen) ->
2609 vlog "page %d took %f sec" l.pageno t;
2610 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2611 begin match state.throttle with
2612 | None ->
2613 let preloadedpages =
2614 if conf.preload
2615 then preloadlayout state.y
2616 else state.layout
2618 let evict () =
2619 let set =
2620 List.fold_left (fun s l -> IntSet.add l.pageno s)
2621 IntSet.empty preloadedpages
2623 let evictedpages =
2624 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2625 if not (IntSet.mem pageno set)
2626 then (
2627 wcmd "freepage %s" opaque;
2628 key :: accu
2630 else accu
2631 ) state.pagemap []
2633 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2635 evict ();
2636 state.currently <- Idle;
2637 if gen = state.gen
2638 then (
2639 tilepage l.pageno pageopaque state.layout;
2640 load state.layout;
2641 load preloadedpages;
2642 if pagevisible state.layout l.pageno
2643 && layoutready state.layout
2644 then G.postRedisplay "page";
2647 | Some (layout, _, _) ->
2648 state.currently <- Idle;
2649 tilepage l.pageno pageopaque layout;
2650 load state.layout
2651 end;
2653 | _ ->
2654 dolog "Inconsistent loading state";
2655 logcurrently state.currently;
2656 exit 1
2659 | "tile" :: args :: [] ->
2660 let (x, y, opaque, size, t) =
2661 scan args "%u %u %s %u %f"
2662 (fun x y p size t -> (x, y, p, size, t))
2664 begin match state.currently with
2665 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2666 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2668 unmappbo opaque;
2669 if tilew != conf.tilew || tileh != conf.tileh
2670 then (
2671 wcmd "freetile %s" opaque;
2672 state.currently <- Idle;
2673 load state.layout;
2675 else (
2676 puttileopaque l col row gen cs angle opaque size t;
2677 state.memused <- state.memused + size;
2678 state.uioh#infochanged Memused;
2679 gctiles ();
2680 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2681 opaque, size) state.tilelru;
2683 let layout =
2684 match state.throttle with
2685 | None -> state.layout
2686 | Some (layout, _, _) -> layout
2689 state.currently <- Idle;
2690 if gen = state.gen
2691 && conf.colorspace = cs
2692 && conf.angle = angle
2693 && tilevisible layout l.pageno x y
2694 then conttiling l.pageno pageopaque;
2696 begin match state.throttle with
2697 | None ->
2698 preload state.layout;
2699 if gen = state.gen
2700 && conf.colorspace = cs
2701 && conf.angle = angle
2702 && tilevisible state.layout l.pageno x y
2703 && (not !wtmode || layoutready state.layout)
2704 then G.postRedisplay "tile nothrottle";
2706 | Some (layout, y, _) ->
2707 let ready = layoutready layout in
2708 if ready
2709 then (
2710 state.y <- y;
2711 state.layout <- layout;
2712 state.throttle <- None;
2713 G.postRedisplay "throttle";
2715 else load layout;
2716 end;
2719 | _ ->
2720 dolog "Inconsistent tiling state";
2721 logcurrently state.currently;
2722 exit 1
2725 | "pdim" :: args :: [] ->
2726 let (n, w, h, _) as pdim =
2727 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2729 let pdim =
2730 match conf.fitmodel, conf.columns with
2731 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2732 | _ -> pdim
2734 state.uioh#infochanged Pdim;
2735 state.pdims <- pdim :: state.pdims
2737 | "o" :: args :: [] ->
2738 let (l, n, t, h, pos) =
2739 scan args "%u %u %d %u %n"
2740 (fun l n t h pos -> l, n, t, h, pos)
2742 let s = String.sub args pos (String.length args - pos) in
2743 let outline = (s, l, (n, float t /. float h, 0.0)) in
2744 begin match state.currently with
2745 | Outlining outlines ->
2746 state.currently <- Outlining (outline :: outlines)
2747 | Idle ->
2748 state.currently <- Outlining [outline]
2749 | currently ->
2750 dolog "invalid outlining state";
2751 logcurrently currently
2754 | "a" :: args :: [] ->
2755 let (n, l, t) =
2756 scan args "%u %d %d" (fun n l t -> n, l, t)
2758 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2760 | "info" :: args :: [] ->
2761 state.docinfo <- (1, args) :: state.docinfo
2763 | "infoend" :: [] ->
2764 state.uioh#infochanged Docinfo;
2765 state.docinfo <- List.rev state.docinfo
2767 | _ ->
2768 error "unknown cmd `%S'" cmds
2771 let onhist cb =
2772 let rc = cb.rc in
2773 let action = function
2774 | HCprev -> cbget cb ~-1
2775 | HCnext -> cbget cb 1
2776 | HCfirst -> cbget cb ~-(cb.rc)
2777 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2778 and cancel () = cb.rc <- rc
2779 in (action, cancel)
2782 let search pattern forward =
2783 match conf.columns with
2784 | Csplit _ ->
2785 showtext '!' "searching does not work properly in split columns mode"
2786 | _ ->
2787 if nonemptystr pattern
2788 then
2789 let pn, py =
2790 match state.layout with
2791 | [] -> 0, 0
2792 | l :: _ ->
2793 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2795 wcmd "search %d %d %d %d,%s\000"
2796 (btod conf.icase) pn py (btod forward) pattern;
2799 let intentry text key =
2800 let c =
2801 if key >= 32 && key < 127
2802 then Char.chr key
2803 else '\000'
2805 match c with
2806 | '0' .. '9' ->
2807 let text = addchar text c in
2808 TEcont text
2810 | _ ->
2811 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2812 TEcont text
2815 let linknentry text key =
2816 let c =
2817 if key >= 32 && key < 127
2818 then Char.chr key
2819 else '\000'
2821 match c with
2822 | 'a' .. 'z' ->
2823 let text = addchar text c in
2824 TEcont text
2826 | _ ->
2827 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2828 TEcont text
2831 let linkndone f s =
2832 if nonemptystr s
2833 then (
2834 let n =
2835 let l = String.length s in
2836 let rec loop pos n = if pos = l then n else
2837 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2838 loop (pos+1) (n*26 + m)
2839 in loop 0 0
2841 let rec loop n = function
2842 | [] -> ()
2843 | l :: rest ->
2844 match getopaque l.pageno with
2845 | None -> loop n rest
2846 | Some opaque ->
2847 let m = getlinkcount opaque in
2848 if n < m
2849 then (
2850 let under = getlink opaque n in
2851 f under
2853 else loop (n-m) rest
2855 loop n state.layout;
2859 let textentry text key =
2860 if key land 0xff00 = 0xff00
2861 then TEcont text
2862 else TEcont (text ^ toutf8 key)
2865 let reqlayout angle fitmodel =
2866 match state.throttle with
2867 | None ->
2868 if nogeomcmds state.geomcmds
2869 then state.anchor <- getanchor ();
2870 conf.angle <- angle mod 360;
2871 if conf.angle != 0
2872 then (
2873 match state.mode with
2874 | LinkNav _ -> state.mode <- View
2875 | _ -> ()
2877 conf.fitmodel <- fitmodel;
2878 invalidate "reqlayout"
2879 (fun () ->
2880 wcmd "reqlayout %d %d %d"
2881 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2883 | _ -> ()
2886 let settrim trimmargins trimfuzz =
2887 if nogeomcmds state.geomcmds
2888 then state.anchor <- getanchor ();
2889 conf.trimmargins <- trimmargins;
2890 conf.trimfuzz <- trimfuzz;
2891 let x0, y0, x1, y1 = trimfuzz in
2892 invalidate "settrim"
2893 (fun () ->
2894 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2895 flushpages ();
2898 let setzoom zoom =
2899 match state.throttle with
2900 | None ->
2901 let zoom = max 0.0001 zoom in
2902 if zoom <> conf.zoom
2903 then (
2904 state.prevzoom <- (conf.zoom, state.x);
2905 conf.zoom <- zoom;
2906 reshape state.winw state.winh;
2907 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2910 | Some (layout, y, started) ->
2911 let time =
2912 match conf.maxwait with
2913 | None -> 0.0
2914 | Some t -> t
2916 let dt = now () -. started in
2917 if dt > time
2918 then (
2919 state.y <- y;
2920 load layout;
2924 let setcolumns mode columns coverA coverB =
2925 state.prevcolumns <- Some (conf.columns, conf.zoom);
2926 if columns < 0
2927 then (
2928 if isbirdseye mode
2929 then showtext '!' "split mode doesn't work in bird's eye"
2930 else (
2931 conf.columns <- Csplit (-columns, [||]);
2932 state.x <- 0;
2933 conf.zoom <- 1.0;
2936 else (
2937 if columns < 2
2938 then (
2939 conf.columns <- Csingle [||];
2940 state.x <- 0;
2941 setzoom 1.0;
2943 else (
2944 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2945 conf.zoom <- 1.0;
2948 reshape state.winw state.winh;
2951 let enterbirdseye () =
2952 let zoom = float conf.thumbw /. float state.winw in
2953 let birdseyepageno =
2954 let cy = state.winh / 2 in
2955 let fold = function
2956 | [] -> 0
2957 | l :: rest ->
2958 let rec fold best = function
2959 | [] -> best.pageno
2960 | l :: rest ->
2961 let d = cy - (l.pagedispy + l.pagevh/2)
2962 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2963 if abs d < abs dbest
2964 then fold l rest
2965 else best.pageno
2966 in fold l rest
2968 fold state.layout
2970 state.mode <- Birdseye (
2971 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2973 conf.zoom <- zoom;
2974 conf.presentation <- false;
2975 conf.interpagespace <- 10;
2976 conf.hlinks <- false;
2977 conf.fitmodel <- FitProportional;
2978 state.x <- 0;
2979 state.mstate <- Mnone;
2980 conf.maxwait <- None;
2981 conf.columns <- (
2982 match conf.beyecolumns with
2983 | Some c ->
2984 conf.zoom <- 1.0;
2985 Cmulti ((c, 0, 0), [||])
2986 | None -> Csingle [||]
2988 Wsi.setcursor Wsi.CURSOR_INHERIT;
2989 if conf.verbose
2990 then
2991 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2992 (100.0*.zoom)
2993 else
2994 state.text <- ""
2996 reshape state.winw state.winh;
2999 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
3000 state.mode <- View;
3001 conf.zoom <- c.zoom;
3002 conf.presentation <- c.presentation;
3003 conf.interpagespace <- c.interpagespace;
3004 conf.maxwait <- c.maxwait;
3005 conf.hlinks <- c.hlinks;
3006 conf.fitmodel <- c.fitmodel;
3007 conf.beyecolumns <- (
3008 match conf.columns with
3009 | Cmulti ((c, _, _), _) -> Some c
3010 | Csingle _ -> None
3011 | Csplit _ -> failwith "leaving bird's eye split mode"
3013 conf.columns <- (
3014 match c.columns with
3015 | Cmulti (c, _) -> Cmulti (c, [||])
3016 | Csingle _ -> Csingle [||]
3017 | Csplit (c, _) -> Csplit (c, [||])
3019 if conf.verbose
3020 then
3021 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
3022 (100.0*.conf.zoom)
3024 reshape state.winw state.winh;
3025 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
3026 state.x <- leftx;
3029 let togglebirdseye () =
3030 match state.mode with
3031 | Birdseye vals -> leavebirdseye vals true
3032 | View -> enterbirdseye ()
3033 | _ -> ()
3036 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3037 let pageno = max 0 (pageno - incr) in
3038 let rec loop = function
3039 | [] -> gotopage1 pageno 0
3040 | l :: _ when l.pageno = pageno ->
3041 if l.pagedispy >= 0 && l.pagey = 0
3042 then G.postRedisplay "upbirdseye"
3043 else gotopage1 pageno 0
3044 | _ :: rest -> loop rest
3046 loop state.layout;
3047 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
3050 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3051 let pageno = min (state.pagecount - 1) (pageno + incr) in
3052 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3053 let rec loop = function
3054 | [] ->
3055 let y, h = getpageyh pageno in
3056 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
3057 gotoy (clamp dy)
3058 | l :: _ when l.pageno = pageno ->
3059 if l.pagevh != l.pageh
3060 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
3061 else G.postRedisplay "downbirdseye"
3062 | _ :: rest -> loop rest
3064 loop state.layout
3067 let optentry mode _ key =
3068 let btos b = if b then "on" else "off" in
3069 if key >= 32 && key < 127
3070 then
3071 let c = Char.chr key in
3072 match c with
3073 | 's' ->
3074 let ondone s =
3075 try conf.scrollstep <- int_of_string s with exc ->
3076 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3078 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3080 | 'A' ->
3081 let ondone s =
3083 conf.autoscrollstep <- int_of_string s;
3084 if state.autoscroll <> None
3085 then state.autoscroll <- Some conf.autoscrollstep
3086 with exc ->
3087 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3089 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3091 | 'C' ->
3092 let ondone s =
3094 let n, a, b = multicolumns_of_string s in
3095 setcolumns mode n a b;
3096 with exc ->
3097 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3099 TEswitch ("columns: ", "", None, textentry, ondone, true)
3101 | 'Z' ->
3102 let ondone s =
3104 let zoom = float (int_of_string s) /. 100.0 in
3105 setzoom zoom
3106 with exc ->
3107 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3109 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3111 | 't' ->
3112 let ondone s =
3114 conf.thumbw <- bound (int_of_string s) 2 4096;
3115 state.text <-
3116 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3117 begin match mode with
3118 | Birdseye beye ->
3119 leavebirdseye beye false;
3120 enterbirdseye ();
3121 | _ -> ();
3123 with exc ->
3124 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3126 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3128 | 'R' ->
3129 let ondone s =
3130 match try
3131 Some (int_of_string s)
3132 with exc ->
3133 state.text <- Printf.sprintf "bad integer `%s': %s"
3134 s (exntos exc);
3135 None
3136 with
3137 | Some angle -> reqlayout angle conf.fitmodel
3138 | None -> ()
3140 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3142 | 'i' ->
3143 conf.icase <- not conf.icase;
3144 TEdone ("case insensitive search " ^ (btos conf.icase))
3146 | 'p' ->
3147 conf.preload <- not conf.preload;
3148 gotoy state.y;
3149 TEdone ("preload " ^ (btos conf.preload))
3151 | 'v' ->
3152 conf.verbose <- not conf.verbose;
3153 TEdone ("verbose " ^ (btos conf.verbose))
3155 | 'd' ->
3156 conf.debug <- not conf.debug;
3157 TEdone ("debug " ^ (btos conf.debug))
3159 | 'h' ->
3160 conf.maxhfit <- not conf.maxhfit;
3161 state.maxy <- calcheight ();
3162 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3164 | 'c' ->
3165 conf.crophack <- not conf.crophack;
3166 TEdone ("crophack " ^ btos conf.crophack)
3168 | 'a' ->
3169 let s =
3170 match conf.maxwait with
3171 | None ->
3172 conf.maxwait <- Some infinity;
3173 "always wait for page to complete"
3174 | Some _ ->
3175 conf.maxwait <- None;
3176 "show placeholder if page is not ready"
3178 TEdone s
3180 | 'f' ->
3181 conf.underinfo <- not conf.underinfo;
3182 TEdone ("underinfo " ^ btos conf.underinfo)
3184 | 'P' ->
3185 conf.savebmarks <- not conf.savebmarks;
3186 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3188 | 'S' ->
3189 let ondone s =
3191 let pageno, py =
3192 match state.layout with
3193 | [] -> 0, 0
3194 | l :: _ ->
3195 l.pageno, l.pagey
3197 conf.interpagespace <- int_of_string s;
3198 docolumns conf.columns;
3199 state.maxy <- calcheight ();
3200 let y = getpagey pageno in
3201 gotoy (y + py)
3202 with exc ->
3203 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3205 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3207 | 'l' ->
3208 let fm =
3209 match conf.fitmodel with
3210 | FitProportional -> FitWidth
3211 | _ -> FitProportional
3213 reqlayout conf.angle fm;
3214 TEdone ("proportional display " ^ btos (fm == FitProportional))
3216 | 'T' ->
3217 settrim (not conf.trimmargins) conf.trimfuzz;
3218 TEdone ("trim margins " ^ btos conf.trimmargins)
3220 | 'I' ->
3221 conf.invert <- not conf.invert;
3222 TEdone ("invert colors " ^ btos conf.invert)
3224 | 'x' ->
3225 let ondone s =
3226 cbput state.hists.sel s;
3227 conf.selcmd <- s;
3229 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3230 textentry, ondone, true)
3232 | 'M' ->
3233 if conf.pax == None
3234 then conf.pax <- Some (ref (0.0, 0, 0))
3235 else conf.pax <- None;
3236 TEdone ("PAX " ^ btos (conf.pax != None))
3238 | _ ->
3239 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3240 TEstop
3241 else
3242 TEcont state.text
3245 class type lvsource = object
3246 method getitemcount : int
3247 method getitem : int -> (string * int)
3248 method hasaction : int -> bool
3249 method exit :
3250 uioh:uioh ->
3251 cancel:bool ->
3252 active:int ->
3253 first:int ->
3254 pan:int ->
3255 qsearch:string ->
3256 uioh option
3257 method getactive : int
3258 method getfirst : int
3259 method getqsearch : string
3260 method setqsearch : string -> unit
3261 method getpan : int
3262 end;;
3264 class virtual lvsourcebase = object
3265 val mutable m_active = 0
3266 val mutable m_first = 0
3267 val mutable m_qsearch = ""
3268 val mutable m_pan = 0
3269 method getactive = m_active
3270 method getfirst = m_first
3271 method getqsearch = m_qsearch
3272 method getpan = m_pan
3273 method setqsearch s = m_qsearch <- s
3274 end;;
3276 let withoutlastutf8 s =
3277 let len = String.length s in
3278 if len = 0
3279 then s
3280 else
3281 let rec find pos =
3282 if pos = 0
3283 then pos
3284 else
3285 let b = Char.code s.[pos] in
3286 if b land 0b11000000 = 0b11000000
3287 then pos
3288 else find (pos-1)
3290 let first =
3291 if Char.code s.[len-1] land 0x80 = 0
3292 then len-1
3293 else find (len-1)
3295 String.sub s 0 first;
3298 let textentrykeyboard
3299 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3300 let key =
3301 if key >= 0xffb0 && key <= 0xffb9
3302 then key - 0xffb0 + 48 else key
3304 let enttext te =
3305 state.mode <- Textentry (te, onleave);
3306 state.text <- "";
3307 enttext ();
3308 G.postRedisplay "textentrykeyboard enttext";
3310 let histaction cmd =
3311 match opthist with
3312 | None -> ()
3313 | Some (action, _) ->
3314 state.mode <- Textentry (
3315 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3317 G.postRedisplay "textentry histaction"
3319 match key with
3320 | 0xff08 -> (* backspace *)
3321 let s = withoutlastutf8 text in
3322 let len = String.length s in
3323 if cancelonempty && len = 0
3324 then (
3325 onleave Cancel;
3326 G.postRedisplay "textentrykeyboard after cancel";
3328 else (
3329 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3332 | 0xff0d | 0xff8d -> (* (kp) enter *)
3333 ondone text;
3334 onleave Confirm;
3335 G.postRedisplay "textentrykeyboard after confirm"
3337 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3338 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3339 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3340 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3342 | 0xff1b -> (* escape*)
3343 if emptystr text
3344 then (
3345 begin match opthist with
3346 | None -> ()
3347 | Some (_, onhistcancel) -> onhistcancel ()
3348 end;
3349 onleave Cancel;
3350 state.text <- "";
3351 G.postRedisplay "textentrykeyboard after cancel2"
3353 else (
3354 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3357 | 0xff9f | 0xffff -> () (* delete *)
3359 | _ when key != 0
3360 && key land 0xff00 != 0xff00 (* keyboard *)
3361 && key land 0xfe00 != 0xfe00 (* xkb *)
3362 && key land 0xfd00 != 0xfd00 (* 3270 *)
3364 begin match onkey text key with
3365 | TEdone text ->
3366 ondone text;
3367 onleave Confirm;
3368 G.postRedisplay "textentrykeyboard after confirm2";
3370 | TEcont text ->
3371 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3373 | TEstop ->
3374 onleave Cancel;
3375 G.postRedisplay "textentrykeyboard after cancel3"
3377 | TEswitch te ->
3378 state.mode <- Textentry (te, onleave);
3379 G.postRedisplay "textentrykeyboard switch";
3380 end;
3382 | _ ->
3383 vlog "unhandled key %s" (Wsi.keyname key)
3386 let firstof first active =
3387 if first > active || abs (first - active) > fstate.maxrows - 1
3388 then max 0 (active - (fstate.maxrows/2))
3389 else first
3392 let calcfirst first active =
3393 if active > first
3394 then
3395 let rows = active - first in
3396 if rows > fstate.maxrows then active - fstate.maxrows else first
3397 else active
3400 let scrollph y maxy =
3401 let sh = float (maxy + state.winh) /. float state.winh in
3402 let sh = float state.winh /. sh in
3403 let sh = max sh (float conf.scrollh) in
3405 let percent = float y /. float maxy in
3406 let position = (float state.winh -. sh) *. percent in
3408 let position =
3409 if position +. sh > float state.winh
3410 then float state.winh -. sh
3411 else position
3413 position, sh;
3416 let coe s = (s :> uioh);;
3418 class listview ~(source:lvsource) ~trusted ~modehash =
3419 object (self)
3420 val m_pan = source#getpan
3421 val m_first = source#getfirst
3422 val m_active = source#getactive
3423 val m_qsearch = source#getqsearch
3424 val m_prev_uioh = state.uioh
3426 method private elemunder y =
3427 let n = y / (fstate.fontsize+1) in
3428 if m_first + n < source#getitemcount
3429 then (
3430 if source#hasaction (m_first + n)
3431 then Some (m_first + n)
3432 else None
3434 else None
3436 method display =
3437 Gl.enable `blend;
3438 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3439 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3440 GlDraw.rect (0., 0.) (float state.winw, float state.winh);
3441 GlDraw.color (1., 1., 1.);
3442 Gl.enable `texture_2d;
3443 let fs = fstate.fontsize in
3444 let nfs = fs + 1 in
3445 let ww = fstate.wwidth in
3446 let tabw = 30.0*.ww in
3447 let itemcount = source#getitemcount in
3448 let rec loop row =
3449 if (row - m_first) > fstate.maxrows
3450 then ()
3451 else (
3452 if row >= 0 && row < itemcount
3453 then (
3454 let (s, level) = source#getitem row in
3455 let y = (row - m_first) * nfs in
3456 let x = 5.0 +. float (level + m_pan) *. ww in
3457 if row = m_active
3458 then (
3459 Gl.disable `texture_2d;
3460 GlDraw.polygon_mode `both `line;
3461 let alpha = if source#hasaction row then 0.9 else 0.3 in
3462 GlDraw.color (1., 1., 1.) ~alpha;
3463 GlDraw.rect (1., float (y + 1))
3464 (float (state.winw - conf.scrollbw - 1), float (y + fs + 3));
3465 GlDraw.polygon_mode `both `fill;
3466 GlDraw.color (1., 1., 1.);
3467 Gl.enable `texture_2d;
3470 let drawtabularstring s =
3471 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3472 if trusted
3473 then
3474 let tabpos = try String.index s '\t' with Not_found -> -1 in
3475 if tabpos > 0
3476 then
3477 let len = String.length s - tabpos - 1 in
3478 let s1 = String.sub s 0 tabpos
3479 and s2 = String.sub s (tabpos + 1) len in
3480 let nx = drawstr x s1 in
3481 let sw = nx -. x in
3482 let x = x +. (max tabw sw) in
3483 drawstr x s2
3484 else
3485 drawstr x s
3486 else
3487 drawstr x s
3489 let _ = drawtabularstring s in
3490 loop (row+1)
3494 loop m_first;
3495 Gl.disable `blend;
3496 Gl.disable `texture_2d;
3498 method updownlevel incr =
3499 let len = source#getitemcount in
3500 let curlevel =
3501 if m_active >= 0 && m_active < len
3502 then snd (source#getitem m_active)
3503 else -1
3505 let rec flow i =
3506 if i = len then i-1 else if i = -1 then 0 else
3507 let _, l = source#getitem i in
3508 if l != curlevel then i else flow (i+incr)
3510 let active = flow m_active in
3511 let first = calcfirst m_first active in
3512 G.postRedisplay "outline updownlevel";
3513 {< m_active = active; m_first = first >}
3515 method private key1 key mask =
3516 let set1 active first qsearch =
3517 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3519 let search active pattern incr =
3520 let active = if active = -1 then m_first else active in
3521 let dosearch re =
3522 let rec loop n =
3523 if n >= 0 && n < source#getitemcount
3524 then (
3525 let s, _ = source#getitem n in
3527 (try ignore (Str.search_forward re s 0); true
3528 with Not_found -> false)
3529 then Some n
3530 else loop (n + incr)
3532 else None
3534 loop active
3537 let re = Str.regexp_case_fold pattern in
3538 dosearch re
3539 with Failure s ->
3540 state.text <- s;
3541 None
3543 let itemcount = source#getitemcount in
3544 let find start incr =
3545 let rec find i =
3546 if i = -1 || i = itemcount
3547 then -1
3548 else (
3549 if source#hasaction i
3550 then i
3551 else find (i + incr)
3554 find start
3556 let set active first =
3557 let first = bound first 0 (itemcount - fstate.maxrows) in
3558 state.text <- "";
3559 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3561 let navigate incr =
3562 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3563 let active, first =
3564 let incr1 = if incr > 0 then 1 else -1 in
3565 if isvisible m_first m_active
3566 then
3567 let next =
3568 let next = m_active + incr in
3569 let next =
3570 if next < 0 || next >= itemcount
3571 then -1
3572 else find next incr1
3574 if abs (m_active - next) > fstate.maxrows
3575 then -1
3576 else next
3578 if next = -1
3579 then
3580 let first = m_first + incr in
3581 let first = bound first 0 (itemcount - fstate.maxrows) in
3582 let next =
3583 let next = m_active + incr in
3584 let next = bound next 0 (itemcount - 1) in
3585 find next ~-incr1
3587 let active =
3588 if next = -1
3589 then m_active
3590 else (
3591 if isvisible first next
3592 then next
3593 else m_active
3596 active, first
3597 else
3598 let first = min next m_first in
3599 let first =
3600 if abs (next - first) > fstate.maxrows
3601 then first + incr
3602 else first
3604 next, first
3605 else
3606 let first = m_first + incr in
3607 let first = bound first 0 (itemcount - 1) in
3608 let active =
3609 let next = m_active + incr in
3610 let next = bound next 0 (itemcount - 1) in
3611 let next = find next incr1 in
3612 let active =
3613 if next = -1 || abs (m_active - first) > fstate.maxrows
3614 then (
3615 let active = if m_active = -1 then next else m_active in
3616 active
3618 else next
3620 if isvisible first active
3621 then active
3622 else -1
3624 active, first
3626 G.postRedisplay "listview navigate";
3627 set active first;
3629 match key with
3630 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3631 let incr = if key = 0x72 then -1 else 1 in
3632 let active, first =
3633 match search (m_active + incr) m_qsearch incr with
3634 | None ->
3635 state.text <- m_qsearch ^ " [not found]";
3636 m_active, m_first
3637 | Some active ->
3638 state.text <- m_qsearch;
3639 active, firstof m_first active
3641 G.postRedisplay "listview ctrl-r/s";
3642 set1 active first m_qsearch;
3644 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3645 if m_active >= 0 && m_active < source#getitemcount
3646 then (
3647 let s, _ = source#getitem m_active in
3648 selstring s;
3650 coe self
3652 | 0xff08 -> (* backspace *)
3653 if emptystr m_qsearch
3654 then coe self
3655 else (
3656 let qsearch = withoutlastutf8 m_qsearch in
3657 if emptystr qsearch
3658 then (
3659 state.text <- "";
3660 G.postRedisplay "listview empty qsearch";
3661 set1 m_active m_first "";
3663 else
3664 let active, first =
3665 match search m_active qsearch ~-1 with
3666 | None ->
3667 state.text <- qsearch ^ " [not found]";
3668 m_active, m_first
3669 | Some active ->
3670 state.text <- qsearch;
3671 active, firstof m_first active
3673 G.postRedisplay "listview backspace qsearch";
3674 set1 active first qsearch
3677 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3678 let pattern = m_qsearch ^ toutf8 key in
3679 let active, first =
3680 match search m_active pattern 1 with
3681 | None ->
3682 state.text <- pattern ^ " [not found]";
3683 m_active, m_first
3684 | Some active ->
3685 state.text <- pattern;
3686 active, firstof m_first active
3688 G.postRedisplay "listview qsearch add";
3689 set1 active first pattern;
3691 | 0xff1b -> (* escape *)
3692 state.text <- "";
3693 if emptystr m_qsearch
3694 then (
3695 G.postRedisplay "list view escape";
3696 begin
3697 match
3698 source#exit (coe self) true m_active m_first m_pan m_qsearch
3699 with
3700 | None -> m_prev_uioh
3701 | Some uioh -> uioh
3704 else (
3705 G.postRedisplay "list view kill qsearch";
3706 source#setqsearch "";
3707 coe {< m_qsearch = "" >}
3710 | 0xff0d | 0xff8d -> (* (kp) enter *)
3711 state.text <- "";
3712 let self = {< m_qsearch = "" >} in
3713 source#setqsearch "";
3714 let opt =
3715 G.postRedisplay "listview enter";
3716 if m_active >= 0 && m_active < source#getitemcount
3717 then (
3718 source#exit (coe self) false m_active m_first m_pan "";
3720 else (
3721 source#exit (coe self) true m_active m_first m_pan "";
3724 begin match opt with
3725 | None -> m_prev_uioh
3726 | Some uioh -> uioh
3729 | 0xff9f | 0xffff -> (* (kp) delete *)
3730 coe self
3732 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3733 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3734 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3735 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3737 | 0xff53 | 0xff98 -> (* (kp) right *)
3738 state.text <- "";
3739 G.postRedisplay "listview right";
3740 coe {< m_pan = m_pan - 1 >}
3742 | 0xff51 | 0xff96 -> (* (kp) left *)
3743 state.text <- "";
3744 G.postRedisplay "listview left";
3745 coe {< m_pan = m_pan + 1 >}
3747 | 0xff50 | 0xff95 -> (* (kp) home *)
3748 let active = find 0 1 in
3749 G.postRedisplay "listview home";
3750 set active 0;
3752 | 0xff57 | 0xff9c -> (* (kp) end *)
3753 let first = max 0 (itemcount - fstate.maxrows) in
3754 let active = find (itemcount - 1) ~-1 in
3755 G.postRedisplay "listview end";
3756 set active first;
3758 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3759 coe self
3761 | _ ->
3762 dolog "listview unknown key %#x" key; coe self
3764 method key key mask =
3765 match state.mode with
3766 | Textentry te -> textentrykeyboard key mask te; coe self
3767 | _ -> self#key1 key mask
3769 method button button down x y _ =
3770 let opt =
3771 match button with
3772 | 1 when x > state.winw - conf.scrollbw ->
3773 G.postRedisplay "listview scroll";
3774 if down
3775 then
3776 let _, position, sh = self#scrollph in
3777 if y > truncate position && y < truncate (position +. sh)
3778 then (
3779 state.mstate <- Mscrolly;
3780 Some (coe self)
3782 else
3783 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3784 let first = truncate (s *. float source#getitemcount) in
3785 let first = min source#getitemcount first in
3786 Some (coe {< m_first = first; m_active = first >})
3787 else (
3788 state.mstate <- Mnone;
3789 Some (coe self);
3791 | 1 when not down ->
3792 begin match self#elemunder y with
3793 | Some n ->
3794 G.postRedisplay "listview click";
3795 source#exit
3796 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3797 | _ ->
3798 Some (coe self)
3800 | n when (n == 4 || n == 5) && not down ->
3801 let len = source#getitemcount in
3802 let first =
3803 if n = 5 && m_first + fstate.maxrows >= len
3804 then
3805 m_first
3806 else
3807 let first = m_first + (if n == 4 then -1 else 1) in
3808 bound first 0 (len - 1)
3810 G.postRedisplay "listview wheel";
3811 Some (coe {< m_first = first >})
3812 | n when (n = 6 || n = 7) && not down ->
3813 let inc = if n = 7 then -1 else 1 in
3814 G.postRedisplay "listview hwheel";
3815 Some (coe {< m_pan = m_pan + inc >})
3816 | _ ->
3817 Some (coe self)
3819 match opt with
3820 | None -> m_prev_uioh
3821 | Some uioh -> uioh
3823 method motion _ y =
3824 match state.mstate with
3825 | Mscrolly ->
3826 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3827 let first = truncate (s *. float source#getitemcount) in
3828 let first = min source#getitemcount first in
3829 G.postRedisplay "listview motion";
3830 coe {< m_first = first; m_active = first >}
3831 | _ -> coe self
3833 method pmotion x y =
3834 if x < state.winw - conf.scrollbw
3835 then
3836 let n =
3837 match self#elemunder y with
3838 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3839 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3841 let o =
3842 if n != m_active
3843 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3844 else self
3846 coe o
3847 else (
3848 Wsi.setcursor Wsi.CURSOR_INHERIT;
3849 coe self
3852 method infochanged _ = ()
3854 method scrollpw = (0, 0.0, 0.0)
3855 method scrollph =
3856 let nfs = fstate.fontsize + 1 in
3857 let y = m_first * nfs in
3858 let itemcount = source#getitemcount in
3859 let maxi = max 0 (itemcount - fstate.maxrows) in
3860 let maxy = maxi * nfs in
3861 let p, h = scrollph y maxy in
3862 conf.scrollbw, p, h
3864 method modehash = modehash
3865 method eformsgs = false
3866 end;;
3868 class outlinelistview ~source =
3869 let settext autonarrow s =
3870 if autonarrow
3871 then state.text <- "[" ^ s ^ "]"
3872 else state.text <- s
3874 object (self)
3875 inherit listview
3876 ~source:(source :> lvsource)
3877 ~trusted:false
3878 ~modehash:(findkeyhash conf "outline")
3879 as super
3881 val m_autonarrow = false
3883 method key key mask =
3884 let maxrows =
3885 if emptystr state.text
3886 then fstate.maxrows
3887 else fstate.maxrows - 2
3889 let calcfirst first active =
3890 if active > first
3891 then
3892 let rows = active - first in
3893 if rows > maxrows then active - maxrows else first
3894 else active
3896 let navigate incr =
3897 let active = m_active + incr in
3898 let active = bound active 0 (source#getitemcount - 1) in
3899 let first = calcfirst m_first active in
3900 G.postRedisplay "outline navigate";
3901 coe {< m_active = active; m_first = first >}
3903 let navscroll first =
3904 let active =
3905 let dist = m_active - first in
3906 if dist < 0
3907 then first
3908 else (
3909 if dist < maxrows
3910 then m_active
3911 else first + maxrows
3914 G.postRedisplay "outline navscroll";
3915 coe {< m_first = first; m_active = active >}
3917 let ctrl = Wsi.withctrl mask in
3918 match key with
3919 | 97 when ctrl -> (* ctrl-a *)
3920 if m_autonarrow
3921 then source#denarrow
3922 else source#narrow m_qsearch;
3923 settext (not m_autonarrow) m_qsearch;
3924 G.postRedisplay "toggle auto narrowing";
3925 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3927 | 110 when ctrl -> (* ctrl-n *)
3928 source#narrow m_qsearch;
3929 if not m_autonarrow
3930 then source#add_narrow_pattern m_qsearch;
3931 G.postRedisplay "outline ctrl-n";
3932 coe {< m_first = 0; m_active = 0 >}
3934 | 117 when ctrl -> (* ctrl-u *)
3935 source#del_narrow_pattern;
3936 let pattern = source#renarrow in
3937 G.postRedisplay "outline ctrl-u";
3938 let text =
3939 if emptystr pattern then "" else "Narrowed to " ^ pattern
3941 settext m_autonarrow text;
3942 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
3944 | 108 when ctrl -> (* ctrl-l *)
3945 let first = max 0 (m_active - (fstate.maxrows / 2)) in
3946 G.postRedisplay "outline ctrl-l";
3947 coe {< m_first = first >}
3949 | 0xff1b -> (* escape *)
3950 let o = super#key key mask in
3951 if m_autonarrow
3952 then (
3953 if nonemptystr m_qsearch
3954 then (
3955 source#add_narrow_pattern m_qsearch;
3956 settext true "";
3961 | 0xff0d | 0xff8d when m_autonarrow -> (* (kp) enter *)
3962 if nonemptystr m_qsearch
3963 then source#add_narrow_pattern m_qsearch;
3964 super#key key mask
3966 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
3967 let pattern = m_qsearch ^ toutf8 key in
3968 G.postRedisplay "outlinelistview autonarrow add";
3969 source#narrow pattern;
3970 settext true pattern;
3971 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3973 | key when m_autonarrow && key = 0xff08 -> (* backspace *)
3974 if emptystr m_qsearch
3975 then coe self
3976 else
3977 let pattern = withoutlastutf8 m_qsearch in
3978 G.postRedisplay "outlinelistview autonarrow backspace";
3979 ignore (source#renarrow);
3980 source#narrow pattern;
3981 settext true pattern;
3982 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
3984 | 0xff9f | 0xffff -> (* (kp) delete *)
3985 source#remove m_active;
3986 G.postRedisplay "outline delete";
3987 let active = max 0 (m_active-1) in
3988 coe {< m_first = firstof m_first active;
3989 m_active = active >}
3991 | 0xff52 | 0xff97 when ctrl -> (* ctrl-(kp) up *)
3992 navscroll (max 0 (m_first - 1))
3994 | 0xff54 | 0xff99 when ctrl -> (* ctrl-(kp) down *)
3995 navscroll (min (source#getitemcount - 1) (m_first + 1))
3997 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3998 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3999 | 0xff55 | 0xff9a -> (* (kp) prior *)
4000 navigate ~-(fstate.maxrows)
4001 | 0xff56 | 0xff9b -> (* (kp) next *)
4002 navigate fstate.maxrows
4004 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
4005 let o =
4006 if ctrl
4007 then (
4008 G.postRedisplay "outline ctrl right";
4009 {< m_pan = m_pan + 1 >}
4011 else self#updownlevel 1
4013 coe o
4015 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
4016 let o =
4017 if ctrl
4018 then (
4019 G.postRedisplay "outline ctrl left";
4020 {< m_pan = m_pan - 1 >}
4022 else self#updownlevel ~-1
4024 coe o
4026 | 0xff50 | 0xff95 -> (* (kp) home *)
4027 G.postRedisplay "outline home";
4028 coe {< m_first = 0; m_active = 0 >}
4030 | 0xff57 | 0xff9c -> (* (kp) end *)
4031 let active = source#getitemcount - 1 in
4032 let first = max 0 (active - fstate.maxrows) in
4033 G.postRedisplay "outline end";
4034 coe {< m_active = active; m_first = first >}
4036 | _ -> super#key key mask
4039 let outlinesource usebookmarks =
4040 let empty = [||] in
4041 (object (self)
4042 inherit lvsourcebase
4043 val mutable m_items = empty
4044 val mutable m_orig_items = empty
4045 val mutable m_narrow_patterns = []
4046 val mutable m_hadremovals = false
4048 method getitemcount =
4049 Array.length m_items + (if m_hadremovals then 1 else 0)
4051 method getitem n =
4052 if n == Array.length m_items && m_hadremovals
4053 then
4054 ("[Confirm removal]", 0)
4055 else
4056 let s, n, _ = m_items.(n) in
4057 (s, n)
4059 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4060 ignore (uioh, first, qsearch);
4061 let confrimremoval = m_hadremovals && active = Array.length m_items in
4062 let items =
4063 if m_narrow_patterns = []
4064 then m_orig_items
4065 else m_items
4067 if not cancel
4068 then (
4069 if not confrimremoval
4070 then (
4071 let _, _, ((pageno, y, _) as anchor) = m_items.(active) in
4072 let y = getanchory
4073 (if conf.presentation then (pageno, y, 1.0) else anchor)
4075 gotoghyll y;
4076 m_items <- items;
4078 else (
4079 state.bookmarks <- Array.to_list m_items;
4080 m_orig_items <- m_items;
4083 else m_items <- items;
4084 m_pan <- pan;
4085 None
4087 method hasaction _ = true
4089 method greetmsg =
4090 if Array.length m_items != Array.length m_orig_items
4091 then
4092 let s =
4093 match m_narrow_patterns with
4094 | one :: [] -> one
4095 | many -> String.concat " --> " (List.rev many)
4097 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4098 else ""
4100 method narrow pattern =
4101 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4102 match reopt with
4103 | None -> ()
4104 | Some re ->
4105 let rec loop accu n =
4106 if n = -1
4107 then m_items <- Array.of_list accu
4108 else
4109 let (s, _, _) as o = m_items.(n) in
4110 let accu =
4111 if (try ignore (Str.search_forward re s 0); true
4112 with Not_found -> false)
4113 then o :: accu
4114 else accu
4116 loop accu (n-1)
4118 loop [] (Array.length m_items - 1)
4120 method denarrow =
4121 m_orig_items <- (
4122 if usebookmarks
4123 then Array.of_list state.bookmarks
4124 else state.outlines
4126 m_items <- m_orig_items
4128 method remove m =
4129 if usebookmarks
4130 then
4131 if m >= 0 && m < Array.length m_items
4132 then (
4133 m_hadremovals <- true;
4134 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4135 let n = if n >= m then n+1 else n in
4136 m_items.(n)
4140 method add_narrow_pattern pattern =
4141 m_narrow_patterns <- pattern :: m_narrow_patterns
4143 method del_narrow_pattern =
4144 match m_narrow_patterns with
4145 | _ :: rest -> m_narrow_patterns <- rest
4146 | [] -> ()
4148 method renarrow =
4149 self#denarrow;
4150 match m_narrow_patterns with
4151 | pattern :: [] -> self#narrow pattern; pattern
4152 | list ->
4153 List.fold_left (fun accu pattern ->
4154 self#narrow pattern;
4155 accu ^ " --> " ^ pattern) "" list
4157 method reset anchor items =
4158 m_hadremovals <- false;
4159 if m_orig_items == empty
4160 then (
4161 m_orig_items <- items;
4162 if m_narrow_patterns == []
4163 then m_items <- items;
4165 let rely = getanchory anchor in
4166 let active =
4167 let rec loop n best bestd =
4168 if n = Array.length m_items
4169 then best
4170 else
4171 let (_, _, anchor) = m_items.(n) in
4172 let orely = getanchory anchor in
4173 let d = abs (orely - rely) in
4174 if d < bestd
4175 then loop (n+1) n d
4176 else loop (n+1) best bestd
4178 loop 0 ~-1 max_int
4180 m_active <- active;
4181 m_first <- firstof m_first active
4182 end)
4185 let enterselector usebookmarks =
4186 let source = outlinesource usebookmarks in
4187 fun errmsg ->
4188 let outlines =
4189 if usebookmarks
4190 then Array.of_list state.bookmarks
4191 else state.outlines
4193 if Array.length outlines = 0
4194 then (
4195 showtext ' ' errmsg;
4197 else (
4198 state.text <- source#greetmsg;
4199 Wsi.setcursor Wsi.CURSOR_INHERIT;
4200 let anchor = getanchor () in
4201 source#reset anchor outlines;
4202 state.uioh <- coe (new outlinelistview ~source);
4203 G.postRedisplay "enter selector";
4207 let enteroutlinemode =
4208 let f = enterselector false in
4209 fun ()-> f "Document has no outline";
4212 let enterbookmarkmode =
4213 let f = enterselector true in
4214 fun () -> f "Document has no bookmarks (yet)";
4217 let color_of_string s =
4218 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4219 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4223 let color_to_string (r, g, b) =
4224 let r = truncate (r *. 256.0)
4225 and g = truncate (g *. 256.0)
4226 and b = truncate (b *. 256.0) in
4227 Printf.sprintf "%d/%d/%d" r g b
4230 let irect_of_string s =
4231 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4234 let irect_to_string (x0,y0,x1,y1) =
4235 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4238 let makecheckers () =
4239 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4240 following to say:
4241 converted by Issac Trotts. July 25, 2002 *)
4242 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4243 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4244 let id = GlTex.gen_texture () in
4245 GlTex.bind_texture `texture_2d id;
4246 GlPix.store (`unpack_alignment 1);
4247 GlTex.image2d image;
4248 List.iter (GlTex.parameter ~target:`texture_2d)
4249 [ `mag_filter `nearest; `min_filter `nearest ];
4253 let setcheckers enabled =
4254 match state.texid with
4255 | None ->
4256 if enabled then state.texid <- Some (makecheckers ())
4258 | Some texid ->
4259 if not enabled
4260 then (
4261 GlTex.delete_texture texid;
4262 state.texid <- None;
4266 let int_of_string_with_suffix s =
4267 let l = String.length s in
4268 let s1, shift =
4269 if l > 1
4270 then
4271 let suffix = Char.lowercase s.[l-1] in
4272 match suffix with
4273 | 'k' -> String.sub s 0 (l-1), 10
4274 | 'm' -> String.sub s 0 (l-1), 20
4275 | 'g' -> String.sub s 0 (l-1), 30
4276 | _ -> s, 0
4277 else s, 0
4279 let n = int_of_string s1 in
4280 let m = n lsl shift in
4281 if m < 0 || m < n
4282 then raise (Failure "value too large")
4283 else m
4286 let string_with_suffix_of_int n =
4287 if n = 0
4288 then "0"
4289 else
4290 let units = [(30, "G"); (20, "M"); (10, "K")] in
4291 let prettyint n =
4292 let rec loop s n =
4293 let h = n mod 1000 in
4294 let n = n / 1000 in
4295 if n = 0
4296 then string_of_int h ^ s
4297 else (
4298 let s = Printf.sprintf "_%03d%s" h s in
4299 loop s n
4302 loop "" n
4304 let rec find = function
4305 | [] -> prettyint n
4306 | (shift, suffix) :: rest ->
4307 if (n land ((1 lsl shift) - 1)) = 0
4308 then prettyint (n lsr shift) ^ suffix
4309 else find rest
4311 find units
4314 let defghyllscroll = (40, 8, 32);;
4315 let ghyllscroll_of_string s =
4316 let (n, a, b) as nab =
4317 if s = "default"
4318 then defghyllscroll
4319 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
4321 if n <= a || n <= b || a >= b
4322 then error "invalid ghyll N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)"
4323 n a b;
4324 nab;
4327 let ghyllscroll_to_string ((n, a, b) as nab) =
4328 if nab = defghyllscroll
4329 then "default"
4330 else Printf.sprintf "%d,%d,%d" n a b;
4333 let describe_location () =
4334 let fn = page_of_y state.y in
4335 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4336 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4337 let percent =
4338 if maxy <= 0
4339 then 100.
4340 else (100. *. (float state.y /. float maxy))
4342 if fn = ln
4343 then
4344 Printf.sprintf "page %d of %d [%.2f%%]"
4345 (fn+1) state.pagecount percent
4346 else
4347 Printf.sprintf
4348 "pages %d-%d of %d [%.2f%%]"
4349 (fn+1) (ln+1) state.pagecount percent
4352 let setpresentationmode v =
4353 let n = page_of_y state.y in
4354 state.anchor <- (n, 0.0, 1.0);
4355 conf.presentation <- v;
4356 if conf.fitmodel = FitPage
4357 then reqlayout conf.angle conf.fitmodel;
4358 represent ();
4361 let enterinfomode =
4362 let btos b = if b then "\xe2\x88\x9a" else "" in
4363 let showextended = ref false in
4364 let leave mode = function
4365 | Confirm -> state.mode <- mode
4366 | Cancel -> state.mode <- mode in
4367 let src =
4368 (object
4369 val mutable m_first_time = true
4370 val mutable m_l = []
4371 val mutable m_a = [||]
4372 val mutable m_prev_uioh = nouioh
4373 val mutable m_prev_mode = View
4375 inherit lvsourcebase
4377 method reset prev_mode prev_uioh =
4378 m_a <- Array.of_list (List.rev m_l);
4379 m_l <- [];
4380 m_prev_mode <- prev_mode;
4381 m_prev_uioh <- prev_uioh;
4382 if m_first_time
4383 then (
4384 let rec loop n =
4385 if n >= Array.length m_a
4386 then ()
4387 else
4388 match m_a.(n) with
4389 | _, _, _, Action _ -> m_active <- n
4390 | _ -> loop (n+1)
4392 loop 0;
4393 m_first_time <- false;
4396 method int name get set =
4397 m_l <-
4398 (name, `int get, 1, Action (
4399 fun u ->
4400 let ondone s =
4401 try set (int_of_string s)
4402 with exn ->
4403 state.text <- Printf.sprintf "bad integer `%s': %s"
4404 s (exntos exn)
4406 state.text <- "";
4407 let te = name ^ ": ", "", None, intentry, ondone, true in
4408 state.mode <- Textentry (te, leave m_prev_mode);
4410 )) :: m_l
4412 method int_with_suffix name get set =
4413 m_l <-
4414 (name, `intws get, 1, Action (
4415 fun u ->
4416 let ondone s =
4417 try set (int_of_string_with_suffix s)
4418 with exn ->
4419 state.text <- Printf.sprintf "bad integer `%s': %s"
4420 s (exntos exn)
4422 state.text <- "";
4423 let te =
4424 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4426 state.mode <- Textentry (te, leave m_prev_mode);
4428 )) :: m_l
4430 method bool ?(offset=1) ?(btos=btos) name get set =
4431 m_l <-
4432 (name, `bool (btos, get), offset, Action (
4433 fun u ->
4434 let v = get () in
4435 set (not v);
4437 )) :: m_l
4439 method color name get set =
4440 m_l <-
4441 (name, `color get, 1, Action (
4442 fun u ->
4443 let invalid = (nan, nan, nan) in
4444 let ondone s =
4445 let c =
4446 try color_of_string s
4447 with exn ->
4448 state.text <- Printf.sprintf "bad color `%s': %s"
4449 s (exntos exn);
4450 invalid
4452 if c <> invalid
4453 then set c;
4455 let te = name ^ ": ", "", None, textentry, ondone, true in
4456 state.text <- color_to_string (get ());
4457 state.mode <- Textentry (te, leave m_prev_mode);
4459 )) :: m_l
4461 method string name get set =
4462 m_l <-
4463 (name, `string get, 1, Action (
4464 fun u ->
4465 let ondone s = set s in
4466 let te = name ^ ": ", "", None, textentry, ondone, true in
4467 state.mode <- Textentry (te, leave m_prev_mode);
4469 )) :: m_l
4471 method colorspace name get set =
4472 m_l <-
4473 (name, `string get, 1, Action (
4474 fun _ ->
4475 let source =
4476 (object
4477 inherit lvsourcebase
4479 initializer
4480 m_active <- CSTE.to_int conf.colorspace;
4481 m_first <- 0;
4483 method getitemcount =
4484 Array.length CSTE.names
4485 method getitem n =
4486 (CSTE.names.(n), 0)
4487 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4488 ignore (uioh, first, pan, qsearch);
4489 if not cancel then set active;
4490 None
4491 method hasaction _ = true
4492 end)
4494 state.text <- "";
4495 let modehash = findkeyhash conf "info" in
4496 coe (new listview ~source ~trusted:true ~modehash)
4497 )) :: m_l
4499 method paxmark name get set =
4500 m_l <-
4501 (name, `string get, 1, Action (
4502 fun _ ->
4503 let source =
4504 (object
4505 inherit lvsourcebase
4507 initializer
4508 m_active <- MTE.to_int conf.paxmark;
4509 m_first <- 0;
4511 method getitemcount = Array.length MTE.names
4512 method getitem n = (MTE.names.(n), 0)
4513 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4514 ignore (uioh, first, pan, qsearch);
4515 if not cancel then set active;
4516 None
4517 method hasaction _ = true
4518 end)
4520 state.text <- "";
4521 let modehash = findkeyhash conf "info" in
4522 coe (new listview ~source ~trusted:true ~modehash)
4523 )) :: m_l
4525 method fitmodel name get set =
4526 m_l <-
4527 (name, `string get, 1, Action (
4528 fun _ ->
4529 let source =
4530 (object
4531 inherit lvsourcebase
4533 initializer
4534 m_active <- FMTE.to_int conf.fitmodel;
4535 m_first <- 0;
4537 method getitemcount = Array.length FMTE.names
4538 method getitem n = (FMTE.names.(n), 0)
4539 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4540 ignore (uioh, first, pan, qsearch);
4541 if not cancel then set active;
4542 None
4543 method hasaction _ = true
4544 end)
4546 state.text <- "";
4547 let modehash = findkeyhash conf "info" in
4548 coe (new listview ~source ~trusted:true ~modehash)
4549 )) :: m_l
4551 method caption s offset =
4552 m_l <- (s, `empty, offset, Noaction) :: m_l
4554 method caption2 s f offset =
4555 m_l <- (s, `string f, offset, Noaction) :: m_l
4557 method getitemcount = Array.length m_a
4559 method getitem n =
4560 let tostr = function
4561 | `int f -> string_of_int (f ())
4562 | `intws f -> string_with_suffix_of_int (f ())
4563 | `string f -> f ()
4564 | `color f -> color_to_string (f ())
4565 | `bool (btos, f) -> btos (f ())
4566 | `empty -> ""
4568 let name, t, offset, _ = m_a.(n) in
4569 ((let s = tostr t in
4570 if nonemptystr s
4571 then Printf.sprintf "%s\t%s" name s
4572 else name),
4573 offset)
4575 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4576 let uiohopt =
4577 if not cancel
4578 then (
4579 m_qsearch <- qsearch;
4580 let uioh =
4581 match m_a.(active) with
4582 | _, _, _, Action f -> f uioh
4583 | _ -> uioh
4585 Some uioh
4587 else None
4589 m_active <- active;
4590 m_first <- first;
4591 m_pan <- pan;
4592 uiohopt
4594 method hasaction n =
4595 match m_a.(n) with
4596 | _, _, _, Action _ -> true
4597 | _ -> false
4598 end)
4600 let rec fillsrc prevmode prevuioh =
4601 let sep () = src#caption "" 0 in
4602 let colorp name get set =
4603 src#string name
4604 (fun () -> color_to_string (get ()))
4605 (fun v ->
4607 let c = color_of_string v in
4608 set c
4609 with exn ->
4610 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4613 let oldmode = state.mode in
4614 let birdseye = isbirdseye state.mode in
4616 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4618 src#bool "presentation mode"
4619 (fun () -> conf.presentation)
4620 (fun v -> setpresentationmode v);
4622 src#bool "ignore case in searches"
4623 (fun () -> conf.icase)
4624 (fun v -> conf.icase <- v);
4626 src#bool "preload"
4627 (fun () -> conf.preload)
4628 (fun v -> conf.preload <- v);
4630 src#bool "highlight links"
4631 (fun () -> conf.hlinks)
4632 (fun v -> conf.hlinks <- v);
4634 src#bool "under info"
4635 (fun () -> conf.underinfo)
4636 (fun v -> conf.underinfo <- v);
4638 src#bool "persistent bookmarks"
4639 (fun () -> conf.savebmarks)
4640 (fun v -> conf.savebmarks <- v);
4642 src#fitmodel "fit model"
4643 (fun () -> FMTE.to_string conf.fitmodel)
4644 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4646 src#bool "trim margins"
4647 (fun () -> conf.trimmargins)
4648 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4650 src#bool "persistent location"
4651 (fun () -> conf.jumpback)
4652 (fun v -> conf.jumpback <- v);
4654 sep ();
4655 src#int "inter-page space"
4656 (fun () -> conf.interpagespace)
4657 (fun n ->
4658 conf.interpagespace <- n;
4659 docolumns conf.columns;
4660 let pageno, py =
4661 match state.layout with
4662 | [] -> 0, 0
4663 | l :: _ ->
4664 l.pageno, l.pagey
4666 state.maxy <- calcheight ();
4667 let y = getpagey pageno in
4668 gotoy (y + py)
4671 src#int "page bias"
4672 (fun () -> conf.pagebias)
4673 (fun v -> conf.pagebias <- v);
4675 src#int "scroll step"
4676 (fun () -> conf.scrollstep)
4677 (fun n -> conf.scrollstep <- n);
4679 src#int "horizontal scroll step"
4680 (fun () -> conf.hscrollstep)
4681 (fun v -> conf.hscrollstep <- v);
4683 src#int "auto scroll step"
4684 (fun () ->
4685 match state.autoscroll with
4686 | Some step -> step
4687 | _ -> conf.autoscrollstep)
4688 (fun n ->
4689 if state.autoscroll <> None
4690 then state.autoscroll <- Some n;
4691 conf.autoscrollstep <- n);
4693 src#int "zoom"
4694 (fun () -> truncate (conf.zoom *. 100.))
4695 (fun v -> setzoom ((float v) /. 100.));
4697 src#int "rotation"
4698 (fun () -> conf.angle)
4699 (fun v -> reqlayout v conf.fitmodel);
4701 src#int "scroll bar width"
4702 (fun () -> conf.scrollbw)
4703 (fun v ->
4704 conf.scrollbw <- v;
4705 reshape state.winw state.winh;
4708 src#int "scroll handle height"
4709 (fun () -> conf.scrollh)
4710 (fun v -> conf.scrollh <- v;);
4712 src#int "thumbnail width"
4713 (fun () -> conf.thumbw)
4714 (fun v ->
4715 conf.thumbw <- min 4096 v;
4716 match oldmode with
4717 | Birdseye beye ->
4718 leavebirdseye beye false;
4719 enterbirdseye ()
4720 | _ -> ()
4723 let mode = state.mode in
4724 src#string "columns"
4725 (fun () ->
4726 match conf.columns with
4727 | Csingle _ -> "1"
4728 | Cmulti (multi, _) -> multicolumns_to_string multi
4729 | Csplit (count, _) -> "-" ^ string_of_int count
4731 (fun v ->
4732 let n, a, b = multicolumns_of_string v in
4733 setcolumns mode n a b);
4735 sep ();
4736 src#caption "Pixmap cache" 0;
4737 src#int_with_suffix "size (advisory)"
4738 (fun () -> conf.memlimit)
4739 (fun v -> conf.memlimit <- v);
4741 src#caption2 "used"
4742 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4743 (string_with_suffix_of_int state.memused)
4744 (Hashtbl.length state.tilemap)) 1;
4746 sep ();
4747 src#caption "Layout" 0;
4748 src#caption2 "Dimension"
4749 (fun () ->
4750 Printf.sprintf "%dx%d (virtual %dx%d)"
4751 state.winw state.winh
4752 state.w state.maxy)
4754 if conf.debug
4755 then
4756 src#caption2 "Position" (fun () ->
4757 Printf.sprintf "%dx%d" state.x state.y
4759 else
4760 src#caption2 "Position" (fun () -> describe_location ()) 1
4763 sep ();
4764 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4765 "Save these parameters as global defaults at exit"
4766 (fun () -> conf.bedefault)
4767 (fun v -> conf.bedefault <- v)
4770 sep ();
4771 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4772 src#bool ~offset:0 ~btos "Extended parameters"
4773 (fun () -> !showextended)
4774 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4775 if !showextended
4776 then (
4777 src#bool "checkers"
4778 (fun () -> conf.checkers)
4779 (fun v -> conf.checkers <- v; setcheckers v);
4780 src#bool "update cursor"
4781 (fun () -> conf.updatecurs)
4782 (fun v -> conf.updatecurs <- v);
4783 src#bool "verbose"
4784 (fun () -> conf.verbose)
4785 (fun v -> conf.verbose <- v);
4786 src#bool "invert colors"
4787 (fun () -> conf.invert)
4788 (fun v -> conf.invert <- v);
4789 src#bool "max fit"
4790 (fun () -> conf.maxhfit)
4791 (fun v -> conf.maxhfit <- v);
4792 src#bool "redirect stderr"
4793 (fun () -> conf.redirectstderr)
4794 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4795 src#bool "pax mode"
4796 (fun () -> conf.pax != None)
4797 (fun v ->
4798 if v
4799 then conf.pax <- Some (ref (now (), 0, 0))
4800 else conf.pax <- None);
4801 src#string "uri launcher"
4802 (fun () -> conf.urilauncher)
4803 (fun v -> conf.urilauncher <- v);
4804 src#string "path launcher"
4805 (fun () -> conf.pathlauncher)
4806 (fun v -> conf.pathlauncher <- v);
4807 src#string "tile size"
4808 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4809 (fun v ->
4811 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4812 conf.tilew <- max 64 w;
4813 conf.tileh <- max 64 h;
4814 flushtiles ();
4815 with exn ->
4816 state.text <- Printf.sprintf "bad tile size `%s': %s"
4817 v (exntos exn)
4819 src#int "texture count"
4820 (fun () -> conf.texcount)
4821 (fun v ->
4822 if realloctexts v
4823 then conf.texcount <- v
4824 else showtext '!' " Failed to set texture count please retry later"
4826 src#int "slice height"
4827 (fun () -> conf.sliceheight)
4828 (fun v ->
4829 conf.sliceheight <- v;
4830 wcmd "sliceh %d" conf.sliceheight;
4832 src#int "anti-aliasing level"
4833 (fun () -> conf.aalevel)
4834 (fun v ->
4835 conf.aalevel <- bound v 0 8;
4836 state.anchor <- getanchor ();
4837 opendoc state.path state.password;
4839 src#string "page scroll scaling factor"
4840 (fun () -> string_of_float conf.pgscale)
4841 (fun v ->
4843 let s = float_of_string v in
4844 conf.pgscale <- s
4845 with exn ->
4846 state.text <- Printf.sprintf
4847 "bad page scroll scaling factor `%s': %s" v (exntos exn)
4850 src#int "ui font size"
4851 (fun () -> fstate.fontsize)
4852 (fun v -> setfontsize (bound v 5 100));
4853 src#int "hint font size"
4854 (fun () -> conf.hfsize)
4855 (fun v -> conf.hfsize <- bound v 5 100);
4856 colorp "background color"
4857 (fun () -> conf.bgcolor)
4858 (fun v -> conf.bgcolor <- v);
4859 src#bool "crop hack"
4860 (fun () -> conf.crophack)
4861 (fun v -> conf.crophack <- v);
4862 src#string "trim fuzz"
4863 (fun () -> irect_to_string conf.trimfuzz)
4864 (fun v ->
4866 conf.trimfuzz <- irect_of_string v;
4867 if conf.trimmargins
4868 then settrim true conf.trimfuzz;
4869 with exn ->
4870 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
4872 src#string "throttle"
4873 (fun () ->
4874 match conf.maxwait with
4875 | None -> "show place holder if page is not ready"
4876 | Some time ->
4877 if time = infinity
4878 then "wait for page to fully render"
4879 else
4880 "wait " ^ string_of_float time
4881 ^ " seconds before showing placeholder"
4883 (fun v ->
4885 let f = float_of_string v in
4886 if f <= 0.0
4887 then conf.maxwait <- None
4888 else conf.maxwait <- Some f
4889 with exn ->
4890 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
4892 src#string "ghyll scroll"
4893 (fun () ->
4894 match conf.ghyllscroll with
4895 | None -> ""
4896 | Some nab -> ghyllscroll_to_string nab
4898 (fun v ->
4900 let gs =
4901 if emptystr v
4902 then None
4903 else Some (ghyllscroll_of_string v)
4905 conf.ghyllscroll <- gs
4906 with exn ->
4907 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
4909 src#string "selection command"
4910 (fun () -> conf.selcmd)
4911 (fun v -> conf.selcmd <- v);
4912 src#string "synctex command"
4913 (fun () -> conf.stcmd)
4914 (fun v -> conf.stcmd <- v);
4915 src#string "pax command"
4916 (fun () -> conf.paxcmd)
4917 (fun v -> conf.paxcmd <- v);
4918 src#colorspace "color space"
4919 (fun () -> CSTE.to_string conf.colorspace)
4920 (fun v ->
4921 conf.colorspace <- CSTE.of_int v;
4922 wcmd "cs %d" v;
4923 load state.layout;
4925 src#paxmark "pax mark method"
4926 (fun () -> MTE.to_string conf.paxmark)
4927 (fun v -> conf.paxmark <- MTE.of_int v);
4928 if pbousable ()
4929 then
4930 src#bool "use PBO"
4931 (fun () -> conf.usepbo)
4932 (fun v -> conf.usepbo <- v);
4933 src#bool "mouse wheel scrolls pages"
4934 (fun () -> conf.wheelbypage)
4935 (fun v -> conf.wheelbypage <- v);
4936 src#bool "open remote links in a new instance"
4937 (fun () -> conf.riani)
4938 (fun v -> conf.riani <- v);
4941 sep ();
4942 src#caption "Document" 0;
4943 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4944 src#caption2 "Pages"
4945 (fun () -> string_of_int state.pagecount) 1;
4946 src#caption2 "Dimensions"
4947 (fun () -> string_of_int (List.length state.pdims)) 1;
4948 if conf.trimmargins
4949 then (
4950 sep ();
4951 src#caption "Trimmed margins" 0;
4952 src#caption2 "Dimensions"
4953 (fun () -> string_of_int (List.length state.pdims)) 1;
4956 sep ();
4957 src#caption "OpenGL" 0;
4958 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
4959 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
4961 sep ();
4962 src#caption "Location" 0;
4963 if nonemptystr state.origin
4964 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
4965 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
4967 src#reset prevmode prevuioh;
4969 fun () ->
4970 state.text <- "";
4971 let prevmode = state.mode
4972 and prevuioh = state.uioh in
4973 fillsrc prevmode prevuioh;
4974 let source = (src :> lvsource) in
4975 let modehash = findkeyhash conf "info" in
4976 state.uioh <- coe (object (self)
4977 inherit listview ~source ~trusted:true ~modehash as super
4978 val mutable m_prevmemused = 0
4979 method infochanged = function
4980 | Memused ->
4981 if m_prevmemused != state.memused
4982 then (
4983 m_prevmemused <- state.memused;
4984 G.postRedisplay "memusedchanged";
4986 | Pdim -> G.postRedisplay "pdimchanged"
4987 | Docinfo -> fillsrc prevmode prevuioh
4989 method key key mask =
4990 if not (Wsi.withctrl mask)
4991 then
4992 match key with
4993 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
4994 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
4995 | _ -> super#key key mask
4996 else super#key key mask
4997 end);
4998 G.postRedisplay "info";
5001 let enterhelpmode =
5002 let source =
5003 (object
5004 inherit lvsourcebase
5005 method getitemcount = Array.length state.help
5006 method getitem n =
5007 let s, l, _ = state.help.(n) in
5008 (s, l)
5010 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5011 let optuioh =
5012 if not cancel
5013 then (
5014 m_qsearch <- qsearch;
5015 match state.help.(active) with
5016 | _, _, Action f -> Some (f uioh)
5017 | _ -> Some (uioh)
5019 else None
5021 m_active <- active;
5022 m_first <- first;
5023 m_pan <- pan;
5024 optuioh
5026 method hasaction n =
5027 match state.help.(n) with
5028 | _, _, Action _ -> true
5029 | _ -> false
5031 initializer
5032 m_active <- -1
5033 end)
5034 in fun () ->
5035 let modehash = findkeyhash conf "help" in
5036 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
5037 G.postRedisplay "help";
5040 let entermsgsmode =
5041 let msgsource =
5042 let re = Str.regexp "[\r\n]" in
5043 (object
5044 inherit lvsourcebase
5045 val mutable m_items = [||]
5047 method getitemcount = 1 + Array.length m_items
5049 method getitem n =
5050 if n = 0
5051 then "[Clear]", 0
5052 else m_items.(n-1), 0
5054 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5055 ignore uioh;
5056 if not cancel
5057 then (
5058 if active = 0
5059 then Buffer.clear state.errmsgs;
5060 m_qsearch <- qsearch;
5062 m_active <- active;
5063 m_first <- first;
5064 m_pan <- pan;
5065 None
5067 method hasaction n =
5068 n = 0
5070 method reset =
5071 state.newerrmsgs <- false;
5072 let l = Str.split re (Buffer.contents state.errmsgs) in
5073 m_items <- Array.of_list l
5075 initializer
5076 m_active <- 0
5077 end)
5078 in fun () ->
5079 state.text <- "";
5080 msgsource#reset;
5081 let source = (msgsource :> lvsource) in
5082 let modehash = findkeyhash conf "listview" in
5083 state.uioh <- coe (object
5084 inherit listview ~source ~trusted:false ~modehash as super
5085 method display =
5086 if state.newerrmsgs
5087 then msgsource#reset;
5088 super#display
5089 end);
5090 G.postRedisplay "msgs";
5093 let quickbookmark ?title () =
5094 match state.layout with
5095 | [] -> ()
5096 | l :: _ ->
5097 let title =
5098 match title with
5099 | None ->
5100 let sec = Unix.gettimeofday () in
5101 let tm = Unix.localtime sec in
5102 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
5103 (l.pageno+1)
5104 tm.Unix.tm_mday
5105 tm.Unix.tm_mon
5106 (tm.Unix.tm_year + 1900)
5107 tm.Unix.tm_hour
5108 tm.Unix.tm_min
5109 | Some title -> title
5111 state.bookmarks <- (title, 0, getanchor1 l) :: state.bookmarks
5114 let setautoscrollspeed step goingdown =
5115 let incr = max 1 ((abs step) / 2) in
5116 let incr = if goingdown then incr else -incr in
5117 let astep = step + incr in
5118 state.autoscroll <- Some astep;
5121 let gotounder under =
5122 let getpath filename =
5123 let path =
5124 if nonemptystr filename
5125 then
5126 if Filename.is_relative filename
5127 then
5128 let dir = Filename.dirname state.path in
5129 let dir =
5130 if Filename.is_implicit dir
5131 then Filename.concat (Sys.getcwd ()) dir
5132 else dir
5134 Filename.concat dir filename
5135 else filename
5136 else ""
5138 if Sys.file_exists path
5139 then path
5140 else ""
5142 match under with
5143 | Ulinkgoto (pageno, top) ->
5144 if pageno >= 0
5145 then (
5146 addnav ();
5147 gotopage1 pageno top;
5150 | Ulinkuri s ->
5151 gotouri s
5153 | Uremote (filename, pageno) ->
5154 let path = getpath filename in
5155 if nonemptystr path
5156 then (
5157 if conf.riani
5158 then
5159 let command = !selfexec ^ " " ^ path in
5160 try popen command []
5161 with exn ->
5162 Printf.eprintf
5163 "failed to execute `%s': %s\n" command (exntos exn);
5164 flush stderr;
5165 else
5166 let anchor = getanchor () in
5167 let ranchor = state.path, state.password, anchor, state.origin in
5168 state.origin <- "";
5169 state.anchor <- (pageno, 0.0, 0.0);
5170 state.ranchors <- ranchor :: state.ranchors;
5171 opendoc path "";
5173 else showtext '!' ("Could not find " ^ filename)
5175 | Uremotedest (filename, destname) ->
5176 let path = getpath filename in
5177 if nonemptystr path
5178 then (
5179 if conf.riani
5180 then
5181 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
5182 try popen command []
5183 with exn ->
5184 Printf.eprintf
5185 "failed to execute `%s': %s\n" command (exntos exn);
5186 flush stderr;
5187 else
5188 let anchor = getanchor () in
5189 let ranchor = state.path, state.password, anchor, state.origin in
5190 state.origin <- "";
5191 state.nameddest <- destname;
5192 state.ranchors <- ranchor :: state.ranchors;
5193 opendoc path "";
5195 else showtext '!' ("Could not find " ^ filename)
5197 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
5200 let canpan () =
5201 match conf.columns with
5202 | Csplit _ -> true
5203 | _ -> state.x != 0 || conf.zoom > 1.0
5206 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5208 let existsinrow pageno (columns, coverA, coverB) p =
5209 let last = ((pageno - coverA) mod columns) + columns in
5210 let rec any = function
5211 | [] -> false
5212 | l :: rest ->
5213 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5214 then p l
5215 else (
5216 if not (p l)
5217 then (if l.pageno = last then false else any rest)
5218 else true
5221 any state.layout
5224 let nextpage () =
5225 match state.layout with
5226 | [] ->
5227 let pageno = page_of_y state.y in
5228 gotoghyll (getpagey (pageno+1))
5229 | l :: rest ->
5230 match conf.columns with
5231 | Csingle _ ->
5232 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5233 then
5234 let y = clamp (pgscale state.winh) in
5235 gotoghyll y
5236 else
5237 let pageno = min (l.pageno+1) (state.pagecount-1) in
5238 gotoghyll (getpagey pageno)
5239 | Cmulti ((c, _, _) as cl, _) ->
5240 if conf.presentation
5241 && (existsinrow l.pageno cl
5242 (fun l -> l.pageh > l.pagey + l.pagevh))
5243 then
5244 let y = clamp (pgscale state.winh) in
5245 gotoghyll y
5246 else
5247 let pageno = min (l.pageno+c) (state.pagecount-1) in
5248 gotoghyll (getpagey pageno)
5249 | Csplit (n, _) ->
5250 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5251 then
5252 let pagey, pageh = getpageyh l.pageno in
5253 let pagey = pagey + pageh * l.pagecol in
5254 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5255 gotoghyll (pagey + pageh + ips)
5258 let prevpage () =
5259 match state.layout with
5260 | [] ->
5261 let pageno = page_of_y state.y in
5262 gotoghyll (getpagey (pageno-1))
5263 | l :: _ ->
5264 match conf.columns with
5265 | Csingle _ ->
5266 if conf.presentation && l.pagey != 0
5267 then
5268 gotoghyll (clamp (pgscale ~-(state.winh)))
5269 else
5270 let pageno = max 0 (l.pageno-1) in
5271 gotoghyll (getpagey pageno)
5272 | Cmulti ((c, _, coverB) as cl, _) ->
5273 if conf.presentation &&
5274 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5275 then
5276 gotoghyll (clamp (pgscale ~-(state.winh)))
5277 else
5278 let decr =
5279 if l.pageno = state.pagecount - coverB
5280 then 1
5281 else c
5283 let pageno = max 0 (l.pageno-decr) in
5284 gotoghyll (getpagey pageno)
5285 | Csplit (n, _) ->
5286 let y =
5287 if l.pagecol = 0
5288 then
5289 if l.pageno = 0
5290 then l.pagey
5291 else
5292 let pageno = max 0 (l.pageno-1) in
5293 let pagey, pageh = getpageyh pageno in
5294 pagey + (n-1)*pageh
5295 else
5296 let pagey, pageh = getpageyh l.pageno in
5297 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5299 gotoghyll y
5302 let viewkeyboard key mask =
5303 let enttext te =
5304 let mode = state.mode in
5305 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5306 state.text <- "";
5307 enttext ();
5308 G.postRedisplay "view:enttext"
5310 let ctrl = Wsi.withctrl mask in
5311 let key =
5312 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5314 match key with
5315 | 81 -> (* Q *)
5316 exit 0
5318 | 0xff63 -> (* insert *)
5319 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5320 then (
5321 state.mode <- LinkNav (Ltgendir 0);
5322 gotoy state.y;
5324 else showtext '!' "Keyboard link navigation does not work under rotation"
5326 | 0xff1b | 113 -> (* escape / q *)
5327 begin match state.mstate with
5328 | Mzoomrect _ ->
5329 state.mstate <- Mnone;
5330 Wsi.setcursor Wsi.CURSOR_INHERIT;
5331 G.postRedisplay "kill zoom rect";
5332 | _ ->
5333 begin match state.mode with
5334 | LinkNav _ ->
5335 state.mode <- View;
5336 G.postRedisplay "esc leave linknav"
5337 | _ ->
5338 match state.ranchors with
5339 | [] -> raise Quit
5340 | (path, password, anchor, origin) :: rest ->
5341 state.ranchors <- rest;
5342 state.anchor <- anchor;
5343 state.origin <- origin;
5344 state.nameddest <- "";
5345 opendoc path password
5346 end;
5347 end;
5349 | 0xff08 -> (* backspace *)
5350 gotoghyll (getnav ~-1)
5352 | 111 -> (* o *)
5353 enteroutlinemode ()
5355 | 117 -> (* u *)
5356 state.rects <- [];
5357 state.text <- "";
5358 G.postRedisplay "dehighlight";
5360 | 47 | 63 -> (* / ? *)
5361 let ondone isforw s =
5362 cbput state.hists.pat s;
5363 state.searchpattern <- s;
5364 search s isforw
5366 let s = String.create 1 in
5367 s.[0] <- Char.chr key;
5368 enttext (s, "", Some (onhist state.hists.pat),
5369 textentry, ondone (key = 47), true)
5371 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5372 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5373 setzoom (conf.zoom +. incr)
5375 | 43 | 0xffab -> (* + *)
5376 let ondone s =
5377 let n =
5378 try int_of_string s with exc ->
5379 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5380 max_int
5382 if n != max_int
5383 then (
5384 conf.pagebias <- n;
5385 state.text <- "page bias is now " ^ string_of_int n;
5388 enttext ("page bias: ", "", None, intentry, ondone, true)
5390 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5391 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5392 setzoom (max 0.01 (conf.zoom -. decr))
5394 | 45 | 0xffad -> (* - *)
5395 let ondone msg = state.text <- msg in
5396 enttext (
5397 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5398 optentry state.mode, ondone, true
5401 | 48 when ctrl -> (* ctrl-0 *)
5402 if conf.zoom = 1.0
5403 then (
5404 state.x <- 0;
5405 gotoy state.y
5407 else setzoom 1.0
5409 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5410 let cols =
5411 match conf.columns with
5412 | Csingle _ | Cmulti _ -> 1
5413 | Csplit (n, _) -> n
5415 let h = state.winh -
5416 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5418 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5419 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5420 then setzoom zoom
5422 | 51 when ctrl -> (* ctrl-3 *)
5423 let fm =
5424 match conf.fitmodel with
5425 | FitWidth -> FitProportional
5426 | FitProportional -> FitPage
5427 | FitPage -> FitWidth
5429 state.text <- "fit model: " ^ FMTE.to_string fm;
5430 reqlayout conf.angle fm
5432 | 0xffc6 -> (* f9 *)
5433 togglebirdseye ()
5435 | 57 when ctrl -> (* ctrl-9 *)
5436 togglebirdseye ()
5438 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5439 when not ctrl -> (* 0..9 *)
5440 let ondone s =
5441 let n =
5442 try int_of_string s with exc ->
5443 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5446 if n >= 0
5447 then (
5448 addnav ();
5449 cbput state.hists.pag (string_of_int n);
5450 gotopage1 (n + conf.pagebias - 1) 0;
5453 let pageentry text key =
5454 match Char.unsafe_chr key with
5455 | 'g' -> TEdone text
5456 | _ -> intentry text key
5458 let text = "x" in text.[0] <- Char.chr key;
5459 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5461 | 98 -> (* b *)
5462 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5463 reshape state.winw state.winh;
5465 | 66 -> (* B *)
5466 state.bzoom <- not state.bzoom;
5467 state.rects <- [];
5468 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5470 | 108 -> (* l *)
5471 conf.hlinks <- not conf.hlinks;
5472 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5473 G.postRedisplay "toggle highlightlinks";
5475 | 70 -> (* F *)
5476 state.glinks <- true;
5477 let mode = state.mode in
5478 state.mode <- Textentry (
5479 (":", "", None, linknentry, linkndone gotounder, false),
5480 (fun _ ->
5481 state.glinks <- false;
5482 state.mode <- mode)
5484 state.text <- "";
5485 G.postRedisplay "view:linkent(F)"
5487 | 121 -> (* y *)
5488 state.glinks <- true;
5489 let mode = state.mode in
5490 state.mode <- Textentry (
5492 ":", "", None, linknentry, linkndone (fun under ->
5493 selstring (undertext under);
5494 ), false
5496 fun _ ->
5497 state.glinks <- false;
5498 state.mode <- mode
5500 state.text <- "";
5501 G.postRedisplay "view:linkent"
5503 | 97 -> (* a *)
5504 begin match state.autoscroll with
5505 | Some step ->
5506 conf.autoscrollstep <- step;
5507 state.autoscroll <- None
5508 | None ->
5509 if conf.autoscrollstep = 0
5510 then state.autoscroll <- Some 1
5511 else state.autoscroll <- Some conf.autoscrollstep
5514 | 112 when ctrl -> (* ctrl-p *)
5515 launchpath ()
5517 | 80 -> (* P *)
5518 setpresentationmode (not conf.presentation);
5519 showtext ' ' ("presentation mode " ^
5520 if conf.presentation then "on" else "off");
5522 | 102 -> (* f *)
5523 if List.mem Wsi.Fullscreen state.winstate
5524 then Wsi.reshape conf.cwinw conf.cwinh
5525 else Wsi.fullscreen ()
5527 | 112 | 78 -> (* p|N *)
5528 search state.searchpattern false
5530 | 110 | 0xffc0 -> (* n|F3 *)
5531 search state.searchpattern true
5533 | 116 -> (* t *)
5534 begin match state.layout with
5535 | [] -> ()
5536 | l :: _ ->
5537 gotoghyll (getpagey l.pageno)
5540 | 32 -> (* space *)
5541 nextpage ()
5543 | 0xff9f | 0xffff -> (* delete *)
5544 prevpage ()
5546 | 61 -> (* = *)
5547 showtext ' ' (describe_location ());
5549 | 119 -> (* w *)
5550 begin match state.layout with
5551 | [] -> ()
5552 | l :: _ ->
5553 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5554 G.postRedisplay "w"
5557 | 39 -> (* ' *)
5558 enterbookmarkmode ()
5560 | 104 | 0xffbe -> (* h|F1 *)
5561 enterhelpmode ()
5563 | 105 -> (* i *)
5564 enterinfomode ()
5566 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5567 entermsgsmode ()
5569 | 109 -> (* m *)
5570 let ondone s =
5571 match state.layout with
5572 | l :: _ ->
5573 if nonemptystr s
5574 then
5575 state.bookmarks <- (s, 0, getanchor1 l) :: state.bookmarks
5576 | _ -> ()
5578 enttext ("bookmark: ", "", None, textentry, ondone, true)
5580 | 126 -> (* ~ *)
5581 quickbookmark ();
5582 showtext ' ' "Quick bookmark added";
5584 | 122 -> (* z *)
5585 begin match state.layout with
5586 | l :: _ ->
5587 let rect = getpdimrect l.pagedimno in
5588 let w, h =
5589 if conf.crophack
5590 then
5591 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5592 truncate (1.2 *. (rect.(3) -. rect.(0))))
5593 else
5594 (truncate (rect.(1) -. rect.(0)),
5595 truncate (rect.(3) -. rect.(0)))
5597 let w = truncate ((float w)*.conf.zoom)
5598 and h = truncate ((float h)*.conf.zoom) in
5599 if w != 0 && h != 0
5600 then (
5601 state.anchor <- getanchor ();
5602 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5604 G.postRedisplay "z";
5606 | [] -> ()
5609 | 120 -> state.roam ()
5610 | 60 | 62 -> (* < > *)
5611 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5613 | 91 | 93 -> (* [ ] *)
5614 conf.colorscale <-
5615 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5617 G.postRedisplay "brightness";
5619 | 99 when state.mode = View -> (* [alt-]c *)
5620 if Wsi.withalt mask
5621 then (
5622 if conf.zoom > 1.0
5623 then
5624 let m = (wadjsb state.winw - state.w) / 2 in
5625 state.x <- m;
5626 gotoy_and_clear_text state.y
5628 else
5629 let (c, a, b), z =
5630 match state.prevcolumns with
5631 | None -> (1, 0, 0), 1.0
5632 | Some (columns, z) ->
5633 let cab =
5634 match columns with
5635 | Csplit (c, _) -> -c, 0, 0
5636 | Cmulti ((c, a, b), _) -> c, a, b
5637 | Csingle _ -> 1, 0, 0
5639 cab, z
5641 setcolumns View c a b;
5642 setzoom z
5644 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
5645 -> (* ctrl-shift- (kp) [up|down] *)
5646 let zoom, x = state.prevzoom in
5647 setzoom zoom;
5648 state.x <- x;
5650 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5651 begin match state.autoscroll with
5652 | None ->
5653 begin match state.mode with
5654 | Birdseye beye -> upbirdseye 1 beye
5655 | _ ->
5656 if ctrl
5657 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5658 else (
5659 if not (Wsi.withshift mask) && conf.presentation
5660 then prevpage ()
5661 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5664 | Some n ->
5665 setautoscrollspeed n false
5668 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5669 begin match state.autoscroll with
5670 | None ->
5671 begin match state.mode with
5672 | Birdseye beye -> downbirdseye 1 beye
5673 | _ ->
5674 if ctrl
5675 then gotoy_and_clear_text (clamp (state.winh/2))
5676 else (
5677 if not (Wsi.withshift mask) && conf.presentation
5678 then nextpage ()
5679 else gotoy_and_clear_text (clamp conf.scrollstep)
5682 | Some n ->
5683 setautoscrollspeed n true
5686 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5687 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5688 if canpan ()
5689 then
5690 let dx =
5691 if ctrl
5692 then state.winw / 2
5693 else conf.hscrollstep
5695 let dx = if key = 0xff51 or key = 0xff96 then dx else -dx in
5696 state.x <- panbound (state.x + dx);
5697 gotoy_and_clear_text state.y
5698 else (
5699 state.text <- "";
5700 G.postRedisplay "left/right"
5703 | 0xff55 | 0xff9a -> (* (kp) prior *)
5704 let y =
5705 if ctrl
5706 then
5707 match state.layout with
5708 | [] -> state.y
5709 | l :: _ -> state.y - l.pagey
5710 else
5711 clamp (pgscale (-state.winh))
5713 gotoghyll y
5715 | 0xff56 | 0xff9b -> (* (kp) next *)
5716 let y =
5717 if ctrl
5718 then
5719 match List.rev state.layout with
5720 | [] -> state.y
5721 | l :: _ -> getpagey l.pageno
5722 else
5723 clamp (pgscale state.winh)
5725 gotoghyll y
5727 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5728 gotoghyll 0
5729 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5730 gotoghyll (clamp state.maxy)
5732 | 0xff53 | 0xff98
5733 when Wsi.withalt mask -> (* alt-(kp) right *)
5734 gotoghyll (getnav 1)
5735 | 0xff51 | 0xff96
5736 when Wsi.withalt mask -> (* alt-(kp) left *)
5737 gotoghyll (getnav ~-1)
5739 | 114 -> (* r *)
5740 reload ()
5742 | 118 when conf.debug -> (* v *)
5743 state.rects <- [];
5744 List.iter (fun l ->
5745 match getopaque l.pageno with
5746 | None -> ()
5747 | Some opaque ->
5748 let x0, y0, x1, y1 = pagebbox opaque in
5749 let a,b = float x0, float y0 in
5750 let c,d = float x1, float y0 in
5751 let e,f = float x1, float y1 in
5752 let h,j = float x0, float y1 in
5753 let rect = (a,b,c,d,e,f,h,j) in
5754 debugrect rect;
5755 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5756 ) state.layout;
5757 G.postRedisplay "v";
5759 | _ ->
5760 vlog "huh? %s" (Wsi.keyname key)
5763 let linknavkeyboard key mask linknav =
5764 let getpage pageno =
5765 let rec loop = function
5766 | [] -> None
5767 | l :: _ when l.pageno = pageno -> Some l
5768 | _ :: rest -> loop rest
5769 in loop state.layout
5771 let doexact (pageno, n) =
5772 match getopaque pageno, getpage pageno with
5773 | Some opaque, Some l ->
5774 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5775 then
5776 let under = getlink opaque n in
5777 G.postRedisplay "link gotounder";
5778 gotounder under;
5779 state.mode <- View;
5780 else
5781 let opt, dir =
5782 match key with
5783 | 0xff50 -> (* home *)
5784 Some (findlink opaque LDfirst), -1
5786 | 0xff57 -> (* end *)
5787 Some (findlink opaque LDlast), 1
5789 | 0xff51 -> (* left *)
5790 Some (findlink opaque (LDleft n)), -1
5792 | 0xff53 -> (* right *)
5793 Some (findlink opaque (LDright n)), 1
5795 | 0xff52 -> (* up *)
5796 Some (findlink opaque (LDup n)), -1
5798 | 0xff54 -> (* down *)
5799 Some (findlink opaque (LDdown n)), 1
5801 | _ -> None, 0
5803 let pwl l dir =
5804 begin match findpwl l.pageno dir with
5805 | Pwlnotfound -> ()
5806 | Pwl pageno ->
5807 let notfound dir =
5808 state.mode <- LinkNav (Ltgendir dir);
5809 let y, h = getpageyh pageno in
5810 let y =
5811 if dir < 0
5812 then y + h - state.winh
5813 else y
5815 gotoy y
5817 begin match getopaque pageno, getpage pageno with
5818 | Some opaque, Some _ ->
5819 let link =
5820 let ld = if dir > 0 then LDfirst else LDlast in
5821 findlink opaque ld
5823 begin match link with
5824 | Lfound m ->
5825 showlinktype (getlink opaque m);
5826 state.mode <- LinkNav (Ltexact (pageno, m));
5827 G.postRedisplay "linknav jpage";
5828 | _ -> notfound dir
5829 end;
5830 | _ -> notfound dir
5831 end;
5832 end;
5834 begin match opt with
5835 | Some Lnotfound -> pwl l dir;
5836 | Some (Lfound m) ->
5837 if m = n
5838 then pwl l dir
5839 else (
5840 let _, y0, _, y1 = getlinkrect opaque m in
5841 if y0 < l.pagey
5842 then gotopage1 l.pageno y0
5843 else (
5844 let d = fstate.fontsize + 1 in
5845 if y1 - l.pagey > l.pagevh - d
5846 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5847 else G.postRedisplay "linknav";
5849 showlinktype (getlink opaque m);
5850 state.mode <- LinkNav (Ltexact (l.pageno, m));
5853 | None -> viewkeyboard key mask
5854 end;
5855 | _ -> viewkeyboard key mask
5857 if key = 0xff63
5858 then (
5859 state.mode <- View;
5860 G.postRedisplay "leave linknav"
5862 else
5863 match linknav with
5864 | Ltgendir _ -> viewkeyboard key mask
5865 | Ltexact exact -> doexact exact
5868 let keyboard key mask =
5869 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5870 then wcmd "interrupt"
5871 else state.uioh <- state.uioh#key key mask
5874 let birdseyekeyboard key mask
5875 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5876 let incr =
5877 match conf.columns with
5878 | Csingle _ -> 1
5879 | Cmulti ((c, _, _), _) -> c
5880 | Csplit _ -> failwith "bird's eye split mode"
5882 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5883 match key with
5884 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5885 let y, h = getpageyh pageno in
5886 let top = (state.winh - h) / 2 in
5887 gotoy (max 0 (y - top))
5888 | 0xff0d (* enter *)
5889 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5890 | 0xff1b -> leavebirdseye beye true (* escape *)
5891 | 0xff52 -> upbirdseye incr beye (* up *)
5892 | 0xff54 -> downbirdseye incr beye (* down *)
5893 | 0xff51 -> upbirdseye 1 beye (* left *)
5894 | 0xff53 -> downbirdseye 1 beye (* right *)
5896 | 0xff55 -> (* prior *)
5897 begin match state.layout with
5898 | l :: _ ->
5899 if l.pagey != 0
5900 then (
5901 state.mode <- Birdseye (
5902 oconf, leftx, l.pageno, hooverpageno, anchor
5904 gotopage1 l.pageno 0;
5906 else (
5907 let layout = layout (state.y-state.winh) (pgh state.layout) in
5908 match layout with
5909 | [] -> gotoy (clamp (-state.winh))
5910 | l :: _ ->
5911 state.mode <- Birdseye (
5912 oconf, leftx, l.pageno, hooverpageno, anchor
5914 gotopage1 l.pageno 0
5917 | [] -> gotoy (clamp (-state.winh))
5918 end;
5920 | 0xff56 -> (* next *)
5921 begin match List.rev state.layout with
5922 | l :: _ ->
5923 let layout = layout (state.y + (pgh state.layout)) state.winh in
5924 begin match layout with
5925 | [] ->
5926 let incr = l.pageh - l.pagevh in
5927 if incr = 0
5928 then (
5929 state.mode <-
5930 Birdseye (
5931 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
5933 G.postRedisplay "birdseye pagedown";
5935 else gotoy (clamp (incr + conf.interpagespace*2));
5937 | l :: _ ->
5938 state.mode <-
5939 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
5940 gotopage1 l.pageno 0;
5943 | [] -> gotoy (clamp state.winh)
5944 end;
5946 | 0xff50 -> (* home *)
5947 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
5948 gotopage1 0 0
5950 | 0xff57 -> (* end *)
5951 let pageno = state.pagecount - 1 in
5952 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
5953 if not (pagevisible state.layout pageno)
5954 then
5955 let h =
5956 match List.rev state.pdims with
5957 | [] -> state.winh
5958 | (_, _, h, _) :: _ -> h
5960 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
5961 else G.postRedisplay "birdseye end";
5962 | _ -> viewkeyboard key mask
5965 let drawpage l =
5966 let color =
5967 match state.mode with
5968 | Textentry _ -> scalecolor 0.4
5969 | LinkNav _
5970 | View -> scalecolor 1.0
5971 | Birdseye (_, _, pageno, hooverpageno, _) ->
5972 if l.pageno = hooverpageno
5973 then scalecolor 0.9
5974 else (
5975 if l.pageno = pageno
5976 then scalecolor 1.0
5977 else scalecolor 0.8
5980 drawtiles l color;
5983 let postdrawpage l linkindexbase =
5984 match getopaque l.pageno with
5985 | Some opaque ->
5986 if tileready l l.pagex l.pagey
5987 then
5988 let x = l.pagedispx - l.pagex
5989 and y = l.pagedispy - l.pagey in
5990 let hlmask =
5991 match conf.columns with
5992 | Csingle _ | Cmulti _ ->
5993 (if conf.hlinks then 1 else 0)
5994 + (if state.glinks
5995 && not (isbirdseye state.mode) then 2 else 0)
5996 | _ -> 0
5998 let s =
5999 match state.mode with
6000 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
6001 | _ -> ""
6003 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
6004 else 0
6005 | _ -> 0
6008 let scrollindicator () =
6009 let sbw, ph, sh = state.uioh#scrollph in
6010 let sbh, pw, sw = state.uioh#scrollpw in
6012 GlDraw.color (0.64, 0.64, 0.64);
6013 GlDraw.rect
6014 (float (state.winw - sbw), 0.)
6015 (float state.winw, float state.winh)
6017 GlDraw.rect
6018 (0., float (state.winh - sbh))
6019 (float (wadjsb state.winw - 1), float state.winh)
6021 GlDraw.color (0.0, 0.0, 0.0);
6023 GlDraw.rect
6024 (float (state.winw - sbw), ph)
6025 (float state.winw, ph +. sh)
6027 GlDraw.rect
6028 (pw, float (state.winh - sbh))
6029 (pw +. sw, float state.winh)
6033 let showsel () =
6034 match state.mstate with
6035 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
6038 | Msel ((x0, y0), (x1, y1)) ->
6039 let rec loop = function
6040 | l :: ls ->
6041 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
6042 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
6043 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
6044 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
6045 then
6046 match getopaque l.pageno with
6047 | Some opaque ->
6048 let x0, y0 = pagetranslatepoint l x0 y0 in
6049 let x1, y1 = pagetranslatepoint l x1 y1 in
6050 seltext opaque (x0, y0, x1, y1);
6051 | _ -> ()
6052 else loop ls
6053 | [] -> ()
6055 loop state.layout
6058 let showrects rects =
6059 Gl.enable `blend;
6060 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
6061 GlDraw.polygon_mode `both `fill;
6062 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6063 List.iter
6064 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
6065 List.iter (fun l ->
6066 if l.pageno = pageno
6067 then (
6068 let dx = float (l.pagedispx - l.pagex) in
6069 let dy = float (l.pagedispy - l.pagey) in
6070 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
6071 GlDraw.begins `quads;
6073 GlDraw.vertex2 (x0+.dx, y0+.dy);
6074 GlDraw.vertex2 (x1+.dx, y1+.dy);
6075 GlDraw.vertex2 (x2+.dx, y2+.dy);
6076 GlDraw.vertex2 (x3+.dx, y3+.dy);
6078 GlDraw.ends ();
6080 ) state.layout
6081 ) rects
6083 Gl.disable `blend;
6086 let display () =
6087 GlClear.color (scalecolor2 conf.bgcolor);
6088 GlClear.clear [`color];
6089 List.iter drawpage state.layout;
6090 let rects =
6091 match state.mode with
6092 | LinkNav (Ltexact (pageno, linkno)) ->
6093 begin match getopaque pageno with
6094 | Some opaque ->
6095 let x0, y0, x1, y1 = getlinkrect opaque linkno in
6096 (pageno, 5, (
6097 float x0, float y0,
6098 float x1, float y0,
6099 float x1, float y1,
6100 float x0, float y1)
6101 ) :: state.rects
6102 | None -> state.rects
6104 | _ -> state.rects
6106 showrects rects;
6107 let rec postloop linkindexbase = function
6108 | l :: rest ->
6109 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
6110 postloop linkindexbase rest
6111 | [] -> ()
6113 showsel ();
6114 postloop 0 state.layout;
6115 state.uioh#display;
6116 begin match state.mstate with
6117 | Mzoomrect ((x0, y0), (x1, y1)) ->
6118 Gl.enable `blend;
6119 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
6120 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6121 GlDraw.rect (float x0, float y0)
6122 (float x1, float y1);
6123 Gl.disable `blend;
6124 | _ -> ()
6125 end;
6126 enttext ();
6127 scrollindicator ();
6128 Wsi.swapb ();
6131 let zoomrect x y x1 y1 =
6132 let x0 = min x x1
6133 and x1 = max x x1
6134 and y0 = min y y1 in
6135 gotoy (state.y + y0);
6136 state.anchor <- getanchor ();
6137 let zoom = (float state.w) /. float (x1 - x0) in
6138 let margin =
6139 match conf.fitmodel, conf.columns with
6140 | FitPage, Csplit _ ->
6141 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
6143 | _, _ ->
6144 let adjw = wadjsb state.winw in
6145 if state.w < adjw
6146 then (adjw - state.w) / 2
6147 else 0
6149 state.x <- (state.x + margin) - x0;
6150 setzoom zoom;
6151 Wsi.setcursor Wsi.CURSOR_INHERIT;
6152 state.mstate <- Mnone;
6155 let zoomblock x y =
6156 let g opaque l px py =
6157 match rectofblock opaque px py with
6158 | Some a ->
6159 let x0 = a.(0) -. 20. in
6160 let x1 = a.(1) +. 20. in
6161 let y0 = a.(2) -. 20. in
6162 let zoom = (float state.w) /. (x1 -. x0) in
6163 let pagey = getpagey l.pageno in
6164 gotoy_and_clear_text (pagey + truncate y0);
6165 state.anchor <- getanchor ();
6166 let margin = (state.w - l.pagew)/2 in
6167 state.x <- -truncate x0 - margin;
6168 setzoom zoom;
6169 None
6170 | None -> None
6172 match conf.columns with
6173 | Csplit _ ->
6174 showtext '!' "block zooming does not work properly in split columns mode"
6175 | _ -> onppundermouse g x y ()
6178 let scrollx x =
6179 let winw = wadjsb state.winw - 1 in
6180 let s = float x /. float winw in
6181 let destx = truncate (float (state.w + winw) *. s) in
6182 state.x <- winw - destx;
6183 gotoy_and_clear_text state.y;
6184 state.mstate <- Mscrollx;
6187 let scrolly y =
6188 let s = float y /. float state.winh in
6189 let desty = truncate (float (state.maxy - state.winh) *. s) in
6190 gotoy_and_clear_text desty;
6191 state.mstate <- Mscrolly;
6194 let viewmouse button down x y mask =
6195 match button with
6196 | n when (n == 4 || n == 5) && not down ->
6197 if Wsi.withctrl mask
6198 then (
6199 match state.mstate with
6200 | Mzoom (oldn, i) ->
6201 if oldn = n
6202 then (
6203 if i = 2
6204 then
6205 let incr =
6206 match n with
6207 | 5 ->
6208 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6209 | _ ->
6210 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6212 let zoom = conf.zoom -. incr in
6213 setzoom zoom;
6214 state.mstate <- Mzoom (n, 0);
6215 else
6216 state.mstate <- Mzoom (n, i+1);
6218 else state.mstate <- Mzoom (n, 0)
6220 | _ -> state.mstate <- Mzoom (n, 0)
6222 else (
6223 match state.autoscroll with
6224 | Some step -> setautoscrollspeed step (n=4)
6225 | None ->
6226 if conf.wheelbypage || conf.presentation
6227 then (
6228 if n = 4
6229 then prevpage ()
6230 else nextpage ()
6232 else
6233 let incr =
6234 if n = 4
6235 then -conf.scrollstep
6236 else conf.scrollstep
6238 let incr = incr * 2 in
6239 let y = clamp incr in
6240 gotoy_and_clear_text y
6243 | n when (n = 6 || n = 7) && not down && canpan () ->
6244 state.x <-
6245 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6246 gotoy_and_clear_text state.y
6248 | 1 when Wsi.withshift mask ->
6249 state.mstate <- Mnone;
6250 if not down
6251 then (
6252 match unproject x y with
6253 | Some (pageno, ux, uy) ->
6254 let cmd = Printf.sprintf
6255 "%s %s %d %d %d"
6256 conf.stcmd state.path pageno ux uy
6258 popen cmd []
6259 | None -> ()
6262 | 1 when Wsi.withctrl mask ->
6263 if down
6264 then (
6265 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6266 state.mstate <- Mpan (x, y)
6268 else
6269 state.mstate <- Mnone
6271 | 3 ->
6272 if down
6273 then (
6274 Wsi.setcursor Wsi.CURSOR_CYCLE;
6275 let p = (x, y) in
6276 state.mstate <- Mzoomrect (p, p)
6278 else (
6279 match state.mstate with
6280 | Mzoomrect ((x0, y0), _) ->
6281 if abs (x-x0) > 10 && abs (y - y0) > 10
6282 then zoomrect x0 y0 x y
6283 else (
6284 state.mstate <- Mnone;
6285 Wsi.setcursor Wsi.CURSOR_INHERIT;
6286 G.postRedisplay "kill accidental zoom rect";
6288 | _ ->
6289 Wsi.setcursor Wsi.CURSOR_INHERIT;
6290 state.mstate <- Mnone
6293 | 1 when x > state.winw - vscrollw () ->
6294 if down
6295 then
6296 let _, position, sh = state.uioh#scrollph in
6297 if y > truncate position && y < truncate (position +. sh)
6298 then state.mstate <- Mscrolly
6299 else scrolly y
6300 else
6301 state.mstate <- Mnone
6303 | 1 when y > state.winh - hscrollh () ->
6304 if down
6305 then
6306 let _, position, sw = state.uioh#scrollpw in
6307 if x > truncate position && x < truncate (position +. sw)
6308 then state.mstate <- Mscrollx
6309 else scrollx x
6310 else
6311 state.mstate <- Mnone
6313 | 1 when state.bzoom -> if not down then zoomblock x y
6315 | 1 ->
6316 let dest = if down then getunder x y else Unone in
6317 begin match dest with
6318 | Ulinkgoto _
6319 | Ulinkuri _
6320 | Uremote _ | Uremotedest _
6321 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6322 gotounder dest
6324 | Unone when down ->
6325 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6326 state.mstate <- Mpan (x, y);
6328 | Unone | Utext _ ->
6329 if down
6330 then (
6331 if conf.angle mod 360 = 0
6332 then (
6333 state.mstate <- Msel ((x, y), (x, y));
6334 G.postRedisplay "mouse select";
6337 else (
6338 match state.mstate with
6339 | Mnone -> ()
6341 | Mzoom _ | Mscrollx | Mscrolly ->
6342 state.mstate <- Mnone
6344 | Mzoomrect ((x0, y0), _) ->
6345 zoomrect x0 y0 x y
6347 | Mpan _ ->
6348 Wsi.setcursor Wsi.CURSOR_INHERIT;
6349 state.mstate <- Mnone
6351 | Msel ((x0, y0), (x1, y1)) ->
6352 let rec loop = function
6353 | [] -> ()
6354 | l :: rest ->
6355 let inside =
6356 let a0 = l.pagedispy in
6357 let a1 = a0 + l.pagevh in
6358 let b0 = l.pagedispx in
6359 let b1 = b0 + l.pagevw in
6360 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6361 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6363 if inside
6364 then
6365 match getopaque l.pageno with
6366 | Some opaque ->
6367 begin
6368 match Ne.pipe () with
6369 | Ne.Exn exn ->
6370 showtext '!'
6371 (Printf.sprintf
6372 "can not create sel pipe: %s"
6373 (exntos exn));
6374 | Ne.Res (r, w) ->
6375 let doclose what fd =
6376 Ne.clo fd (fun msg ->
6377 dolog "%s close failed: %s" what msg)
6380 popen conf.selcmd [r, 0; w, -1];
6381 copysel w opaque true;
6382 doclose "pipe/r" r;
6383 G.postRedisplay "copysel";
6384 with exn ->
6385 dolog "can not execute %S: %s"
6386 conf.selcmd (exntos exn);
6387 doclose "pipe/r" r;
6388 doclose "pipe/w" w;
6390 | None -> ()
6391 else loop rest
6393 loop state.layout;
6394 Wsi.setcursor Wsi.CURSOR_INHERIT;
6395 state.mstate <- Mnone;
6399 | _ -> ()
6402 let birdseyemouse button down x y mask
6403 (conf, leftx, _, hooverpageno, anchor) =
6404 match button with
6405 | 1 when down ->
6406 let rec loop = function
6407 | [] -> ()
6408 | l :: rest ->
6409 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6410 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6411 then (
6412 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6414 else loop rest
6416 loop state.layout
6417 | 3 -> ()
6418 | _ -> viewmouse button down x y mask
6421 let mouse button down x y mask =
6422 state.uioh <- state.uioh#button button down x y mask;
6425 let motion ~x ~y =
6426 state.uioh <- state.uioh#motion x y
6429 let pmotion ~x ~y =
6430 state.uioh <- state.uioh#pmotion x y;
6433 let uioh = object
6434 method display = ()
6436 method key key mask =
6437 begin match state.mode with
6438 | Textentry textentry -> textentrykeyboard key mask textentry
6439 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6440 | View -> viewkeyboard key mask
6441 | LinkNav linknav -> linknavkeyboard key mask linknav
6442 end;
6443 state.uioh
6445 method button button bstate x y mask =
6446 begin match state.mode with
6447 | LinkNav _
6448 | View -> viewmouse button bstate x y mask
6449 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6450 | Textentry _ -> ()
6451 end;
6452 state.uioh
6454 method motion x y =
6455 begin match state.mode with
6456 | Textentry _ -> ()
6457 | View | Birdseye _ | LinkNav _ ->
6458 match state.mstate with
6459 | Mzoom _ | Mnone -> ()
6461 | Mpan (x0, y0) ->
6462 let dx = x - x0
6463 and dy = y0 - y in
6464 state.mstate <- Mpan (x, y);
6465 if canpan ()
6466 then state.x <- panbound (state.x + dx);
6467 let y = clamp dy in
6468 gotoy_and_clear_text y
6470 | Msel (a, _) ->
6471 state.mstate <- Msel (a, (x, y));
6472 G.postRedisplay "motion select";
6474 | Mscrolly ->
6475 let y = min state.winh (max 0 y) in
6476 scrolly y
6478 | Mscrollx ->
6479 let x = min state.winw (max 0 x) in
6480 scrollx x
6482 | Mzoomrect (p0, _) ->
6483 state.mstate <- Mzoomrect (p0, (x, y));
6484 G.postRedisplay "motion zoomrect";
6485 end;
6486 state.uioh
6488 method pmotion x y =
6489 begin match state.mode with
6490 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6491 let rec loop = function
6492 | [] ->
6493 if hooverpageno != -1
6494 then (
6495 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6496 G.postRedisplay "pmotion birdseye no hoover";
6498 | l :: rest ->
6499 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6500 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6501 then (
6502 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6503 G.postRedisplay "pmotion birdseye hoover";
6505 else loop rest
6507 loop state.layout
6509 | Textentry _ -> ()
6511 | LinkNav _
6512 | View ->
6513 match state.mstate with
6514 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6516 | Mnone ->
6517 updateunder x y;
6518 match conf.pax with
6519 | None -> ()
6520 | Some r ->
6521 let past, _, _ = !r in
6522 let now = now () in
6523 let delta = now -. past in
6524 if delta > 0.01
6525 then paxunder x y
6526 else r := (now, x, y)
6527 end;
6528 state.uioh
6530 method infochanged _ = ()
6532 method scrollph =
6533 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6534 let p, h =
6535 if maxy = 0
6536 then 0.0, float state.winh
6537 else scrollph state.y maxy
6539 vscrollw (), p, h
6541 method scrollpw =
6542 let winw = wadjsb state.winw in
6543 let fwinw = float winw in
6544 let sw =
6545 let sw = fwinw /. float state.w in
6546 let sw = fwinw *. sw in
6547 max sw (float conf.scrollh)
6549 let position =
6550 let maxx = state.w + winw in
6551 let x = winw - state.x in
6552 let percent = float x /. float maxx in
6553 (fwinw -. sw) *. percent
6555 hscrollh (), position, sw
6557 method modehash =
6558 let modename =
6559 match state.mode with
6560 | LinkNav _ -> "links"
6561 | Textentry _ -> "textentry"
6562 | Birdseye _ -> "birdseye"
6563 | View -> "view"
6565 findkeyhash conf modename
6567 method eformsgs = true
6568 end;;
6570 module Config =
6571 struct
6572 open Parser
6574 let fontpath = ref "";;
6576 module KeyMap =
6577 Map.Make (struct type t = (int * int) let compare = compare end);;
6579 let unent s =
6580 let l = String.length s in
6581 let b = Buffer.create l in
6582 unent b s 0 l;
6583 Buffer.contents b;
6586 let home =
6587 try Sys.getenv "HOME"
6588 with exn ->
6589 prerr_endline
6590 ("Can not determine home directory location: " ^ exntos exn);
6594 let modifier_of_string = function
6595 | "alt" -> Wsi.altmask
6596 | "shift" -> Wsi.shiftmask
6597 | "ctrl" | "control" -> Wsi.ctrlmask
6598 | "meta" -> Wsi.metamask
6599 | _ -> 0
6602 let key_of_string =
6603 let r = Str.regexp "-" in
6604 fun s ->
6605 let elems = Str.full_split r s in
6606 let f n k m =
6607 let g s =
6608 let m1 = modifier_of_string s in
6609 if m1 = 0
6610 then (Wsi.namekey s, m)
6611 else (k, m lor m1)
6612 in function
6613 | Str.Delim s when n land 1 = 0 -> g s
6614 | Str.Text s -> g s
6615 | Str.Delim _ -> (k, m)
6617 let rec loop n k m = function
6618 | [] -> (k, m)
6619 | x :: xs ->
6620 let k, m = f n k m x in
6621 loop (n+1) k m xs
6623 loop 0 0 0 elems
6626 let keys_of_string =
6627 let r = Str.regexp "[ \t]" in
6628 fun s ->
6629 let elems = Str.split r s in
6630 List.map key_of_string elems
6633 let copykeyhashes c =
6634 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6637 let config_of c attrs =
6638 let apply c k v =
6640 match k with
6641 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6642 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6643 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6644 | "preload" -> { c with preload = bool_of_string v }
6645 | "page-bias" -> { c with pagebias = int_of_string v }
6646 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6647 | "horizontal-scroll-step" ->
6648 { c with hscrollstep = max (int_of_string v) 1 }
6649 | "auto-scroll-step" ->
6650 { c with autoscrollstep = max 0 (int_of_string v) }
6651 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6652 | "crop-hack" -> { c with crophack = bool_of_string v }
6653 | "throttle" ->
6654 let mw =
6655 match String.lowercase v with
6656 | "true" -> Some infinity
6657 | "false" -> None
6658 | f -> Some (float_of_string f)
6660 { c with maxwait = mw}
6661 | "highlight-links" -> { c with hlinks = bool_of_string v }
6662 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6663 | "vertical-margin" ->
6664 { c with interpagespace = max 0 (int_of_string v) }
6665 | "zoom" ->
6666 let zoom = float_of_string v /. 100. in
6667 let zoom = max zoom 0.0 in
6668 { c with zoom = zoom }
6669 | "presentation" -> { c with presentation = bool_of_string v }
6670 | "rotation-angle" -> { c with angle = int_of_string v }
6671 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6672 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6673 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6674 | "proportional-display" ->
6675 let fm =
6676 if bool_of_string v
6677 then FitProportional
6678 else FitWidth
6680 { c with fitmodel = fm }
6681 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6682 | "pixmap-cache-size" ->
6683 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6684 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6685 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6686 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6687 | "persistent-location" -> { c with jumpback = bool_of_string v }
6688 | "background-color" -> { c with bgcolor = color_of_string v }
6689 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6690 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6691 | "mupdf-store-size" ->
6692 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6693 | "checkers" -> { c with checkers = bool_of_string v }
6694 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6695 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6696 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6697 | "uri-launcher" -> { c with urilauncher = unent v }
6698 | "path-launcher" -> { c with pathlauncher = unent v }
6699 | "color-space" -> { c with colorspace = CSTE.of_string v }
6700 | "invert-colors" -> { c with invert = bool_of_string v }
6701 | "brightness" -> { c with colorscale = float_of_string v }
6702 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6703 | "ghyllscroll" ->
6704 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
6705 | "columns" ->
6706 let (n, _, _) as nab = multicolumns_of_string v in
6707 if n < 0
6708 then { c with columns = Csplit (-n, [||]) }
6709 else { c with columns = Cmulti (nab, [||]) }
6710 | "birds-eye-columns" ->
6711 { c with beyecolumns = Some (max (int_of_string v) 2) }
6712 | "selection-command" -> { c with selcmd = unent v }
6713 | "synctex-command" -> { c with stcmd = unent v }
6714 | "pax-command" -> { c with paxcmd = unent v }
6715 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6716 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6717 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6718 | "use-pbo" -> { c with usepbo = bool_of_string v }
6719 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6720 | "horizontal-scrollbar-visible" ->
6721 let b =
6722 if bool_of_string v
6723 then c.scrollb lor scrollbhv
6724 else c.scrollb land (lnot scrollbhv)
6726 { c with scrollb = b }
6727 | "vertical-scrollbar-visible" ->
6728 let b =
6729 if bool_of_string v
6730 then c.scrollb lor scrollbvv
6731 else c.scrollb land (lnot scrollbvv)
6733 { c with scrollb = b }
6734 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6735 | "point-and-x" ->
6736 { c with pax =
6737 if bool_of_string v
6738 then Some (ref (0.0, 0, 0))
6739 else None }
6740 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
6741 | _ -> c
6742 with exn ->
6743 prerr_endline ("Error processing attribute (`" ^
6744 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6747 let rec fold c = function
6748 | [] -> c
6749 | (k, v) :: rest ->
6750 let c = apply c k v in
6751 fold c rest
6753 fold { c with keyhashes = copykeyhashes c } attrs;
6756 let fromstring f pos n v d =
6757 try f v
6758 with exn ->
6759 dolog "Error processing attribute (%S=%S) at %d\n%s"
6760 n v pos (exntos exn)
6765 let bookmark_of attrs =
6766 let rec fold title page rely visy = function
6767 | ("title", v) :: rest -> fold v page rely visy rest
6768 | ("page", v) :: rest -> fold title v rely visy rest
6769 | ("rely", v) :: rest -> fold title page v visy rest
6770 | ("visy", v) :: rest -> fold title page rely v rest
6771 | _ :: rest -> fold title page rely visy rest
6772 | [] -> title, page, rely, visy
6774 fold "invalid" "0" "0" "0" attrs
6777 let doc_of attrs =
6778 let rec fold path page rely pan visy = function
6779 | ("path", v) :: rest -> fold v page rely pan visy rest
6780 | ("page", v) :: rest -> fold path v rely pan visy rest
6781 | ("rely", v) :: rest -> fold path page v pan visy rest
6782 | ("pan", v) :: rest -> fold path page rely v visy rest
6783 | ("visy", v) :: rest -> fold path page rely pan v rest
6784 | _ :: rest -> fold path page rely pan visy rest
6785 | [] -> path, page, rely, pan, visy
6787 fold "" "0" "0" "0" "0" attrs
6790 let map_of attrs =
6791 let rec fold rs ls = function
6792 | ("out", v) :: rest -> fold v ls rest
6793 | ("in", v) :: rest -> fold rs v rest
6794 | _ :: rest -> fold ls rs rest
6795 | [] -> ls, rs
6797 fold "" "" attrs
6800 let setconf dst src =
6801 dst.scrollbw <- src.scrollbw;
6802 dst.scrollh <- src.scrollh;
6803 dst.icase <- src.icase;
6804 dst.preload <- src.preload;
6805 dst.pagebias <- src.pagebias;
6806 dst.verbose <- src.verbose;
6807 dst.scrollstep <- src.scrollstep;
6808 dst.maxhfit <- src.maxhfit;
6809 dst.crophack <- src.crophack;
6810 dst.autoscrollstep <- src.autoscrollstep;
6811 dst.maxwait <- src.maxwait;
6812 dst.hlinks <- src.hlinks;
6813 dst.underinfo <- src.underinfo;
6814 dst.interpagespace <- src.interpagespace;
6815 dst.zoom <- src.zoom;
6816 dst.presentation <- src.presentation;
6817 dst.angle <- src.angle;
6818 dst.cwinw <- src.cwinw;
6819 dst.cwinh <- src.cwinh;
6820 dst.savebmarks <- src.savebmarks;
6821 dst.memlimit <- src.memlimit;
6822 dst.fitmodel <- src.fitmodel;
6823 dst.texcount <- src.texcount;
6824 dst.sliceheight <- src.sliceheight;
6825 dst.thumbw <- src.thumbw;
6826 dst.jumpback <- src.jumpback;
6827 dst.bgcolor <- src.bgcolor;
6828 dst.tilew <- src.tilew;
6829 dst.tileh <- src.tileh;
6830 dst.mustoresize <- src.mustoresize;
6831 dst.checkers <- src.checkers;
6832 dst.aalevel <- src.aalevel;
6833 dst.trimmargins <- src.trimmargins;
6834 dst.trimfuzz <- src.trimfuzz;
6835 dst.urilauncher <- src.urilauncher;
6836 dst.colorspace <- src.colorspace;
6837 dst.invert <- src.invert;
6838 dst.colorscale <- src.colorscale;
6839 dst.redirectstderr <- src.redirectstderr;
6840 dst.ghyllscroll <- src.ghyllscroll;
6841 dst.columns <- src.columns;
6842 dst.beyecolumns <- src.beyecolumns;
6843 dst.selcmd <- src.selcmd;
6844 dst.updatecurs <- src.updatecurs;
6845 dst.pathlauncher <- src.pathlauncher;
6846 dst.keyhashes <- copykeyhashes src;
6847 dst.hfsize <- src.hfsize;
6848 dst.hscrollstep <- src.hscrollstep;
6849 dst.pgscale <- src.pgscale;
6850 dst.usepbo <- src.usepbo;
6851 dst.wheelbypage <- src.wheelbypage;
6852 dst.stcmd <- src.stcmd;
6853 dst.paxcmd <- src.paxcmd;
6854 dst.scrollb <- src.scrollb;
6855 dst.riani <- src.riani;
6856 dst.paxmark <- src.paxmark;
6857 dst.pax <-
6858 if src.pax = None
6859 then None
6860 else Some ((ref (0.0, 0, 0)));
6863 let get s =
6864 let h = Hashtbl.create 10 in
6865 let dc = { defconf with angle = defconf.angle } in
6866 let rec toplevel v t spos _ =
6867 match t with
6868 | Vdata | Vcdata | Vend -> v
6869 | Vopen ("llppconfig", _, closed) ->
6870 if closed
6871 then v
6872 else { v with f = llppconfig }
6873 | Vopen _ ->
6874 error "unexpected subelement at top level" s spos
6875 | Vclose _ -> error "unexpected close at top level" s spos
6877 and llppconfig v t spos _ =
6878 match t with
6879 | Vdata | Vcdata -> v
6880 | Vend -> error "unexpected end of input in llppconfig" s spos
6881 | Vopen ("defaults", attrs, closed) ->
6882 let c = config_of dc attrs in
6883 setconf dc c;
6884 if closed
6885 then v
6886 else { v with f = defaults }
6888 | Vopen ("ui-font", attrs, closed) ->
6889 let rec getsize size = function
6890 | [] -> size
6891 | ("size", v) :: rest ->
6892 let size =
6893 fromstring int_of_string spos "size" v fstate.fontsize in
6894 getsize size rest
6895 | l -> getsize size l
6897 fstate.fontsize <- getsize fstate.fontsize attrs;
6898 if closed
6899 then v
6900 else { v with f = uifont (Buffer.create 10) }
6902 | Vopen ("doc", attrs, closed) ->
6903 let pathent, spage, srely, span, svisy = doc_of attrs in
6904 let path = unent pathent
6905 and pageno = fromstring int_of_string spos "page" spage 0
6906 and rely = fromstring float_of_string spos "rely" srely 0.0
6907 and pan = fromstring int_of_string spos "pan" span 0
6908 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6909 let c = config_of dc attrs in
6910 let anchor = (pageno, rely, visy) in
6911 if closed
6912 then (Hashtbl.add h path (c, [], pan, anchor); v)
6913 else { v with f = doc path pan anchor c [] }
6915 | Vopen _ ->
6916 error "unexpected subelement in llppconfig" s spos
6918 | Vclose "llppconfig" -> { v with f = toplevel }
6919 | Vclose _ -> error "unexpected close in llppconfig" s spos
6921 and defaults v t spos _ =
6922 match t with
6923 | Vdata | Vcdata -> v
6924 | Vend -> error "unexpected end of input in defaults" s spos
6925 | Vopen ("keymap", attrs, closed) ->
6926 let modename =
6927 try List.assoc "mode" attrs
6928 with Not_found -> "global" in
6929 if closed
6930 then v
6931 else
6932 let ret keymap =
6933 let h = findkeyhash dc modename in
6934 KeyMap.iter (Hashtbl.replace h) keymap;
6935 defaults
6937 { v with f = pkeymap ret KeyMap.empty }
6939 | Vopen (_, _, _) ->
6940 error "unexpected subelement in defaults" s spos
6942 | Vclose "defaults" ->
6943 { v with f = llppconfig }
6945 | Vclose _ -> error "unexpected close in defaults" s spos
6947 and uifont b v t spos epos =
6948 match t with
6949 | Vdata | Vcdata ->
6950 Buffer.add_substring b s spos (epos - spos);
6952 | Vopen (_, _, _) ->
6953 error "unexpected subelement in ui-font" s spos
6954 | Vclose "ui-font" ->
6955 if emptystr !fontpath
6956 then fontpath := Buffer.contents b;
6957 { v with f = llppconfig }
6958 | Vclose _ -> error "unexpected close in ui-font" s spos
6959 | Vend -> error "unexpected end of input in ui-font" s spos
6961 and doc path pan anchor c bookmarks v t spos _ =
6962 match t with
6963 | Vdata | Vcdata -> v
6964 | Vend -> error "unexpected end of input in doc" s spos
6965 | Vopen ("bookmarks", _, closed) ->
6966 if closed
6967 then v
6968 else { v with f = pbookmarks path pan anchor c bookmarks }
6970 | Vopen ("keymap", attrs, closed) ->
6971 let modename =
6972 try List.assoc "mode" attrs
6973 with Not_found -> "global"
6975 if closed
6976 then v
6977 else
6978 let ret keymap =
6979 let h = findkeyhash c modename in
6980 KeyMap.iter (Hashtbl.replace h) keymap;
6981 doc path pan anchor c bookmarks
6983 { v with f = pkeymap ret KeyMap.empty }
6985 | Vopen (_, _, _) ->
6986 error "unexpected subelement in doc" s spos
6988 | Vclose "doc" ->
6989 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
6990 { v with f = llppconfig }
6992 | Vclose _ -> error "unexpected close in doc" s spos
6994 and pkeymap ret keymap v t spos _ =
6995 match t with
6996 | Vdata | Vcdata -> v
6997 | Vend -> error "unexpected end of input in keymap" s spos
6998 | Vopen ("map", attrs, closed) ->
6999 let r, l = map_of attrs in
7000 let kss = fromstring keys_of_string spos "in" r [] in
7001 let lss = fromstring keys_of_string spos "out" l [] in
7002 let keymap =
7003 match kss with
7004 | [] -> keymap
7005 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
7006 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
7008 if closed
7009 then { v with f = pkeymap ret keymap }
7010 else
7011 let f () = v in
7012 { v with f = skip "map" f }
7014 | Vopen _ ->
7015 error "unexpected subelement in keymap" s spos
7017 | Vclose "keymap" ->
7018 { v with f = ret keymap }
7020 | Vclose _ -> error "unexpected close in keymap" s spos
7022 and pbookmarks path pan anchor c bookmarks v t spos _ =
7023 match t with
7024 | Vdata | Vcdata -> v
7025 | Vend -> error "unexpected end of input in bookmarks" s spos
7026 | Vopen ("item", attrs, closed) ->
7027 let titleent, spage, srely, svisy = bookmark_of attrs in
7028 let page = fromstring int_of_string spos "page" spage 0
7029 and rely = fromstring float_of_string spos "rely" srely 0.0
7030 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7031 let bookmarks =
7032 (unent titleent, 0, (page, rely, visy)) :: bookmarks
7034 if closed
7035 then { v with f = pbookmarks path pan anchor c bookmarks }
7036 else
7037 let f () = v in
7038 { v with f = skip "item" f }
7040 | Vopen _ ->
7041 error "unexpected subelement in bookmarks" s spos
7043 | Vclose "bookmarks" ->
7044 { v with f = doc path pan anchor c bookmarks }
7046 | Vclose _ -> error "unexpected close in bookmarks" s spos
7048 and skip tag f v t spos _ =
7049 match t with
7050 | Vdata | Vcdata -> v
7051 | Vend ->
7052 error ("unexpected end of input in skipped " ^ tag) s spos
7053 | Vopen (tag', _, closed) ->
7054 if closed
7055 then v
7056 else
7057 let f' () = { v with f = skip tag f } in
7058 { v with f = skip tag' f' }
7059 | Vclose ctag ->
7060 if tag = ctag
7061 then f ()
7062 else error ("unexpected close in skipped " ^ tag) s spos
7065 parse { f = toplevel; accu = () } s;
7066 h, dc;
7069 let do_load f ic =
7071 let len = in_channel_length ic in
7072 let s = String.create len in
7073 really_input ic s 0 len;
7074 f s;
7075 with
7076 | Parse_error (msg, s, pos) ->
7077 let subs = subs s pos in
7078 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
7080 | exn ->
7081 failwith ("config load error: " ^ exntos exn)
7084 let defconfpath =
7085 let dir =
7087 let dir = Filename.concat home ".config" in
7088 if Sys.is_directory dir then dir else home
7089 with _ -> home
7091 Filename.concat dir "llpp.conf"
7094 let confpath = ref defconfpath;;
7096 let load1 f =
7097 if Sys.file_exists !confpath
7098 then
7099 match
7100 (try Some (open_in_bin !confpath)
7101 with exn ->
7102 prerr_endline
7103 ("Error opening configuration file `" ^ !confpath ^ "': " ^
7104 exntos exn);
7105 None
7107 with
7108 | Some ic ->
7109 let success =
7111 f (do_load get ic)
7112 with exn ->
7113 prerr_endline
7114 ("Error loading configuration from `" ^ !confpath ^ "': " ^
7115 exntos exn);
7116 false
7118 close_in ic;
7119 success
7121 | None -> false
7122 else
7123 f (Hashtbl.create 0, defconf)
7126 let load () =
7127 let f (h, dc) =
7128 let pc, pb, px, pa =
7130 let key =
7131 if emptystr state.origin
7132 then state.path
7133 else state.origin
7135 Hashtbl.find h (Filename.basename key)
7136 with Not_found -> dc, [], 0, emptyanchor
7138 setconf defconf dc;
7139 setconf conf pc;
7140 state.bookmarks <- pb;
7141 state.x <- px;
7142 if conf.jumpback
7143 then state.anchor <- pa;
7144 cbput state.hists.nav pa;
7145 true
7147 load1 f
7150 let add_attrs bb always dc c =
7151 let ob s a b =
7152 if always || a != b
7153 then Printf.bprintf bb "\n %s='%b'" s a
7154 and op s a b =
7155 if always || a <> b
7156 then Printf.bprintf bb "\n %s='%b'" s (a != None)
7157 and oi s a b =
7158 if always || a != b
7159 then Printf.bprintf bb "\n %s='%d'" s a
7160 and oI s a b =
7161 if always || a != b
7162 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
7163 and oz s a b =
7164 if always || a <> b
7165 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
7166 and oF s a b =
7167 if always || a <> b
7168 then Printf.bprintf bb "\n %s='%f'" s a
7169 and oc s a b =
7170 if always || a <> b
7171 then
7172 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
7173 and oC s a b =
7174 if always || a <> b
7175 then
7176 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
7177 and oR s a b =
7178 if always || a <> b
7179 then
7180 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
7181 and os s a b =
7182 if always || a <> b
7183 then
7184 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7185 and og s a b =
7186 if always || a <> b
7187 then
7188 match a with
7189 | None -> ()
7190 | Some (_N, _A, _B) ->
7191 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7192 and oW s a b =
7193 if always || a <> b
7194 then
7195 let v =
7196 match a with
7197 | None -> "false"
7198 | Some f ->
7199 if f = infinity
7200 then "true"
7201 else string_of_float f
7203 Printf.bprintf bb "\n %s='%s'" s v
7204 and oco s a b =
7205 if always || a <> b
7206 then
7207 match a with
7208 | Cmulti ((n, a, b), _) when n > 1 ->
7209 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7210 | Csplit (n, _) when n > 1 ->
7211 Printf.bprintf bb "\n %s='%d'" s ~-n
7212 | _ -> ()
7213 and obeco s a b =
7214 if always || a <> b
7215 then
7216 match a with
7217 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7218 | _ -> ()
7219 and oFm s a b =
7220 if always || a <> b
7221 then
7222 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7223 and oSv s a b m =
7224 if always || a <> b
7225 then
7226 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7227 and oPm s a b =
7228 if always || a <> b
7229 then
7230 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
7232 oi "width" c.cwinw dc.cwinw;
7233 oi "height" c.cwinh dc.cwinh;
7234 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7235 oi "scroll-handle-height" c.scrollh dc.scrollh;
7236 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7237 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7238 ob "case-insensitive-search" c.icase dc.icase;
7239 ob "preload" c.preload dc.preload;
7240 oi "page-bias" c.pagebias dc.pagebias;
7241 oi "scroll-step" c.scrollstep dc.scrollstep;
7242 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7243 ob "max-height-fit" c.maxhfit dc.maxhfit;
7244 ob "crop-hack" c.crophack dc.crophack;
7245 oW "throttle" c.maxwait dc.maxwait;
7246 ob "highlight-links" c.hlinks dc.hlinks;
7247 ob "under-cursor-info" c.underinfo dc.underinfo;
7248 oi "vertical-margin" c.interpagespace dc.interpagespace;
7249 oz "zoom" c.zoom dc.zoom;
7250 ob "presentation" c.presentation dc.presentation;
7251 oi "rotation-angle" c.angle dc.angle;
7252 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7253 oFm "fit-model" c.fitmodel dc.fitmodel;
7254 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7255 oi "tex-count" c.texcount dc.texcount;
7256 oi "slice-height" c.sliceheight dc.sliceheight;
7257 oi "thumbnail-width" c.thumbw dc.thumbw;
7258 ob "persistent-location" c.jumpback dc.jumpback;
7259 oc "background-color" c.bgcolor dc.bgcolor;
7260 oi "tile-width" c.tilew dc.tilew;
7261 oi "tile-height" c.tileh dc.tileh;
7262 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7263 ob "checkers" c.checkers dc.checkers;
7264 oi "aalevel" c.aalevel dc.aalevel;
7265 ob "trim-margins" c.trimmargins dc.trimmargins;
7266 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7267 os "uri-launcher" c.urilauncher dc.urilauncher;
7268 os "path-launcher" c.pathlauncher dc.pathlauncher;
7269 oC "color-space" c.colorspace dc.colorspace;
7270 ob "invert-colors" c.invert dc.invert;
7271 oF "brightness" c.colorscale dc.colorscale;
7272 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7273 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7274 oco "columns" c.columns dc.columns;
7275 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7276 os "selection-command" c.selcmd dc.selcmd;
7277 os "synctex-command" c.stcmd dc.stcmd;
7278 os "pax-command" c.paxcmd dc.paxcmd;
7279 ob "update-cursor" c.updatecurs dc.updatecurs;
7280 oi "hint-font-size" c.hfsize dc.hfsize;
7281 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7282 oF "page-scroll-scale" c.pgscale dc.pgscale;
7283 ob "use-pbo" c.usepbo dc.usepbo;
7284 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7285 ob "remote-in-a-new-instance" c.riani dc.riani;
7286 op "point-and-x" c.pax dc.pax;
7287 oPm "point-and-x-mark" c.paxmark dc.paxmark;
7290 let keymapsbuf always dc c =
7291 let bb = Buffer.create 16 in
7292 let rec loop = function
7293 | [] -> ()
7294 | (modename, h) :: rest ->
7295 let dh = findkeyhash dc modename in
7296 if always || h <> dh
7297 then (
7298 if Hashtbl.length h > 0
7299 then (
7300 if Buffer.length bb > 0
7301 then Buffer.add_char bb '\n';
7302 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7303 Hashtbl.iter (fun i o ->
7304 let isdifferent = always ||
7306 let dO = Hashtbl.find dh i in
7307 dO <> o
7308 with Not_found -> true
7310 if isdifferent
7311 then
7312 let addkm (k, m) =
7313 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7314 if Wsi.withalt m then Buffer.add_string bb "alt-";
7315 if Wsi.withshift m then Buffer.add_string bb "shift-";
7316 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7317 Buffer.add_string bb (Wsi.keyname k);
7319 let addkms l =
7320 let rec loop = function
7321 | [] -> ()
7322 | km :: [] -> addkm km
7323 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7325 loop l
7327 Buffer.add_string bb "<map in='";
7328 addkm i;
7329 match o with
7330 | KMinsrt km ->
7331 Buffer.add_string bb "' out='";
7332 addkm km;
7333 Buffer.add_string bb "'/>\n"
7335 | KMinsrl kms ->
7336 Buffer.add_string bb "' out='";
7337 addkms kms;
7338 Buffer.add_string bb "'/>\n"
7340 | KMmulti (ins, kms) ->
7341 Buffer.add_char bb ' ';
7342 addkms ins;
7343 Buffer.add_string bb "' out='";
7344 addkms kms;
7345 Buffer.add_string bb "'/>\n"
7346 ) h;
7347 Buffer.add_string bb "</keymap>";
7350 loop rest
7352 loop c.keyhashes;
7356 let save () =
7357 let uifontsize = fstate.fontsize in
7358 let bb = Buffer.create 32768 in
7359 let relx = float state.x /. float state.winw in
7360 let w, h, x =
7361 let cx w = truncate (relx *. float w) in
7362 List.fold_left
7363 (fun (w, h, x) ws ->
7364 match ws with
7365 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7366 | Wsi.MaxVert -> (w, conf.cwinh, x)
7367 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7369 (state.winw, state.winh, state.x) state.winstate
7371 conf.cwinw <- w;
7372 conf.cwinh <- h;
7373 let f (h, dc) =
7374 let dc = if conf.bedefault then conf else dc in
7375 Buffer.add_string bb "<llppconfig>\n";
7377 if nonemptystr !fontpath
7378 then
7379 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7380 uifontsize
7381 !fontpath
7382 else (
7383 if uifontsize <> 14
7384 then
7385 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7388 Buffer.add_string bb "<defaults ";
7389 add_attrs bb true dc dc;
7390 let kb = keymapsbuf true dc dc in
7391 if Buffer.length kb > 0
7392 then (
7393 Buffer.add_string bb ">\n";
7394 Buffer.add_buffer bb kb;
7395 Buffer.add_string bb "\n</defaults>\n";
7397 else Buffer.add_string bb "/>\n";
7399 let adddoc path pan anchor c bookmarks =
7400 if bookmarks == [] && c = dc && anchor = emptyanchor
7401 then ()
7402 else (
7403 Printf.bprintf bb "<doc path='%s'"
7404 (enent path 0 (String.length path));
7406 if anchor <> emptyanchor
7407 then (
7408 let n, rely, visy = anchor in
7409 Printf.bprintf bb " page='%d'" n;
7410 if rely > 1e-6
7411 then
7412 Printf.bprintf bb " rely='%f'" rely
7414 if abs_float visy > 1e-6
7415 then
7416 Printf.bprintf bb " visy='%f'" visy
7420 if pan != 0
7421 then Printf.bprintf bb " pan='%d'" pan;
7423 add_attrs bb false dc c;
7424 let kb = keymapsbuf false dc c in
7426 begin match bookmarks with
7427 | [] ->
7428 if Buffer.length kb > 0
7429 then (
7430 Buffer.add_string bb ">\n";
7431 Buffer.add_buffer bb kb;
7432 Buffer.add_string bb "\n</doc>\n";
7434 else Buffer.add_string bb "/>\n"
7435 | _ ->
7436 Buffer.add_string bb ">\n<bookmarks>\n";
7437 List.iter (fun (title, _level, (page, rely, visy)) ->
7438 Printf.bprintf bb
7439 "<item title='%s' page='%d'"
7440 (enent title 0 (String.length title))
7441 page
7443 if rely > 1e-6
7444 then
7445 Printf.bprintf bb " rely='%f'" rely
7447 if abs_float visy > 1e-6
7448 then
7449 Printf.bprintf bb " visy='%f'" visy
7451 Buffer.add_string bb "/>\n";
7452 ) bookmarks;
7453 Buffer.add_string bb "</bookmarks>";
7454 if Buffer.length kb > 0
7455 then (
7456 Buffer.add_string bb "\n";
7457 Buffer.add_buffer bb kb;
7459 Buffer.add_string bb "\n</doc>\n";
7460 end;
7464 let pan, conf =
7465 match state.mode with
7466 | Birdseye (c, pan, _, _, _) ->
7467 let beyecolumns =
7468 match conf.columns with
7469 | Cmulti ((c, _, _), _) -> Some c
7470 | Csingle _ -> None
7471 | Csplit _ -> None
7472 and columns =
7473 match c.columns with
7474 | Cmulti (c, _) -> Cmulti (c, [||])
7475 | Csingle _ -> Csingle [||]
7476 | Csplit _ -> failwith "quit from bird's eye while split"
7478 pan, { c with beyecolumns = beyecolumns; columns = columns }
7479 | _ -> x, conf
7481 let basename = Filename.basename
7482 (if emptystr state.origin then state.path else state.origin)
7484 adddoc basename pan (getanchor ())
7485 (let conf =
7486 let autoscrollstep =
7487 match state.autoscroll with
7488 | Some step -> step
7489 | None -> conf.autoscrollstep
7491 match state.mode with
7492 | Birdseye (bc, _, _, _, _) ->
7493 { conf with
7494 zoom = bc.zoom;
7495 presentation = bc.presentation;
7496 interpagespace = bc.interpagespace;
7497 maxwait = bc.maxwait;
7498 autoscrollstep = autoscrollstep }
7499 | _ -> { conf with autoscrollstep = autoscrollstep }
7500 in conf)
7501 (if conf.savebmarks then state.bookmarks else []);
7503 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7504 if basename <> path
7505 then adddoc path x anchor c bookmarks
7506 ) h;
7507 Buffer.add_string bb "</llppconfig>\n";
7508 true;
7510 if load1 f && Buffer.length bb > 0
7511 then
7513 let tmp = !confpath ^ ".tmp" in
7514 let oc = open_out_bin tmp in
7515 Buffer.output_buffer oc bb;
7516 close_out oc;
7517 Unix.rename tmp !confpath;
7518 with exn ->
7519 prerr_endline
7520 ("error while saving configuration: " ^ exntos exn)
7522 end;;
7524 let adderrmsg src msg =
7525 Buffer.add_string state.errmsgs msg;
7526 state.newerrmsgs <- true;
7527 G.postRedisplay src
7530 let adderrfmt src fmt =
7531 Format.kprintf (fun s -> adderrmsg src s) fmt;
7534 let ract cmds =
7535 let cl = splitatspace cmds in
7536 let scan s fmt f =
7537 try Scanf.sscanf s fmt f
7538 with exn ->
7539 adderrfmt "remote exec"
7540 "error processing '%S': %s\n" cmds (exntos exn)
7542 match cl with
7543 | "reload" :: [] -> reload ()
7544 | "goto" :: args :: [] ->
7545 scan args "%u %f %f"
7546 (fun pageno x y ->
7547 let cmd, _ = state.geomcmds in
7548 if emptystr cmd
7549 then gotopagexy pageno x y
7550 else
7551 let f prevf () =
7552 gotopagexy pageno x y;
7553 prevf ()
7555 state.reprf <- f state.reprf
7557 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7558 | "rect" :: args :: [] ->
7559 scan args "%u %u %f %f %f %f"
7560 (fun pageno color x0 y0 x1 y1 ->
7561 onpagerect pageno (fun w h ->
7562 let _,w1,h1,_ = getpagedim pageno in
7563 let sw = float w1 /. float w
7564 and sh = float h1 /. float h in
7565 let x0s = x0 *. sw
7566 and x1s = x1 *. sw
7567 and y0s = y0 *. sh
7568 and y1s = y1 *. sh in
7569 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7570 debugrect rect;
7571 state.rects <- (pageno, color, rect) :: state.rects;
7572 G.postRedisplay "rect";
7575 | "activatewin" :: [] -> Wsi.activatewin ()
7576 | "quit" :: [] -> raise Quit
7577 | _ ->
7578 adderrfmt "remote command"
7579 "error processing remote command: %S\n" cmds;
7582 let remote =
7583 let scratch = String.create 80 in
7584 let buf = Buffer.create 80 in
7585 fun fd ->
7586 let rec tempfr () =
7587 try Some (Unix.read fd scratch 0 80)
7588 with
7589 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7590 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7591 | exn -> raise exn
7593 match tempfr () with
7594 | None -> Some fd
7595 | Some n ->
7596 if n = 0
7597 then (
7598 Unix.close fd;
7599 if Buffer.length buf > 0
7600 then (
7601 let s = Buffer.contents buf in
7602 Buffer.clear buf;
7603 ract s;
7605 None
7607 else
7608 let rec eat ppos =
7609 let nlpos =
7611 let pos = String.index_from scratch ppos '\n' in
7612 if pos >= n then -1 else pos
7613 with Not_found -> -1
7615 if nlpos >= 0
7616 then (
7617 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7618 let s = Buffer.contents buf in
7619 Buffer.clear buf;
7620 ract s;
7621 eat (nlpos+1);
7623 else (
7624 Buffer.add_substring buf scratch ppos (n-ppos);
7625 Some fd
7627 in eat 0
7630 let remoteopen path =
7631 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7632 with exn ->
7633 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7634 None
7637 let () =
7638 let trimcachepath = ref "" in
7639 let rcmdpath = ref "" in
7640 selfexec := Sys.executable_name;
7641 Arg.parse
7642 (Arg.align
7643 [("-p", Arg.String (fun s -> state.password <- s),
7644 "<password> Set password");
7646 ("-f", Arg.String
7647 (fun s ->
7648 Config.fontpath := s;
7649 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7651 "<path> Set path to the user interface font");
7653 ("-c", Arg.String
7654 (fun s ->
7655 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7656 Config.confpath := s),
7657 "<path> Set path to the configuration file");
7659 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7660 "<path> Set path to the trim cache file");
7662 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7663 "<named-destination> Set named destination");
7665 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7666 ("-cxack", Arg.Set cxack, " Cut corners");
7668 ("-remote", Arg.String (fun s -> rcmdpath := s),
7669 "<path> Set path to the remote commands source");
7671 ("-origin", Arg.String (fun s -> state.origin <- s),
7672 "<original-path> Set original path");
7674 ("-v", Arg.Unit (fun () ->
7675 Printf.printf
7676 "%s\nconfiguration path: %s\n"
7677 (version ())
7678 Config.defconfpath
7680 exit 0), " Print version and exit");
7683 (fun s -> state.path <- s)
7684 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7686 if !wtmode
7687 then selfexec := !selfexec ^ " -wtmode";
7689 if emptystr state.path
7690 then (prerr_endline "file name missing"; exit 1);
7692 if not (Config.load ())
7693 then prerr_endline "failed to load configuration";
7695 let wsfd, winw, winh = Wsi.init (object
7696 val mutable m_hack = false
7697 method expose = if not m_hack then G.postRedisplay "expose"
7698 method visible = G.postRedisplay "visible"
7699 method display = m_hack <- false; display ()
7700 method reshape w h =
7701 m_hack <- w < state.winw && h < state.winh;
7702 reshape w h
7703 method mouse b d x y m = mouse b d x y m
7704 method motion x y = state.mpos <- (x, y); motion x y
7705 method pmotion x y = state.mpos <- (x, y); pmotion x y
7706 method key k m =
7707 let mascm = m land (
7708 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7709 ) in
7710 match state.keystate with
7711 | KSnone ->
7712 let km = k, mascm in
7713 begin
7714 match
7715 let modehash = state.uioh#modehash in
7716 try Hashtbl.find modehash km
7717 with Not_found ->
7718 try Hashtbl.find (findkeyhash conf "global") km
7719 with Not_found -> KMinsrt (k, m)
7720 with
7721 | KMinsrt (k, m) -> keyboard k m
7722 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7723 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7725 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7726 List.iter (fun (k, m) -> keyboard k m) insrt;
7727 state.keystate <- KSnone
7728 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7729 state.keystate <- KSinto (keys, insrt)
7730 | _ ->
7731 state.keystate <- KSnone
7733 method enter x y = state.mpos <- (x, y); pmotion x y
7734 method leave = state.mpos <- (-1, -1)
7735 method winstate wsl = state.winstate <- wsl; m_hack <- false
7736 method quit = raise Quit
7737 end) conf.cwinw conf.cwinh (platform = Posx) in
7739 state.wsfd <- wsfd;
7741 if not (
7742 List.exists GlMisc.check_extension
7743 [ "GL_ARB_texture_rectangle"
7744 ; "GL_EXT_texture_recangle"
7745 ; "GL_NV_texture_rectangle" ]
7747 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7749 if (
7750 let r = GlMisc.get_string `renderer in
7751 let p = "Mesa DRI Intel(" in
7752 let l = String.length p in
7753 String.length r > l && String.sub r 0 l = p
7755 then (
7756 defconf.sliceheight <- 1024;
7757 defconf.texcount <- 32;
7758 defconf.usepbo <- true;
7761 let cr, sw =
7762 match Ne.pipe () with
7763 | Ne.Exn exn ->
7764 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7765 exit 1
7766 | Ne.Res rw -> rw
7767 and sr, cw =
7768 match Ne.pipe () with
7769 | Ne.Exn exn ->
7770 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7771 exit 1
7772 | Ne.Res rw -> rw
7775 cloexec cr;
7776 cloexec sw;
7777 cloexec sr;
7778 cloexec cw;
7780 setcheckers conf.checkers;
7781 redirectstderr ();
7782 if conf.redirectstderr
7783 then
7784 at_exit (fun () ->
7785 let s = Buffer.contents state.errmsgs ^
7786 (match state.errfd with
7787 | Some fd ->
7788 let s = String.create (80*24) in
7789 let n =
7791 let r, _, _ = Unix.select [fd] [] [] 0.0 in
7792 if List.mem fd r
7793 then Unix.read fd s 0 (String.length s)
7794 else 0
7795 with _ -> 0
7797 if n = 0
7798 then ""
7799 else String.sub s 0 n
7800 | None -> ""
7803 try ignore (Unix.write state.stderr s 0 (String.length s))
7804 with exn -> print_endline (exntos exn)
7808 init (cr, cw) (
7809 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7810 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7811 !Config.fontpath, !trimcachepath,
7812 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7814 state.sr <- sr;
7815 state.sw <- sw;
7816 state.text <- "Opening " ^ (mbtoutf8 state.path);
7817 reshape winw winh;
7818 opendoc state.path state.password;
7819 state.uioh <- uioh;
7820 display ();
7821 Wsi.mapwin ();
7822 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7823 let optrfd =
7824 ref (
7825 if nonemptystr !rcmdpath
7826 then remoteopen !rcmdpath
7827 else None
7831 let rec loop deadline =
7832 let r =
7833 match state.errfd with
7834 | None -> [state.sr; state.wsfd]
7835 | Some fd -> [state.sr; state.wsfd; fd]
7837 let r =
7838 match !optrfd with
7839 | None -> r
7840 | Some fd -> fd :: r
7842 if state.redisplay
7843 then (
7844 state.redisplay <- false;
7845 display ();
7847 let timeout =
7848 let now = now () in
7849 if deadline > now
7850 then (
7851 if deadline = infinity
7852 then ~-.1.0
7853 else max 0.0 (deadline -. now)
7855 else 0.0
7857 let r, _, _ =
7858 try Unix.select r [] [] timeout
7859 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7861 begin match r with
7862 | [] ->
7863 state.ghyll None;
7864 let newdeadline =
7865 if state.ghyll == noghyll
7866 then
7867 match state.autoscroll with
7868 | Some step when step != 0 ->
7869 let y = state.y + step in
7870 let y =
7871 if y < 0
7872 then state.maxy
7873 else if y >= state.maxy then 0 else y
7875 gotoy y;
7876 if state.mode = View
7877 then state.text <- "";
7878 deadline +. 0.01
7879 | _ -> infinity
7880 else deadline +. 0.01
7882 loop newdeadline
7884 | l ->
7885 let rec checkfds = function
7886 | [] -> ()
7887 | fd :: rest when fd = state.sr ->
7888 let cmd = readcmd state.sr in
7889 act cmd;
7890 checkfds rest
7892 | fd :: rest when fd = state.wsfd ->
7893 Wsi.readresp fd;
7894 checkfds rest
7896 | fd :: rest when Some fd = !optrfd ->
7897 begin match remote fd with
7898 | None -> optrfd := remoteopen !rcmdpath;
7899 | opt -> optrfd := opt
7900 end;
7901 checkfds rest
7903 | fd :: rest ->
7904 let s = String.create 80 in
7905 let n = tempfailureretry (Unix.read fd s 0) 80 in
7906 if conf.redirectstderr
7907 then (
7908 Buffer.add_substring state.errmsgs s 0 n;
7909 state.newerrmsgs <- true;
7910 state.redisplay <- true;
7912 else (
7913 prerr_string (String.sub s 0 n);
7914 flush stderr;
7916 checkfds rest
7918 checkfds l;
7919 let newdeadline =
7920 let deadline1 =
7921 if deadline = infinity
7922 then now () +. 0.01
7923 else deadline
7925 match state.autoscroll with
7926 | Some step when step != 0 -> deadline1
7927 | _ -> if state.ghyll == noghyll then infinity else deadline1
7929 loop newdeadline
7930 end;
7933 loop infinity;
7934 with Quit ->
7935 Config.save ();