Add gotor[d] to remote interface
[llpp.git] / main.ml
blobaa0b7f347bbf7f4bfc38cf172e800688792bb165
1 open Utils;;
3 exception Quit;;
5 type under =
6 | Unone
7 | Ulinkuri of string
8 | Ulinkgoto of (int * int)
9 | Utext of facename
10 | Uunexpected of string
11 | Ulaunch of launchcommand
12 | Unamed of destname
13 | Uremote of (filename * pageno)
14 | Uremotedest of (filename * destname)
15 and facename = string
16 and launchcommand = string
17 and filename = string
18 and pageno = int
19 and destname = string;;
21 type mark =
22 | Mark_page
23 | Mark_block
24 | Mark_line
25 | Mark_word
28 type params = (angle * fitmodel * trimparams
29 * texcount * sliceheight * memsize
30 * colorspace * fontpath * trimcachepath
31 * haspbo)
32 and width = int
33 and height = int
34 and leftx = int
35 and opaque = string
36 and recttype = int
37 and pixmapsize = int
38 and angle = int
39 and trimmargins = bool
40 and interpagespace = int
41 and texcount = int
42 and sliceheight = int
43 and gen = int
44 and top = float
45 and dtop = float
46 and fontpath = string
47 and trimcachepath = string
48 and memsize = int
49 and aalevel = int
50 and irect = (int * int * int * int)
51 and trimparams = (trimmargins * irect)
52 and colorspace = | Rgb | Bgr | Gray
53 and fitmodel = | FitWidth | FitProportional | FitPage
54 and haspbo = bool
55 and uri = string
56 and caption = string
59 type x = int
60 and y = int
61 and tilex = int
62 and tiley = int
63 and tileparams = (x * y * width * height * tilex * tiley)
66 type link =
67 | Lnotfound
68 | Lfound of int
69 and linkdir =
70 | LDfirst
71 | LDlast
72 | LDfirstvisible of (int * int * int)
73 | LDleft of int
74 | LDright of int
75 | LDdown of int
76 | LDup of int
79 type pagewithlinks =
80 | Pwlnotfound
81 | Pwl of int
84 type keymap =
85 | KMinsrt of key
86 | KMinsrl of key list
87 | KMmulti of key list * key list
88 and key = int * int
89 and keyhash = (key, keymap) Hashtbl.t
90 and keystate =
91 | KSnone
92 | KSinto of (key list * key list)
95 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
96 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
98 type pipe = (Unix.file_descr * Unix.file_descr);;
100 external init : pipe -> params -> unit = "ml_init";;
101 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
102 external copysel : Unix.file_descr -> opaque -> bool -> unit = "ml_copysel";;
103 external getpdimrect : int -> float array = "ml_getpdimrect";;
104 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
105 external markunder : string -> int -> int -> mark -> bool = "ml_markunder";;
106 external clearmark : string -> unit = "ml_clearmark";;
107 external zoomforh : int -> int -> int -> int -> float = "ml_zoom_for_height";;
108 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
109 external measurestr : int -> string -> float = "ml_measure_string";;
110 external postprocess :
111 opaque -> int -> int -> int -> (int * string * int) -> int
112 = "ml_postprocess";;
113 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
114 external platform : unit -> platform = "ml_platform";;
115 external setaalevel : int -> unit = "ml_setaalevel";;
116 external realloctexts : int -> bool = "ml_realloctexts";;
117 external findlink : opaque -> linkdir -> link = "ml_findlink";;
118 external getlink : opaque -> int -> under = "ml_getlink";;
119 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
120 external getlinkcount : opaque -> int = "ml_getlinkcount";;
121 external findpwl : int -> int -> pagewithlinks = "ml_find_page_with_links"
122 external popen : string -> (Unix.file_descr * int) list -> unit = "ml_popen";;
123 external getpbo : width -> height -> colorspace -> string = "ml_getpbo";;
124 external freepbo : string -> unit = "ml_freepbo";;
125 external unmappbo : string -> unit = "ml_unmappbo";;
126 external pbousable : unit -> bool = "ml_pbo_usable";;
127 external unproject : opaque -> int -> int -> (int * int) option
128 = "ml_unproject";;
129 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
130 external rectofblock : opaque -> int -> int -> float array option
131 = "ml_rectofblock";;
132 external fz_version : unit -> string = "ml_fz_version";;
133 external begintiles : unit -> unit = "ml_begintiles";;
134 external endtiles : unit -> unit = "ml_endtiles";;
136 let platform_to_string = function
137 | Punknown -> "unknown"
138 | Plinux -> "Linux"
139 | Posx -> "OSX"
140 | Psun -> "Sun"
141 | Pfreebsd -> "FreeBSD"
142 | Pdragonflybsd -> "DragonflyBSD"
143 | Popenbsd -> "OpenBSD"
144 | Pnetbsd -> "NetBSD"
145 | Pcygwin -> "Cygwin"
148 let platform = platform ();;
150 let now = Unix.gettimeofday;;
152 let selfexec = ref "";;
154 let popen cmd fda =
155 if platform = Pcygwin
156 then (
157 let sh = "/bin/sh" in
158 let args = [|sh; "-c"; cmd|] in
159 let rec std si so se = function
160 | [] -> si, so, se
161 | (fd, 0) :: rest -> std fd so se rest
162 | (fd, -1) :: rest ->
163 Unix.set_close_on_exec fd;
164 std si so se rest
165 | (_, n) :: _ ->
166 failwith ("unexpected fdn in cygwin popen " ^ string_of_int n)
168 let si, so, se = std Unix.stdin Unix.stdout Unix.stderr fda in
169 ignore (Unix.create_process sh args si so se)
171 else popen cmd fda;
174 type mpos = int * int
175 and mstate =
176 | Msel of (mpos * mpos)
177 | Mpan of mpos
178 | Mscrolly | Mscrollx
179 | Mzoom of (int * int)
180 | Mzoomrect of (mpos * mpos)
181 | Mnone
184 type textentry = string * string * onhist option * onkey * ondone * cancelonempty
185 and onkey = string -> int -> te
186 and ondone = string -> unit
187 and histcancel = unit -> unit
188 and onhist = ((histcmd -> string) * histcancel)
189 and histcmd = HCnext | HCprev | HCfirst | HClast
190 and cancelonempty = bool
191 and te =
192 | TEstop
193 | TEdone of string
194 | TEcont of string
195 | TEswitch of textentry
198 type 'a circbuf =
199 { store : 'a array
200 ; mutable rc : int
201 ; mutable wc : int
202 ; mutable len : int
206 let bound v minv maxv =
207 max minv (min maxv v);
210 let cbnew n v =
211 { store = Array.create n v
212 ; rc = 0
213 ; wc = 0
214 ; len = 0
218 let cbcap b = Array.length b.store;;
220 let cbput b v =
221 let cap = cbcap b in
222 b.store.(b.wc) <- v;
223 b.wc <- (b.wc + 1) mod cap;
224 b.rc <- b.wc;
225 b.len <- min (b.len + 1) cap;
228 let cbempty b = b.len = 0;;
230 let cbgetg b circular dir =
231 if cbempty b
232 then b.store.(0)
233 else
234 let rc = b.rc + dir in
235 let rc =
236 if circular
237 then (
238 if rc = -1
239 then b.len-1
240 else (
241 if rc >= b.len
242 then 0
243 else rc
246 else bound rc 0 (b.len-1)
248 b.rc <- rc;
249 b.store.(rc);
252 let cbget b = cbgetg b false;;
253 let cbgetc b = cbgetg b true;;
255 let drawstring size x y s =
256 Gl.enable `blend;
257 Gl.enable `texture_2d;
258 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
259 ignore (drawstr size x y s);
260 Gl.disable `blend;
261 Gl.disable `texture_2d;
264 let drawstring1 size x y s =
265 drawstr size x y s;
268 let drawstring2 size x y fmt =
269 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
272 type page =
273 { pageno : int
274 ; pagedimno : int
275 ; pagew : int
276 ; pageh : int
277 ; pagex : int
278 ; pagey : int
279 ; pagevw : int
280 ; pagevh : int
281 ; pagedispx : int
282 ; pagedispy : int
283 ; pagecol : int
287 let debugl l =
288 dolog "l %d dim=%d {" l.pageno l.pagedimno;
289 dolog " WxH %dx%d" l.pagew l.pageh;
290 dolog " vWxH %dx%d" l.pagevw l.pagevh;
291 dolog " pagex,y %d,%d" l.pagex l.pagey;
292 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
293 dolog " column %d" l.pagecol;
294 dolog "}";
297 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
298 dolog "rect {";
299 dolog " x0,y0=(% f, % f)" x0 y0;
300 dolog " x1,y1=(% f, % f)" x1 y1;
301 dolog " x2,y2=(% f, % f)" x2 y2;
302 dolog " x3,y3=(% f, % f)" x3 y3;
303 dolog "}";
306 type multicolumns = multicol * pagegeom
307 and singlecolumn = pagegeom
308 and splitcolumns = columncount * pagegeom
309 and pagegeom = ((pdimno * x * y * (pageno * width * height * leftx)) array)
310 and multicol = columncount * covercount * covercount
311 and pdimno = int
312 and columncount = int
313 and covercount = int;;
315 type scrollb = int;;
316 let scrollbvv = 1;;
317 let scrollbhv = 2;;
319 type conf =
320 { mutable scrollbw : int
321 ; mutable scrollh : int
322 ; mutable scrollb : scrollb
323 ; mutable icase : bool
324 ; mutable preload : bool
325 ; mutable pagebias : int
326 ; mutable verbose : bool
327 ; mutable debug : bool
328 ; mutable scrollstep : int
329 ; mutable hscrollstep : int
330 ; mutable maxhfit : bool
331 ; mutable crophack : bool
332 ; mutable autoscrollstep : int
333 ; mutable maxwait : float option
334 ; mutable hlinks : bool
335 ; mutable underinfo : bool
336 ; mutable interpagespace : interpagespace
337 ; mutable zoom : float
338 ; mutable presentation : bool
339 ; mutable angle : angle
340 ; mutable cwinw : int
341 ; mutable cwinh : int
342 ; mutable savebmarks : bool
343 ; mutable fitmodel : fitmodel
344 ; mutable trimmargins : trimmargins
345 ; mutable trimfuzz : irect
346 ; mutable memlimit : memsize
347 ; mutable texcount : texcount
348 ; mutable sliceheight : sliceheight
349 ; mutable thumbw : width
350 ; mutable jumpback : bool
351 ; mutable bgcolor : (float * float * float)
352 ; mutable bedefault : bool
353 ; mutable tilew : int
354 ; mutable tileh : int
355 ; mutable mustoresize : memsize
356 ; mutable checkers : bool
357 ; mutable aalevel : int
358 ; mutable urilauncher : string
359 ; mutable pathlauncher : string
360 ; mutable colorspace : colorspace
361 ; mutable invert : bool
362 ; mutable colorscale : float
363 ; mutable redirectstderr : bool
364 ; mutable ghyllscroll : (int * int * int) option
365 ; mutable columns : columns
366 ; mutable beyecolumns : columncount option
367 ; mutable selcmd : string
368 ; mutable paxcmd : string
369 ; mutable updatecurs : bool
370 ; mutable keyhashes : (string * keyhash) list
371 ; mutable hfsize : int
372 ; mutable pgscale : float
373 ; mutable usepbo : bool
374 ; mutable wheelbypage : bool
375 ; mutable stcmd : string
376 ; mutable riani : bool
377 ; mutable pax : (float * int * int) ref option
378 ; mutable paxmark : mark
380 and columns =
381 | Csingle of singlecolumn
382 | Cmulti of multicolumns
383 | Csplit of splitcolumns
386 type anchor = pageno * top * dtop;;
388 type outlinekind =
389 | Onone
390 | Oanchor of anchor
391 | Ouri of uri
392 | Olaunch of launchcommand
393 | Oremote of (filename * pageno)
394 | Oremotedest of (filename * destname)
395 and outline = (caption * outlinelevel * outlinekind)
396 and outlinelevel = int
399 type rect = float * float * float * float * float * float * float * float;;
401 type tile = opaque * pixmapsize * elapsed
402 and elapsed = float;;
403 type pagemapkey = pageno * gen;;
404 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
405 and row = int
406 and col = int;;
408 let emptyanchor = (0, 0.0, 0.0);;
410 type infochange = | Memused | Docinfo | Pdim;;
412 class type uioh = object
413 method display : unit
414 method key : int -> int -> uioh
415 method button : int -> bool -> int -> int -> int -> uioh
416 method motion : int -> int -> uioh
417 method pmotion : int -> int -> uioh
418 method infochanged : infochange -> unit
419 method scrollpw : (int * float * float)
420 method scrollph : (int * float * float)
421 method modehash : keyhash
422 method eformsgs : bool
423 end;;
425 type mode =
426 | Birdseye of (conf * leftx * pageno * pageno * anchor)
427 | Textentry of (textentry * onleave)
428 | View
429 | LinkNav of linktarget
430 and onleave = leavetextentrystatus -> unit
431 and leavetextentrystatus = | Cancel | Confirm
432 and helpitem = string * int * action
433 and action =
434 | Noaction
435 | Action of (uioh -> uioh)
436 and linktarget =
437 | Ltexact of (pageno * int)
438 | Ltgendir of int
441 let isbirdseye = function Birdseye _ -> true | _ -> false;;
442 let istextentry = function Textentry _ -> true | _ -> false;;
444 type currently =
445 | Idle
446 | Loading of (page * gen)
447 | Tiling of (
448 page * opaque * colorspace * angle * gen * col * row * width * height
450 | Outlining of outline list
453 let emptykeyhash = Hashtbl.create 0;;
454 let nouioh : uioh = object (self)
455 method display = ()
456 method key _ _ = self
457 method button _ _ _ _ _ = self
458 method motion _ _ = self
459 method pmotion _ _ = self
460 method infochanged _ = ()
461 method scrollpw = (0, nan, nan)
462 method scrollph = (0, nan, nan)
463 method modehash = emptykeyhash
464 method eformsgs = false
465 end;;
467 type state =
468 { mutable sr : Unix.file_descr
469 ; mutable sw : Unix.file_descr
470 ; mutable wsfd : Unix.file_descr
471 ; mutable errfd : Unix.file_descr option
472 ; mutable stderr : Unix.file_descr
473 ; mutable errmsgs : Buffer.t
474 ; mutable newerrmsgs : bool
475 ; mutable w : int
476 ; mutable x : int
477 ; mutable y : int
478 ; mutable anchor : anchor
479 ; mutable ranchors : (string * string * anchor * string) list
480 ; mutable maxy : int
481 ; mutable layout : page list
482 ; pagemap : (pagemapkey, opaque) Hashtbl.t
483 ; tilemap : (tilemapkey, tile) Hashtbl.t
484 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
485 ; mutable pdims : (pageno * width * height * leftx) list
486 ; mutable pagecount : int
487 ; mutable currently : currently
488 ; mutable mstate : mstate
489 ; mutable searchpattern : string
490 ; mutable rects : (pageno * recttype * rect) list
491 ; mutable rects1 : (pageno * recttype * rect) list
492 ; mutable text : string
493 ; mutable winstate : Wsi.winstate list
494 ; mutable mode : mode
495 ; mutable uioh : uioh
496 ; mutable outlines : outline array
497 ; mutable bookmarks : outline list
498 ; mutable path : string
499 ; mutable password : string
500 ; mutable nameddest : string
501 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
502 ; mutable memused : memsize
503 ; mutable gen : gen
504 ; mutable throttle : (page list * int * float) option
505 ; mutable autoscroll : int option
506 ; mutable ghyll : (int option -> unit)
507 ; mutable help : helpitem array
508 ; mutable docinfo : (int * string) list
509 ; mutable texid : GlTex.texture_id option
510 ; hists : hists
511 ; mutable prevzoom : (float * int)
512 ; mutable progress : float
513 ; mutable redisplay : bool
514 ; mutable mpos : mpos
515 ; mutable keystate : keystate
516 ; mutable glinks : bool
517 ; mutable prevcolumns : (columns * float) option
518 ; mutable winw : int
519 ; mutable winh : int
520 ; mutable reprf : (unit -> unit)
521 ; mutable origin : string
522 ; mutable roam : (unit -> unit)
523 ; mutable bzoom : bool
524 ; mutable traw : [`float] Raw.t
525 ; mutable vraw : [`float] Raw.t
527 and hists =
528 { pat : string circbuf
529 ; pag : string circbuf
530 ; nav : anchor circbuf
531 ; sel : string circbuf
535 let defconf =
536 { scrollbw = 7
537 ; scrollh = 12
538 ; scrollb = scrollbhv lor scrollbvv
539 ; icase = true
540 ; preload = true
541 ; pagebias = 0
542 ; verbose = false
543 ; debug = false
544 ; scrollstep = 24
545 ; hscrollstep = 24
546 ; maxhfit = true
547 ; crophack = false
548 ; autoscrollstep = 2
549 ; maxwait = None
550 ; hlinks = false
551 ; underinfo = false
552 ; interpagespace = 2
553 ; zoom = 1.0
554 ; presentation = false
555 ; angle = 0
556 ; cwinw = 900
557 ; cwinh = 900
558 ; savebmarks = true
559 ; fitmodel = FitProportional
560 ; trimmargins = false
561 ; trimfuzz = (0,0,0,0)
562 ; memlimit = 32 lsl 20
563 ; texcount = 256
564 ; sliceheight = 24
565 ; thumbw = 76
566 ; jumpback = true
567 ; bgcolor = (0.5, 0.5, 0.5)
568 ; bedefault = false
569 ; tilew = 2048
570 ; tileh = 2048
571 ; mustoresize = 256 lsl 20
572 ; checkers = true
573 ; aalevel = 8
574 ; urilauncher =
575 (match platform with
576 | Plinux | Pfreebsd | Pdragonflybsd
577 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
578 | Posx -> "open \"%s\""
579 | Pcygwin -> "cygstart \"%s\""
580 | Punknown -> "echo %s")
581 ; pathlauncher = "lp \"%s\""
582 ; selcmd =
583 (match platform with
584 | Plinux | Pfreebsd | Pdragonflybsd
585 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
586 | Posx -> "pbcopy"
587 | Pcygwin -> "wsel"
588 | Punknown -> "cat")
589 ; paxcmd = "cat"
590 ; colorspace = Rgb
591 ; invert = false
592 ; colorscale = 1.0
593 ; redirectstderr = false
594 ; ghyllscroll = None
595 ; columns = Csingle [||]
596 ; beyecolumns = None
597 ; updatecurs = false
598 ; hfsize = 12
599 ; pgscale = 1.0
600 ; usepbo = false
601 ; wheelbypage = false
602 ; stcmd = "echo SyncTex"
603 ; riani = false
604 ; pax = None
605 ; paxmark = Mark_word
606 ; keyhashes =
607 let mk n = (n, Hashtbl.create 1) in
608 [ mk "global"
609 ; mk "info"
610 ; mk "help"
611 ; mk "outline"
612 ; mk "listview"
613 ; mk "birdseye"
614 ; mk "textentry"
615 ; mk "links"
616 ; mk "view"
621 let wtmode = ref false;;
622 let cxack = ref false;;
624 let findkeyhash c name =
625 try List.assoc name c.keyhashes
626 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
629 let conf = { defconf with angle = defconf.angle };;
631 let pgscale h = truncate (float h *. conf.pgscale);;
633 type fontstate =
634 { mutable fontsize : int
635 ; mutable wwidth : float
636 ; mutable maxrows : int
640 let fstate =
641 { fontsize = 14
642 ; wwidth = nan
643 ; maxrows = -1
647 let geturl s =
648 let colonpos = try String.index s ':' with Not_found -> -1 in
649 let len = String.length s in
650 if colonpos >= 0 && colonpos + 3 < len
651 then (
652 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
653 then
654 let schemestartpos =
655 try String.rindex_from s colonpos ' '
656 with Not_found -> -1
658 let scheme =
659 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
661 match scheme with
662 | "http" | "ftp" | "mailto" ->
663 let epos =
664 try String.index_from s colonpos ' '
665 with Not_found -> len
667 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
668 | _ -> ""
669 else ""
671 else ""
674 let gotouri uri =
675 if emptystr conf.urilauncher
676 then print_endline uri
677 else (
678 let url = geturl uri in
679 if emptystr url
680 then Printf.eprintf "obtained empty url from uri %S" uri
681 else
682 let re = Str.regexp "%s" in
683 let command = Str.global_replace re url conf.urilauncher in
684 try popen command []
685 with exn ->
686 Printf.eprintf
687 "failed to execute `%s': %s\n" command (exntos exn);
688 flush stderr;
692 let version () =
693 Printf.sprintf "llpp version %s, fitz %s, ocaml %s (%s/%dbit)"
694 Help.version (fz_version ()) Sys.ocaml_version
695 (platform_to_string platform) Sys.word_size
698 let makehelp () =
699 let strings = version () :: "" :: Help.keys in
700 Array.of_list (
701 List.map (fun s ->
702 let url = geturl s in
703 if nonemptystr url
704 then (s, 0, Action (fun u -> gotouri url; u))
705 else (s, 0, Noaction)
706 ) strings);
709 let noghyll _ = ();;
710 let firstgeomcmds = "", [];;
711 let noreprf () = ();;
713 let state =
714 { sr = Unix.stdin
715 ; sw = Unix.stdin
716 ; wsfd = Unix.stdin
717 ; errfd = None
718 ; stderr = Unix.stderr
719 ; errmsgs = Buffer.create 0
720 ; newerrmsgs = false
721 ; x = 0
722 ; y = 0
723 ; w = 0
724 ; anchor = emptyanchor
725 ; ranchors = []
726 ; layout = []
727 ; maxy = max_int
728 ; tilelru = Queue.create ()
729 ; pagemap = Hashtbl.create 10
730 ; tilemap = Hashtbl.create 10
731 ; pdims = []
732 ; pagecount = 0
733 ; currently = Idle
734 ; mstate = Mnone
735 ; rects = []
736 ; rects1 = []
737 ; text = ""
738 ; mode = View
739 ; winstate = []
740 ; searchpattern = ""
741 ; outlines = [||]
742 ; bookmarks = []
743 ; path = ""
744 ; password = ""
745 ; nameddest = ""
746 ; geomcmds = firstgeomcmds
747 ; hists =
748 { nav = cbnew 10 emptyanchor
749 ; pat = cbnew 10 ""
750 ; pag = cbnew 10 ""
751 ; sel = cbnew 10 ""
753 ; memused = 0
754 ; gen = 0
755 ; throttle = None
756 ; autoscroll = None
757 ; ghyll = noghyll
758 ; help = makehelp ()
759 ; docinfo = []
760 ; texid = None
761 ; prevzoom = (1.0, 0)
762 ; progress = -1.0
763 ; uioh = nouioh
764 ; redisplay = true
765 ; mpos = (-1, -1)
766 ; keystate = KSnone
767 ; glinks = false
768 ; prevcolumns = None
769 ; winw = -1
770 ; winh = -1
771 ; reprf = noreprf
772 ; origin = ""
773 ; roam = (fun () -> ())
774 ; bzoom = false
775 ; traw = Raw.create_static `float 8
776 ; vraw = Raw.create_static `float 8
780 let hscrollh () =
781 if (conf.scrollb land scrollbhv = 0)
782 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
783 then 0
784 else conf.scrollbw
787 let vscrollw () =
788 if (conf.scrollb land scrollbvv = 0)
789 then 0
790 else conf.scrollbw
793 let wadjsb w = w - vscrollw ();;
795 let setfontsize n =
796 fstate.fontsize <- n;
797 fstate.wwidth <- measurestr fstate.fontsize "w";
798 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
801 let vlog fmt =
802 if conf.verbose
803 then
804 Printf.kprintf prerr_endline fmt
805 else
806 Printf.kprintf ignore fmt
809 let launchpath () =
810 if emptystr conf.pathlauncher
811 then print_endline state.path
812 else (
813 let re = Str.regexp "%s" in
814 let command = Str.global_replace re state.path conf.pathlauncher in
815 try popen command []
816 with exn ->
817 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
818 flush stderr;
822 module Ne = struct
823 type 'a t = | Res of 'a | Exn of exn;;
825 let pipe () =
826 try Res (Unix.pipe ())
827 with exn -> Exn exn
830 let clo fd f =
831 try tempfailureretry Unix.close fd
832 with exn -> f (exntos exn)
835 let dup fd =
836 try Res (tempfailureretry Unix.dup fd)
837 with exn -> Exn exn
840 let dup2 fd1 fd2 =
841 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
842 with exn -> Exn exn
844 end;;
846 let redirectstderr () =
847 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
848 if conf.redirectstderr
849 then
850 match Ne.pipe () with
851 | Ne.Exn exn ->
852 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
854 | Ne.Res (r, w) ->
855 begin match Ne.dup Unix.stderr with
856 | Ne.Exn exn ->
857 dolog "failed to dup stderr: %s" (exntos exn);
858 Ne.clo r (clofail "pipe/r");
859 Ne.clo w (clofail "pipe/w");
861 | Ne.Res dupstderr ->
862 begin match Ne.dup2 w Unix.stderr with
863 | Ne.Exn exn ->
864 dolog "failed to dup2 to stderr: %s" (exntos exn);
865 Ne.clo dupstderr (clofail "stderr duplicate");
866 Ne.clo r (clofail "redir pipe/r");
867 Ne.clo w (clofail "redir pipe/w");
869 | Ne.Res () ->
870 state.stderr <- dupstderr;
871 state.errfd <- Some r;
872 end;
874 else (
875 state.newerrmsgs <- false;
876 begin match state.errfd with
877 | Some fd ->
878 begin match Ne.dup2 state.stderr Unix.stderr with
879 | Ne.Exn exn ->
880 dolog "failed to dup2 original stderr: %s" (exntos exn)
881 | Ne.Res () ->
882 Ne.clo fd (clofail "dup of stderr");
883 state.errfd <- None;
884 end;
885 | None -> ()
886 end;
887 prerr_string (Buffer.contents state.errmsgs);
888 flush stderr;
889 Buffer.clear state.errmsgs;
893 module G =
894 struct
895 let postRedisplay who =
896 if conf.verbose
897 then prerr_endline ("redisplay for " ^ who);
898 state.redisplay <- true;
900 end;;
902 let getopaque pageno =
903 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
904 with Not_found -> None
907 let putopaque pageno opaque =
908 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
911 let pagetranslatepoint l x y =
912 let dy = y - l.pagedispy in
913 let y = dy + l.pagey in
914 let dx = x - l.pagedispx in
915 let x = dx + l.pagex in
916 (x, y);
919 let onppundermouse g x y d =
920 let rec f = function
921 | l :: rest ->
922 begin match getopaque l.pageno with
923 | Some opaque ->
924 let x0 = l.pagedispx in
925 let x1 = x0 + l.pagevw in
926 let y0 = l.pagedispy in
927 let y1 = y0 + l.pagevh in
928 if y >= y0 && y <= y1 && x >= x0 && x <= x1
929 then
930 let px, py = pagetranslatepoint l x y in
931 match g opaque l px py with
932 | Some res -> res
933 | None -> f rest
934 else f rest
935 | _ ->
936 f rest
938 | [] -> d
940 f state.layout
943 let getunder x y =
944 let g opaque l px py =
945 if state.bzoom
946 then (
947 match rectofblock opaque px py with
948 | Some a ->
949 let rect = (a.(0),a.(2),a.(1),a.(2),a.(1),a.(3),a.(0),a.(3)) in
950 state.rects <- [l.pageno, l.pageno mod 3, rect];
951 G.postRedisplay "getunder";
952 | None -> ()
954 match whatsunder opaque px py with
955 | Unone -> None
956 | under -> Some under
958 onppundermouse g x y Unone
961 let unproject x y =
962 let g opaque l x y =
963 match unproject opaque x y with
964 | Some (x, y) -> Some (Some (l.pageno, x, y))
965 | None -> None
967 onppundermouse g x y None;
970 let showtext c s =
971 state.text <- Printf.sprintf "%c%s" c s;
972 G.postRedisplay "showtext";
975 let paxunder x y =
976 let g opaque l px py =
977 if markunder opaque px py conf.paxmark
978 then (
979 Some (fun () ->
980 match getopaque l.pageno with
981 | None -> ()
982 | Some opaque ->
983 match Ne.pipe () with
984 | Ne.Exn exn ->
985 showtext '!'
986 (Printf.sprintf
987 "can not create mark pipe: %s"
988 (exntos exn));
989 | Ne.Res (r, w) ->
990 let doclose what fd =
991 Ne.clo fd (fun msg ->
992 dolog "%s close failed: %s" what msg)
995 popen conf.paxcmd [r, 0; w, -1];
996 copysel w opaque false;
997 doclose "pipe/r" r;
998 G.postRedisplay "paxunder";
999 with exn ->
1000 dolog "can not execute %S: %s"
1001 conf.paxcmd (exntos exn);
1002 doclose "pipe/r" r;
1003 doclose "pipe/w" w;
1006 else None
1008 G.postRedisplay "paxunder";
1009 if conf.paxmark = Mark_page
1010 then
1011 List.iter (fun l ->
1012 match getopaque l.pageno with
1013 | None -> ()
1014 | Some opaque -> clearmark opaque) state.layout;
1015 state.roam <-
1016 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
1019 let selstring s =
1020 match Ne.pipe () with
1021 | Ne.Exn exn ->
1022 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
1023 | Ne.Res (r, w) ->
1024 let popened =
1025 try popen conf.selcmd [r, 0; w, -1]; true
1026 with exn ->
1027 showtext '!'
1028 (Printf.sprintf "failed to execute %s: %s"
1029 conf.selcmd (exntos exn));
1030 false
1032 let clo cap fd =
1033 Ne.clo fd (fun msg ->
1034 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
1037 if popened
1038 then (
1040 let l = String.length s in
1041 let n = tempfailureretry (Unix.write w s 0) l in
1042 if n != l
1043 then
1044 showtext '!'
1045 (Printf.sprintf
1046 "failed to write %d characters to sel pipe, wrote %d"
1049 with exn ->
1050 showtext '!'
1051 (Printf.sprintf "failed to write to sel pipe: %s"
1052 (exntos exn)
1055 else dolog "%s" s;
1056 clo "pipe/r" r;
1057 clo "pipe/w" w;
1060 let undertext = function
1061 | Unone -> "none"
1062 | Ulinkuri s -> s
1063 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
1064 | Utext s -> "font: " ^ s
1065 | Uunexpected s -> "unexpected: " ^ s
1066 | Ulaunch s -> "launch: " ^ s
1067 | Unamed s -> "named: " ^ s
1068 | Uremote (filename, pageno) ->
1069 Printf.sprintf "%s: page %d" filename (pageno+1)
1070 | Uremotedest (filename, destname) ->
1071 Printf.sprintf "%s: destination %S" filename destname
1074 let updateunder x y =
1075 match getunder x y with
1076 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
1077 | Ulinkuri uri ->
1078 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1079 Wsi.setcursor Wsi.CURSOR_INFO
1080 | Ulinkgoto (pageno, _) ->
1081 if conf.underinfo
1082 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
1083 Wsi.setcursor Wsi.CURSOR_INFO
1084 | Utext s ->
1085 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1086 Wsi.setcursor Wsi.CURSOR_TEXT
1087 | Uunexpected s ->
1088 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
1089 Wsi.setcursor Wsi.CURSOR_INHERIT
1090 | Ulaunch s ->
1091 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
1092 Wsi.setcursor Wsi.CURSOR_INHERIT
1093 | Unamed s ->
1094 if conf.underinfo then showtext 'n' ("amed: " ^ s);
1095 Wsi.setcursor Wsi.CURSOR_INHERIT
1096 | Uremote (filename, pageno) ->
1097 if conf.underinfo then showtext 'r'
1098 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
1099 Wsi.setcursor Wsi.CURSOR_INFO
1100 | Uremotedest (filename, destname) ->
1101 if conf.underinfo then showtext 'r'
1102 (Printf.sprintf "emote destination: %s (%S)" filename destname);
1103 Wsi.setcursor Wsi.CURSOR_INFO
1106 let showlinktype under =
1107 if conf.underinfo
1108 then
1109 match under with
1110 | Unone -> ()
1111 | under ->
1112 let s = undertext under in
1113 showtext ' ' s
1116 let addchar s c =
1117 let b = Buffer.create (String.length s + 1) in
1118 Buffer.add_string b s;
1119 Buffer.add_char b c;
1120 Buffer.contents b;
1123 module type TextEnumType =
1125 type t
1126 val name : string
1127 val names : string array
1128 end;;
1130 module TextEnumMake (Ten : TextEnumType) =
1131 struct
1132 let names = Ten.names;;
1133 let to_int (t : Ten.t) = Obj.magic t;;
1134 let to_string t = names.(to_int t);;
1135 let of_int n : Ten.t = Obj.magic n;;
1136 let of_string s =
1137 let rec find i =
1138 if i = Array.length names
1139 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
1140 else (
1141 if Ten.names.(i) = s
1142 then of_int i
1143 else find (i+1)
1145 in find 0;;
1146 end;;
1148 module CSTE = TextEnumMake (struct
1149 type t = colorspace;;
1150 let name = "colorspace";;
1151 let names = [|"rgb"; "bgr"; "gray"|];;
1152 end);;
1154 module MTE = TextEnumMake (struct
1155 type t = mark;;
1156 let name = "mark";;
1157 let names = [|"page"; "block"; "line"; "word"|];;
1158 end);;
1160 module FMTE = TextEnumMake (struct
1161 type t= fitmodel;;
1162 let name = "fitmodel";;
1163 let names = [|"width"; "proportional"; "page"|];;
1164 end);;
1166 let intentry_with_suffix text key =
1167 let c =
1168 if key >= 32 && key < 127
1169 then Char.chr key
1170 else '\000'
1172 match Char.lowercase c with
1173 | '0' .. '9' ->
1174 let text = addchar text c in
1175 TEcont text
1177 | 'k' | 'm' | 'g' ->
1178 let text = addchar text c in
1179 TEcont text
1181 | _ ->
1182 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1183 TEcont text
1186 let multicolumns_to_string (n, a, b) =
1187 if a = 0 && b = 0
1188 then Printf.sprintf "%d" n
1189 else Printf.sprintf "%d,%d,%d" n a b;
1192 let multicolumns_of_string s =
1194 (int_of_string s, 0, 0)
1195 with _ ->
1196 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1197 if a > 1 || b > 1
1198 then failwith "subtly broken"; (n, a, b)
1202 let readcmd fd =
1203 let s = "xxxx" in
1204 let n = tempfailureretry (Unix.read fd s 0) 4 in
1205 if n != 4 then error "incomplete read(len) = %d" n;
1206 let len = 0
1207 lor (Char.code s.[0] lsl 24)
1208 lor (Char.code s.[1] lsl 16)
1209 lor (Char.code s.[2] lsl 8)
1210 lor (Char.code s.[3] lsl 0)
1212 let s = String.create len in
1213 let n = tempfailureretry (Unix.read fd s 0) len in
1214 if n != len then error "incomplete read(data) %d vs %d" n len;
1218 let btod b = if b then 1 else 0;;
1220 let wcmd fmt =
1221 let b = Buffer.create 16 in
1222 Buffer.add_string b "llll";
1223 Printf.kbprintf
1224 (fun b ->
1225 let s = Buffer.contents b in
1226 let n = String.length s in
1227 let len = n - 4 in
1228 (* dolog "wcmd %S" (String.sub s 4 len); *)
1229 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1230 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1231 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1232 s.[3] <- Char.chr (len land 0xff);
1233 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1234 if n' != n then error "write failed %d vs %d" n' n;
1235 ) b fmt;
1238 let calcips h =
1239 let d = state.winh - h in
1240 max conf.interpagespace ((d + 1) / 2)
1243 let rowyh (c, coverA, coverB) b n =
1244 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1245 then
1246 let _, _, vy, (_, _, h, _) = b.(n) in
1247 (vy, h)
1248 else
1249 let n' = n - coverA in
1250 let d = n' mod c in
1251 let s = n - d in
1252 let e = min state.pagecount (s + c) in
1253 let rec find m miny maxh = if m = e then miny, maxh else
1254 let _, _, y, (_, _, h, _) = b.(m) in
1255 let miny = min miny y in
1256 let maxh = max maxh h in
1257 find (m+1) miny maxh
1258 in find s max_int 0
1261 let calcheight () =
1262 match conf.columns with
1263 | Cmulti ((_, _, _) as cl, b) ->
1264 if Array.length b > 0
1265 then
1266 let y, h = rowyh cl b (Array.length b - 1) in
1267 y + h + (if conf.presentation then calcips h else 0)
1268 else 0
1269 | Csingle b ->
1270 if Array.length b > 0
1271 then
1272 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1273 y + h + (if conf.presentation then calcips h else 0)
1274 else 0
1275 | Csplit (_, b) ->
1276 if Array.length b > 0
1277 then
1278 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1279 y + h
1280 else 0
1283 let getpageywh pageno =
1284 let pageno = bound pageno 0 (state.pagecount-1) in
1285 match conf.columns with
1286 | Csingle b ->
1287 if Array.length b = 0
1288 then 0, 0, 0
1289 else
1290 let (_, _, y, (_, w, h, _)) = b.(pageno) in
1291 let y =
1292 if conf.presentation
1293 then y - calcips h
1294 else y
1296 y, w, h
1297 | Cmulti (cl, b) ->
1298 if Array.length b = 0
1299 then 0, 0, 0
1300 else
1301 let y, h = rowyh cl b pageno in
1302 let (_, _, _, (_, w, _, _)) = b.(pageno) in
1303 let y =
1304 if conf.presentation
1305 then y - calcips h
1306 else y
1308 y, w, h
1309 | Csplit (c, b) ->
1310 if Array.length b = 0
1311 then 0, 0, 0
1312 else
1313 let n = pageno*c in
1314 let (_, _, y, (_, w, h, _)) = b.(n) in
1315 y, w / c, h
1318 let getpageyh pageno =
1319 let y,_,h = getpageywh pageno in
1320 y, h;
1323 let getpagedim pageno =
1324 let rec f ppdim l =
1325 match l with
1326 | (n, _, _, _) as pdim :: rest ->
1327 if n >= pageno
1328 then (if n = pageno then pdim else ppdim)
1329 else f pdim rest
1331 | [] -> ppdim
1333 f (-1, -1, -1, -1) state.pdims
1336 let getpagey pageno = fst (getpageyh pageno);;
1338 let nogeomcmds cmds =
1339 match cmds with
1340 | s, [] -> emptystr s
1341 | _ -> false
1344 let page_of_y y =
1345 let ((c, coverA, coverB) as cl), b =
1346 match conf.columns with
1347 | Csingle b -> (1, 0, 0), b
1348 | Cmulti (c, b) -> c, b
1349 | Csplit (_, b) -> (1, 0, 0), b
1351 if Array.length b = 0
1352 then -1
1353 else
1354 let rec bsearch nmin nmax =
1355 if nmin > nmax
1356 then bound nmin 0 (state.pagecount-1)
1357 else
1358 let n = (nmax + nmin) / 2 in
1359 let vy, h = rowyh cl b n in
1360 let y0, y1 =
1361 if conf.presentation
1362 then
1363 let ips = calcips h in
1364 let y0 = vy - ips in
1365 let y1 = vy + h + ips in
1366 y0, y1
1367 else (
1368 if n = 0
1369 then 0, vy + h + conf.interpagespace
1370 else
1371 let y0 = vy - conf.interpagespace in
1372 y0, y0 + h + conf.interpagespace
1375 if y >= y0 && y < y1
1376 then (
1377 if c = 1
1378 then n
1379 else (
1380 if n > coverA
1381 then
1382 if n < state.pagecount - coverB
1383 then ((n-coverA)/c)*c + coverA
1384 else n
1385 else n
1388 else (
1389 if y > y0
1390 then bsearch (n+1) nmax
1391 else bsearch nmin (n-1)
1394 bsearch 0 (state.pagecount-1);
1397 let layoutN ((columns, coverA, coverB), b) y sh =
1398 let sh = sh - (hscrollh ()) in
1399 let rec fold accu n =
1400 if n = Array.length b
1401 then accu
1402 else
1403 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1404 if (vy - y) > sh &&
1405 (n = coverA - 1
1406 || n = state.pagecount - coverB
1407 || (n - coverA) mod columns = columns - 1)
1408 then accu
1409 else
1410 let accu =
1411 if vy + h > y
1412 then
1413 let pagey = max 0 (y - vy) in
1414 let pagedispy = if pagey > 0 then 0 else vy - y in
1415 let pagedispx, pagex =
1416 let pdx =
1417 if n = coverA - 1 || n = state.pagecount - coverB
1418 then state.x + (wadjsb state.winw - w) / 2
1419 else dx + xoff + state.x
1421 if pdx < 0
1422 then 0, -pdx
1423 else pdx, 0
1425 let pagevw =
1426 let vw = wadjsb state.winw - pagedispx in
1427 let pw = w - pagex in
1428 min vw pw
1430 let pagevh = min (h - pagey) (sh - pagedispy) in
1431 if pagevw > 0 && pagevh > 0
1432 then
1433 let e =
1434 { pageno = n
1435 ; pagedimno = pdimno
1436 ; pagew = w
1437 ; pageh = h
1438 ; pagex = pagex
1439 ; pagey = pagey
1440 ; pagevw = pagevw
1441 ; pagevh = pagevh
1442 ; pagedispx = pagedispx
1443 ; pagedispy = pagedispy
1444 ; pagecol = 0
1447 e :: accu
1448 else
1449 accu
1450 else
1451 accu
1453 fold accu (n+1)
1455 if Array.length b = 0
1456 then []
1457 else List.rev (fold [] (page_of_y y))
1460 let layoutS (columns, b) y sh =
1461 let sh = sh - hscrollh () in
1462 let rec fold accu n =
1463 if n = Array.length b
1464 then accu
1465 else
1466 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1467 if (vy - y) > sh
1468 then accu
1469 else
1470 let accu =
1471 if vy + pageh > y
1472 then
1473 let x = xoff + state.x in
1474 let pagey = max 0 (y - vy) in
1475 let pagedispy = if pagey > 0 then 0 else vy - y in
1476 let pagedispx, pagex =
1477 if px = 0
1478 then (
1479 if x < 0
1480 then 0, -x
1481 else x, 0
1483 else (
1484 let px = px - x in
1485 if px < 0
1486 then -px, 0
1487 else 0, px
1490 let pagecolw = pagew/columns in
1491 let pagedispx =
1492 if pagecolw < state.winw
1493 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
1494 else pagedispx
1496 let pagevw =
1497 let vw = wadjsb state.winw - pagedispx in
1498 let pw = pagew - pagex in
1499 min vw pw
1501 let pagevw = min pagevw pagecolw in
1502 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1503 if pagevw > 0 && pagevh > 0
1504 then
1505 let e =
1506 { pageno = n/columns
1507 ; pagedimno = pdimno
1508 ; pagew = pagew
1509 ; pageh = pageh
1510 ; pagex = pagex
1511 ; pagey = pagey
1512 ; pagevw = pagevw
1513 ; pagevh = pagevh
1514 ; pagedispx = pagedispx
1515 ; pagedispy = pagedispy
1516 ; pagecol = n mod columns
1519 e :: accu
1520 else
1521 accu
1522 else
1523 accu
1525 fold accu (n+1)
1527 List.rev (fold [] 0)
1530 let layout y sh =
1531 if nogeomcmds state.geomcmds
1532 then
1533 match conf.columns with
1534 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1535 | Cmulti c -> layoutN c y sh
1536 | Csplit s -> layoutS s y sh
1537 else []
1540 let clamp incr =
1541 let y = state.y + incr in
1542 let y = max 0 y in
1543 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1547 let itertiles l f =
1548 let tilex = l.pagex mod conf.tilew in
1549 let tiley = l.pagey mod conf.tileh in
1551 let col = l.pagex / conf.tilew in
1552 let row = l.pagey / conf.tileh in
1554 let rec rowloop row y0 dispy h =
1555 if h = 0
1556 then ()
1557 else (
1558 let dh = conf.tileh - y0 in
1559 let dh = min h dh in
1560 let rec colloop col x0 dispx w =
1561 if w = 0
1562 then ()
1563 else (
1564 let dw = conf.tilew - x0 in
1565 let dw = min w dw in
1567 f col row dispx dispy x0 y0 dw dh;
1568 colloop (col+1) 0 (dispx+dw) (w-dw)
1571 colloop col tilex l.pagedispx l.pagevw;
1572 rowloop (row+1) 0 (dispy+dh) (h-dh)
1575 if l.pagevw > 0 && l.pagevh > 0
1576 then rowloop row tiley l.pagedispy l.pagevh;
1579 let gettileopaque l col row =
1580 let key =
1581 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1583 try Some (Hashtbl.find state.tilemap key)
1584 with Not_found -> None
1587 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1588 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1589 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1592 let filledrect x0 y0 x1 y1 =
1593 GlArray.disable `texture_coord;
1594 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
1595 GlArray.vertex `two state.vraw;
1596 GlArray.draw_arrays `triangle_strip 0 4;
1597 GlArray.enable `texture_coord;
1600 let linerect x0 y0 x1 y1 =
1601 GlArray.disable `texture_coord;
1602 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
1603 GlArray.vertex `two state.vraw;
1604 GlArray.draw_arrays `line_loop 0 4;
1605 GlArray.enable `texture_coord;
1608 let drawtiles l color =
1609 GlDraw.color color;
1610 begintiles ();
1611 let f col row x y tilex tiley w h =
1612 match gettileopaque l col row with
1613 | Some (opaque, _, t) ->
1614 let params = x, y, w, h, tilex, tiley in
1615 if conf.invert
1616 then (
1617 Gl.enable `blend;
1618 GlFunc.blend_func `zero `one_minus_src_color;
1620 drawtile params opaque;
1621 if conf.invert
1622 then Gl.disable `blend;
1623 if conf.debug
1624 then (
1625 endtiles ();
1626 let s = Printf.sprintf
1627 "%d[%d,%d] %f sec"
1628 l.pageno col row t
1630 let w = measurestr fstate.fontsize s in
1631 GlDraw.color (0.0, 0.0, 0.0);
1632 filledrect (float (x-2))
1633 (float (y-2))
1634 (float (x+2) +. w)
1635 (float (y + fstate.fontsize + 2));
1636 GlDraw.color (1.0, 1.0, 1.0);
1637 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1638 begintiles ();
1641 | None ->
1642 endtiles ();
1643 let w =
1644 let lw = wadjsb state.winw - x in
1645 min lw w
1646 and h =
1647 let lh = state.winh - y in
1648 min lh h
1650 begin match state.texid with
1651 | Some id ->
1652 Gl.enable `texture_2d;
1653 GlTex.bind_texture `texture_2d id;
1654 let x0 = float x
1655 and y0 = float y
1656 and x1 = float (x+w)
1657 and y1 = float (y+h) in
1659 let tw = float w /. 16.0
1660 and th = float h /. 16.0 in
1661 let tx0 = float tilex /. 16.0
1662 and ty0 = float tiley /. 16.0 in
1663 let tx1 = tx0 +. tw
1664 and ty1 = ty0 +. th in
1665 Raw.sets_float state.vraw ~pos:0
1666 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
1667 Raw.sets_float state.traw ~pos:0
1668 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
1669 GlArray.vertex `two state.vraw;
1670 GlArray.tex_coord `two state.traw;
1671 GlArray.draw_arrays `triangle_strip 0 4;
1673 | None ->
1674 GlDraw.color (1.0, 1.0, 1.0);
1675 filledrect (float x) (float y) (float (x+w)) (float (y+h));
1676 end;
1677 if w > 128 && h > fstate.fontsize + 10
1678 then (
1679 GlDraw.color (0.0, 0.0, 0.0);
1680 let c, r =
1681 if conf.verbose
1682 then (col*conf.tilew, row*conf.tileh)
1683 else col, row
1685 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1687 GlDraw.color color;
1688 begintiles ();
1690 itertiles l f;
1691 endtiles ();
1694 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1696 let tilevisible1 l x y =
1697 let ax0 = l.pagex
1698 and ax1 = l.pagex + l.pagevw
1699 and ay0 = l.pagey
1700 and ay1 = l.pagey + l.pagevh in
1702 let bx0 = x
1703 and by0 = y in
1704 let bx1 = min (bx0 + conf.tilew) l.pagew
1705 and by1 = min (by0 + conf.tileh) l.pageh in
1707 let rx0 = max ax0 bx0
1708 and ry0 = max ay0 by0
1709 and rx1 = min ax1 bx1
1710 and ry1 = min ay1 by1 in
1712 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1713 nonemptyintersection
1716 let tilevisible layout n x y =
1717 let rec findpageinlayout m = function
1718 | l :: rest when l.pageno = n ->
1719 tilevisible1 l x y || (
1720 match conf.columns with
1721 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1722 | _ -> false
1724 | _ :: rest -> findpageinlayout 0 rest
1725 | [] -> false
1727 findpageinlayout 0 layout;
1730 let tileready l x y =
1731 tilevisible1 l x y &&
1732 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1735 let tilepage n p layout =
1736 let rec loop = function
1737 | l :: rest ->
1738 if l.pageno = n
1739 then
1740 let f col row _ _ _ _ _ _ =
1741 if state.currently = Idle
1742 then
1743 match gettileopaque l col row with
1744 | Some _ -> ()
1745 | None ->
1746 let x = col*conf.tilew
1747 and y = row*conf.tileh in
1748 let w =
1749 let w = l.pagew - x in
1750 min w conf.tilew
1752 let h =
1753 let h = l.pageh - y in
1754 min h conf.tileh
1756 let pbo =
1757 if conf.usepbo
1758 then getpbo w h conf.colorspace
1759 else "0"
1761 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1762 state.currently <-
1763 Tiling (
1764 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1765 conf.tilew, conf.tileh
1768 itertiles l f;
1769 else
1770 loop rest
1772 | [] -> ()
1774 if nogeomcmds state.geomcmds
1775 then loop layout;
1778 let preloadlayout y =
1779 let y = if y < state.winh then 0 else y - state.winh in
1780 let h = state.winh*3 in
1781 layout y h;
1784 let load pages =
1785 let rec loop pages =
1786 if state.currently != Idle
1787 then ()
1788 else
1789 match pages with
1790 | l :: rest ->
1791 begin match getopaque l.pageno with
1792 | None ->
1793 wcmd "page %d %d" l.pageno l.pagedimno;
1794 state.currently <- Loading (l, state.gen);
1795 | Some opaque ->
1796 tilepage l.pageno opaque pages;
1797 loop rest
1798 end;
1799 | _ -> ()
1801 if nogeomcmds state.geomcmds
1802 then loop pages
1805 let preload pages =
1806 load pages;
1807 if conf.preload && state.currently = Idle
1808 then load (preloadlayout state.y);
1811 let layoutready layout =
1812 let rec fold all ls =
1813 all && match ls with
1814 | l :: rest ->
1815 let seen = ref false in
1816 let allvisible = ref true in
1817 let foo col row _ _ _ _ _ _ =
1818 seen := true;
1819 allvisible := !allvisible &&
1820 begin match gettileopaque l col row with
1821 | Some _ -> true
1822 | None -> false
1825 itertiles l foo;
1826 fold (!seen && !allvisible) rest
1827 | [] -> true
1829 let alltilesvisible = fold true layout in
1830 alltilesvisible;
1833 let gotoy y =
1834 let y = bound y 0 state.maxy in
1835 let y, layout, proceed =
1836 match conf.maxwait with
1837 | Some time when state.ghyll == noghyll ->
1838 begin match state.throttle with
1839 | None ->
1840 let layout = layout y state.winh in
1841 let ready = layoutready layout in
1842 if not ready
1843 then (
1844 load layout;
1845 state.throttle <- Some (layout, y, now ());
1847 else G.postRedisplay "gotoy showall (None)";
1848 y, layout, ready
1849 | Some (_, _, started) ->
1850 let dt = now () -. started in
1851 if dt > time
1852 then (
1853 state.throttle <- None;
1854 let layout = layout y state.winh in
1855 load layout;
1856 G.postRedisplay "maxwait";
1857 y, layout, true
1859 else -1, [], false
1862 | _ ->
1863 let layout = layout y state.winh in
1864 if not !wtmode || layoutready layout
1865 then G.postRedisplay "gotoy ready";
1866 y, layout, true
1868 if proceed
1869 then (
1870 state.y <- y;
1871 state.layout <- layout;
1872 begin match state.mode with
1873 | LinkNav (Ltexact (pageno, linkno)) ->
1874 let rec loop = function
1875 | [] ->
1876 state.mode <- LinkNav (Ltgendir 0)
1877 | l :: _ when l.pageno = pageno ->
1878 begin match getopaque pageno with
1879 | None ->
1880 state.mode <- LinkNav (Ltgendir 0)
1881 | Some opaque ->
1882 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1883 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1884 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1885 then state.mode <- LinkNav (Ltgendir 0)
1887 | _ :: rest -> loop rest
1889 loop layout
1890 | _ -> ()
1891 end;
1892 begin match state.mode with
1893 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1894 if not (pagevisible layout pageno)
1895 then (
1896 match state.layout with
1897 | [] -> ()
1898 | l :: _ ->
1899 state.mode <- Birdseye (
1900 conf, leftx, l.pageno, hooverpageno, anchor
1903 | LinkNav (Ltgendir dir as lt) ->
1904 let linknav =
1905 let rec loop = function
1906 | [] -> lt
1907 | l :: rest ->
1908 match getopaque l.pageno with
1909 | None -> loop rest
1910 | Some opaque ->
1911 let link =
1912 let ld =
1913 if dir = 0
1914 then LDfirstvisible (l.pagex, l.pagey, dir)
1915 else (
1916 if dir > 0 then LDfirst else LDlast
1919 findlink opaque ld
1921 match link with
1922 | Lnotfound -> loop rest
1923 | Lfound n ->
1924 showlinktype (getlink opaque n);
1925 Ltexact (l.pageno, n)
1927 loop state.layout
1929 state.mode <- LinkNav linknav
1930 | _ -> ()
1931 end;
1932 preload layout;
1934 state.ghyll <- noghyll;
1935 if conf.updatecurs
1936 then (
1937 let mx, my = state.mpos in
1938 updateunder mx my;
1942 let conttiling pageno opaque =
1943 tilepage pageno opaque
1944 (if conf.preload then preloadlayout state.y else state.layout)
1947 let gotoy_and_clear_text y =
1948 if not conf.verbose then state.text <- "";
1949 gotoy y;
1952 let getanchor1 l =
1953 let top =
1954 let coloff = l.pagecol * l.pageh in
1955 float (l.pagey + coloff) /. float l.pageh
1957 let dtop =
1958 if l.pagedispy = 0
1959 then
1961 else (
1962 if conf.presentation
1963 then float l.pagedispy /. float (calcips l.pageh)
1964 else float l.pagedispy /. float conf.interpagespace
1967 (l.pageno, top, dtop)
1970 let getanchor () =
1971 match state.layout with
1972 | l :: _ -> getanchor1 l
1973 | [] ->
1974 let n = page_of_y state.y in
1975 if n = -1
1976 then state.anchor
1977 else
1978 let y, h = getpageyh n in
1979 let dy = y - state.y in
1980 let dtop =
1981 if conf.presentation
1982 then
1983 let ips = calcips h in
1984 float (dy + ips) /. float ips
1985 else
1986 float dy /. float conf.interpagespace
1988 (n, 0.0, dtop)
1991 let getanchory (n, top, dtop) =
1992 let y, h = getpageyh n in
1993 if conf.presentation
1994 then
1995 let ips = calcips h in
1996 y + truncate (top*.float h -. dtop*.float ips) + ips;
1997 else
1998 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
2001 let gotoanchor anchor =
2002 gotoy (getanchory anchor);
2005 let addnav () =
2006 cbput state.hists.nav (getanchor ());
2009 let getnav dir =
2010 let anchor = cbgetc state.hists.nav dir in
2011 getanchory anchor;
2014 let gotoghyll y =
2015 let scroll f n a b =
2016 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
2017 let snake f a b =
2018 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
2019 if f < a
2020 then s (float f /. float a)
2021 else (
2022 if f > b
2023 then 1.0 -. s ((float (f-b) /. float (n-b)))
2024 else 1.0
2027 snake f a b
2028 and summa n a b =
2029 let ins = float a *. 0.5
2030 and outs = float (n-b) *. 0.5 in
2031 let ones = b - a in
2032 ins +. outs +. float ones
2034 let rec set (_N, _A, _B) y sy =
2035 let sum = summa _N _A _B in
2036 let dy = float (y - sy) in
2037 state.ghyll <- (
2038 let rec gf n y1 o =
2039 if n >= _N
2040 then state.ghyll <- noghyll
2041 else
2042 let go n =
2043 let s = scroll n _N _A _B in
2044 let y1 = y1 +. ((s *. dy) /. sum) in
2045 gotoy_and_clear_text (truncate y1);
2046 state.ghyll <- gf (n+1) y1;
2048 match o with
2049 | None -> go n
2050 | Some y' -> set (_N/2, 1, 1) y' state.y
2052 gf 0 (float state.y)
2055 match conf.ghyllscroll with
2056 | None ->
2057 gotoy_and_clear_text y
2058 | Some nab ->
2059 if state.ghyll == noghyll
2060 then set nab y state.y
2061 else state.ghyll (Some y)
2064 let gotopage n top =
2065 let y, h = getpageyh n in
2066 let y = y + (truncate (top *. float h)) in
2067 gotoghyll y
2070 let gotopage1 n top =
2071 let y = getpagey n in
2072 let y = y + top in
2073 gotoghyll y
2076 let invalidate s f =
2077 state.layout <- [];
2078 state.pdims <- [];
2079 state.rects <- [];
2080 state.rects1 <- [];
2081 match state.geomcmds with
2082 | ps, [] when emptystr ps ->
2083 f ();
2084 state.geomcmds <- s, [];
2086 | ps, [] ->
2087 state.geomcmds <- ps, [s, f];
2089 | ps, (s', _) :: rest when s' = s ->
2090 state.geomcmds <- ps, ((s, f) :: rest);
2092 | ps, cmds ->
2093 state.geomcmds <- ps, ((s, f) :: cmds);
2096 let flushpages () =
2097 Hashtbl.iter (fun _ opaque ->
2098 wcmd "freepage %s" opaque;
2099 ) state.pagemap;
2100 Hashtbl.clear state.pagemap;
2103 let flushtiles () =
2104 if not (Queue.is_empty state.tilelru)
2105 then (
2106 Queue.iter (fun (k, p, s) ->
2107 wcmd "freetile %s" p;
2108 state.memused <- state.memused - s;
2109 Hashtbl.remove state.tilemap k;
2110 ) state.tilelru;
2111 state.uioh#infochanged Memused;
2112 Queue.clear state.tilelru;
2114 load state.layout;
2117 let stateh h =
2118 let h = truncate (float h*.conf.zoom) in
2119 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2120 h - d
2123 let opendoc path password =
2124 state.path <- path;
2125 state.password <- password;
2126 state.gen <- state.gen + 1;
2127 state.docinfo <- [];
2129 flushpages ();
2130 setaalevel conf.aalevel;
2131 let titlepath =
2132 if emptystr state.origin
2133 then path
2134 else state.origin
2136 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2137 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
2138 invalidate "reqlayout"
2139 (fun () ->
2140 wcmd "reqlayout %d %d %d %s\000"
2141 conf.angle (FMTE.to_int conf.fitmodel)
2142 (stateh state.winh) state.nameddest
2146 let reload () =
2147 state.anchor <- getanchor ();
2148 opendoc state.path state.password;
2151 let scalecolor c =
2152 let c = c *. conf.colorscale in
2153 (c, c, c);
2156 let scalecolor2 (r, g, b) =
2157 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2160 let docolumns = function
2161 | Csingle _ ->
2162 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2163 let rec loop pageno pdimno pdim y ph pdims =
2164 if pageno = state.pagecount
2165 then ()
2166 else
2167 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2168 match pdims with
2169 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2170 pdimno+1, pdim, rest
2171 | _ ->
2172 pdimno, pdim, pdims
2174 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2175 let y = y +
2176 (if conf.presentation
2177 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2178 else (if pageno = 0 then 0 else conf.interpagespace)
2181 a.(pageno) <- (pdimno, x, y, pdim);
2182 loop (pageno+1) pdimno pdim (y + h) h pdims
2184 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2185 conf.columns <- Csingle a;
2187 | Cmulti ((columns, coverA, coverB), _) ->
2188 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2189 let rec loop pageno pdimno pdim x y rowh pdims =
2190 let rec fixrow m = if m = pageno then () else
2191 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2192 if h < rowh
2193 then (
2194 let y = y + (rowh - h) / 2 in
2195 a.(m) <- (pdimno, x, y, pdim);
2197 fixrow (m+1)
2199 if pageno = state.pagecount
2200 then fixrow (((pageno - 1) / columns) * columns)
2201 else
2202 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2203 match pdims with
2204 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2205 pdimno+1, pdim, rest
2206 | _ ->
2207 pdimno, pdim, pdims
2209 let x, y, rowh' =
2210 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2211 then (
2212 let x = (wadjsb state.winw - w) / 2 in
2213 let ips =
2214 if conf.presentation then calcips h else conf.interpagespace in
2215 x, y + ips + rowh, h
2217 else (
2218 if (pageno - coverA) mod columns = 0
2219 then (
2220 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2221 let y =
2222 if conf.presentation
2223 then
2224 let ips = calcips h in
2225 y + (if pageno = 0 then 0 else calcips rowh + ips)
2226 else
2227 y + (if pageno = 0 then 0 else conf.interpagespace)
2229 x, y + rowh, h
2231 else x, y, max rowh h
2234 let y =
2235 if pageno > 1 && (pageno - coverA) mod columns = 0
2236 then (
2237 let y =
2238 if pageno = columns && conf.presentation
2239 then (
2240 let ips = calcips rowh in
2241 for i = 0 to pred columns
2243 let (pdimno, x, y, pdim) = a.(i) in
2244 a.(i) <- (pdimno, x, y+ips, pdim)
2245 done;
2246 y+ips;
2248 else y
2250 fixrow (pageno - columns);
2253 else y
2255 a.(pageno) <- (pdimno, x, y, pdim);
2256 let x = x + w + xoff*2 + conf.interpagespace in
2257 loop (pageno+1) pdimno pdim x y rowh' pdims
2259 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2260 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2262 | Csplit (c, _) ->
2263 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2264 let rec loop pageno pdimno pdim y pdims =
2265 if pageno = state.pagecount
2266 then ()
2267 else
2268 let pdimno, ((_, w, h, _) as pdim), pdims =
2269 match pdims with
2270 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2271 pdimno+1, pdim, rest
2272 | _ ->
2273 pdimno, pdim, pdims
2275 let cw = w / c in
2276 let rec loop1 n x y =
2277 if n = c then y else (
2278 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2279 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2282 let y = loop1 0 0 y in
2283 loop (pageno+1) pdimno pdim y pdims
2285 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2286 conf.columns <- Csplit (c, a);
2289 let represent () =
2290 docolumns conf.columns;
2291 state.maxy <- calcheight ();
2292 if state.reprf == noreprf
2293 then (
2294 match state.mode with
2295 | Birdseye (_, _, pageno, _, _) ->
2296 let y, h = getpageyh pageno in
2297 let top = (state.winh - h) / 2 in
2298 gotoy (max 0 (y - top))
2299 | _ -> gotoanchor state.anchor
2301 else (
2302 state.reprf ();
2303 state.reprf <- noreprf;
2307 let reshape w h =
2308 GlDraw.viewport 0 0 w h;
2309 let firsttime = state.geomcmds == firstgeomcmds in
2310 if not firsttime && nogeomcmds state.geomcmds
2311 then state.anchor <- getanchor ();
2313 state.winw <- w;
2314 let w = wadjsb (truncate (float w *. conf.zoom)) in
2315 let w = max w 2 in
2316 state.winh <- h;
2317 setfontsize fstate.fontsize;
2318 GlMat.mode `modelview;
2319 GlMat.load_identity ();
2321 GlMat.mode `projection;
2322 GlMat.load_identity ();
2323 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2324 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2325 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2327 let relx =
2328 if conf.zoom <= 1.0
2329 then 0.0
2330 else float state.x /. float state.w
2332 invalidate "geometry"
2333 (fun () ->
2334 state.w <- w;
2335 if not firsttime
2336 then state.x <- truncate (relx *. float w);
2337 let w =
2338 match conf.columns with
2339 | Csingle _ -> w
2340 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2341 | Csplit (c, _) -> w * c
2343 wcmd "geometry %d %d %d"
2344 w (stateh h) (FMTE.to_int conf.fitmodel)
2348 let enttext () =
2349 let len = String.length state.text in
2350 let drawstring s =
2351 let hscrollh =
2352 match state.mode with
2353 | Textentry _ | View | LinkNav _ ->
2354 let h, _, _ = state.uioh#scrollpw in
2356 | _ -> 0
2358 let rect x w =
2359 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
2360 (x+.w) (float (state.winh - hscrollh))
2363 let w = float (wadjsb state.winw - 1) in
2364 if state.progress >= 0.0 && state.progress < 1.0
2365 then (
2366 GlDraw.color (0.3, 0.3, 0.3);
2367 let w1 = w *. state.progress in
2368 rect 0.0 w1;
2369 GlDraw.color (0.0, 0.0, 0.0);
2370 rect w1 (w-.w1)
2372 else (
2373 GlDraw.color (0.0, 0.0, 0.0);
2374 rect 0.0 w;
2377 GlDraw.color (1.0, 1.0, 1.0);
2378 drawstring fstate.fontsize
2379 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2381 let s =
2382 match state.mode with
2383 | Textentry ((prefix, text, _, _, _, _), _) ->
2384 let s =
2385 if len > 0
2386 then
2387 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2388 else
2389 Printf.sprintf "%s%s_" prefix text
2393 | _ -> state.text
2395 let s =
2396 if state.newerrmsgs
2397 then (
2398 if not (istextentry state.mode) && state.uioh#eformsgs
2399 then
2400 let s1 = "(press 'e' to review error messasges)" in
2401 if nonemptystr s then s ^ " " ^ s1 else s1
2402 else s
2404 else s
2406 if nonemptystr s
2407 then drawstring s
2410 let gctiles () =
2411 let len = Queue.length state.tilelru in
2412 let layout = lazy (
2413 match state.throttle with
2414 | None ->
2415 if conf.preload
2416 then preloadlayout state.y
2417 else state.layout
2418 | Some (layout, _, _) ->
2419 layout
2420 ) in
2421 let rec loop qpos =
2422 if state.memused <= conf.memlimit
2423 then ()
2424 else (
2425 if qpos < len
2426 then
2427 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2428 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2429 let (_, pw, ph, _) = getpagedim n in
2431 gen = state.gen
2432 && colorspace = conf.colorspace
2433 && angle = conf.angle
2434 && pagew = pw
2435 && pageh = ph
2436 && (
2437 let x = col*conf.tilew
2438 and y = row*conf.tileh in
2439 tilevisible (Lazy.force_val layout) n x y
2441 then Queue.push lruitem state.tilelru
2442 else (
2443 freepbo p;
2444 wcmd "freetile %s" p;
2445 state.memused <- state.memused - s;
2446 state.uioh#infochanged Memused;
2447 Hashtbl.remove state.tilemap k;
2449 loop (qpos+1)
2452 loop 0
2455 let logcurrently = function
2456 | Idle -> dolog "Idle"
2457 | Loading (l, gen) ->
2458 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2459 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2460 dolog
2461 "Tiling %d[%d,%d] page=%s cs=%s angle"
2462 l.pageno col row pageopaque
2463 (CSTE.to_string colorspace)
2465 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2466 angle gen conf.angle state.gen
2467 tilew tileh
2468 conf.tilew conf.tileh
2470 | Outlining _ ->
2471 dolog "outlining"
2474 let splitatspace =
2475 let r = Str.regexp " " in
2476 fun s -> Str.bounded_split r s 2;
2479 let onpagerect pageno f =
2480 let b =
2481 match conf.columns with
2482 | Cmulti (_, b) -> b
2483 | Csingle b -> b
2484 | Csplit (_, b) -> b
2486 if pageno >= 0 && pageno < Array.length b
2487 then
2488 let (_, _, _, (w, h, _, _)) = b.(pageno) in
2489 f w h
2492 let gotopagexy1 pageno x y =
2493 let _,w1,h1,leftx = getpagedim pageno in
2494 let top = y /. (float h1) in
2495 let left = x /. (float w1) in
2496 let py, w, h = getpageywh pageno in
2497 let wh = state.winh - hscrollh () in
2498 let x = left *. (float w) in
2499 let x = leftx + state.x + truncate x in
2500 let sx =
2501 if x < 0 || x >= wadjsb state.winw
2502 then state.x - x
2503 else state.x
2505 let pdy = truncate (top *. float h) in
2506 let y' = py + pdy in
2507 let dy = y' - state.y in
2508 let sy =
2509 if x != state.x || not (dy > 0 && dy < wh)
2510 then (
2511 if conf.presentation
2512 then
2513 if abs (py - y') > wh
2514 then y'
2515 else py
2516 else y';
2518 else state.y
2520 if state.x != sx || state.y != sy
2521 then (
2522 let x, y =
2523 if !wtmode
2524 then (
2525 let ww = wadjsb state.winw in
2526 let qx = sx / ww
2527 and qy = pdy / wh in
2528 let x = qx * ww
2529 and y = py + qy * wh in
2530 let x = if -x + ww > w1 then -(w1-ww) else x
2531 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2532 let y =
2533 if conf.presentation
2534 then
2535 if abs (py - y') > wh
2536 then y'
2537 else py
2538 else y';
2540 (x, y)
2542 else (sx, sy)
2544 state.x <- x;
2545 gotoy_and_clear_text y;
2547 else gotoy_and_clear_text state.y;
2550 let gotopagexy pageno x y =
2551 match state.mode with
2552 | Birdseye _ -> gotopage pageno 0.0
2553 | _ -> gotopagexy1 pageno x y
2556 let act cmds =
2557 (* dolog "%S" cmds; *)
2558 let cl = splitatspace cmds in
2559 let scan s fmt f =
2560 try Scanf.sscanf s fmt f
2561 with exn ->
2562 dolog "error processing '%S': %s" cmds (exntos exn);
2563 exit 1
2565 let addoutline outline =
2566 match state.currently with
2567 | Outlining outlines ->
2568 state.currently <- Outlining (outline :: outlines)
2569 | Idle -> state.currently <- Outlining [outline]
2570 | currently ->
2571 dolog "invalid outlining state";
2572 logcurrently currently
2574 match cl with
2575 | "clear" :: [] ->
2576 state.uioh#infochanged Pdim;
2577 state.pdims <- [];
2579 | "clearrects" :: [] ->
2580 state.rects <- state.rects1;
2581 G.postRedisplay "clearrects";
2583 | "continue" :: args :: [] ->
2584 let n = scan args "%u" (fun n -> n) in
2585 state.pagecount <- n;
2586 begin match state.currently with
2587 | Outlining l ->
2588 state.currently <- Idle;
2589 state.outlines <- Array.of_list (List.rev l)
2590 | _ -> ()
2591 end;
2593 let cur, cmds = state.geomcmds in
2594 if emptystr cur
2595 then failwith "umpossible";
2597 begin match List.rev cmds with
2598 | [] ->
2599 state.geomcmds <- "", [];
2600 represent ();
2601 | (s, f) :: rest ->
2602 f ();
2603 state.geomcmds <- s, List.rev rest;
2604 end;
2605 if conf.maxwait = None && not !wtmode
2606 then G.postRedisplay "continue";
2608 | "title" :: args :: [] ->
2609 Wsi.settitle args
2611 | "msg" :: args :: [] ->
2612 showtext ' ' args
2614 | "vmsg" :: args :: [] ->
2615 if conf.verbose
2616 then showtext ' ' args
2618 | "emsg" :: args :: [] ->
2619 Buffer.add_string state.errmsgs args;
2620 state.newerrmsgs <- true;
2621 G.postRedisplay "error message"
2623 | "progress" :: args :: [] ->
2624 let progress, text =
2625 scan args "%f %n"
2626 (fun f pos ->
2627 f, String.sub args pos (String.length args - pos))
2629 state.text <- text;
2630 state.progress <- progress;
2631 G.postRedisplay "progress"
2633 | "firstmatch" :: args :: [] ->
2634 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2635 scan args "%u %d %f %f %f %f %f %f %f %f"
2636 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2637 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2639 let y = (getpagey pageno) + truncate y0 in
2640 addnav ();
2641 gotoy y;
2642 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2644 | "match" :: args :: [] ->
2645 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2646 scan args "%u %d %f %f %f %f %f %f %f %f"
2647 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2648 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2650 state.rects1 <-
2651 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2653 | "page" :: args :: [] ->
2654 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2655 begin match state.currently with
2656 | Loading (l, gen) ->
2657 vlog "page %d took %f sec" l.pageno t;
2658 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2659 begin match state.throttle with
2660 | None ->
2661 let preloadedpages =
2662 if conf.preload
2663 then preloadlayout state.y
2664 else state.layout
2666 let evict () =
2667 let set =
2668 List.fold_left (fun s l -> IntSet.add l.pageno s)
2669 IntSet.empty preloadedpages
2671 let evictedpages =
2672 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2673 if not (IntSet.mem pageno set)
2674 then (
2675 wcmd "freepage %s" opaque;
2676 key :: accu
2678 else accu
2679 ) state.pagemap []
2681 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2683 evict ();
2684 state.currently <- Idle;
2685 if gen = state.gen
2686 then (
2687 tilepage l.pageno pageopaque state.layout;
2688 load state.layout;
2689 load preloadedpages;
2690 if pagevisible state.layout l.pageno
2691 && layoutready state.layout
2692 then G.postRedisplay "page";
2695 | Some (layout, _, _) ->
2696 state.currently <- Idle;
2697 tilepage l.pageno pageopaque layout;
2698 load state.layout
2699 end;
2701 | _ ->
2702 dolog "Inconsistent loading state";
2703 logcurrently state.currently;
2704 exit 1
2707 | "tile" :: args :: [] ->
2708 let (x, y, opaque, size, t) =
2709 scan args "%u %u %s %u %f"
2710 (fun x y p size t -> (x, y, p, size, t))
2712 begin match state.currently with
2713 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2714 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2716 unmappbo opaque;
2717 if tilew != conf.tilew || tileh != conf.tileh
2718 then (
2719 wcmd "freetile %s" opaque;
2720 state.currently <- Idle;
2721 load state.layout;
2723 else (
2724 puttileopaque l col row gen cs angle opaque size t;
2725 state.memused <- state.memused + size;
2726 state.uioh#infochanged Memused;
2727 gctiles ();
2728 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2729 opaque, size) state.tilelru;
2731 let layout =
2732 match state.throttle with
2733 | None -> state.layout
2734 | Some (layout, _, _) -> layout
2737 state.currently <- Idle;
2738 if gen = state.gen
2739 && conf.colorspace = cs
2740 && conf.angle = angle
2741 && tilevisible layout l.pageno x y
2742 then conttiling l.pageno pageopaque;
2744 begin match state.throttle with
2745 | None ->
2746 preload state.layout;
2747 if gen = state.gen
2748 && conf.colorspace = cs
2749 && conf.angle = angle
2750 && tilevisible state.layout l.pageno x y
2751 && (not !wtmode || layoutready state.layout)
2752 then G.postRedisplay "tile nothrottle";
2754 | Some (layout, y, _) ->
2755 let ready = layoutready layout in
2756 if ready
2757 then (
2758 state.y <- y;
2759 state.layout <- layout;
2760 state.throttle <- None;
2761 G.postRedisplay "throttle";
2763 else load layout;
2764 end;
2767 | _ ->
2768 dolog "Inconsistent tiling state";
2769 logcurrently state.currently;
2770 exit 1
2773 | "pdim" :: args :: [] ->
2774 let (n, w, h, _) as pdim =
2775 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2777 let pdim =
2778 match conf.fitmodel, conf.columns with
2779 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2780 | _ -> pdim
2782 state.uioh#infochanged Pdim;
2783 state.pdims <- pdim :: state.pdims
2785 | "o" :: args :: [] ->
2786 let (l, n, t, h, pos) =
2787 scan args "%u %u %d %u %n"
2788 (fun l n t h pos -> l, n, t, h, pos)
2790 let s = String.sub args pos (String.length args - pos) in
2791 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
2793 | "ou" :: args :: [] ->
2794 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
2795 let s = String.sub args pos len in
2796 let pos2 = pos + len + 1 in
2797 let uri = String.sub args pos2 (String.length args - pos2) in
2798 addoutline (s, l, Ouri uri)
2800 | "on" :: args :: [] ->
2801 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
2802 let s = String.sub args pos (String.length args - pos) in
2803 addoutline (s, l, Onone)
2805 | "a" :: args :: [] ->
2806 let (n, l, t) =
2807 scan args "%u %d %d" (fun n l t -> n, l, t)
2809 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2811 | "info" :: args :: [] ->
2812 state.docinfo <- (1, args) :: state.docinfo
2814 | "infoend" :: [] ->
2815 state.uioh#infochanged Docinfo;
2816 state.docinfo <- List.rev state.docinfo
2818 | _ ->
2819 error "unknown cmd `%S'" cmds
2822 let onhist cb =
2823 let rc = cb.rc in
2824 let action = function
2825 | HCprev -> cbget cb ~-1
2826 | HCnext -> cbget cb 1
2827 | HCfirst -> cbget cb ~-(cb.rc)
2828 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2829 and cancel () = cb.rc <- rc
2830 in (action, cancel)
2833 let search pattern forward =
2834 match conf.columns with
2835 | Csplit _ ->
2836 showtext '!' "searching does not work properly in split columns mode"
2837 | _ ->
2838 if nonemptystr pattern
2839 then
2840 let pn, py =
2841 match state.layout with
2842 | [] -> 0, 0
2843 | l :: _ ->
2844 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2846 wcmd "search %d %d %d %d,%s\000"
2847 (btod conf.icase) pn py (btod forward) pattern;
2850 let intentry text key =
2851 let c =
2852 if key >= 32 && key < 127
2853 then Char.chr key
2854 else '\000'
2856 match c with
2857 | '0' .. '9' ->
2858 let text = addchar text c in
2859 TEcont text
2861 | _ ->
2862 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2863 TEcont text
2866 let linknentry text key =
2867 let c =
2868 if key >= 32 && key < 127
2869 then Char.chr key
2870 else '\000'
2872 match c with
2873 | 'a' .. 'z' ->
2874 let text = addchar text c in
2875 TEcont text
2877 | _ ->
2878 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2879 TEcont text
2882 let linkndone f s =
2883 if nonemptystr s
2884 then (
2885 let n =
2886 let l = String.length s in
2887 let rec loop pos n = if pos = l then n else
2888 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2889 loop (pos+1) (n*26 + m)
2890 in loop 0 0
2892 let rec loop n = function
2893 | [] -> ()
2894 | l :: rest ->
2895 match getopaque l.pageno with
2896 | None -> loop n rest
2897 | Some opaque ->
2898 let m = getlinkcount opaque in
2899 if n < m
2900 then (
2901 let under = getlink opaque n in
2902 f under
2904 else loop (n-m) rest
2906 loop n state.layout;
2910 let textentry text key =
2911 if key land 0xff00 = 0xff00
2912 then TEcont text
2913 else TEcont (text ^ toutf8 key)
2916 let reqlayout angle fitmodel =
2917 match state.throttle with
2918 | None ->
2919 if nogeomcmds state.geomcmds
2920 then state.anchor <- getanchor ();
2921 conf.angle <- angle mod 360;
2922 if conf.angle != 0
2923 then (
2924 match state.mode with
2925 | LinkNav _ -> state.mode <- View
2926 | _ -> ()
2928 conf.fitmodel <- fitmodel;
2929 invalidate "reqlayout"
2930 (fun () ->
2931 wcmd "reqlayout %d %d %d"
2932 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2934 | _ -> ()
2937 let settrim trimmargins trimfuzz =
2938 if nogeomcmds state.geomcmds
2939 then state.anchor <- getanchor ();
2940 conf.trimmargins <- trimmargins;
2941 conf.trimfuzz <- trimfuzz;
2942 let x0, y0, x1, y1 = trimfuzz in
2943 invalidate "settrim"
2944 (fun () ->
2945 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2946 flushpages ();
2949 let setzoom zoom =
2950 match state.throttle with
2951 | None ->
2952 let zoom = max 0.0001 zoom in
2953 if zoom <> conf.zoom
2954 then (
2955 state.prevzoom <- (conf.zoom, state.x);
2956 conf.zoom <- zoom;
2957 reshape state.winw state.winh;
2958 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2961 | Some (layout, y, started) ->
2962 let time =
2963 match conf.maxwait with
2964 | None -> 0.0
2965 | Some t -> t
2967 let dt = now () -. started in
2968 if dt > time
2969 then (
2970 state.y <- y;
2971 load layout;
2975 let setcolumns mode columns coverA coverB =
2976 state.prevcolumns <- Some (conf.columns, conf.zoom);
2977 if columns < 0
2978 then (
2979 if isbirdseye mode
2980 then showtext '!' "split mode doesn't work in bird's eye"
2981 else (
2982 conf.columns <- Csplit (-columns, [||]);
2983 state.x <- 0;
2984 conf.zoom <- 1.0;
2987 else (
2988 if columns < 2
2989 then (
2990 conf.columns <- Csingle [||];
2991 state.x <- 0;
2992 setzoom 1.0;
2994 else (
2995 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
2996 conf.zoom <- 1.0;
2999 reshape state.winw state.winh;
3002 let enterbirdseye () =
3003 let zoom = float conf.thumbw /. float state.winw in
3004 let birdseyepageno =
3005 let cy = state.winh / 2 in
3006 let fold = function
3007 | [] -> 0
3008 | l :: rest ->
3009 let rec fold best = function
3010 | [] -> best.pageno
3011 | l :: rest ->
3012 let d = cy - (l.pagedispy + l.pagevh/2)
3013 and dbest = cy - (best.pagedispy + best.pagevh/2) in
3014 if abs d < abs dbest
3015 then fold l rest
3016 else best.pageno
3017 in fold l rest
3019 fold state.layout
3021 state.mode <- Birdseye (
3022 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
3024 conf.zoom <- zoom;
3025 conf.presentation <- false;
3026 conf.interpagespace <- 10;
3027 conf.hlinks <- false;
3028 conf.fitmodel <- FitProportional;
3029 state.x <- 0;
3030 state.mstate <- Mnone;
3031 conf.maxwait <- None;
3032 conf.columns <- (
3033 match conf.beyecolumns with
3034 | Some c ->
3035 conf.zoom <- 1.0;
3036 Cmulti ((c, 0, 0), [||])
3037 | None -> Csingle [||]
3039 Wsi.setcursor Wsi.CURSOR_INHERIT;
3040 if conf.verbose
3041 then
3042 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
3043 (100.0*.zoom)
3044 else
3045 state.text <- ""
3047 reshape state.winw state.winh;
3050 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
3051 state.mode <- View;
3052 conf.zoom <- c.zoom;
3053 conf.presentation <- c.presentation;
3054 conf.interpagespace <- c.interpagespace;
3055 conf.maxwait <- c.maxwait;
3056 conf.hlinks <- c.hlinks;
3057 conf.fitmodel <- c.fitmodel;
3058 conf.beyecolumns <- (
3059 match conf.columns with
3060 | Cmulti ((c, _, _), _) -> Some c
3061 | Csingle _ -> None
3062 | Csplit _ -> failwith "leaving bird's eye split mode"
3064 conf.columns <- (
3065 match c.columns with
3066 | Cmulti (c, _) -> Cmulti (c, [||])
3067 | Csingle _ -> Csingle [||]
3068 | Csplit (c, _) -> Csplit (c, [||])
3070 if conf.verbose
3071 then
3072 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
3073 (100.0*.conf.zoom)
3075 reshape state.winw state.winh;
3076 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
3077 state.x <- leftx;
3080 let togglebirdseye () =
3081 match state.mode with
3082 | Birdseye vals -> leavebirdseye vals true
3083 | View -> enterbirdseye ()
3084 | _ -> ()
3087 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3088 let pageno = max 0 (pageno - incr) in
3089 let rec loop = function
3090 | [] -> gotopage1 pageno 0
3091 | l :: _ when l.pageno = pageno ->
3092 if l.pagedispy >= 0 && l.pagey = 0
3093 then G.postRedisplay "upbirdseye"
3094 else gotopage1 pageno 0
3095 | _ :: rest -> loop rest
3097 loop state.layout;
3098 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
3101 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3102 let pageno = min (state.pagecount - 1) (pageno + incr) in
3103 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3104 let rec loop = function
3105 | [] ->
3106 let y, h = getpageyh pageno in
3107 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
3108 gotoy (clamp dy)
3109 | l :: _ when l.pageno = pageno ->
3110 if l.pagevh != l.pageh
3111 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
3112 else G.postRedisplay "downbirdseye"
3113 | _ :: rest -> loop rest
3115 loop state.layout
3118 let optentry mode _ key =
3119 let btos b = if b then "on" else "off" in
3120 if key >= 32 && key < 127
3121 then
3122 let c = Char.chr key in
3123 match c with
3124 | 's' ->
3125 let ondone s =
3126 try conf.scrollstep <- int_of_string s with exc ->
3127 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3129 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3131 | 'A' ->
3132 let ondone s =
3134 conf.autoscrollstep <- int_of_string s;
3135 if state.autoscroll <> None
3136 then state.autoscroll <- Some conf.autoscrollstep
3137 with exc ->
3138 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3140 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3142 | 'C' ->
3143 let ondone s =
3145 let n, a, b = multicolumns_of_string s in
3146 setcolumns mode n a b;
3147 with exc ->
3148 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3150 TEswitch ("columns: ", "", None, textentry, ondone, true)
3152 | 'Z' ->
3153 let ondone s =
3155 let zoom = float (int_of_string s) /. 100.0 in
3156 setzoom zoom
3157 with exc ->
3158 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3160 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3162 | 't' ->
3163 let ondone s =
3165 conf.thumbw <- bound (int_of_string s) 2 4096;
3166 state.text <-
3167 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3168 begin match mode with
3169 | Birdseye beye ->
3170 leavebirdseye beye false;
3171 enterbirdseye ();
3172 | _ -> ();
3174 with exc ->
3175 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3177 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3179 | 'R' ->
3180 let ondone s =
3181 match try
3182 Some (int_of_string s)
3183 with exc ->
3184 state.text <- Printf.sprintf "bad integer `%s': %s"
3185 s (exntos exc);
3186 None
3187 with
3188 | Some angle -> reqlayout angle conf.fitmodel
3189 | None -> ()
3191 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3193 | 'i' ->
3194 conf.icase <- not conf.icase;
3195 TEdone ("case insensitive search " ^ (btos conf.icase))
3197 | 'p' ->
3198 conf.preload <- not conf.preload;
3199 gotoy state.y;
3200 TEdone ("preload " ^ (btos conf.preload))
3202 | 'v' ->
3203 conf.verbose <- not conf.verbose;
3204 TEdone ("verbose " ^ (btos conf.verbose))
3206 | 'd' ->
3207 conf.debug <- not conf.debug;
3208 TEdone ("debug " ^ (btos conf.debug))
3210 | 'h' ->
3211 conf.maxhfit <- not conf.maxhfit;
3212 state.maxy <- calcheight ();
3213 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3215 | 'c' ->
3216 conf.crophack <- not conf.crophack;
3217 TEdone ("crophack " ^ btos conf.crophack)
3219 | 'a' ->
3220 let s =
3221 match conf.maxwait with
3222 | None ->
3223 conf.maxwait <- Some infinity;
3224 "always wait for page to complete"
3225 | Some _ ->
3226 conf.maxwait <- None;
3227 "show placeholder if page is not ready"
3229 TEdone s
3231 | 'f' ->
3232 conf.underinfo <- not conf.underinfo;
3233 TEdone ("underinfo " ^ btos conf.underinfo)
3235 | 'P' ->
3236 conf.savebmarks <- not conf.savebmarks;
3237 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3239 | 'S' ->
3240 let ondone s =
3242 let pageno, py =
3243 match state.layout with
3244 | [] -> 0, 0
3245 | l :: _ ->
3246 l.pageno, l.pagey
3248 conf.interpagespace <- int_of_string s;
3249 docolumns conf.columns;
3250 state.maxy <- calcheight ();
3251 let y = getpagey pageno in
3252 gotoy (y + py)
3253 with exc ->
3254 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3256 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3258 | 'l' ->
3259 let fm =
3260 match conf.fitmodel with
3261 | FitProportional -> FitWidth
3262 | _ -> FitProportional
3264 reqlayout conf.angle fm;
3265 TEdone ("proportional display " ^ btos (fm == FitProportional))
3267 | 'T' ->
3268 settrim (not conf.trimmargins) conf.trimfuzz;
3269 TEdone ("trim margins " ^ btos conf.trimmargins)
3271 | 'I' ->
3272 conf.invert <- not conf.invert;
3273 TEdone ("invert colors " ^ btos conf.invert)
3275 | 'x' ->
3276 let ondone s =
3277 cbput state.hists.sel s;
3278 conf.selcmd <- s;
3280 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3281 textentry, ondone, true)
3283 | 'M' ->
3284 if conf.pax == None
3285 then conf.pax <- Some (ref (0.0, 0, 0))
3286 else conf.pax <- None;
3287 TEdone ("PAX " ^ btos (conf.pax != None))
3289 | _ ->
3290 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3291 TEstop
3292 else
3293 TEcont state.text
3296 class type lvsource = object
3297 method getitemcount : int
3298 method getitem : int -> (string * int)
3299 method hasaction : int -> bool
3300 method exit :
3301 uioh:uioh ->
3302 cancel:bool ->
3303 active:int ->
3304 first:int ->
3305 pan:int ->
3306 qsearch:string ->
3307 uioh option
3308 method getactive : int
3309 method getfirst : int
3310 method getqsearch : string
3311 method setqsearch : string -> unit
3312 method getpan : int
3313 end;;
3315 class virtual lvsourcebase = object
3316 val mutable m_active = 0
3317 val mutable m_first = 0
3318 val mutable m_qsearch = ""
3319 val mutable m_pan = 0
3320 method getactive = m_active
3321 method getfirst = m_first
3322 method getqsearch = m_qsearch
3323 method getpan = m_pan
3324 method setqsearch s = m_qsearch <- s
3325 end;;
3327 let withoutlastutf8 s =
3328 let len = String.length s in
3329 if len = 0
3330 then s
3331 else
3332 let rec find pos =
3333 if pos = 0
3334 then pos
3335 else
3336 let b = Char.code s.[pos] in
3337 if b land 0b11000000 = 0b11000000
3338 then pos
3339 else find (pos-1)
3341 let first =
3342 if Char.code s.[len-1] land 0x80 = 0
3343 then len-1
3344 else find (len-1)
3346 String.sub s 0 first;
3349 let textentrykeyboard
3350 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3351 let key =
3352 if key >= 0xffb0 && key <= 0xffb9
3353 then key - 0xffb0 + 48 else key
3355 let enttext te =
3356 state.mode <- Textentry (te, onleave);
3357 state.text <- "";
3358 enttext ();
3359 G.postRedisplay "textentrykeyboard enttext";
3361 let histaction cmd =
3362 match opthist with
3363 | None -> ()
3364 | Some (action, _) ->
3365 state.mode <- Textentry (
3366 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3368 G.postRedisplay "textentry histaction"
3370 match key with
3371 | 0xff08 -> (* backspace *)
3372 if emptystr text && cancelonempty
3373 then (
3374 onleave Cancel;
3375 G.postRedisplay "textentrykeyboard after cancel";
3377 else
3378 let s = withoutlastutf8 text in
3379 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3381 | 0xff0d | 0xff8d -> (* (kp) enter *)
3382 ondone text;
3383 onleave Confirm;
3384 G.postRedisplay "textentrykeyboard after confirm"
3386 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3387 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3388 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3389 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3391 | 0xff1b -> (* escape*)
3392 if emptystr text
3393 then (
3394 begin match opthist with
3395 | None -> ()
3396 | Some (_, onhistcancel) -> onhistcancel ()
3397 end;
3398 onleave Cancel;
3399 state.text <- "";
3400 G.postRedisplay "textentrykeyboard after cancel2"
3402 else (
3403 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3406 | 0xff9f | 0xffff -> () (* delete *)
3408 | _ when key != 0
3409 && key land 0xff00 != 0xff00 (* keyboard *)
3410 && key land 0xfe00 != 0xfe00 (* xkb *)
3411 && key land 0xfd00 != 0xfd00 (* 3270 *)
3413 begin match onkey text key with
3414 | TEdone text ->
3415 ondone text;
3416 onleave Confirm;
3417 G.postRedisplay "textentrykeyboard after confirm2";
3419 | TEcont text ->
3420 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3422 | TEstop ->
3423 onleave Cancel;
3424 G.postRedisplay "textentrykeyboard after cancel3"
3426 | TEswitch te ->
3427 state.mode <- Textentry (te, onleave);
3428 G.postRedisplay "textentrykeyboard switch";
3429 end;
3431 | _ ->
3432 vlog "unhandled key %s" (Wsi.keyname key)
3435 let firstof first active =
3436 if first > active || abs (first - active) > fstate.maxrows - 1
3437 then max 0 (active - (fstate.maxrows/2))
3438 else first
3441 let calcfirst first active =
3442 if active > first
3443 then
3444 let rows = active - first in
3445 if rows > fstate.maxrows then active - fstate.maxrows else first
3446 else active
3449 let scrollph y maxy =
3450 let sh = float (maxy + state.winh) /. float state.winh in
3451 let sh = float state.winh /. sh in
3452 let sh = max sh (float conf.scrollh) in
3454 let percent = float y /. float maxy in
3455 let position = (float state.winh -. sh) *. percent in
3457 let position =
3458 if position +. sh > float state.winh
3459 then float state.winh -. sh
3460 else position
3462 position, sh;
3465 let coe s = (s :> uioh);;
3467 class listview ~(source:lvsource) ~trusted ~modehash =
3468 object (self)
3469 val m_pan = source#getpan
3470 val m_first = source#getfirst
3471 val m_active = source#getactive
3472 val m_qsearch = source#getqsearch
3473 val m_prev_uioh = state.uioh
3475 method private elemunder y =
3476 let n = y / (fstate.fontsize+1) in
3477 if m_first + n < source#getitemcount
3478 then (
3479 if source#hasaction (m_first + n)
3480 then Some (m_first + n)
3481 else None
3483 else None
3485 method display =
3486 Gl.enable `blend;
3487 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3488 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3489 filledrect 0. 0. (float state.winw) (float state.winh);
3490 GlDraw.color (1., 1., 1.);
3491 Gl.enable `texture_2d;
3492 let fs = fstate.fontsize in
3493 let nfs = fs + 1 in
3494 let ww = fstate.wwidth in
3495 let tabw = 30.0*.ww in
3496 let itemcount = source#getitemcount in
3497 let rec loop row =
3498 if (row - m_first) > fstate.maxrows
3499 then ()
3500 else (
3501 if row >= 0 && row < itemcount
3502 then (
3503 let (s, level) = source#getitem row in
3504 let y = (row - m_first) * nfs in
3505 let x = 5.0 +. float (level + m_pan) *. ww in
3506 if row = m_active
3507 then (
3508 Gl.disable `texture_2d;
3509 let alpha = if source#hasaction row then 0.9 else 0.3 in
3510 GlDraw.color (1., 1., 1.) ~alpha;
3511 linerect 1. (float (y + 1))
3512 (float (state.winw - conf.scrollbw - 1)) (float (y + fs + 3));
3513 GlDraw.color (1., 1., 1.);
3514 Gl.enable `texture_2d;
3517 let drawtabularstring s =
3518 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3519 if trusted
3520 then
3521 let tabpos = try String.index s '\t' with Not_found -> -1 in
3522 if tabpos > 0
3523 then
3524 let len = String.length s - tabpos - 1 in
3525 let s1 = String.sub s 0 tabpos
3526 and s2 = String.sub s (tabpos + 1) len in
3527 let nx = drawstr x s1 in
3528 let sw = nx -. x in
3529 let x = x +. (max tabw sw) in
3530 drawstr x s2
3531 else
3532 drawstr x s
3533 else
3534 drawstr x s
3536 let _ = drawtabularstring s in
3537 loop (row+1)
3541 loop m_first;
3542 Gl.disable `blend;
3543 Gl.disable `texture_2d;
3545 method updownlevel incr =
3546 let len = source#getitemcount in
3547 let curlevel =
3548 if m_active >= 0 && m_active < len
3549 then snd (source#getitem m_active)
3550 else -1
3552 let rec flow i =
3553 if i = len then i-1 else if i = -1 then 0 else
3554 let _, l = source#getitem i in
3555 if l != curlevel then i else flow (i+incr)
3557 let active = flow m_active in
3558 let first = calcfirst m_first active in
3559 G.postRedisplay "outline updownlevel";
3560 {< m_active = active; m_first = first >}
3562 method private key1 key mask =
3563 let set1 active first qsearch =
3564 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3566 let search active pattern incr =
3567 let active = if active = -1 then m_first else active in
3568 let dosearch re =
3569 let rec loop n =
3570 if n >= 0 && n < source#getitemcount
3571 then (
3572 let s, _ = source#getitem n in
3574 (try ignore (Str.search_forward re s 0); true
3575 with Not_found -> false)
3576 then Some n
3577 else loop (n + incr)
3579 else None
3581 loop active
3584 let re = Str.regexp_case_fold pattern in
3585 dosearch re
3586 with Failure s ->
3587 state.text <- s;
3588 None
3590 let itemcount = source#getitemcount in
3591 let find start incr =
3592 let rec find i =
3593 if i = -1 || i = itemcount
3594 then -1
3595 else (
3596 if source#hasaction i
3597 then i
3598 else find (i + incr)
3601 find start
3603 let set active first =
3604 let first = bound first 0 (itemcount - fstate.maxrows) in
3605 state.text <- "";
3606 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3608 let navigate incr =
3609 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3610 let active, first =
3611 let incr1 = if incr > 0 then 1 else -1 in
3612 if isvisible m_first m_active
3613 then
3614 let next =
3615 let next = m_active + incr in
3616 let next =
3617 if next < 0 || next >= itemcount
3618 then -1
3619 else find next incr1
3621 if abs (m_active - next) > fstate.maxrows
3622 then -1
3623 else next
3625 if next = -1
3626 then
3627 let first = m_first + incr in
3628 let first = bound first 0 (itemcount - fstate.maxrows) in
3629 let next =
3630 let next = m_active + incr in
3631 let next = bound next 0 (itemcount - 1) in
3632 find next ~-incr1
3634 let active =
3635 if next = -1
3636 then m_active
3637 else (
3638 if isvisible first next
3639 then next
3640 else m_active
3643 active, first
3644 else
3645 let first = min next m_first in
3646 let first =
3647 if abs (next - first) > fstate.maxrows
3648 then first + incr
3649 else first
3651 next, first
3652 else
3653 let first = m_first + incr in
3654 let first = bound first 0 (itemcount - 1) in
3655 let active =
3656 let next = m_active + incr in
3657 let next = bound next 0 (itemcount - 1) in
3658 let next = find next incr1 in
3659 let active =
3660 if next = -1 || abs (m_active - first) > fstate.maxrows
3661 then (
3662 let active = if m_active = -1 then next else m_active in
3663 active
3665 else next
3667 if isvisible first active
3668 then active
3669 else -1
3671 active, first
3673 G.postRedisplay "listview navigate";
3674 set active first;
3676 match key with
3677 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3678 let incr = if key = 0x72 then -1 else 1 in
3679 let active, first =
3680 match search (m_active + incr) m_qsearch incr with
3681 | None ->
3682 state.text <- m_qsearch ^ " [not found]";
3683 m_active, m_first
3684 | Some active ->
3685 state.text <- m_qsearch;
3686 active, firstof m_first active
3688 G.postRedisplay "listview ctrl-r/s";
3689 set1 active first m_qsearch;
3691 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3692 if m_active >= 0 && m_active < source#getitemcount
3693 then (
3694 let s, _ = source#getitem m_active in
3695 selstring s;
3697 coe self
3699 | 0xff08 -> (* backspace *)
3700 if emptystr m_qsearch
3701 then coe self
3702 else (
3703 let qsearch = withoutlastutf8 m_qsearch in
3704 if emptystr qsearch
3705 then (
3706 state.text <- "";
3707 G.postRedisplay "listview empty qsearch";
3708 set1 m_active m_first "";
3710 else
3711 let active, first =
3712 match search m_active qsearch ~-1 with
3713 | None ->
3714 state.text <- qsearch ^ " [not found]";
3715 m_active, m_first
3716 | Some active ->
3717 state.text <- qsearch;
3718 active, firstof m_first active
3720 G.postRedisplay "listview backspace qsearch";
3721 set1 active first qsearch
3724 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3725 let pattern = m_qsearch ^ toutf8 key in
3726 let active, first =
3727 match search m_active pattern 1 with
3728 | None ->
3729 state.text <- pattern ^ " [not found]";
3730 m_active, m_first
3731 | Some active ->
3732 state.text <- pattern;
3733 active, firstof m_first active
3735 G.postRedisplay "listview qsearch add";
3736 set1 active first pattern;
3738 | 0xff1b -> (* escape *)
3739 state.text <- "";
3740 if emptystr m_qsearch
3741 then (
3742 G.postRedisplay "list view escape";
3743 begin
3744 match
3745 source#exit (coe self) true m_active m_first m_pan m_qsearch
3746 with
3747 | None -> m_prev_uioh
3748 | Some uioh -> uioh
3751 else (
3752 G.postRedisplay "list view kill qsearch";
3753 source#setqsearch "";
3754 coe {< m_qsearch = "" >}
3757 | 0xff0d | 0xff8d -> (* (kp) enter *)
3758 state.text <- "";
3759 let self = {< m_qsearch = "" >} in
3760 source#setqsearch "";
3761 let opt =
3762 G.postRedisplay "listview enter";
3763 if m_active >= 0 && m_active < source#getitemcount
3764 then (
3765 source#exit (coe self) false m_active m_first m_pan "";
3767 else (
3768 source#exit (coe self) true m_active m_first m_pan "";
3771 begin match opt with
3772 | None -> m_prev_uioh
3773 | Some uioh -> uioh
3776 | 0xff9f | 0xffff -> (* (kp) delete *)
3777 coe self
3779 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3780 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3781 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3782 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3784 | 0xff53 | 0xff98 -> (* (kp) right *)
3785 state.text <- "";
3786 G.postRedisplay "listview right";
3787 coe {< m_pan = m_pan - 1 >}
3789 | 0xff51 | 0xff96 -> (* (kp) left *)
3790 state.text <- "";
3791 G.postRedisplay "listview left";
3792 coe {< m_pan = m_pan + 1 >}
3794 | 0xff50 | 0xff95 -> (* (kp) home *)
3795 let active = find 0 1 in
3796 G.postRedisplay "listview home";
3797 set active 0;
3799 | 0xff57 | 0xff9c -> (* (kp) end *)
3800 let first = max 0 (itemcount - fstate.maxrows) in
3801 let active = find (itemcount - 1) ~-1 in
3802 G.postRedisplay "listview end";
3803 set active first;
3805 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3806 coe self
3808 | _ ->
3809 dolog "listview unknown key %#x" key; coe self
3811 method key key mask =
3812 match state.mode with
3813 | Textentry te -> textentrykeyboard key mask te; coe self
3814 | _ -> self#key1 key mask
3816 method button button down x y _ =
3817 let opt =
3818 match button with
3819 | 1 when x > state.winw - conf.scrollbw ->
3820 G.postRedisplay "listview scroll";
3821 if down
3822 then
3823 let _, position, sh = self#scrollph in
3824 if y > truncate position && y < truncate (position +. sh)
3825 then (
3826 state.mstate <- Mscrolly;
3827 Some (coe self)
3829 else
3830 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3831 let first = truncate (s *. float source#getitemcount) in
3832 let first = min source#getitemcount first in
3833 Some (coe {< m_first = first; m_active = first >})
3834 else (
3835 state.mstate <- Mnone;
3836 Some (coe self);
3838 | 1 when not down ->
3839 begin match self#elemunder y with
3840 | Some n ->
3841 G.postRedisplay "listview click";
3842 source#exit
3843 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3844 | _ ->
3845 Some (coe self)
3847 | n when (n == 4 || n == 5) && not down ->
3848 let len = source#getitemcount in
3849 let first =
3850 if n = 5 && m_first + fstate.maxrows >= len
3851 then
3852 m_first
3853 else
3854 let first = m_first + (if n == 4 then -1 else 1) in
3855 bound first 0 (len - 1)
3857 G.postRedisplay "listview wheel";
3858 Some (coe {< m_first = first >})
3859 | n when (n = 6 || n = 7) && not down ->
3860 let inc = if n = 7 then -1 else 1 in
3861 G.postRedisplay "listview hwheel";
3862 Some (coe {< m_pan = m_pan + inc >})
3863 | _ ->
3864 Some (coe self)
3866 match opt with
3867 | None -> m_prev_uioh
3868 | Some uioh -> uioh
3870 method motion _ y =
3871 match state.mstate with
3872 | Mscrolly ->
3873 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3874 let first = truncate (s *. float source#getitemcount) in
3875 let first = min source#getitemcount first in
3876 G.postRedisplay "listview motion";
3877 coe {< m_first = first; m_active = first >}
3878 | _ -> coe self
3880 method pmotion x y =
3881 if x < state.winw - conf.scrollbw
3882 then
3883 let n =
3884 match self#elemunder y with
3885 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3886 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3888 let o =
3889 if n != m_active
3890 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3891 else self
3893 coe o
3894 else (
3895 Wsi.setcursor Wsi.CURSOR_INHERIT;
3896 coe self
3899 method infochanged _ = ()
3901 method scrollpw = (0, 0.0, 0.0)
3902 method scrollph =
3903 let nfs = fstate.fontsize + 1 in
3904 let y = m_first * nfs in
3905 let itemcount = source#getitemcount in
3906 let maxi = max 0 (itemcount - fstate.maxrows) in
3907 let maxy = maxi * nfs in
3908 let p, h = scrollph y maxy in
3909 conf.scrollbw, p, h
3911 method modehash = modehash
3912 method eformsgs = false
3913 end;;
3915 class outlinelistview ~source =
3916 let settext autonarrow s =
3917 if autonarrow
3918 then state.text <- "[" ^ s ^ "]"
3919 else state.text <- s
3921 object (self)
3922 inherit listview
3923 ~source:(source :> lvsource)
3924 ~trusted:false
3925 ~modehash:(findkeyhash conf "outline")
3926 as super
3928 val m_autonarrow = false
3930 method key key mask =
3931 let maxrows =
3932 if emptystr state.text
3933 then fstate.maxrows
3934 else fstate.maxrows - 2
3936 let calcfirst first active =
3937 if active > first
3938 then
3939 let rows = active - first in
3940 if rows > maxrows then active - maxrows else first
3941 else active
3943 let navigate incr =
3944 let active = m_active + incr in
3945 let active = bound active 0 (source#getitemcount - 1) in
3946 let first = calcfirst m_first active in
3947 G.postRedisplay "outline navigate";
3948 coe {< m_active = active; m_first = first >}
3950 let navscroll first =
3951 let active =
3952 let dist = m_active - first in
3953 if dist < 0
3954 then first
3955 else (
3956 if dist < maxrows
3957 then m_active
3958 else first + maxrows
3961 G.postRedisplay "outline navscroll";
3962 coe {< m_first = first; m_active = active >}
3964 let ctrl = Wsi.withctrl mask in
3965 match key with
3966 | 97 when ctrl -> (* ctrl-a *)
3967 if m_autonarrow
3968 then source#denarrow
3969 else source#narrow m_qsearch;
3970 settext (not m_autonarrow) m_qsearch;
3971 G.postRedisplay "toggle auto narrowing";
3972 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3974 | 47 when emptystr m_qsearch && not m_autonarrow -> (* / *)
3975 settext true "";
3976 G.postRedisplay "toggle auto narrowing";
3977 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3979 | 110 when ctrl -> (* ctrl-n *)
3980 source#narrow m_qsearch;
3981 if not m_autonarrow
3982 then source#add_narrow_pattern m_qsearch;
3983 G.postRedisplay "outline ctrl-n";
3984 coe {< m_first = 0; m_active = 0 >}
3986 | 115 when ctrl -> (* ctrl-s *)
3987 let active = source#calcactive (getanchor ()) in
3988 let first = firstof m_first active in
3989 G.postRedisplay "outline ctrl-s";
3990 coe {< m_first = first; m_active = active >}
3992 | 117 when ctrl -> (* ctrl-u *)
3993 source#del_narrow_pattern;
3994 let pattern = source#renarrow in
3995 G.postRedisplay "outline ctrl-u";
3996 let text =
3997 if emptystr pattern then "" else "Narrowed to " ^ pattern
3999 settext m_autonarrow text;
4000 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
4002 | 108 when ctrl -> (* ctrl-l *)
4003 let first = max 0 (m_active - (fstate.maxrows / 2)) in
4004 G.postRedisplay "outline ctrl-l";
4005 coe {< m_first = first >}
4007 | 0xff1b -> (* escape *)
4008 let o = super#key key mask in
4009 if m_autonarrow
4010 then (
4011 if nonemptystr m_qsearch
4012 then (
4013 source#add_narrow_pattern m_qsearch;
4014 settext true "";
4019 | 0xff0d | 0xff8d when m_autonarrow -> (* (kp) enter *)
4020 if nonemptystr m_qsearch
4021 then source#add_narrow_pattern m_qsearch;
4022 super#key key mask
4024 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
4025 let pattern = m_qsearch ^ toutf8 key in
4026 G.postRedisplay "outlinelistview autonarrow add";
4027 source#narrow pattern;
4028 settext true pattern;
4029 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4031 | key when m_autonarrow && key = 0xff08 -> (* backspace *)
4032 if emptystr m_qsearch
4033 then coe self
4034 else
4035 let pattern = withoutlastutf8 m_qsearch in
4036 G.postRedisplay "outlinelistview autonarrow backspace";
4037 ignore (source#renarrow);
4038 source#narrow pattern;
4039 settext true pattern;
4040 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4042 | 0xff9f | 0xffff -> (* (kp) delete *)
4043 source#remove m_active;
4044 G.postRedisplay "outline delete";
4045 let active = max 0 (m_active-1) in
4046 coe {< m_first = firstof m_first active;
4047 m_active = active >}
4049 | 0xff52 | 0xff97 when ctrl -> (* ctrl-(kp) up *)
4050 navscroll (max 0 (m_first - 1))
4052 | 0xff54 | 0xff99 when ctrl -> (* ctrl-(kp) down *)
4053 navscroll (min (source#getitemcount - 1) (m_first + 1))
4055 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
4056 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
4057 | 0xff55 | 0xff9a -> (* (kp) prior *)
4058 navigate ~-(fstate.maxrows)
4059 | 0xff56 | 0xff9b -> (* (kp) next *)
4060 navigate fstate.maxrows
4062 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
4063 let o =
4064 if ctrl
4065 then (
4066 G.postRedisplay "outline ctrl right";
4067 {< m_pan = m_pan + 1 >}
4069 else self#updownlevel 1
4071 coe o
4073 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
4074 let o =
4075 if ctrl
4076 then (
4077 G.postRedisplay "outline ctrl left";
4078 {< m_pan = m_pan - 1 >}
4080 else self#updownlevel ~-1
4082 coe o
4084 | 0xff50 | 0xff95 -> (* (kp) home *)
4085 G.postRedisplay "outline home";
4086 coe {< m_first = 0; m_active = 0 >}
4088 | 0xff57 | 0xff9c -> (* (kp) end *)
4089 let active = source#getitemcount - 1 in
4090 let first = max 0 (active - fstate.maxrows) in
4091 G.postRedisplay "outline end";
4092 coe {< m_active = active; m_first = first >}
4094 | _ -> super#key key mask
4097 let gotounder under =
4098 let getpath filename =
4099 let path =
4100 if nonemptystr filename
4101 then
4102 if Filename.is_relative filename
4103 then
4104 let dir = Filename.dirname state.path in
4105 let dir =
4106 if Filename.is_implicit dir
4107 then Filename.concat (Sys.getcwd ()) dir
4108 else dir
4110 Filename.concat dir filename
4111 else filename
4112 else ""
4114 if Sys.file_exists path
4115 then path
4116 else ""
4118 match under with
4119 | Ulinkgoto (pageno, top) ->
4120 if pageno >= 0
4121 then (
4122 addnav ();
4123 gotopage1 pageno top;
4126 | Ulinkuri s ->
4127 gotouri s
4129 | Uremote (filename, pageno) ->
4130 let path = getpath filename in
4131 if nonemptystr path
4132 then (
4133 if conf.riani
4134 then
4135 let command = !selfexec ^ " " ^ path in
4136 try popen command []
4137 with exn ->
4138 Printf.eprintf
4139 "failed to execute `%s': %s\n" command (exntos exn);
4140 flush stderr;
4141 else
4142 let anchor = getanchor () in
4143 let ranchor = state.path, state.password, anchor, state.origin in
4144 state.origin <- "";
4145 state.anchor <- (pageno, 0.0, 0.0);
4146 state.ranchors <- ranchor :: state.ranchors;
4147 opendoc path "";
4149 else showtext '!' ("Could not find " ^ filename)
4151 | Uremotedest (filename, destname) ->
4152 let path = getpath filename in
4153 if nonemptystr path
4154 then (
4155 if conf.riani
4156 then
4157 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4158 try popen command []
4159 with exn ->
4160 Printf.eprintf
4161 "failed to execute `%s': %s\n" command (exntos exn);
4162 flush stderr;
4163 else
4164 let anchor = getanchor () in
4165 let ranchor = state.path, state.password, anchor, state.origin in
4166 state.origin <- "";
4167 state.nameddest <- destname;
4168 state.ranchors <- ranchor :: state.ranchors;
4169 opendoc path "";
4171 else showtext '!' ("Could not find " ^ filename)
4173 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4176 let gotooutline (_, _, kind) =
4177 match kind with
4178 | Onone -> ()
4179 | Oanchor anchor ->
4180 let (pageno, y, _) = anchor in
4181 let y = getanchory
4182 (if conf.presentation then (pageno, y, 1.0) else anchor)
4184 gotoghyll y
4185 | Ouri uri -> gotounder (Ulinkuri uri)
4186 | Olaunch cmd -> gotounder (Ulaunch cmd)
4187 | Oremote remote -> gotounder (Uremote remote)
4188 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4191 let outlinesource usebookmarks =
4192 let empty = [||] in
4193 (object (self)
4194 inherit lvsourcebase
4195 val mutable m_items = empty
4196 val mutable m_orig_items = empty
4197 val mutable m_narrow_patterns = []
4198 val mutable m_hadremovals = false
4200 method getitemcount =
4201 Array.length m_items + (if m_hadremovals then 1 else 0)
4203 method getitem n =
4204 if n == Array.length m_items && m_hadremovals
4205 then
4206 ("[Confirm removal]", 0)
4207 else
4208 let s, n, _ = m_items.(n) in
4209 (s, n)
4211 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4212 ignore (uioh, first, qsearch);
4213 let confrimremoval = m_hadremovals && active = Array.length m_items in
4214 let items =
4215 if m_narrow_patterns = []
4216 then m_orig_items
4217 else m_items
4219 if not cancel
4220 then (
4221 if not confrimremoval
4222 then (
4223 gotooutline m_items.(active);
4224 m_items <- items;
4226 else (
4227 state.bookmarks <- Array.to_list m_items;
4228 m_orig_items <- m_items;
4231 else m_items <- items;
4232 m_pan <- pan;
4233 None
4235 method hasaction _ = true
4237 method greetmsg =
4238 if Array.length m_items != Array.length m_orig_items
4239 then
4240 let s =
4241 match m_narrow_patterns with
4242 | one :: [] -> one
4243 | many -> String.concat "\xe2\x80\xa6" (List.rev many)
4245 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4246 else ""
4248 method narrow pattern =
4249 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4250 match reopt with
4251 | None -> ()
4252 | Some re ->
4253 let rec loop accu n =
4254 if n = -1
4255 then m_items <- Array.of_list accu
4256 else
4257 let (s, _, _) as o = m_items.(n) in
4258 let accu =
4259 if (try ignore (Str.search_forward re s 0); true
4260 with Not_found -> false)
4261 then o :: accu
4262 else accu
4264 loop accu (n-1)
4266 loop [] (Array.length m_items - 1)
4268 method denarrow =
4269 m_orig_items <- (
4270 if usebookmarks
4271 then Array.of_list state.bookmarks
4272 else state.outlines
4274 m_items <- m_orig_items
4276 method remove m =
4277 if usebookmarks
4278 then
4279 if m >= 0 && m < Array.length m_items
4280 then (
4281 m_hadremovals <- true;
4282 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4283 let n = if n >= m then n+1 else n in
4284 m_items.(n)
4288 method add_narrow_pattern pattern =
4289 m_narrow_patterns <- pattern :: m_narrow_patterns
4291 method del_narrow_pattern =
4292 match m_narrow_patterns with
4293 | _ :: rest -> m_narrow_patterns <- rest
4294 | [] -> ()
4296 method renarrow =
4297 self#denarrow;
4298 match m_narrow_patterns with
4299 | pattern :: [] -> self#narrow pattern; pattern
4300 | list ->
4301 List.fold_left (fun accu pattern ->
4302 self#narrow pattern;
4303 pattern ^ "\xe2\x80\xa6" ^ accu) "" list
4305 method calcactive anchor =
4306 let rely = getanchory anchor in
4307 let rec loop n best bestd =
4308 if n = Array.length m_items
4309 then best
4310 else
4311 let _, _, kind = m_items.(n) in
4312 match kind with
4313 | Oanchor anchor ->
4314 let orely = getanchory anchor in
4315 let d = abs (orely - rely) in
4316 if d < bestd
4317 then loop (n+1) n d
4318 else loop (n+1) best bestd
4319 | Onone | Oremote _ | Olaunch _ | Oremotedest _ | Ouri _ ->
4320 loop (n+1) best bestd
4322 loop 0 ~-1 max_int
4324 method reset anchor items =
4325 m_hadremovals <- false;
4326 if m_orig_items == empty
4327 then (
4328 m_orig_items <- items;
4329 if m_narrow_patterns == []
4330 then m_items <- items;
4332 let active = self#calcactive anchor in
4333 m_active <- active;
4334 m_first <- firstof m_first active
4335 end)
4338 let enterselector usebookmarks =
4339 let source = outlinesource usebookmarks in
4340 fun errmsg ->
4341 let outlines =
4342 if usebookmarks
4343 then Array.of_list state.bookmarks
4344 else state.outlines
4346 if Array.length outlines = 0
4347 then (
4348 showtext ' ' errmsg;
4350 else (
4351 state.text <- source#greetmsg;
4352 Wsi.setcursor Wsi.CURSOR_INHERIT;
4353 let anchor = getanchor () in
4354 source#reset anchor outlines;
4355 state.uioh <- coe (new outlinelistview ~source);
4356 G.postRedisplay "enter selector";
4360 let enteroutlinemode =
4361 let f = enterselector false in
4362 fun ()-> f "Document has no outline";
4365 let enterbookmarkmode =
4366 let f = enterselector true in
4367 fun () -> f "Document has no bookmarks (yet)";
4370 let color_of_string s =
4371 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4372 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4376 let color_to_string (r, g, b) =
4377 let r = truncate (r *. 256.0)
4378 and g = truncate (g *. 256.0)
4379 and b = truncate (b *. 256.0) in
4380 Printf.sprintf "%d/%d/%d" r g b
4383 let irect_of_string s =
4384 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4387 let irect_to_string (x0,y0,x1,y1) =
4388 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4391 let makecheckers () =
4392 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4393 following to say:
4394 converted by Issac Trotts. July 25, 2002 *)
4395 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4396 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4397 let id = GlTex.gen_texture () in
4398 GlTex.bind_texture `texture_2d id;
4399 GlPix.store (`unpack_alignment 1);
4400 GlTex.image2d image;
4401 List.iter (GlTex.parameter ~target:`texture_2d)
4402 [ `mag_filter `nearest; `min_filter `nearest ];
4406 let setcheckers enabled =
4407 match state.texid with
4408 | None ->
4409 if enabled then state.texid <- Some (makecheckers ())
4411 | Some texid ->
4412 if not enabled
4413 then (
4414 GlTex.delete_texture texid;
4415 state.texid <- None;
4419 let int_of_string_with_suffix s =
4420 let l = String.length s in
4421 let s1, shift =
4422 if l > 1
4423 then
4424 let suffix = Char.lowercase s.[l-1] in
4425 match suffix with
4426 | 'k' -> String.sub s 0 (l-1), 10
4427 | 'm' -> String.sub s 0 (l-1), 20
4428 | 'g' -> String.sub s 0 (l-1), 30
4429 | _ -> s, 0
4430 else s, 0
4432 let n = int_of_string s1 in
4433 let m = n lsl shift in
4434 if m < 0 || m < n
4435 then raise (Failure "value too large")
4436 else m
4439 let string_with_suffix_of_int n =
4440 if n = 0
4441 then "0"
4442 else
4443 let units = [(30, "G"); (20, "M"); (10, "K")] in
4444 let prettyint n =
4445 let rec loop s n =
4446 let h = n mod 1000 in
4447 let n = n / 1000 in
4448 if n = 0
4449 then string_of_int h ^ s
4450 else (
4451 let s = Printf.sprintf "_%03d%s" h s in
4452 loop s n
4455 loop "" n
4457 let rec find = function
4458 | [] -> prettyint n
4459 | (shift, suffix) :: rest ->
4460 if (n land ((1 lsl shift) - 1)) = 0
4461 then prettyint (n lsr shift) ^ suffix
4462 else find rest
4464 find units
4467 let defghyllscroll = (40, 8, 32);;
4468 let fastghyllscroll = (5,1,2);;
4469 let neatghyllscroll = (10,1,9);;
4470 let ghyllscroll_of_string s =
4471 match s with
4472 | "default" -> Some defghyllscroll
4473 | "fast" -> Some (5,1,2)
4474 | "neat" -> Some (10,1,9)
4475 | "" | "none" -> None
4476 | _ ->
4477 let (n,a,b) as nab =
4478 Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b) in
4479 if n <= a || n <= b || a >= b
4480 then error "invalid ghyll N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)"
4481 n a b;
4482 Some nab
4485 let ghyllscroll_to_string ((n, a, b) as nab) =
4486 (**) if nab = defghyllscroll then "default"
4487 else if nab = fastghyllscroll then "fast"
4488 else if nab = neatghyllscroll then "neat"
4489 else Printf.sprintf "%d,%d,%d" n a b;
4492 let describe_location () =
4493 let fn = page_of_y state.y in
4494 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4495 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4496 let percent =
4497 if maxy <= 0
4498 then 100.
4499 else (100. *. (float state.y /. float maxy))
4501 if fn = ln
4502 then
4503 Printf.sprintf "page %d of %d [%.2f%%]"
4504 (fn+1) state.pagecount percent
4505 else
4506 Printf.sprintf
4507 "pages %d-%d of %d [%.2f%%]"
4508 (fn+1) (ln+1) state.pagecount percent
4511 let setpresentationmode v =
4512 let n = page_of_y state.y in
4513 state.anchor <- (n, 0.0, 1.0);
4514 conf.presentation <- v;
4515 if conf.fitmodel = FitPage
4516 then reqlayout conf.angle conf.fitmodel;
4517 represent ();
4520 let enterinfomode =
4521 let btos b = if b then "\xe2\x88\x9a" else "" in
4522 let showextended = ref false in
4523 let leave mode = function
4524 | Confirm -> state.mode <- mode
4525 | Cancel -> state.mode <- mode in
4526 let src =
4527 (object
4528 val mutable m_first_time = true
4529 val mutable m_l = []
4530 val mutable m_a = [||]
4531 val mutable m_prev_uioh = nouioh
4532 val mutable m_prev_mode = View
4534 inherit lvsourcebase
4536 method reset prev_mode prev_uioh =
4537 m_a <- Array.of_list (List.rev m_l);
4538 m_l <- [];
4539 m_prev_mode <- prev_mode;
4540 m_prev_uioh <- prev_uioh;
4541 if m_first_time
4542 then (
4543 let rec loop n =
4544 if n >= Array.length m_a
4545 then ()
4546 else
4547 match m_a.(n) with
4548 | _, _, _, Action _ -> m_active <- n
4549 | _ -> loop (n+1)
4551 loop 0;
4552 m_first_time <- false;
4555 method int name get set =
4556 m_l <-
4557 (name, `int get, 1, Action (
4558 fun u ->
4559 let ondone s =
4560 try set (int_of_string s)
4561 with exn ->
4562 state.text <- Printf.sprintf "bad integer `%s': %s"
4563 s (exntos exn)
4565 state.text <- "";
4566 let te = name ^ ": ", "", None, intentry, ondone, true in
4567 state.mode <- Textentry (te, leave m_prev_mode);
4569 )) :: m_l
4571 method int_with_suffix name get set =
4572 m_l <-
4573 (name, `intws get, 1, Action (
4574 fun u ->
4575 let ondone s =
4576 try set (int_of_string_with_suffix s)
4577 with exn ->
4578 state.text <- Printf.sprintf "bad integer `%s': %s"
4579 s (exntos exn)
4581 state.text <- "";
4582 let te =
4583 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4585 state.mode <- Textentry (te, leave m_prev_mode);
4587 )) :: m_l
4589 method bool ?(offset=1) ?(btos=btos) name get set =
4590 m_l <-
4591 (name, `bool (btos, get), offset, Action (
4592 fun u ->
4593 let v = get () in
4594 set (not v);
4596 )) :: m_l
4598 method color name get set =
4599 m_l <-
4600 (name, `color get, 1, Action (
4601 fun u ->
4602 let invalid = (nan, nan, nan) in
4603 let ondone s =
4604 let c =
4605 try color_of_string s
4606 with exn ->
4607 state.text <- Printf.sprintf "bad color `%s': %s"
4608 s (exntos exn);
4609 invalid
4611 if c <> invalid
4612 then set c;
4614 let te = name ^ ": ", "", None, textentry, ondone, true in
4615 state.text <- color_to_string (get ());
4616 state.mode <- Textentry (te, leave m_prev_mode);
4618 )) :: m_l
4620 method string name get set =
4621 m_l <-
4622 (name, `string get, 1, Action (
4623 fun u ->
4624 let ondone s = set s in
4625 let te = name ^ ": ", "", None, textentry, ondone, true in
4626 state.mode <- Textentry (te, leave m_prev_mode);
4628 )) :: m_l
4630 method colorspace name get set =
4631 m_l <-
4632 (name, `string get, 1, Action (
4633 fun _ ->
4634 let source =
4635 (object
4636 inherit lvsourcebase
4638 initializer
4639 m_active <- CSTE.to_int conf.colorspace;
4640 m_first <- 0;
4642 method getitemcount =
4643 Array.length CSTE.names
4644 method getitem n =
4645 (CSTE.names.(n), 0)
4646 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4647 ignore (uioh, first, pan, qsearch);
4648 if not cancel then set active;
4649 None
4650 method hasaction _ = true
4651 end)
4653 state.text <- "";
4654 let modehash = findkeyhash conf "info" in
4655 coe (new listview ~source ~trusted:true ~modehash)
4656 )) :: m_l
4658 method paxmark name get set =
4659 m_l <-
4660 (name, `string get, 1, Action (
4661 fun _ ->
4662 let source =
4663 (object
4664 inherit lvsourcebase
4666 initializer
4667 m_active <- MTE.to_int conf.paxmark;
4668 m_first <- 0;
4670 method getitemcount = Array.length MTE.names
4671 method getitem n = (MTE.names.(n), 0)
4672 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4673 ignore (uioh, first, pan, qsearch);
4674 if not cancel then set active;
4675 None
4676 method hasaction _ = true
4677 end)
4679 state.text <- "";
4680 let modehash = findkeyhash conf "info" in
4681 coe (new listview ~source ~trusted:true ~modehash)
4682 )) :: m_l
4684 method fitmodel name get set =
4685 m_l <-
4686 (name, `string get, 1, Action (
4687 fun _ ->
4688 let source =
4689 (object
4690 inherit lvsourcebase
4692 initializer
4693 m_active <- FMTE.to_int conf.fitmodel;
4694 m_first <- 0;
4696 method getitemcount = Array.length FMTE.names
4697 method getitem n = (FMTE.names.(n), 0)
4698 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4699 ignore (uioh, first, pan, qsearch);
4700 if not cancel then set active;
4701 None
4702 method hasaction _ = true
4703 end)
4705 state.text <- "";
4706 let modehash = findkeyhash conf "info" in
4707 coe (new listview ~source ~trusted:true ~modehash)
4708 )) :: m_l
4710 method caption s offset =
4711 m_l <- (s, `empty, offset, Noaction) :: m_l
4713 method caption2 s f offset =
4714 m_l <- (s, `string f, offset, Noaction) :: m_l
4716 method getitemcount = Array.length m_a
4718 method getitem n =
4719 let tostr = function
4720 | `int f -> string_of_int (f ())
4721 | `intws f -> string_with_suffix_of_int (f ())
4722 | `string f -> f ()
4723 | `color f -> color_to_string (f ())
4724 | `bool (btos, f) -> btos (f ())
4725 | `empty -> ""
4727 let name, t, offset, _ = m_a.(n) in
4728 ((let s = tostr t in
4729 if nonemptystr s
4730 then Printf.sprintf "%s\t%s" name s
4731 else name),
4732 offset)
4734 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4735 let uiohopt =
4736 if not cancel
4737 then (
4738 m_qsearch <- qsearch;
4739 let uioh =
4740 match m_a.(active) with
4741 | _, _, _, Action f -> f uioh
4742 | _ -> uioh
4744 Some uioh
4746 else None
4748 m_active <- active;
4749 m_first <- first;
4750 m_pan <- pan;
4751 uiohopt
4753 method hasaction n =
4754 match m_a.(n) with
4755 | _, _, _, Action _ -> true
4756 | _ -> false
4757 end)
4759 let rec fillsrc prevmode prevuioh =
4760 let sep () = src#caption "" 0 in
4761 let colorp name get set =
4762 src#string name
4763 (fun () -> color_to_string (get ()))
4764 (fun v ->
4766 let c = color_of_string v in
4767 set c
4768 with exn ->
4769 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4772 let oldmode = state.mode in
4773 let birdseye = isbirdseye state.mode in
4775 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4777 src#bool "presentation mode"
4778 (fun () -> conf.presentation)
4779 (fun v -> setpresentationmode v);
4781 src#bool "ignore case in searches"
4782 (fun () -> conf.icase)
4783 (fun v -> conf.icase <- v);
4785 src#bool "preload"
4786 (fun () -> conf.preload)
4787 (fun v -> conf.preload <- v);
4789 src#bool "highlight links"
4790 (fun () -> conf.hlinks)
4791 (fun v -> conf.hlinks <- v);
4793 src#bool "under info"
4794 (fun () -> conf.underinfo)
4795 (fun v -> conf.underinfo <- v);
4797 src#bool "persistent bookmarks"
4798 (fun () -> conf.savebmarks)
4799 (fun v -> conf.savebmarks <- v);
4801 src#fitmodel "fit model"
4802 (fun () -> FMTE.to_string conf.fitmodel)
4803 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4805 src#bool "trim margins"
4806 (fun () -> conf.trimmargins)
4807 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4809 src#bool "persistent location"
4810 (fun () -> conf.jumpback)
4811 (fun v -> conf.jumpback <- v);
4813 sep ();
4814 src#int "inter-page space"
4815 (fun () -> conf.interpagespace)
4816 (fun n ->
4817 conf.interpagespace <- n;
4818 docolumns conf.columns;
4819 let pageno, py =
4820 match state.layout with
4821 | [] -> 0, 0
4822 | l :: _ ->
4823 l.pageno, l.pagey
4825 state.maxy <- calcheight ();
4826 let y = getpagey pageno in
4827 gotoy (y + py)
4830 src#int "page bias"
4831 (fun () -> conf.pagebias)
4832 (fun v -> conf.pagebias <- v);
4834 src#int "scroll step"
4835 (fun () -> conf.scrollstep)
4836 (fun n -> conf.scrollstep <- n);
4838 src#int "horizontal scroll step"
4839 (fun () -> conf.hscrollstep)
4840 (fun v -> conf.hscrollstep <- v);
4842 src#int "auto scroll step"
4843 (fun () ->
4844 match state.autoscroll with
4845 | Some step -> step
4846 | _ -> conf.autoscrollstep)
4847 (fun n ->
4848 if state.autoscroll <> None
4849 then state.autoscroll <- Some n;
4850 conf.autoscrollstep <- n);
4852 src#int "zoom"
4853 (fun () -> truncate (conf.zoom *. 100.))
4854 (fun v -> setzoom ((float v) /. 100.));
4856 src#int "rotation"
4857 (fun () -> conf.angle)
4858 (fun v -> reqlayout v conf.fitmodel);
4860 src#int "scroll bar width"
4861 (fun () -> conf.scrollbw)
4862 (fun v ->
4863 conf.scrollbw <- v;
4864 reshape state.winw state.winh;
4867 src#int "scroll handle height"
4868 (fun () -> conf.scrollh)
4869 (fun v -> conf.scrollh <- v;);
4871 src#int "thumbnail width"
4872 (fun () -> conf.thumbw)
4873 (fun v ->
4874 conf.thumbw <- min 4096 v;
4875 match oldmode with
4876 | Birdseye beye ->
4877 leavebirdseye beye false;
4878 enterbirdseye ()
4879 | _ -> ()
4882 let mode = state.mode in
4883 src#string "columns"
4884 (fun () ->
4885 match conf.columns with
4886 | Csingle _ -> "1"
4887 | Cmulti (multi, _) -> multicolumns_to_string multi
4888 | Csplit (count, _) -> "-" ^ string_of_int count
4890 (fun v ->
4891 let n, a, b = multicolumns_of_string v in
4892 setcolumns mode n a b);
4894 sep ();
4895 src#caption "Pixmap cache" 0;
4896 src#int_with_suffix "size (advisory)"
4897 (fun () -> conf.memlimit)
4898 (fun v -> conf.memlimit <- v);
4900 src#caption2 "used"
4901 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4902 (string_with_suffix_of_int state.memused)
4903 (Hashtbl.length state.tilemap)) 1;
4905 sep ();
4906 src#caption "Layout" 0;
4907 src#caption2 "Dimension"
4908 (fun () ->
4909 Printf.sprintf "%dx%d (virtual %dx%d)"
4910 state.winw state.winh
4911 state.w state.maxy)
4913 if conf.debug
4914 then
4915 src#caption2 "Position" (fun () ->
4916 Printf.sprintf "%dx%d" state.x state.y
4918 else
4919 src#caption2 "Position" (fun () -> describe_location ()) 1
4922 sep ();
4923 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4924 "Save these parameters as global defaults at exit"
4925 (fun () -> conf.bedefault)
4926 (fun v -> conf.bedefault <- v)
4929 sep ();
4930 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4931 src#bool ~offset:0 ~btos "Extended parameters"
4932 (fun () -> !showextended)
4933 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4934 if !showextended
4935 then (
4936 src#bool "checkers"
4937 (fun () -> conf.checkers)
4938 (fun v -> conf.checkers <- v; setcheckers v);
4939 src#bool "update cursor"
4940 (fun () -> conf.updatecurs)
4941 (fun v -> conf.updatecurs <- v);
4942 src#bool "verbose"
4943 (fun () -> conf.verbose)
4944 (fun v -> conf.verbose <- v);
4945 src#bool "invert colors"
4946 (fun () -> conf.invert)
4947 (fun v -> conf.invert <- v);
4948 src#bool "max fit"
4949 (fun () -> conf.maxhfit)
4950 (fun v -> conf.maxhfit <- v);
4951 src#bool "redirect stderr"
4952 (fun () -> conf.redirectstderr)
4953 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4954 src#bool "pax mode"
4955 (fun () -> conf.pax != None)
4956 (fun v ->
4957 if v
4958 then conf.pax <- Some (ref (now (), 0, 0))
4959 else conf.pax <- None);
4960 src#string "uri launcher"
4961 (fun () -> conf.urilauncher)
4962 (fun v -> conf.urilauncher <- v);
4963 src#string "path launcher"
4964 (fun () -> conf.pathlauncher)
4965 (fun v -> conf.pathlauncher <- v);
4966 src#string "tile size"
4967 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4968 (fun v ->
4970 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4971 conf.tilew <- max 64 w;
4972 conf.tileh <- max 64 h;
4973 flushtiles ();
4974 with exn ->
4975 state.text <- Printf.sprintf "bad tile size `%s': %s"
4976 v (exntos exn)
4978 src#int "texture count"
4979 (fun () -> conf.texcount)
4980 (fun v ->
4981 if realloctexts v
4982 then conf.texcount <- v
4983 else showtext '!' " Failed to set texture count please retry later"
4985 src#int "slice height"
4986 (fun () -> conf.sliceheight)
4987 (fun v ->
4988 conf.sliceheight <- v;
4989 wcmd "sliceh %d" conf.sliceheight;
4991 src#int "anti-aliasing level"
4992 (fun () -> conf.aalevel)
4993 (fun v ->
4994 conf.aalevel <- bound v 0 8;
4995 state.anchor <- getanchor ();
4996 opendoc state.path state.password;
4998 src#string "page scroll scaling factor"
4999 (fun () -> string_of_float conf.pgscale)
5000 (fun v ->
5002 let s = float_of_string v in
5003 conf.pgscale <- s
5004 with exn ->
5005 state.text <- Printf.sprintf
5006 "bad page scroll scaling factor `%s': %s" v (exntos exn)
5009 src#int "ui font size"
5010 (fun () -> fstate.fontsize)
5011 (fun v -> setfontsize (bound v 5 100));
5012 src#int "hint font size"
5013 (fun () -> conf.hfsize)
5014 (fun v -> conf.hfsize <- bound v 5 100);
5015 colorp "background color"
5016 (fun () -> conf.bgcolor)
5017 (fun v -> conf.bgcolor <- v);
5018 src#bool "crop hack"
5019 (fun () -> conf.crophack)
5020 (fun v -> conf.crophack <- v);
5021 src#string "trim fuzz"
5022 (fun () -> irect_to_string conf.trimfuzz)
5023 (fun v ->
5025 conf.trimfuzz <- irect_of_string v;
5026 if conf.trimmargins
5027 then settrim true conf.trimfuzz;
5028 with exn ->
5029 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
5031 src#string "throttle"
5032 (fun () ->
5033 match conf.maxwait with
5034 | None -> "show place holder if page is not ready"
5035 | Some time ->
5036 if time = infinity
5037 then "wait for page to fully render"
5038 else
5039 "wait " ^ string_of_float time
5040 ^ " seconds before showing placeholder"
5042 (fun v ->
5044 let f = float_of_string v in
5045 if f <= 0.0
5046 then conf.maxwait <- None
5047 else conf.maxwait <- Some f
5048 with exn ->
5049 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
5051 src#string "ghyll scroll"
5052 (fun () ->
5053 match conf.ghyllscroll with
5054 | None -> ""
5055 | Some nab -> ghyllscroll_to_string nab
5057 (fun v ->
5058 try conf.ghyllscroll <- ghyllscroll_of_string v
5059 with exn ->
5060 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
5062 src#string "selection command"
5063 (fun () -> conf.selcmd)
5064 (fun v -> conf.selcmd <- v);
5065 src#string "synctex command"
5066 (fun () -> conf.stcmd)
5067 (fun v -> conf.stcmd <- v);
5068 src#string "pax command"
5069 (fun () -> conf.paxcmd)
5070 (fun v -> conf.paxcmd <- v);
5071 src#colorspace "color space"
5072 (fun () -> CSTE.to_string conf.colorspace)
5073 (fun v ->
5074 conf.colorspace <- CSTE.of_int v;
5075 wcmd "cs %d" v;
5076 load state.layout;
5078 src#paxmark "pax mark method"
5079 (fun () -> MTE.to_string conf.paxmark)
5080 (fun v -> conf.paxmark <- MTE.of_int v);
5081 if pbousable ()
5082 then
5083 src#bool "use PBO"
5084 (fun () -> conf.usepbo)
5085 (fun v -> conf.usepbo <- v);
5086 src#bool "mouse wheel scrolls pages"
5087 (fun () -> conf.wheelbypage)
5088 (fun v -> conf.wheelbypage <- v);
5089 src#bool "open remote links in a new instance"
5090 (fun () -> conf.riani)
5091 (fun v -> conf.riani <- v);
5094 sep ();
5095 src#caption "Document" 0;
5096 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
5097 src#caption2 "Pages"
5098 (fun () -> string_of_int state.pagecount) 1;
5099 src#caption2 "Dimensions"
5100 (fun () -> string_of_int (List.length state.pdims)) 1;
5101 if conf.trimmargins
5102 then (
5103 sep ();
5104 src#caption "Trimmed margins" 0;
5105 src#caption2 "Dimensions"
5106 (fun () -> string_of_int (List.length state.pdims)) 1;
5109 sep ();
5110 src#caption "OpenGL" 0;
5111 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
5112 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
5114 sep ();
5115 src#caption "Location" 0;
5116 if nonemptystr state.origin
5117 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
5118 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
5120 src#reset prevmode prevuioh;
5122 fun () ->
5123 state.text <- "";
5124 let prevmode = state.mode
5125 and prevuioh = state.uioh in
5126 fillsrc prevmode prevuioh;
5127 let source = (src :> lvsource) in
5128 let modehash = findkeyhash conf "info" in
5129 state.uioh <- coe (object (self)
5130 inherit listview ~source ~trusted:true ~modehash as super
5131 val mutable m_prevmemused = 0
5132 method infochanged = function
5133 | Memused ->
5134 if m_prevmemused != state.memused
5135 then (
5136 m_prevmemused <- state.memused;
5137 G.postRedisplay "memusedchanged";
5139 | Pdim -> G.postRedisplay "pdimchanged"
5140 | Docinfo -> fillsrc prevmode prevuioh
5142 method key key mask =
5143 if not (Wsi.withctrl mask)
5144 then
5145 match key with
5146 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
5147 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
5148 | _ -> super#key key mask
5149 else super#key key mask
5150 end);
5151 G.postRedisplay "info";
5154 let enterhelpmode =
5155 let source =
5156 (object
5157 inherit lvsourcebase
5158 method getitemcount = Array.length state.help
5159 method getitem n =
5160 let s, l, _ = state.help.(n) in
5161 (s, l)
5163 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5164 let optuioh =
5165 if not cancel
5166 then (
5167 m_qsearch <- qsearch;
5168 match state.help.(active) with
5169 | _, _, Action f -> Some (f uioh)
5170 | _ -> Some (uioh)
5172 else None
5174 m_active <- active;
5175 m_first <- first;
5176 m_pan <- pan;
5177 optuioh
5179 method hasaction n =
5180 match state.help.(n) with
5181 | _, _, Action _ -> true
5182 | _ -> false
5184 initializer
5185 m_active <- -1
5186 end)
5187 in fun () ->
5188 let modehash = findkeyhash conf "help" in
5189 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
5190 G.postRedisplay "help";
5193 let entermsgsmode =
5194 let msgsource =
5195 let re = Str.regexp "[\r\n]" in
5196 (object
5197 inherit lvsourcebase
5198 val mutable m_items = [||]
5200 method getitemcount = 1 + Array.length m_items
5202 method getitem n =
5203 if n = 0
5204 then "[Clear]", 0
5205 else m_items.(n-1), 0
5207 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5208 ignore uioh;
5209 if not cancel
5210 then (
5211 if active = 0
5212 then Buffer.clear state.errmsgs;
5213 m_qsearch <- qsearch;
5215 m_active <- active;
5216 m_first <- first;
5217 m_pan <- pan;
5218 None
5220 method hasaction n =
5221 n = 0
5223 method reset =
5224 state.newerrmsgs <- false;
5225 let l = Str.split re (Buffer.contents state.errmsgs) in
5226 m_items <- Array.of_list l
5228 initializer
5229 m_active <- 0
5230 end)
5231 in fun () ->
5232 state.text <- "";
5233 msgsource#reset;
5234 let source = (msgsource :> lvsource) in
5235 let modehash = findkeyhash conf "listview" in
5236 state.uioh <- coe (object
5237 inherit listview ~source ~trusted:false ~modehash as super
5238 method display =
5239 if state.newerrmsgs
5240 then msgsource#reset;
5241 super#display
5242 end);
5243 G.postRedisplay "msgs";
5246 let quickbookmark ?title () =
5247 match state.layout with
5248 | [] -> ()
5249 | l :: _ ->
5250 let title =
5251 match title with
5252 | None ->
5253 let sec = Unix.gettimeofday () in
5254 let tm = Unix.localtime sec in
5255 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
5256 (l.pageno+1)
5257 tm.Unix.tm_mday
5258 tm.Unix.tm_mon
5259 (tm.Unix.tm_year + 1900)
5260 tm.Unix.tm_hour
5261 tm.Unix.tm_min
5262 | Some title -> title
5264 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5267 let setautoscrollspeed step goingdown =
5268 let incr = max 1 ((abs step) / 2) in
5269 let incr = if goingdown then incr else -incr in
5270 let astep = step + incr in
5271 state.autoscroll <- Some astep;
5274 let canpan () =
5275 match conf.columns with
5276 | Csplit _ -> true
5277 | _ -> state.x != 0 || conf.zoom > 1.0
5280 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5282 let existsinrow pageno (columns, coverA, coverB) p =
5283 let last = ((pageno - coverA) mod columns) + columns in
5284 let rec any = function
5285 | [] -> false
5286 | l :: rest ->
5287 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5288 then p l
5289 else (
5290 if not (p l)
5291 then (if l.pageno = last then false else any rest)
5292 else true
5295 any state.layout
5298 let nextpage () =
5299 match state.layout with
5300 | [] ->
5301 let pageno = page_of_y state.y in
5302 gotoghyll (getpagey (pageno+1))
5303 | l :: rest ->
5304 match conf.columns with
5305 | Csingle _ ->
5306 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5307 then
5308 let y = clamp (pgscale state.winh) in
5309 gotoghyll y
5310 else
5311 let pageno = min (l.pageno+1) (state.pagecount-1) in
5312 gotoghyll (getpagey pageno)
5313 | Cmulti ((c, _, _) as cl, _) ->
5314 if conf.presentation
5315 && (existsinrow l.pageno cl
5316 (fun l -> l.pageh > l.pagey + l.pagevh))
5317 then
5318 let y = clamp (pgscale state.winh) in
5319 gotoghyll y
5320 else
5321 let pageno = min (l.pageno+c) (state.pagecount-1) in
5322 gotoghyll (getpagey pageno)
5323 | Csplit (n, _) ->
5324 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5325 then
5326 let pagey, pageh = getpageyh l.pageno in
5327 let pagey = pagey + pageh * l.pagecol in
5328 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5329 gotoghyll (pagey + pageh + ips)
5332 let prevpage () =
5333 match state.layout with
5334 | [] ->
5335 let pageno = page_of_y state.y in
5336 gotoghyll (getpagey (pageno-1))
5337 | l :: _ ->
5338 match conf.columns with
5339 | Csingle _ ->
5340 if conf.presentation && l.pagey != 0
5341 then
5342 gotoghyll (clamp (pgscale ~-(state.winh)))
5343 else
5344 let pageno = max 0 (l.pageno-1) in
5345 gotoghyll (getpagey pageno)
5346 | Cmulti ((c, _, coverB) as cl, _) ->
5347 if conf.presentation &&
5348 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5349 then
5350 gotoghyll (clamp (pgscale ~-(state.winh)))
5351 else
5352 let decr =
5353 if l.pageno = state.pagecount - coverB
5354 then 1
5355 else c
5357 let pageno = max 0 (l.pageno-decr) in
5358 gotoghyll (getpagey pageno)
5359 | Csplit (n, _) ->
5360 let y =
5361 if l.pagecol = 0
5362 then
5363 if l.pageno = 0
5364 then l.pagey
5365 else
5366 let pageno = max 0 (l.pageno-1) in
5367 let pagey, pageh = getpageyh pageno in
5368 pagey + (n-1)*pageh
5369 else
5370 let pagey, pageh = getpageyh l.pageno in
5371 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5373 gotoghyll y
5376 let viewkeyboard key mask =
5377 let enttext te =
5378 let mode = state.mode in
5379 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5380 state.text <- "";
5381 enttext ();
5382 G.postRedisplay "view:enttext"
5384 let ctrl = Wsi.withctrl mask in
5385 let key =
5386 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5388 match key with
5389 | 81 -> (* Q *)
5390 exit 0
5392 | 0xff63 -> (* insert *)
5393 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5394 then (
5395 state.mode <- LinkNav (Ltgendir 0);
5396 gotoy state.y;
5398 else showtext '!' "Keyboard link navigation does not work under rotation"
5400 | 0xff1b | 113 -> (* escape / q *)
5401 begin match state.mstate with
5402 | Mzoomrect _ ->
5403 state.mstate <- Mnone;
5404 Wsi.setcursor Wsi.CURSOR_INHERIT;
5405 G.postRedisplay "kill zoom rect";
5406 | _ ->
5407 begin match state.mode with
5408 | LinkNav _ ->
5409 state.mode <- View;
5410 G.postRedisplay "esc leave linknav"
5411 | _ ->
5412 match state.ranchors with
5413 | [] -> raise Quit
5414 | (path, password, anchor, origin) :: rest ->
5415 state.ranchors <- rest;
5416 state.anchor <- anchor;
5417 state.origin <- origin;
5418 state.nameddest <- "";
5419 opendoc path password
5420 end;
5421 end;
5423 | 0xff08 -> (* backspace *)
5424 gotoghyll (getnav ~-1)
5426 | 111 -> (* o *)
5427 enteroutlinemode ()
5429 | 117 -> (* u *)
5430 state.rects <- [];
5431 state.text <- "";
5432 G.postRedisplay "dehighlight";
5434 | 47 | 63 -> (* / ? *)
5435 let ondone isforw s =
5436 cbput state.hists.pat s;
5437 state.searchpattern <- s;
5438 search s isforw
5440 let s = String.create 1 in
5441 s.[0] <- Char.chr key;
5442 enttext (s, "", Some (onhist state.hists.pat),
5443 textentry, ondone (key = 47), true)
5445 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5446 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5447 setzoom (conf.zoom +. incr)
5449 | 43 | 0xffab -> (* + *)
5450 let ondone s =
5451 let n =
5452 try int_of_string s with exc ->
5453 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5454 max_int
5456 if n != max_int
5457 then (
5458 conf.pagebias <- n;
5459 state.text <- "page bias is now " ^ string_of_int n;
5462 enttext ("page bias: ", "", None, intentry, ondone, true)
5464 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5465 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5466 setzoom (max 0.01 (conf.zoom -. decr))
5468 | 45 | 0xffad -> (* - *)
5469 let ondone msg = state.text <- msg in
5470 enttext (
5471 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5472 optentry state.mode, ondone, true
5475 | 48 when ctrl -> (* ctrl-0 *)
5476 if conf.zoom = 1.0
5477 then (
5478 state.x <- 0;
5479 gotoy state.y
5481 else setzoom 1.0
5483 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5484 let cols =
5485 match conf.columns with
5486 | Csingle _ | Cmulti _ -> 1
5487 | Csplit (n, _) -> n
5489 let h = state.winh -
5490 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5492 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5493 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5494 then setzoom zoom
5496 | 51 when ctrl -> (* ctrl-3 *)
5497 let fm =
5498 match conf.fitmodel with
5499 | FitWidth -> FitProportional
5500 | FitProportional -> FitPage
5501 | FitPage -> FitWidth
5503 state.text <- "fit model: " ^ FMTE.to_string fm;
5504 reqlayout conf.angle fm
5506 | 0xffc6 -> (* f9 *)
5507 togglebirdseye ()
5509 | 57 when ctrl -> (* ctrl-9 *)
5510 togglebirdseye ()
5512 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5513 when not ctrl -> (* 0..9 *)
5514 let ondone s =
5515 let n =
5516 try int_of_string s with exc ->
5517 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5520 if n >= 0
5521 then (
5522 addnav ();
5523 cbput state.hists.pag (string_of_int n);
5524 gotopage1 (n + conf.pagebias - 1) 0;
5527 let pageentry text key =
5528 match Char.unsafe_chr key with
5529 | 'g' -> TEdone text
5530 | _ -> intentry text key
5532 let text = "x" in text.[0] <- Char.chr key;
5533 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5535 | 98 -> (* b *)
5536 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5537 reshape state.winw state.winh;
5539 | 66 -> (* B *)
5540 state.bzoom <- not state.bzoom;
5541 state.rects <- [];
5542 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5544 | 108 -> (* l *)
5545 conf.hlinks <- not conf.hlinks;
5546 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5547 G.postRedisplay "toggle highlightlinks";
5549 | 70 -> (* F *)
5550 state.glinks <- true;
5551 let mode = state.mode in
5552 state.mode <- Textentry (
5553 (":", "", None, linknentry, linkndone gotounder, false),
5554 (fun _ ->
5555 state.glinks <- false;
5556 state.mode <- mode)
5558 state.text <- "";
5559 G.postRedisplay "view:linkent(F)"
5561 | 121 -> (* y *)
5562 state.glinks <- true;
5563 let mode = state.mode in
5564 state.mode <- Textentry (
5566 ":", "", None, linknentry, linkndone (fun under ->
5567 selstring (undertext under);
5568 ), false
5570 fun _ ->
5571 state.glinks <- false;
5572 state.mode <- mode
5574 state.text <- "";
5575 G.postRedisplay "view:linkent"
5577 | 97 -> (* a *)
5578 begin match state.autoscroll with
5579 | Some step ->
5580 conf.autoscrollstep <- step;
5581 state.autoscroll <- None
5582 | None ->
5583 if conf.autoscrollstep = 0
5584 then state.autoscroll <- Some 1
5585 else state.autoscroll <- Some conf.autoscrollstep
5588 | 112 when ctrl -> (* ctrl-p *)
5589 launchpath ()
5591 | 80 -> (* P *)
5592 setpresentationmode (not conf.presentation);
5593 showtext ' ' ("presentation mode " ^
5594 if conf.presentation then "on" else "off");
5596 | 102 -> (* f *)
5597 if List.mem Wsi.Fullscreen state.winstate
5598 then Wsi.reshape conf.cwinw conf.cwinh
5599 else Wsi.fullscreen ()
5601 | 112 | 78 -> (* p|N *)
5602 search state.searchpattern false
5604 | 110 | 0xffc0 -> (* n|F3 *)
5605 search state.searchpattern true
5607 | 116 -> (* t *)
5608 begin match state.layout with
5609 | [] -> ()
5610 | l :: _ ->
5611 gotoghyll (getpagey l.pageno)
5614 | 32 -> (* space *)
5615 nextpage ()
5617 | 0xff9f | 0xffff -> (* delete *)
5618 prevpage ()
5620 | 61 -> (* = *)
5621 showtext ' ' (describe_location ());
5623 | 119 -> (* w *)
5624 begin match state.layout with
5625 | [] -> ()
5626 | l :: _ ->
5627 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5628 G.postRedisplay "w"
5631 | 39 -> (* ' *)
5632 enterbookmarkmode ()
5634 | 104 | 0xffbe -> (* h|F1 *)
5635 enterhelpmode ()
5637 | 105 -> (* i *)
5638 enterinfomode ()
5640 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5641 entermsgsmode ()
5643 | 109 -> (* m *)
5644 let ondone s =
5645 match state.layout with
5646 | l :: _ ->
5647 if nonemptystr s
5648 then
5649 state.bookmarks <-
5650 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5651 | _ -> ()
5653 enttext ("bookmark: ", "", None, textentry, ondone, true)
5655 | 126 -> (* ~ *)
5656 quickbookmark ();
5657 showtext ' ' "Quick bookmark added";
5659 | 122 -> (* z *)
5660 begin match state.layout with
5661 | l :: _ ->
5662 let rect = getpdimrect l.pagedimno in
5663 let w, h =
5664 if conf.crophack
5665 then
5666 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5667 truncate (1.2 *. (rect.(3) -. rect.(0))))
5668 else
5669 (truncate (rect.(1) -. rect.(0)),
5670 truncate (rect.(3) -. rect.(0)))
5672 let w = truncate ((float w)*.conf.zoom)
5673 and h = truncate ((float h)*.conf.zoom) in
5674 if w != 0 && h != 0
5675 then (
5676 state.anchor <- getanchor ();
5677 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5679 G.postRedisplay "z";
5681 | [] -> ()
5684 | 120 -> state.roam () (* x *)
5685 | 60 | 62 -> (* < > *)
5686 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5688 | 91 | 93 -> (* [ ] *)
5689 conf.colorscale <-
5690 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5692 G.postRedisplay "brightness";
5694 | 99 when state.mode = View -> (* [alt-]c *)
5695 if Wsi.withalt mask
5696 then (
5697 if conf.zoom > 1.0
5698 then
5699 let m = (wadjsb state.winw - state.w) / 2 in
5700 state.x <- m;
5701 gotoy_and_clear_text state.y
5703 else
5704 let (c, a, b), z =
5705 match state.prevcolumns with
5706 | None -> (1, 0, 0), 1.0
5707 | Some (columns, z) ->
5708 let cab =
5709 match columns with
5710 | Csplit (c, _) -> -c, 0, 0
5711 | Cmulti ((c, a, b), _) -> c, a, b
5712 | Csingle _ -> 1, 0, 0
5714 cab, z
5716 setcolumns View c a b;
5717 setzoom z
5719 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
5720 -> (* ctrl-shift- (kp) [up|down] *)
5721 let zoom, x = state.prevzoom in
5722 setzoom zoom;
5723 state.x <- x;
5725 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5726 begin match state.autoscroll with
5727 | None ->
5728 begin match state.mode with
5729 | Birdseye beye -> upbirdseye 1 beye
5730 | _ ->
5731 if ctrl
5732 then gotoy_and_clear_text (clamp ~-(state.winh/2))
5733 else (
5734 if not (Wsi.withshift mask) && conf.presentation
5735 then prevpage ()
5736 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5739 | Some n ->
5740 setautoscrollspeed n false
5743 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5744 begin match state.autoscroll with
5745 | None ->
5746 begin match state.mode with
5747 | Birdseye beye -> downbirdseye 1 beye
5748 | _ ->
5749 if ctrl
5750 then gotoy_and_clear_text (clamp (state.winh/2))
5751 else (
5752 if not (Wsi.withshift mask) && conf.presentation
5753 then nextpage ()
5754 else gotoy_and_clear_text (clamp conf.scrollstep)
5757 | Some n ->
5758 setautoscrollspeed n true
5761 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5762 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5763 if canpan ()
5764 then
5765 let dx =
5766 if ctrl
5767 then state.winw / 2
5768 else conf.hscrollstep
5770 let dx = if key = 0xff51 || key = 0xff96 then dx else -dx in
5771 state.x <- panbound (state.x + dx);
5772 gotoy_and_clear_text state.y
5773 else (
5774 state.text <- "";
5775 G.postRedisplay "left/right"
5778 | 0xff55 | 0xff9a -> (* (kp) prior *)
5779 let y =
5780 if ctrl
5781 then
5782 match state.layout with
5783 | [] -> state.y
5784 | l :: _ -> state.y - l.pagey
5785 else
5786 clamp (pgscale (-state.winh))
5788 gotoghyll y
5790 | 0xff56 | 0xff9b -> (* (kp) next *)
5791 let y =
5792 if ctrl
5793 then
5794 match List.rev state.layout with
5795 | [] -> state.y
5796 | l :: _ -> getpagey l.pageno
5797 else
5798 clamp (pgscale state.winh)
5800 gotoghyll y
5802 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5803 gotoghyll 0
5804 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5805 gotoghyll (clamp state.maxy)
5807 | 0xff53 | 0xff98
5808 when Wsi.withalt mask -> (* alt-(kp) right *)
5809 gotoghyll (getnav 1)
5810 | 0xff51 | 0xff96
5811 when Wsi.withalt mask -> (* alt-(kp) left *)
5812 gotoghyll (getnav ~-1)
5814 | 114 -> (* r *)
5815 reload ()
5817 | 118 when conf.debug -> (* v *)
5818 state.rects <- [];
5819 List.iter (fun l ->
5820 match getopaque l.pageno with
5821 | None -> ()
5822 | Some opaque ->
5823 let x0, y0, x1, y1 = pagebbox opaque in
5824 let a,b = float x0, float y0 in
5825 let c,d = float x1, float y0 in
5826 let e,f = float x1, float y1 in
5827 let h,j = float x0, float y1 in
5828 let rect = (a,b,c,d,e,f,h,j) in
5829 debugrect rect;
5830 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5831 ) state.layout;
5832 G.postRedisplay "v";
5834 | _ ->
5835 vlog "huh? %s" (Wsi.keyname key)
5838 let linknavkeyboard key mask linknav =
5839 let getpage pageno =
5840 let rec loop = function
5841 | [] -> None
5842 | l :: _ when l.pageno = pageno -> Some l
5843 | _ :: rest -> loop rest
5844 in loop state.layout
5846 let doexact (pageno, n) =
5847 match getopaque pageno, getpage pageno with
5848 | Some opaque, Some l ->
5849 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5850 then
5851 let under = getlink opaque n in
5852 G.postRedisplay "link gotounder";
5853 gotounder under;
5854 state.mode <- View;
5855 else
5856 let opt, dir =
5857 match key with
5858 | 0xff50 -> (* home *)
5859 Some (findlink opaque LDfirst), -1
5861 | 0xff57 -> (* end *)
5862 Some (findlink opaque LDlast), 1
5864 | 0xff51 -> (* left *)
5865 Some (findlink opaque (LDleft n)), -1
5867 | 0xff53 -> (* right *)
5868 Some (findlink opaque (LDright n)), 1
5870 | 0xff52 -> (* up *)
5871 Some (findlink opaque (LDup n)), -1
5873 | 0xff54 -> (* down *)
5874 Some (findlink opaque (LDdown n)), 1
5876 | _ -> None, 0
5878 let pwl l dir =
5879 begin match findpwl l.pageno dir with
5880 | Pwlnotfound -> ()
5881 | Pwl pageno ->
5882 let notfound dir =
5883 state.mode <- LinkNav (Ltgendir dir);
5884 let y, h = getpageyh pageno in
5885 let y =
5886 if dir < 0
5887 then y + h - state.winh
5888 else y
5890 gotoy y
5892 begin match getopaque pageno, getpage pageno with
5893 | Some opaque, Some _ ->
5894 let link =
5895 let ld = if dir > 0 then LDfirst else LDlast in
5896 findlink opaque ld
5898 begin match link with
5899 | Lfound m ->
5900 showlinktype (getlink opaque m);
5901 state.mode <- LinkNav (Ltexact (pageno, m));
5902 G.postRedisplay "linknav jpage";
5903 | _ -> notfound dir
5904 end;
5905 | _ -> notfound dir
5906 end;
5907 end;
5909 begin match opt with
5910 | Some Lnotfound -> pwl l dir;
5911 | Some (Lfound m) ->
5912 if m = n
5913 then pwl l dir
5914 else (
5915 let _, y0, _, y1 = getlinkrect opaque m in
5916 if y0 < l.pagey
5917 then gotopage1 l.pageno y0
5918 else (
5919 let d = fstate.fontsize + 1 in
5920 if y1 - l.pagey > l.pagevh - d
5921 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5922 else G.postRedisplay "linknav";
5924 showlinktype (getlink opaque m);
5925 state.mode <- LinkNav (Ltexact (l.pageno, m));
5928 | None -> viewkeyboard key mask
5929 end;
5930 | _ -> viewkeyboard key mask
5932 if key = 0xff63
5933 then (
5934 state.mode <- View;
5935 G.postRedisplay "leave linknav"
5937 else
5938 match linknav with
5939 | Ltgendir _ -> viewkeyboard key mask
5940 | Ltexact exact -> doexact exact
5943 let keyboard key mask =
5944 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5945 then wcmd "interrupt"
5946 else state.uioh <- state.uioh#key key mask
5949 let birdseyekeyboard key mask
5950 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5951 let incr =
5952 match conf.columns with
5953 | Csingle _ -> 1
5954 | Cmulti ((c, _, _), _) -> c
5955 | Csplit _ -> failwith "bird's eye split mode"
5957 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5958 match key with
5959 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5960 let y, h = getpageyh pageno in
5961 let top = (state.winh - h) / 2 in
5962 gotoy (max 0 (y - top))
5963 | 0xff0d (* enter *)
5964 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5965 | 0xff1b -> leavebirdseye beye true (* escape *)
5966 | 0xff52 -> upbirdseye incr beye (* up *)
5967 | 0xff54 -> downbirdseye incr beye (* down *)
5968 | 0xff51 -> upbirdseye 1 beye (* left *)
5969 | 0xff53 -> downbirdseye 1 beye (* right *)
5971 | 0xff55 -> (* prior *)
5972 begin match state.layout with
5973 | l :: _ ->
5974 if l.pagey != 0
5975 then (
5976 state.mode <- Birdseye (
5977 oconf, leftx, l.pageno, hooverpageno, anchor
5979 gotopage1 l.pageno 0;
5981 else (
5982 let layout = layout (state.y-state.winh) (pgh state.layout) in
5983 match layout with
5984 | [] -> gotoy (clamp (-state.winh))
5985 | l :: _ ->
5986 state.mode <- Birdseye (
5987 oconf, leftx, l.pageno, hooverpageno, anchor
5989 gotopage1 l.pageno 0
5992 | [] -> gotoy (clamp (-state.winh))
5993 end;
5995 | 0xff56 -> (* next *)
5996 begin match List.rev state.layout with
5997 | l :: _ ->
5998 let layout = layout (state.y + (pgh state.layout)) state.winh in
5999 begin match layout with
6000 | [] ->
6001 let incr = l.pageh - l.pagevh in
6002 if incr = 0
6003 then (
6004 state.mode <-
6005 Birdseye (
6006 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
6008 G.postRedisplay "birdseye pagedown";
6010 else gotoy (clamp (incr + conf.interpagespace*2));
6012 | l :: _ ->
6013 state.mode <-
6014 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
6015 gotopage1 l.pageno 0;
6018 | [] -> gotoy (clamp state.winh)
6019 end;
6021 | 0xff50 -> (* home *)
6022 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
6023 gotopage1 0 0
6025 | 0xff57 -> (* end *)
6026 let pageno = state.pagecount - 1 in
6027 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
6028 if not (pagevisible state.layout pageno)
6029 then
6030 let h =
6031 match List.rev state.pdims with
6032 | [] -> state.winh
6033 | (_, _, h, _) :: _ -> h
6035 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
6036 else G.postRedisplay "birdseye end";
6037 | _ -> viewkeyboard key mask
6040 let drawpage l =
6041 let color =
6042 match state.mode with
6043 | Textentry _ -> scalecolor 0.4
6044 | LinkNav _
6045 | View -> scalecolor 1.0
6046 | Birdseye (_, _, pageno, hooverpageno, _) ->
6047 if l.pageno = hooverpageno
6048 then scalecolor 0.9
6049 else (
6050 if l.pageno = pageno
6051 then scalecolor 1.0
6052 else scalecolor 0.8
6055 drawtiles l color;
6058 let postdrawpage l linkindexbase =
6059 match getopaque l.pageno with
6060 | Some opaque ->
6061 if tileready l l.pagex l.pagey
6062 then
6063 let x = l.pagedispx - l.pagex
6064 and y = l.pagedispy - l.pagey in
6065 let hlmask =
6066 match conf.columns with
6067 | Csingle _ | Cmulti _ ->
6068 (if conf.hlinks then 1 else 0)
6069 + (if state.glinks
6070 && not (isbirdseye state.mode) then 2 else 0)
6071 | _ -> 0
6073 let s =
6074 match state.mode with
6075 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
6076 | _ -> ""
6078 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
6079 else 0
6080 | _ -> 0
6083 let scrollindicator () =
6084 let sbw, ph, sh = state.uioh#scrollph in
6085 let sbh, pw, sw = state.uioh#scrollpw in
6087 GlDraw.color (0.64, 0.64, 0.64);
6088 filledrect
6089 (float (state.winw - sbw)) 0.
6090 (float state.winw) (float state.winh)
6092 filledrect
6093 0. (float (state.winh - sbh))
6094 (float (wadjsb state.winw - 1)) (float state.winh)
6096 GlDraw.color (0.0, 0.0, 0.0);
6098 filledrect
6099 (float (state.winw - sbw)) ph
6100 (float state.winw) (ph +. sh)
6102 filledrect
6103 pw (float (state.winh - sbh))
6104 (pw +. sw) (float state.winh)
6108 let showsel () =
6109 match state.mstate with
6110 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
6113 | Msel ((x0, y0), (x1, y1)) ->
6114 let rec loop = function
6115 | l :: ls ->
6116 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
6117 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
6118 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
6119 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
6120 then
6121 match getopaque l.pageno with
6122 | Some opaque ->
6123 let x0, y0 = pagetranslatepoint l x0 y0 in
6124 let x1, y1 = pagetranslatepoint l x1 y1 in
6125 seltext opaque (x0, y0, x1, y1);
6126 | _ -> ()
6127 else loop ls
6128 | [] -> ()
6130 loop state.layout
6133 let showrects = function [] -> () | rects ->
6134 Gl.enable `blend;
6135 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
6136 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6137 List.iter
6138 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
6139 List.iter (fun l ->
6140 if l.pageno = pageno
6141 then (
6142 let dx = float (l.pagedispx - l.pagex) in
6143 let dy = float (l.pagedispy - l.pagey) in
6144 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
6145 Raw.sets_float state.vraw ~pos:0
6146 [| x0+.dx; y0+.dy;
6147 x1+.dx; y1+.dy;
6148 x3+.dx; y3+.dy;
6149 x2+.dx; y2+.dy |];
6150 GlArray.vertex `two state.vraw;
6151 GlArray.draw_arrays `triangle_strip 0 4;
6153 ) state.layout
6154 ) rects
6156 Gl.disable `blend;
6159 let display () =
6160 GlClear.color (scalecolor2 conf.bgcolor);
6161 GlClear.clear [`color];
6162 List.iter drawpage state.layout;
6163 let rects =
6164 match state.mode with
6165 | LinkNav (Ltexact (pageno, linkno)) ->
6166 begin match getopaque pageno with
6167 | Some opaque ->
6168 let x0, y0, x1, y1 = getlinkrect opaque linkno in
6169 (pageno, 5, (
6170 float x0, float y0,
6171 float x1, float y0,
6172 float x1, float y1,
6173 float x0, float y1)
6174 ) :: state.rects
6175 | None -> state.rects
6177 | _ -> state.rects
6179 showrects rects;
6180 let rec postloop linkindexbase = function
6181 | l :: rest ->
6182 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
6183 postloop linkindexbase rest
6184 | [] -> ()
6186 showsel ();
6187 postloop 0 state.layout;
6188 state.uioh#display;
6189 begin match state.mstate with
6190 | Mzoomrect ((x0, y0), (x1, y1)) ->
6191 Gl.enable `blend;
6192 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
6193 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6194 filledrect (float x0) (float y0) (float x1) (float y1);
6195 Gl.disable `blend;
6196 | _ -> ()
6197 end;
6198 enttext ();
6199 scrollindicator ();
6200 Wsi.swapb ();
6203 let zoomrect x y x1 y1 =
6204 let x0 = min x x1
6205 and x1 = max x x1
6206 and y0 = min y y1 in
6207 gotoy (state.y + y0);
6208 state.anchor <- getanchor ();
6209 let zoom = (float state.w) /. float (x1 - x0) in
6210 let margin =
6211 match conf.fitmodel, conf.columns with
6212 | FitPage, Csplit _ ->
6213 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
6215 | _, _ ->
6216 let adjw = wadjsb state.winw in
6217 if state.w < adjw
6218 then (adjw - state.w) / 2
6219 else 0
6221 state.x <- (state.x + margin) - x0;
6222 setzoom zoom;
6223 Wsi.setcursor Wsi.CURSOR_INHERIT;
6224 state.mstate <- Mnone;
6227 let zoomblock x y =
6228 let g opaque l px py =
6229 match rectofblock opaque px py with
6230 | Some a ->
6231 let x0 = a.(0) -. 20. in
6232 let x1 = a.(1) +. 20. in
6233 let y0 = a.(2) -. 20. in
6234 let zoom = (float state.w) /. (x1 -. x0) in
6235 let pagey = getpagey l.pageno in
6236 gotoy_and_clear_text (pagey + truncate y0);
6237 state.anchor <- getanchor ();
6238 let margin = (state.w - l.pagew)/2 in
6239 state.x <- -truncate x0 - margin;
6240 setzoom zoom;
6241 None
6242 | None -> None
6244 match conf.columns with
6245 | Csplit _ ->
6246 showtext '!' "block zooming does not work properly in split columns mode"
6247 | _ -> onppundermouse g x y ()
6250 let scrollx x =
6251 let winw = wadjsb state.winw - 1 in
6252 let s = float x /. float winw in
6253 let destx = truncate (float (state.w + winw) *. s) in
6254 state.x <- winw - destx;
6255 gotoy_and_clear_text state.y;
6256 state.mstate <- Mscrollx;
6259 let scrolly y =
6260 let s = float y /. float state.winh in
6261 let desty = truncate (float (state.maxy - state.winh) *. s) in
6262 gotoy_and_clear_text desty;
6263 state.mstate <- Mscrolly;
6266 let viewmouse button down x y mask =
6267 match button with
6268 | n when (n == 4 || n == 5) && not down ->
6269 if Wsi.withctrl mask
6270 then (
6271 match state.mstate with
6272 | Mzoom (oldn, i) ->
6273 if oldn = n
6274 then (
6275 if i = 2
6276 then
6277 let incr =
6278 match n with
6279 | 5 ->
6280 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6281 | _ ->
6282 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6284 let zoom = conf.zoom -. incr in
6285 setzoom zoom;
6286 state.mstate <- Mzoom (n, 0);
6287 else
6288 state.mstate <- Mzoom (n, i+1);
6290 else state.mstate <- Mzoom (n, 0)
6292 | _ -> state.mstate <- Mzoom (n, 0)
6294 else (
6295 match state.autoscroll with
6296 | Some step -> setautoscrollspeed step (n=4)
6297 | None ->
6298 if conf.wheelbypage || conf.presentation
6299 then (
6300 if n = 4
6301 then prevpage ()
6302 else nextpage ()
6304 else
6305 let incr =
6306 if n = 4
6307 then -conf.scrollstep
6308 else conf.scrollstep
6310 let incr = incr * 2 in
6311 let y = clamp incr in
6312 gotoy_and_clear_text y
6315 | n when (n = 6 || n = 7) && not down && canpan () ->
6316 state.x <-
6317 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6318 gotoy_and_clear_text state.y
6320 | 1 when Wsi.withshift mask ->
6321 state.mstate <- Mnone;
6322 if not down
6323 then (
6324 match unproject x y with
6325 | Some (pageno, ux, uy) ->
6326 let cmd = Printf.sprintf
6327 "%s %s %d %d %d"
6328 conf.stcmd state.path pageno ux uy
6330 popen cmd []
6331 | None -> ()
6334 | 1 when Wsi.withctrl mask ->
6335 if down
6336 then (
6337 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6338 state.mstate <- Mpan (x, y)
6340 else
6341 state.mstate <- Mnone
6343 | 3 ->
6344 if down
6345 then (
6346 Wsi.setcursor Wsi.CURSOR_CYCLE;
6347 let p = (x, y) in
6348 state.mstate <- Mzoomrect (p, p)
6350 else (
6351 match state.mstate with
6352 | Mzoomrect ((x0, y0), _) ->
6353 if abs (x-x0) > 10 && abs (y - y0) > 10
6354 then zoomrect x0 y0 x y
6355 else (
6356 state.mstate <- Mnone;
6357 Wsi.setcursor Wsi.CURSOR_INHERIT;
6358 G.postRedisplay "kill accidental zoom rect";
6360 | _ ->
6361 Wsi.setcursor Wsi.CURSOR_INHERIT;
6362 state.mstate <- Mnone
6365 | 1 when x > state.winw - vscrollw () ->
6366 if down
6367 then
6368 let _, position, sh = state.uioh#scrollph in
6369 if y > truncate position && y < truncate (position +. sh)
6370 then state.mstate <- Mscrolly
6371 else scrolly y
6372 else
6373 state.mstate <- Mnone
6375 | 1 when y > state.winh - hscrollh () ->
6376 if down
6377 then
6378 let _, position, sw = state.uioh#scrollpw in
6379 if x > truncate position && x < truncate (position +. sw)
6380 then state.mstate <- Mscrollx
6381 else scrollx x
6382 else
6383 state.mstate <- Mnone
6385 | 1 when state.bzoom -> if not down then zoomblock x y
6387 | 1 ->
6388 let dest = if down then getunder x y else Unone in
6389 begin match dest with
6390 | Ulinkgoto _
6391 | Ulinkuri _
6392 | Uremote _ | Uremotedest _
6393 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6394 gotounder dest
6396 | Unone when down ->
6397 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6398 state.mstate <- Mpan (x, y);
6400 | Unone | Utext _ ->
6401 if down
6402 then (
6403 if conf.angle mod 360 = 0
6404 then (
6405 state.mstate <- Msel ((x, y), (x, y));
6406 G.postRedisplay "mouse select";
6409 else (
6410 match state.mstate with
6411 | Mnone -> ()
6413 | Mzoom _ | Mscrollx | Mscrolly ->
6414 state.mstate <- Mnone
6416 | Mzoomrect ((x0, y0), _) ->
6417 zoomrect x0 y0 x y
6419 | Mpan _ ->
6420 Wsi.setcursor Wsi.CURSOR_INHERIT;
6421 state.mstate <- Mnone
6423 | Msel ((x0, y0), (x1, y1)) ->
6424 let rec loop = function
6425 | [] -> ()
6426 | l :: rest ->
6427 let inside =
6428 let a0 = l.pagedispy in
6429 let a1 = a0 + l.pagevh in
6430 let b0 = l.pagedispx in
6431 let b1 = b0 + l.pagevw in
6432 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6433 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6435 if inside
6436 then
6437 match getopaque l.pageno with
6438 | Some opaque ->
6439 begin
6440 match Ne.pipe () with
6441 | Ne.Exn exn ->
6442 showtext '!'
6443 (Printf.sprintf
6444 "can not create sel pipe: %s"
6445 (exntos exn));
6446 | Ne.Res (r, w) ->
6447 let doclose what fd =
6448 Ne.clo fd (fun msg ->
6449 dolog "%s close failed: %s" what msg)
6452 popen conf.selcmd [r, 0; w, -1];
6453 copysel w opaque true;
6454 doclose "pipe/r" r;
6455 G.postRedisplay "copysel";
6456 with exn ->
6457 dolog "can not execute %S: %s"
6458 conf.selcmd (exntos exn);
6459 doclose "pipe/r" r;
6460 doclose "pipe/w" w;
6462 | None -> ()
6463 else loop rest
6465 loop state.layout;
6466 Wsi.setcursor Wsi.CURSOR_INHERIT;
6467 state.mstate <- Mnone;
6471 | _ -> ()
6474 let birdseyemouse button down x y mask
6475 (conf, leftx, _, hooverpageno, anchor) =
6476 match button with
6477 | 1 when down ->
6478 let rec loop = function
6479 | [] -> ()
6480 | l :: rest ->
6481 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6482 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6483 then (
6484 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6486 else loop rest
6488 loop state.layout
6489 | 3 -> ()
6490 | _ -> viewmouse button down x y mask
6493 let uioh = object
6494 method display = ()
6496 method key key mask =
6497 begin match state.mode with
6498 | Textentry textentry -> textentrykeyboard key mask textentry
6499 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6500 | View -> viewkeyboard key mask
6501 | LinkNav linknav -> linknavkeyboard key mask linknav
6502 end;
6503 state.uioh
6505 method button button bstate x y mask =
6506 begin match state.mode with
6507 | LinkNav _
6508 | View -> viewmouse button bstate x y mask
6509 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6510 | Textentry _ -> ()
6511 end;
6512 state.uioh
6514 method motion x y =
6515 begin match state.mode with
6516 | Textentry _ -> ()
6517 | View | Birdseye _ | LinkNav _ ->
6518 match state.mstate with
6519 | Mzoom _ | Mnone -> ()
6521 | Mpan (x0, y0) ->
6522 let dx = x - x0
6523 and dy = y0 - y in
6524 state.mstate <- Mpan (x, y);
6525 if canpan ()
6526 then state.x <- panbound (state.x + dx);
6527 let y = clamp dy in
6528 gotoy_and_clear_text y
6530 | Msel (a, _) ->
6531 state.mstate <- Msel (a, (x, y));
6532 G.postRedisplay "motion select";
6534 | Mscrolly ->
6535 let y = min state.winh (max 0 y) in
6536 scrolly y
6538 | Mscrollx ->
6539 let x = min state.winw (max 0 x) in
6540 scrollx x
6542 | Mzoomrect (p0, _) ->
6543 state.mstate <- Mzoomrect (p0, (x, y));
6544 G.postRedisplay "motion zoomrect";
6545 end;
6546 state.uioh
6548 method pmotion x y =
6549 begin match state.mode with
6550 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6551 let rec loop = function
6552 | [] ->
6553 if hooverpageno != -1
6554 then (
6555 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6556 G.postRedisplay "pmotion birdseye no hoover";
6558 | l :: rest ->
6559 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6560 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6561 then (
6562 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6563 G.postRedisplay "pmotion birdseye hoover";
6565 else loop rest
6567 loop state.layout
6569 | Textentry _ -> ()
6571 | LinkNav _
6572 | View ->
6573 match state.mstate with
6574 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6576 | Mnone ->
6577 updateunder x y;
6578 match conf.pax with
6579 | None -> ()
6580 | Some r ->
6581 let past, _, _ = !r in
6582 let now = now () in
6583 let delta = now -. past in
6584 if delta > 0.01
6585 then paxunder x y
6586 else r := (now, x, y)
6587 end;
6588 state.uioh
6590 method infochanged _ = ()
6592 method scrollph =
6593 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6594 let p, h =
6595 if maxy = 0
6596 then 0.0, float state.winh
6597 else scrollph state.y maxy
6599 vscrollw (), p, h
6601 method scrollpw =
6602 let winw = wadjsb state.winw in
6603 let fwinw = float winw in
6604 let sw =
6605 let sw = fwinw /. float state.w in
6606 let sw = fwinw *. sw in
6607 max sw (float conf.scrollh)
6609 let position =
6610 let maxx = state.w + winw in
6611 let x = winw - state.x in
6612 let percent = float x /. float maxx in
6613 (fwinw -. sw) *. percent
6615 hscrollh (), position, sw
6617 method modehash =
6618 let modename =
6619 match state.mode with
6620 | LinkNav _ -> "links"
6621 | Textentry _ -> "textentry"
6622 | Birdseye _ -> "birdseye"
6623 | View -> "view"
6625 findkeyhash conf modename
6627 method eformsgs = true
6628 end;;
6630 module Config =
6631 struct
6632 open Parser
6634 let fontpath = ref "";;
6636 module KeyMap =
6637 Map.Make (struct type t = (int * int) let compare = compare end);;
6639 let unent s =
6640 let l = String.length s in
6641 let b = Buffer.create l in
6642 unent b s 0 l;
6643 Buffer.contents b;
6646 let home =
6647 try Sys.getenv "HOME"
6648 with exn ->
6649 prerr_endline
6650 ("Can not determine home directory location: " ^ exntos exn);
6654 let modifier_of_string = function
6655 | "alt" -> Wsi.altmask
6656 | "shift" -> Wsi.shiftmask
6657 | "ctrl" | "control" -> Wsi.ctrlmask
6658 | "meta" -> Wsi.metamask
6659 | _ -> 0
6662 let key_of_string =
6663 let r = Str.regexp "-" in
6664 fun s ->
6665 let elems = Str.full_split r s in
6666 let f n k m =
6667 let g s =
6668 let m1 = modifier_of_string s in
6669 if m1 = 0
6670 then (Wsi.namekey s, m)
6671 else (k, m lor m1)
6672 in function
6673 | Str.Delim s when n land 1 = 0 -> g s
6674 | Str.Text s -> g s
6675 | Str.Delim _ -> (k, m)
6677 let rec loop n k m = function
6678 | [] -> (k, m)
6679 | x :: xs ->
6680 let k, m = f n k m x in
6681 loop (n+1) k m xs
6683 loop 0 0 0 elems
6686 let keys_of_string =
6687 let r = Str.regexp "[ \t]" in
6688 fun s ->
6689 let elems = Str.split r s in
6690 List.map key_of_string elems
6693 let copykeyhashes c =
6694 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6697 let config_of c attrs =
6698 let apply c k v =
6700 match k with
6701 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6702 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6703 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6704 | "preload" -> { c with preload = bool_of_string v }
6705 | "page-bias" -> { c with pagebias = int_of_string v }
6706 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6707 | "horizontal-scroll-step" ->
6708 { c with hscrollstep = max (int_of_string v) 1 }
6709 | "auto-scroll-step" ->
6710 { c with autoscrollstep = max 0 (int_of_string v) }
6711 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6712 | "crop-hack" -> { c with crophack = bool_of_string v }
6713 | "throttle" ->
6714 let mw =
6715 match String.lowercase v with
6716 | "true" -> Some infinity
6717 | "false" -> None
6718 | f -> Some (float_of_string f)
6720 { c with maxwait = mw}
6721 | "highlight-links" -> { c with hlinks = bool_of_string v }
6722 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6723 | "vertical-margin" ->
6724 { c with interpagespace = max 0 (int_of_string v) }
6725 | "zoom" ->
6726 let zoom = float_of_string v /. 100. in
6727 let zoom = max zoom 0.0 in
6728 { c with zoom = zoom }
6729 | "presentation" -> { c with presentation = bool_of_string v }
6730 | "rotation-angle" -> { c with angle = int_of_string v }
6731 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6732 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6733 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6734 | "proportional-display" ->
6735 let fm =
6736 if bool_of_string v
6737 then FitProportional
6738 else FitWidth
6740 { c with fitmodel = fm }
6741 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6742 | "pixmap-cache-size" ->
6743 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6744 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6745 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6746 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6747 | "persistent-location" -> { c with jumpback = bool_of_string v }
6748 | "background-color" -> { c with bgcolor = color_of_string v }
6749 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6750 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6751 | "mupdf-store-size" ->
6752 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6753 | "checkers" -> { c with checkers = bool_of_string v }
6754 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6755 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6756 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6757 | "uri-launcher" -> { c with urilauncher = unent v }
6758 | "path-launcher" -> { c with pathlauncher = unent v }
6759 | "color-space" -> { c with colorspace = CSTE.of_string v }
6760 | "invert-colors" -> { c with invert = bool_of_string v }
6761 | "brightness" -> { c with colorscale = float_of_string v }
6762 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6763 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
6764 | "columns" ->
6765 let (n, _, _) as nab = multicolumns_of_string v in
6766 if n < 0
6767 then { c with columns = Csplit (-n, [||]) }
6768 else { c with columns = Cmulti (nab, [||]) }
6769 | "birds-eye-columns" ->
6770 { c with beyecolumns = Some (max (int_of_string v) 2) }
6771 | "selection-command" -> { c with selcmd = unent v }
6772 | "synctex-command" -> { c with stcmd = unent v }
6773 | "pax-command" -> { c with paxcmd = unent v }
6774 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6775 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6776 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6777 | "use-pbo" -> { c with usepbo = bool_of_string v }
6778 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6779 | "horizontal-scrollbar-visible" ->
6780 let b =
6781 if bool_of_string v
6782 then c.scrollb lor scrollbhv
6783 else c.scrollb land (lnot scrollbhv)
6785 { c with scrollb = b }
6786 | "vertical-scrollbar-visible" ->
6787 let b =
6788 if bool_of_string v
6789 then c.scrollb lor scrollbvv
6790 else c.scrollb land (lnot scrollbvv)
6792 { c with scrollb = b }
6793 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6794 | "point-and-x" ->
6795 { c with pax =
6796 if bool_of_string v
6797 then Some (ref (0.0, 0, 0))
6798 else None }
6799 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
6800 | _ -> c
6801 with exn ->
6802 prerr_endline ("Error processing attribute (`" ^
6803 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6806 let rec fold c = function
6807 | [] -> c
6808 | (k, v) :: rest ->
6809 let c = apply c k v in
6810 fold c rest
6812 fold { c with keyhashes = copykeyhashes c } attrs;
6815 let fromstring f pos n v d =
6816 try f v
6817 with exn ->
6818 dolog "Error processing attribute (%S=%S) at %d\n%s"
6819 n v pos (exntos exn)
6824 let bookmark_of attrs =
6825 let rec fold title page rely visy = function
6826 | ("title", v) :: rest -> fold v page rely visy rest
6827 | ("page", v) :: rest -> fold title v rely visy rest
6828 | ("rely", v) :: rest -> fold title page v visy rest
6829 | ("visy", v) :: rest -> fold title page rely v rest
6830 | _ :: rest -> fold title page rely visy rest
6831 | [] -> title, page, rely, visy
6833 fold "invalid" "0" "0" "0" attrs
6836 let doc_of attrs =
6837 let rec fold path page rely pan visy = function
6838 | ("path", v) :: rest -> fold v page rely pan visy rest
6839 | ("page", v) :: rest -> fold path v rely pan visy rest
6840 | ("rely", v) :: rest -> fold path page v pan visy rest
6841 | ("pan", v) :: rest -> fold path page rely v visy rest
6842 | ("visy", v) :: rest -> fold path page rely pan v rest
6843 | _ :: rest -> fold path page rely pan visy rest
6844 | [] -> path, page, rely, pan, visy
6846 fold "" "0" "0" "0" "0" attrs
6849 let map_of attrs =
6850 let rec fold rs ls = function
6851 | ("out", v) :: rest -> fold v ls rest
6852 | ("in", v) :: rest -> fold rs v rest
6853 | _ :: rest -> fold ls rs rest
6854 | [] -> ls, rs
6856 fold "" "" attrs
6859 let setconf dst src =
6860 dst.scrollbw <- src.scrollbw;
6861 dst.scrollh <- src.scrollh;
6862 dst.icase <- src.icase;
6863 dst.preload <- src.preload;
6864 dst.pagebias <- src.pagebias;
6865 dst.verbose <- src.verbose;
6866 dst.scrollstep <- src.scrollstep;
6867 dst.maxhfit <- src.maxhfit;
6868 dst.crophack <- src.crophack;
6869 dst.autoscrollstep <- src.autoscrollstep;
6870 dst.maxwait <- src.maxwait;
6871 dst.hlinks <- src.hlinks;
6872 dst.underinfo <- src.underinfo;
6873 dst.interpagespace <- src.interpagespace;
6874 dst.zoom <- src.zoom;
6875 dst.presentation <- src.presentation;
6876 dst.angle <- src.angle;
6877 dst.cwinw <- src.cwinw;
6878 dst.cwinh <- src.cwinh;
6879 dst.savebmarks <- src.savebmarks;
6880 dst.memlimit <- src.memlimit;
6881 dst.fitmodel <- src.fitmodel;
6882 dst.texcount <- src.texcount;
6883 dst.sliceheight <- src.sliceheight;
6884 dst.thumbw <- src.thumbw;
6885 dst.jumpback <- src.jumpback;
6886 dst.bgcolor <- src.bgcolor;
6887 dst.tilew <- src.tilew;
6888 dst.tileh <- src.tileh;
6889 dst.mustoresize <- src.mustoresize;
6890 dst.checkers <- src.checkers;
6891 dst.aalevel <- src.aalevel;
6892 dst.trimmargins <- src.trimmargins;
6893 dst.trimfuzz <- src.trimfuzz;
6894 dst.urilauncher <- src.urilauncher;
6895 dst.colorspace <- src.colorspace;
6896 dst.invert <- src.invert;
6897 dst.colorscale <- src.colorscale;
6898 dst.redirectstderr <- src.redirectstderr;
6899 dst.ghyllscroll <- src.ghyllscroll;
6900 dst.columns <- src.columns;
6901 dst.beyecolumns <- src.beyecolumns;
6902 dst.selcmd <- src.selcmd;
6903 dst.updatecurs <- src.updatecurs;
6904 dst.pathlauncher <- src.pathlauncher;
6905 dst.keyhashes <- copykeyhashes src;
6906 dst.hfsize <- src.hfsize;
6907 dst.hscrollstep <- src.hscrollstep;
6908 dst.pgscale <- src.pgscale;
6909 dst.usepbo <- src.usepbo;
6910 dst.wheelbypage <- src.wheelbypage;
6911 dst.stcmd <- src.stcmd;
6912 dst.paxcmd <- src.paxcmd;
6913 dst.scrollb <- src.scrollb;
6914 dst.riani <- src.riani;
6915 dst.paxmark <- src.paxmark;
6916 dst.pax <-
6917 if src.pax = None
6918 then None
6919 else Some ((ref (0.0, 0, 0)));
6922 let get s =
6923 let h = Hashtbl.create 10 in
6924 let dc = { defconf with angle = defconf.angle } in
6925 let rec toplevel v t spos _ =
6926 match t with
6927 | Vdata | Vcdata | Vend -> v
6928 | Vopen ("llppconfig", _, closed) ->
6929 if closed
6930 then v
6931 else { v with f = llppconfig }
6932 | Vopen _ ->
6933 error "unexpected subelement at top level" s spos
6934 | Vclose _ -> error "unexpected close at top level" s spos
6936 and llppconfig v t spos _ =
6937 match t with
6938 | Vdata | Vcdata -> v
6939 | Vend -> error "unexpected end of input in llppconfig" s spos
6940 | Vopen ("defaults", attrs, closed) ->
6941 let c = config_of dc attrs in
6942 setconf dc c;
6943 if closed
6944 then v
6945 else { v with f = defaults }
6947 | Vopen ("ui-font", attrs, closed) ->
6948 let rec getsize size = function
6949 | [] -> size
6950 | ("size", v) :: rest ->
6951 let size =
6952 fromstring int_of_string spos "size" v fstate.fontsize in
6953 getsize size rest
6954 | l -> getsize size l
6956 fstate.fontsize <- getsize fstate.fontsize attrs;
6957 if closed
6958 then v
6959 else { v with f = uifont (Buffer.create 10) }
6961 | Vopen ("doc", attrs, closed) ->
6962 let pathent, spage, srely, span, svisy = doc_of attrs in
6963 let path = unent pathent
6964 and pageno = fromstring int_of_string spos "page" spage 0
6965 and rely = fromstring float_of_string spos "rely" srely 0.0
6966 and pan = fromstring int_of_string spos "pan" span 0
6967 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
6968 let c = config_of dc attrs in
6969 let anchor = (pageno, rely, visy) in
6970 if closed
6971 then (Hashtbl.add h path (c, [], pan, anchor); v)
6972 else { v with f = doc path pan anchor c [] }
6974 | Vopen _ ->
6975 error "unexpected subelement in llppconfig" s spos
6977 | Vclose "llppconfig" -> { v with f = toplevel }
6978 | Vclose _ -> error "unexpected close in llppconfig" s spos
6980 and defaults v t spos _ =
6981 match t with
6982 | Vdata | Vcdata -> v
6983 | Vend -> error "unexpected end of input in defaults" s spos
6984 | Vopen ("keymap", attrs, closed) ->
6985 let modename =
6986 try List.assoc "mode" attrs
6987 with Not_found -> "global" in
6988 if closed
6989 then v
6990 else
6991 let ret keymap =
6992 let h = findkeyhash dc modename in
6993 KeyMap.iter (Hashtbl.replace h) keymap;
6994 defaults
6996 { v with f = pkeymap ret KeyMap.empty }
6998 | Vopen (_, _, _) ->
6999 error "unexpected subelement in defaults" s spos
7001 | Vclose "defaults" ->
7002 { v with f = llppconfig }
7004 | Vclose _ -> error "unexpected close in defaults" s spos
7006 and uifont b v t spos epos =
7007 match t with
7008 | Vdata | Vcdata ->
7009 Buffer.add_substring b s spos (epos - spos);
7011 | Vopen (_, _, _) ->
7012 error "unexpected subelement in ui-font" s spos
7013 | Vclose "ui-font" ->
7014 if emptystr !fontpath
7015 then fontpath := Buffer.contents b;
7016 { v with f = llppconfig }
7017 | Vclose _ -> error "unexpected close in ui-font" s spos
7018 | Vend -> error "unexpected end of input in ui-font" s spos
7020 and doc path pan anchor c bookmarks v t spos _ =
7021 match t with
7022 | Vdata | Vcdata -> v
7023 | Vend -> error "unexpected end of input in doc" s spos
7024 | Vopen ("bookmarks", _, closed) ->
7025 if closed
7026 then v
7027 else { v with f = pbookmarks path pan anchor c bookmarks }
7029 | Vopen ("keymap", attrs, closed) ->
7030 let modename =
7031 try List.assoc "mode" attrs
7032 with Not_found -> "global"
7034 if closed
7035 then v
7036 else
7037 let ret keymap =
7038 let h = findkeyhash c modename in
7039 KeyMap.iter (Hashtbl.replace h) keymap;
7040 doc path pan anchor c bookmarks
7042 { v with f = pkeymap ret KeyMap.empty }
7044 | Vopen (_, _, _) ->
7045 error "unexpected subelement in doc" s spos
7047 | Vclose "doc" ->
7048 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
7049 { v with f = llppconfig }
7051 | Vclose _ -> error "unexpected close in doc" s spos
7053 and pkeymap ret keymap v t spos _ =
7054 match t with
7055 | Vdata | Vcdata -> v
7056 | Vend -> error "unexpected end of input in keymap" s spos
7057 | Vopen ("map", attrs, closed) ->
7058 let r, l = map_of attrs in
7059 let kss = fromstring keys_of_string spos "in" r [] in
7060 let lss = fromstring keys_of_string spos "out" l [] in
7061 let keymap =
7062 match kss with
7063 | [] -> keymap
7064 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
7065 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
7067 if closed
7068 then { v with f = pkeymap ret keymap }
7069 else
7070 let f () = v in
7071 { v with f = skip "map" f }
7073 | Vopen _ ->
7074 error "unexpected subelement in keymap" s spos
7076 | Vclose "keymap" ->
7077 { v with f = ret keymap }
7079 | Vclose _ -> error "unexpected close in keymap" s spos
7081 and pbookmarks path pan anchor c bookmarks v t spos _ =
7082 match t with
7083 | Vdata | Vcdata -> v
7084 | Vend -> error "unexpected end of input in bookmarks" s spos
7085 | Vopen ("item", attrs, closed) ->
7086 let titleent, spage, srely, svisy = bookmark_of attrs in
7087 let page = fromstring int_of_string spos "page" spage 0
7088 and rely = fromstring float_of_string spos "rely" srely 0.0
7089 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7090 let bookmarks =
7091 (unent titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
7093 if closed
7094 then { v with f = pbookmarks path pan anchor c bookmarks }
7095 else
7096 let f () = v in
7097 { v with f = skip "item" f }
7099 | Vopen _ ->
7100 error "unexpected subelement in bookmarks" s spos
7102 | Vclose "bookmarks" ->
7103 { v with f = doc path pan anchor c bookmarks }
7105 | Vclose _ -> error "unexpected close in bookmarks" s spos
7107 and skip tag f v t spos _ =
7108 match t with
7109 | Vdata | Vcdata -> v
7110 | Vend ->
7111 error ("unexpected end of input in skipped " ^ tag) s spos
7112 | Vopen (tag', _, closed) ->
7113 if closed
7114 then v
7115 else
7116 let f' () = { v with f = skip tag f } in
7117 { v with f = skip tag' f' }
7118 | Vclose ctag ->
7119 if tag = ctag
7120 then f ()
7121 else error ("unexpected close in skipped " ^ tag) s spos
7124 parse { f = toplevel; accu = () } s;
7125 h, dc;
7128 let do_load f ic =
7130 let len = in_channel_length ic in
7131 let s = String.create len in
7132 really_input ic s 0 len;
7133 f s;
7134 with
7135 | Parse_error (msg, s, pos) ->
7136 let subs = subs s pos in
7137 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
7139 | exn ->
7140 failwith ("config load error: " ^ exntos exn)
7143 let defconfpath =
7144 let dir =
7146 let dir = Filename.concat home ".config" in
7147 if Sys.is_directory dir then dir else home
7148 with _ -> home
7150 Filename.concat dir "llpp.conf"
7153 let confpath = ref defconfpath;;
7155 let load1 f =
7156 if Sys.file_exists !confpath
7157 then
7158 match
7159 (try Some (open_in_bin !confpath)
7160 with exn ->
7161 prerr_endline
7162 ("Error opening configuration file `" ^ !confpath ^ "': " ^
7163 exntos exn);
7164 None
7166 with
7167 | Some ic ->
7168 let success =
7170 f (do_load get ic)
7171 with exn ->
7172 prerr_endline
7173 ("Error loading configuration from `" ^ !confpath ^ "': " ^
7174 exntos exn);
7175 false
7177 close_in ic;
7178 success
7180 | None -> false
7181 else
7182 f (Hashtbl.create 0, defconf)
7185 let load () =
7186 let f (h, dc) =
7187 let pc, pb, px, pa =
7189 let key =
7190 if emptystr state.origin
7191 then state.path
7192 else state.origin
7194 Hashtbl.find h (Filename.basename key)
7195 with Not_found -> dc, [], 0, emptyanchor
7197 setconf defconf dc;
7198 setconf conf pc;
7199 state.bookmarks <- pb;
7200 state.x <- px;
7201 if conf.jumpback
7202 then state.anchor <- pa;
7203 cbput state.hists.nav pa;
7204 true
7206 load1 f
7209 let add_attrs bb always dc c =
7210 let ob s a b =
7211 if always || a != b
7212 then Printf.bprintf bb "\n %s='%b'" s a
7213 and op s a b =
7214 if always || a <> b
7215 then Printf.bprintf bb "\n %s='%b'" s (a != None)
7216 and oi s a b =
7217 if always || a != b
7218 then Printf.bprintf bb "\n %s='%d'" s a
7219 and oI s a b =
7220 if always || a != b
7221 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
7222 and oz s a b =
7223 if always || a <> b
7224 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
7225 and oF s a b =
7226 if always || a <> b
7227 then Printf.bprintf bb "\n %s='%f'" s a
7228 and oc s a b =
7229 if always || a <> b
7230 then
7231 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
7232 and oC s a b =
7233 if always || a <> b
7234 then
7235 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
7236 and oR s a b =
7237 if always || a <> b
7238 then
7239 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
7240 and os s a b =
7241 if always || a <> b
7242 then
7243 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7244 and og s a b =
7245 if always || a <> b
7246 then
7247 match a with
7248 | Some (_N, _A, _B) ->
7249 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7250 | None ->
7251 match b with
7252 | None -> ()
7253 | _ ->
7254 Printf.bprintf bb "\n %s='none'" s
7255 and oW s a b =
7256 if always || a <> b
7257 then
7258 let v =
7259 match a with
7260 | None -> "false"
7261 | Some f ->
7262 if f = infinity
7263 then "true"
7264 else string_of_float f
7266 Printf.bprintf bb "\n %s='%s'" s v
7267 and oco s a b =
7268 if always || a <> b
7269 then
7270 match a with
7271 | Cmulti ((n, a, b), _) when n > 1 ->
7272 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7273 | Csplit (n, _) when n > 1 ->
7274 Printf.bprintf bb "\n %s='%d'" s ~-n
7275 | _ -> ()
7276 and obeco s a b =
7277 if always || a <> b
7278 then
7279 match a with
7280 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7281 | _ -> ()
7282 and oFm s a b =
7283 if always || a <> b
7284 then
7285 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7286 and oSv s a b m =
7287 if always || a <> b
7288 then
7289 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7290 and oPm s a b =
7291 if always || a <> b
7292 then
7293 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
7295 oi "width" c.cwinw dc.cwinw;
7296 oi "height" c.cwinh dc.cwinh;
7297 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7298 oi "scroll-handle-height" c.scrollh dc.scrollh;
7299 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7300 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7301 ob "case-insensitive-search" c.icase dc.icase;
7302 ob "preload" c.preload dc.preload;
7303 oi "page-bias" c.pagebias dc.pagebias;
7304 oi "scroll-step" c.scrollstep dc.scrollstep;
7305 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7306 ob "max-height-fit" c.maxhfit dc.maxhfit;
7307 ob "crop-hack" c.crophack dc.crophack;
7308 oW "throttle" c.maxwait dc.maxwait;
7309 ob "highlight-links" c.hlinks dc.hlinks;
7310 ob "under-cursor-info" c.underinfo dc.underinfo;
7311 oi "vertical-margin" c.interpagespace dc.interpagespace;
7312 oz "zoom" c.zoom dc.zoom;
7313 ob "presentation" c.presentation dc.presentation;
7314 oi "rotation-angle" c.angle dc.angle;
7315 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7316 oFm "fit-model" c.fitmodel dc.fitmodel;
7317 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7318 oi "tex-count" c.texcount dc.texcount;
7319 oi "slice-height" c.sliceheight dc.sliceheight;
7320 oi "thumbnail-width" c.thumbw dc.thumbw;
7321 ob "persistent-location" c.jumpback dc.jumpback;
7322 oc "background-color" c.bgcolor dc.bgcolor;
7323 oi "tile-width" c.tilew dc.tilew;
7324 oi "tile-height" c.tileh dc.tileh;
7325 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7326 ob "checkers" c.checkers dc.checkers;
7327 oi "aalevel" c.aalevel dc.aalevel;
7328 ob "trim-margins" c.trimmargins dc.trimmargins;
7329 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7330 os "uri-launcher" c.urilauncher dc.urilauncher;
7331 os "path-launcher" c.pathlauncher dc.pathlauncher;
7332 oC "color-space" c.colorspace dc.colorspace;
7333 ob "invert-colors" c.invert dc.invert;
7334 oF "brightness" c.colorscale dc.colorscale;
7335 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7336 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7337 oco "columns" c.columns dc.columns;
7338 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7339 os "selection-command" c.selcmd dc.selcmd;
7340 os "synctex-command" c.stcmd dc.stcmd;
7341 os "pax-command" c.paxcmd dc.paxcmd;
7342 ob "update-cursor" c.updatecurs dc.updatecurs;
7343 oi "hint-font-size" c.hfsize dc.hfsize;
7344 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7345 oF "page-scroll-scale" c.pgscale dc.pgscale;
7346 ob "use-pbo" c.usepbo dc.usepbo;
7347 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7348 ob "remote-in-a-new-instance" c.riani dc.riani;
7349 op "point-and-x" c.pax dc.pax;
7350 oPm "point-and-x-mark" c.paxmark dc.paxmark;
7353 let keymapsbuf always dc c =
7354 let bb = Buffer.create 16 in
7355 let rec loop = function
7356 | [] -> ()
7357 | (modename, h) :: rest ->
7358 let dh = findkeyhash dc modename in
7359 if always || h <> dh
7360 then (
7361 if Hashtbl.length h > 0
7362 then (
7363 if Buffer.length bb > 0
7364 then Buffer.add_char bb '\n';
7365 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7366 Hashtbl.iter (fun i o ->
7367 let isdifferent = always ||
7369 let dO = Hashtbl.find dh i in
7370 dO <> o
7371 with Not_found -> true
7373 if isdifferent
7374 then
7375 let addkm (k, m) =
7376 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7377 if Wsi.withalt m then Buffer.add_string bb "alt-";
7378 if Wsi.withshift m then Buffer.add_string bb "shift-";
7379 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7380 Buffer.add_string bb (Wsi.keyname k);
7382 let addkms l =
7383 let rec loop = function
7384 | [] -> ()
7385 | km :: [] -> addkm km
7386 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7388 loop l
7390 Buffer.add_string bb "<map in='";
7391 addkm i;
7392 match o with
7393 | KMinsrt km ->
7394 Buffer.add_string bb "' out='";
7395 addkm km;
7396 Buffer.add_string bb "'/>\n"
7398 | KMinsrl kms ->
7399 Buffer.add_string bb "' out='";
7400 addkms kms;
7401 Buffer.add_string bb "'/>\n"
7403 | KMmulti (ins, kms) ->
7404 Buffer.add_char bb ' ';
7405 addkms ins;
7406 Buffer.add_string bb "' out='";
7407 addkms kms;
7408 Buffer.add_string bb "'/>\n"
7409 ) h;
7410 Buffer.add_string bb "</keymap>";
7413 loop rest
7415 loop c.keyhashes;
7419 let save () =
7420 let uifontsize = fstate.fontsize in
7421 let bb = Buffer.create 32768 in
7422 let relx = float state.x /. float state.winw in
7423 let w, h, x =
7424 let cx w = truncate (relx *. float w) in
7425 List.fold_left
7426 (fun (w, h, x) ws ->
7427 match ws with
7428 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7429 | Wsi.MaxVert -> (w, conf.cwinh, x)
7430 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7432 (state.winw, state.winh, state.x) state.winstate
7434 conf.cwinw <- w;
7435 conf.cwinh <- h;
7436 let f (h, dc) =
7437 let dc = if conf.bedefault then conf else dc in
7438 Buffer.add_string bb "<llppconfig>\n";
7440 if nonemptystr !fontpath
7441 then
7442 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7443 uifontsize
7444 !fontpath
7445 else (
7446 if uifontsize <> 14
7447 then
7448 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7451 Buffer.add_string bb "<defaults";
7452 add_attrs bb true dc dc;
7453 let kb = keymapsbuf true dc dc in
7454 if Buffer.length kb > 0
7455 then (
7456 Buffer.add_string bb ">\n";
7457 Buffer.add_buffer bb kb;
7458 Buffer.add_string bb "\n</defaults>\n";
7460 else Buffer.add_string bb "/>\n";
7462 let adddoc path pan anchor c bookmarks =
7463 if bookmarks == [] && c = dc && anchor = emptyanchor
7464 then ()
7465 else (
7466 Printf.bprintf bb "<doc path='%s'"
7467 (enent path 0 (String.length path));
7469 if anchor <> emptyanchor
7470 then (
7471 let n, rely, visy = anchor in
7472 Printf.bprintf bb " page='%d'" n;
7473 if rely > 1e-6
7474 then
7475 Printf.bprintf bb " rely='%f'" rely
7477 if abs_float visy > 1e-6
7478 then
7479 Printf.bprintf bb " visy='%f'" visy
7483 if pan != 0
7484 then Printf.bprintf bb " pan='%d'" pan;
7486 add_attrs bb false dc c;
7487 let kb = keymapsbuf false dc c in
7489 begin match bookmarks with
7490 | [] ->
7491 if Buffer.length kb > 0
7492 then (
7493 Buffer.add_string bb ">\n";
7494 Buffer.add_buffer bb kb;
7495 Buffer.add_string bb "\n</doc>\n";
7497 else Buffer.add_string bb "/>\n"
7498 | _ ->
7499 Buffer.add_string bb ">\n<bookmarks>\n";
7500 List.iter (fun (title, _, kind) ->
7501 begin match kind with
7502 | Oanchor (page, rely, visy) ->
7503 Printf.bprintf bb
7504 "<item title='%s' page='%d'"
7505 (enent title 0 (String.length title))
7506 page
7508 if rely > 1e-6
7509 then
7510 Printf.bprintf bb " rely='%f'" rely
7512 if abs_float visy > 1e-6
7513 then
7514 Printf.bprintf bb " visy='%f'" visy
7516 | Onone | Ouri _ | Oremote _ | Oremotedest _ | Olaunch _ ->
7517 failwith "unexpected link in bookmarks"
7518 end;
7519 Buffer.add_string bb "/>\n";
7520 ) bookmarks;
7521 Buffer.add_string bb "</bookmarks>";
7522 if Buffer.length kb > 0
7523 then (
7524 Buffer.add_string bb "\n";
7525 Buffer.add_buffer bb kb;
7527 Buffer.add_string bb "\n</doc>\n";
7528 end;
7532 let pan, conf =
7533 match state.mode with
7534 | Birdseye (c, pan, _, _, _) ->
7535 let beyecolumns =
7536 match conf.columns with
7537 | Cmulti ((c, _, _), _) -> Some c
7538 | Csingle _ -> None
7539 | Csplit _ -> None
7540 and columns =
7541 match c.columns with
7542 | Cmulti (c, _) -> Cmulti (c, [||])
7543 | Csingle _ -> Csingle [||]
7544 | Csplit _ -> failwith "quit from bird's eye while split"
7546 pan, { c with beyecolumns = beyecolumns; columns = columns }
7547 | _ -> x, conf
7549 let basename = Filename.basename
7550 (if emptystr state.origin then state.path else state.origin)
7552 adddoc basename pan (getanchor ())
7553 (let conf =
7554 let autoscrollstep =
7555 match state.autoscroll with
7556 | Some step -> step
7557 | None -> conf.autoscrollstep
7559 match state.mode with
7560 | Birdseye (bc, _, _, _, _) ->
7561 { conf with
7562 zoom = bc.zoom;
7563 presentation = bc.presentation;
7564 interpagespace = bc.interpagespace;
7565 maxwait = bc.maxwait;
7566 autoscrollstep = autoscrollstep }
7567 | _ -> { conf with autoscrollstep = autoscrollstep }
7568 in conf)
7569 (if conf.savebmarks then state.bookmarks else []);
7571 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7572 if basename <> path
7573 then adddoc path x anchor c bookmarks
7574 ) h;
7575 Buffer.add_string bb "</llppconfig>\n";
7576 true;
7578 if load1 f && Buffer.length bb > 0
7579 then
7581 let tmp = !confpath ^ ".tmp" in
7582 let oc = open_out_bin tmp in
7583 Buffer.output_buffer oc bb;
7584 close_out oc;
7585 Unix.rename tmp !confpath;
7586 with exn ->
7587 prerr_endline
7588 ("error while saving configuration: " ^ exntos exn)
7590 end;;
7592 let adderrmsg src msg =
7593 Buffer.add_string state.errmsgs msg;
7594 state.newerrmsgs <- true;
7595 G.postRedisplay src
7598 let adderrfmt src fmt =
7599 Format.kprintf (fun s -> adderrmsg src s) fmt;
7602 let ract cmds =
7603 let cl = splitatspace cmds in
7604 let scan s fmt f =
7605 try Scanf.sscanf s fmt f
7606 with exn ->
7607 adderrfmt "remote exec"
7608 "error processing '%S': %s\n" cmds (exntos exn)
7610 match cl with
7611 | "reload" :: [] -> reload ()
7612 | "goto" :: args :: [] ->
7613 scan args "%u %f %f"
7614 (fun pageno x y ->
7615 let cmd, _ = state.geomcmds in
7616 if emptystr cmd
7617 then gotopagexy pageno x y
7618 else
7619 let f prevf () =
7620 gotopagexy pageno x y;
7621 prevf ()
7623 state.reprf <- f state.reprf
7625 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7626 | "gotor" :: args :: [] ->
7627 scan args "%S %u"
7628 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
7629 | "gotord" :: args :: [] ->
7630 scan args "%S %S"
7631 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
7632 | "rect" :: args :: [] ->
7633 scan args "%u %u %f %f %f %f"
7634 (fun pageno color x0 y0 x1 y1 ->
7635 onpagerect pageno (fun w h ->
7636 let _,w1,h1,_ = getpagedim pageno in
7637 let sw = float w1 /. float w
7638 and sh = float h1 /. float h in
7639 let x0s = x0 *. sw
7640 and x1s = x1 *. sw
7641 and y0s = y0 *. sh
7642 and y1s = y1 *. sh in
7643 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7644 debugrect rect;
7645 state.rects <- (pageno, color, rect) :: state.rects;
7646 G.postRedisplay "rect";
7649 | "activatewin" :: [] -> Wsi.activatewin ()
7650 | "quit" :: [] -> raise Quit
7651 | _ ->
7652 adderrfmt "remote command"
7653 "error processing remote command: %S\n" cmds;
7656 let remote =
7657 let scratch = String.create 80 in
7658 let buf = Buffer.create 80 in
7659 fun fd ->
7660 let rec tempfr () =
7661 try Some (Unix.read fd scratch 0 80)
7662 with
7663 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7664 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7665 | exn -> raise exn
7667 match tempfr () with
7668 | None -> Some fd
7669 | Some n ->
7670 if n = 0
7671 then (
7672 Unix.close fd;
7673 if Buffer.length buf > 0
7674 then (
7675 let s = Buffer.contents buf in
7676 Buffer.clear buf;
7677 ract s;
7679 None
7681 else
7682 let rec eat ppos =
7683 let nlpos =
7685 let pos = String.index_from scratch ppos '\n' in
7686 if pos >= n then -1 else pos
7687 with Not_found -> -1
7689 if nlpos >= 0
7690 then (
7691 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7692 let s = Buffer.contents buf in
7693 Buffer.clear buf;
7694 ract s;
7695 eat (nlpos+1);
7697 else (
7698 Buffer.add_substring buf scratch ppos (n-ppos);
7699 Some fd
7701 in eat 0
7704 let remoteopen path =
7705 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7706 with exn ->
7707 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7708 None
7711 let () =
7712 let trimcachepath = ref "" in
7713 let rcmdpath = ref "" in
7714 selfexec := Sys.executable_name;
7715 Arg.parse
7716 (Arg.align
7717 [("-p", Arg.String (fun s -> state.password <- s),
7718 "<password> Set password");
7720 ("-f", Arg.String
7721 (fun s ->
7722 Config.fontpath := s;
7723 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7725 "<path> Set path to the user interface font");
7727 ("-c", Arg.String
7728 (fun s ->
7729 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7730 Config.confpath := s),
7731 "<path> Set path to the configuration file");
7733 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7734 "<path> Set path to the trim cache file");
7736 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7737 "<named-destination> Set named destination");
7739 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7740 ("-cxack", Arg.Set cxack, " Cut corners");
7742 ("-remote", Arg.String (fun s -> rcmdpath := s),
7743 "<path> Set path to the remote commands source");
7745 ("-origin", Arg.String (fun s -> state.origin <- s),
7746 "<original-path> Set original path");
7748 ("-v", Arg.Unit (fun () ->
7749 Printf.printf
7750 "%s\nconfiguration path: %s\n"
7751 (version ())
7752 Config.defconfpath
7754 exit 0), " Print version and exit");
7757 (fun s -> state.path <- s)
7758 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7760 if !wtmode
7761 then selfexec := !selfexec ^ " -wtmode";
7763 if emptystr state.path
7764 then (prerr_endline "file name missing"; exit 1);
7766 if not (Config.load ())
7767 then prerr_endline "failed to load configuration";
7769 let wsfd, winw, winh = Wsi.init (object
7770 val mutable m_hack = false
7771 method expose = if not m_hack then G.postRedisplay "expose"
7772 method visible = G.postRedisplay "visible"
7773 method display = m_hack <- false; display ()
7774 method reshape w h =
7775 m_hack <- w < state.winw && h < state.winh;
7776 reshape w h
7777 method mouse b d x y m = state.uioh <- state.uioh#button b d x y m
7778 method motion x y =
7779 state.mpos <- (x, y);
7780 state.uioh <- state.uioh#motion x y
7781 method pmotion x y =
7782 state.mpos <- (x, y);
7783 state.uioh <- state.uioh#pmotion x y
7784 method key k m =
7785 let mascm = m land (
7786 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7787 ) in
7788 match state.keystate with
7789 | KSnone ->
7790 let km = k, mascm in
7791 begin
7792 match
7793 let modehash = state.uioh#modehash in
7794 try Hashtbl.find modehash km
7795 with Not_found ->
7796 try Hashtbl.find (findkeyhash conf "global") km
7797 with Not_found -> KMinsrt (k, m)
7798 with
7799 | KMinsrt (k, m) -> keyboard k m
7800 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7801 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7803 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7804 List.iter (fun (k, m) -> keyboard k m) insrt;
7805 state.keystate <- KSnone
7806 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7807 state.keystate <- KSinto (keys, insrt)
7808 | _ ->
7809 state.keystate <- KSnone
7811 method enter x y =
7812 state.mpos <- (x, y);
7813 state.uioh <- state.uioh#pmotion x y
7814 method leave = state.mpos <- (-1, -1)
7815 method winstate wsl = state.winstate <- wsl; m_hack <- false
7816 method quit = raise Quit
7817 end) conf.cwinw conf.cwinh (platform = Posx) in
7819 state.wsfd <- wsfd;
7821 if not (
7822 List.exists GlMisc.check_extension
7823 [ "GL_ARB_texture_rectangle"
7824 ; "GL_EXT_texture_recangle"
7825 ; "GL_NV_texture_rectangle" ]
7827 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7829 if (
7830 let r = GlMisc.get_string `renderer in
7831 let p = "Mesa DRI Intel(" in
7832 let l = String.length p in
7833 String.length r > l && String.sub r 0 l = p
7835 then (
7836 defconf.sliceheight <- 1024;
7837 defconf.texcount <- 32;
7838 defconf.usepbo <- true;
7841 let cr, sw =
7842 match Ne.pipe () with
7843 | Ne.Exn exn ->
7844 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7845 exit 1
7846 | Ne.Res rw -> rw
7847 and sr, cw =
7848 match Ne.pipe () with
7849 | Ne.Exn exn ->
7850 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7851 exit 1
7852 | Ne.Res rw -> rw
7855 cloexec cr;
7856 cloexec sw;
7857 cloexec sr;
7858 cloexec cw;
7860 setcheckers conf.checkers;
7861 redirectstderr ();
7862 if conf.redirectstderr
7863 then
7864 at_exit (fun () ->
7865 let s = Buffer.contents state.errmsgs ^
7866 (match state.errfd with
7867 | Some fd ->
7868 let s = String.create (80*24) in
7869 let n =
7871 let r, _, _ = Unix.select [fd] [] [] 0.0 in
7872 if List.mem fd r
7873 then Unix.read fd s 0 (String.length s)
7874 else 0
7875 with _ -> 0
7877 if n = 0
7878 then ""
7879 else String.sub s 0 n
7880 | None -> ""
7883 try ignore (Unix.write state.stderr s 0 (String.length s))
7884 with exn -> print_endline (exntos exn)
7888 init (cr, cw) (
7889 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
7890 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
7891 !Config.fontpath, !trimcachepath,
7892 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
7894 List.iter GlArray.enable [`texture_coord; `vertex];
7895 state.sr <- sr;
7896 state.sw <- sw;
7897 state.text <- "Opening " ^ (mbtoutf8 state.path);
7898 reshape winw winh;
7899 opendoc state.path state.password;
7900 state.uioh <- uioh;
7901 display ();
7902 Wsi.mapwin ();
7903 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
7904 let optrfd =
7905 ref (
7906 if nonemptystr !rcmdpath
7907 then remoteopen !rcmdpath
7908 else None
7912 let rec loop deadline =
7913 let r =
7914 match state.errfd with
7915 | None -> [state.sr; state.wsfd]
7916 | Some fd -> [state.sr; state.wsfd; fd]
7918 let r =
7919 match !optrfd with
7920 | None -> r
7921 | Some fd -> fd :: r
7923 if state.redisplay
7924 then (
7925 state.redisplay <- false;
7926 display ();
7928 let timeout =
7929 let now = now () in
7930 if deadline > now
7931 then (
7932 if deadline = infinity
7933 then ~-.1.0
7934 else max 0.0 (deadline -. now)
7936 else 0.0
7938 let r, _, _ =
7939 try Unix.select r [] [] timeout
7940 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
7942 begin match r with
7943 | [] ->
7944 state.ghyll None;
7945 let newdeadline =
7946 if state.ghyll == noghyll
7947 then
7948 match state.autoscroll with
7949 | Some step when step != 0 ->
7950 let y = state.y + step in
7951 let y =
7952 if y < 0
7953 then state.maxy
7954 else if y >= state.maxy then 0 else y
7956 gotoy y;
7957 if state.mode = View
7958 then state.text <- "";
7959 deadline +. 0.01
7960 | _ -> infinity
7961 else deadline +. 0.01
7963 loop newdeadline
7965 | l ->
7966 let rec checkfds = function
7967 | [] -> ()
7968 | fd :: rest when fd = state.sr ->
7969 let cmd = readcmd state.sr in
7970 act cmd;
7971 checkfds rest
7973 | fd :: rest when fd = state.wsfd ->
7974 Wsi.readresp fd;
7975 checkfds rest
7977 | fd :: rest when Some fd = !optrfd ->
7978 begin match remote fd with
7979 | None -> optrfd := remoteopen !rcmdpath;
7980 | opt -> optrfd := opt
7981 end;
7982 checkfds rest
7984 | fd :: rest ->
7985 let s = String.create 80 in
7986 let n = tempfailureretry (Unix.read fd s 0) 80 in
7987 if conf.redirectstderr
7988 then (
7989 Buffer.add_substring state.errmsgs s 0 n;
7990 state.newerrmsgs <- true;
7991 state.redisplay <- true;
7993 else (
7994 prerr_string (String.sub s 0 n);
7995 flush stderr;
7997 checkfds rest
7999 checkfds l;
8000 let newdeadline =
8001 let deadline1 =
8002 if deadline = infinity
8003 then now () +. 0.01
8004 else deadline
8006 match state.autoscroll with
8007 | Some step when step != 0 -> deadline1
8008 | _ -> if state.ghyll == noghyll then infinity else deadline1
8010 loop newdeadline
8011 end;
8014 loop infinity;
8015 with Quit ->
8016 Config.save ();