Better error message
[llpp.git] / main.ml
blobc220111283490ec4daefcceac16db2f82228ffbe
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 multiclick : int -> int -> int -> int -> uioh
417 method motion : int -> int -> uioh
418 method pmotion : int -> int -> uioh
419 method infochanged : infochange -> unit
420 method scrollpw : (int * float * float)
421 method scrollph : (int * float * float)
422 method modehash : keyhash
423 method eformsgs : bool
424 end;;
426 type mode =
427 | Birdseye of (conf * leftx * pageno * pageno * anchor)
428 | Textentry of (textentry * onleave)
429 | View
430 | LinkNav of linktarget
431 and onleave = leavetextentrystatus -> unit
432 and leavetextentrystatus = | Cancel | Confirm
433 and helpitem = string * int * action
434 and action =
435 | Noaction
436 | Action of (uioh -> uioh)
437 and linktarget =
438 | Ltexact of (pageno * int)
439 | Ltgendir of int
442 let isbirdseye = function Birdseye _ -> true | _ -> false;;
443 let istextentry = function Textentry _ -> true | _ -> false;;
445 type currently =
446 | Idle
447 | Loading of (page * gen)
448 | Tiling of (
449 page * opaque * colorspace * angle * gen * col * row * width * height
451 | Outlining of outline list
454 let emptykeyhash = Hashtbl.create 0;;
455 let nouioh : uioh = object (self)
456 method display = ()
457 method key _ _ = self
458 method multiclick _ _ _ _ = self
459 method button _ _ _ _ _ = self
460 method motion _ _ = self
461 method pmotion _ _ = self
462 method infochanged _ = ()
463 method scrollpw = (0, nan, nan)
464 method scrollph = (0, nan, nan)
465 method modehash = emptykeyhash
466 method eformsgs = false
467 end;;
469 type state =
470 { mutable sr : Unix.file_descr
471 ; mutable sw : Unix.file_descr
472 ; mutable wsfd : Unix.file_descr
473 ; mutable errfd : Unix.file_descr option
474 ; mutable stderr : Unix.file_descr
475 ; mutable errmsgs : Buffer.t
476 ; mutable newerrmsgs : bool
477 ; mutable w : int
478 ; mutable x : int
479 ; mutable y : int
480 ; mutable anchor : anchor
481 ; mutable ranchors : (string * string * anchor * string) list
482 ; mutable maxy : int
483 ; mutable layout : page list
484 ; pagemap : (pagemapkey, opaque) Hashtbl.t
485 ; tilemap : (tilemapkey, tile) Hashtbl.t
486 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
487 ; mutable pdims : (pageno * width * height * leftx) list
488 ; mutable pagecount : int
489 ; mutable currently : currently
490 ; mutable mstate : mstate
491 ; mutable searchpattern : string
492 ; mutable rects : (pageno * recttype * rect) list
493 ; mutable rects1 : (pageno * recttype * rect) list
494 ; mutable text : string
495 ; mutable winstate : Wsi.winstate list
496 ; mutable mode : mode
497 ; mutable uioh : uioh
498 ; mutable outlines : outline array
499 ; mutable bookmarks : outline list
500 ; mutable path : string
501 ; mutable password : string
502 ; mutable nameddest : string
503 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
504 ; mutable memused : memsize
505 ; mutable gen : gen
506 ; mutable throttle : (page list * int * float) option
507 ; mutable autoscroll : int option
508 ; mutable ghyll : (int option -> unit)
509 ; mutable help : helpitem array
510 ; mutable docinfo : (int * string) list
511 ; mutable texid : GlTex.texture_id option
512 ; hists : hists
513 ; mutable prevzoom : (float * int)
514 ; mutable progress : float
515 ; mutable redisplay : bool
516 ; mutable mpos : mpos
517 ; mutable keystate : keystate
518 ; mutable glinks : bool
519 ; mutable prevcolumns : (columns * float) option
520 ; mutable winw : int
521 ; mutable winh : int
522 ; mutable reprf : (unit -> unit)
523 ; mutable origin : string
524 ; mutable roam : (unit -> unit)
525 ; mutable bzoom : bool
526 ; mutable traw : [`float] Raw.t
527 ; mutable vraw : [`float] Raw.t
529 and hists =
530 { pat : string circbuf
531 ; pag : string circbuf
532 ; nav : anchor circbuf
533 ; sel : string circbuf
537 let defconf =
538 { scrollbw = 7
539 ; scrollh = 12
540 ; scrollb = scrollbhv lor scrollbvv
541 ; icase = true
542 ; preload = true
543 ; pagebias = 0
544 ; verbose = false
545 ; debug = false
546 ; scrollstep = 24
547 ; hscrollstep = 24
548 ; maxhfit = true
549 ; crophack = false
550 ; autoscrollstep = 2
551 ; maxwait = None
552 ; hlinks = false
553 ; underinfo = false
554 ; interpagespace = 2
555 ; zoom = 1.0
556 ; presentation = false
557 ; angle = 0
558 ; cwinw = 900
559 ; cwinh = 900
560 ; savebmarks = true
561 ; fitmodel = FitProportional
562 ; trimmargins = false
563 ; trimfuzz = (0,0,0,0)
564 ; memlimit = 32 lsl 20
565 ; texcount = 256
566 ; sliceheight = 24
567 ; thumbw = 76
568 ; jumpback = true
569 ; bgcolor = (0.5, 0.5, 0.5)
570 ; bedefault = false
571 ; tilew = 2048
572 ; tileh = 2048
573 ; mustoresize = 256 lsl 20
574 ; checkers = true
575 ; aalevel = 8
576 ; urilauncher =
577 (match platform with
578 | Plinux | Pfreebsd | Pdragonflybsd
579 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
580 | Posx -> "open \"%s\""
581 | Pcygwin -> "cygstart \"%s\""
582 | Punknown -> "echo %s")
583 ; pathlauncher = "lp \"%s\""
584 ; selcmd =
585 (match platform with
586 | Plinux | Pfreebsd | Pdragonflybsd
587 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
588 | Posx -> "pbcopy"
589 | Pcygwin -> "wsel"
590 | Punknown -> "cat")
591 ; paxcmd = "cat"
592 ; colorspace = Rgb
593 ; invert = false
594 ; colorscale = 1.0
595 ; redirectstderr = false
596 ; ghyllscroll = None
597 ; columns = Csingle [||]
598 ; beyecolumns = None
599 ; updatecurs = false
600 ; hfsize = 12
601 ; pgscale = 1.0
602 ; usepbo = false
603 ; wheelbypage = false
604 ; stcmd = "echo SyncTex"
605 ; riani = false
606 ; pax = None
607 ; paxmark = Mark_word
608 ; keyhashes =
609 let mk n = (n, Hashtbl.create 1) in
610 [ mk "global"
611 ; mk "info"
612 ; mk "help"
613 ; mk "outline"
614 ; mk "listview"
615 ; mk "birdseye"
616 ; mk "textentry"
617 ; mk "links"
618 ; mk "view"
623 let wtmode = ref false;;
624 let cxack = ref false;;
626 let findkeyhash c name =
627 try List.assoc name c.keyhashes
628 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
631 let conf = { defconf with angle = defconf.angle };;
633 let pgscale h = truncate (float h *. conf.pgscale);;
635 type fontstate =
636 { mutable fontsize : int
637 ; mutable wwidth : float
638 ; mutable maxrows : int
642 let fstate =
643 { fontsize = 14
644 ; wwidth = nan
645 ; maxrows = -1
649 let geturl s =
650 let colonpos = try String.index s ':' with Not_found -> -1 in
651 let len = String.length s in
652 if colonpos >= 0 && colonpos + 3 < len
653 then (
654 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
655 then
656 let schemestartpos =
657 try String.rindex_from s colonpos ' '
658 with Not_found -> -1
660 let scheme =
661 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
663 match scheme with
664 | "http" | "ftp" | "mailto" ->
665 let epos =
666 try String.index_from s colonpos ' '
667 with Not_found -> len
669 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
670 | _ -> ""
671 else ""
673 else ""
676 let gotouri uri =
677 if emptystr conf.urilauncher
678 then print_endline uri
679 else (
680 let url = geturl uri in
681 if emptystr url
682 then Printf.eprintf "obtained empty url from uri %S" uri
683 else
684 let re = Str.regexp "%s" in
685 let command = Str.global_replace re url conf.urilauncher in
686 try popen command []
687 with exn ->
688 Printf.eprintf
689 "failed to execute `%s': %s\n" command (exntos exn);
690 flush stderr;
694 let version () =
695 Printf.sprintf "llpp version %s, fitz %s, ocaml %s (%s/%dbit)"
696 Help.version (fz_version ()) Sys.ocaml_version
697 (platform_to_string platform) Sys.word_size
700 let makehelp () =
701 let strings = version () :: "" :: Help.keys in
702 Array.of_list (
703 List.map (fun s ->
704 let url = geturl s in
705 if nonemptystr url
706 then (s, 0, Action (fun u -> gotouri url; u))
707 else (s, 0, Noaction)
708 ) strings);
711 let noghyll _ = ();;
712 let firstgeomcmds = "", [];;
713 let noreprf () = ();;
714 let noroam () = ();;
716 let state =
717 { sr = Unix.stdin
718 ; sw = Unix.stdin
719 ; wsfd = Unix.stdin
720 ; errfd = None
721 ; stderr = Unix.stderr
722 ; errmsgs = Buffer.create 0
723 ; newerrmsgs = false
724 ; x = 0
725 ; y = 0
726 ; w = 0
727 ; anchor = emptyanchor
728 ; ranchors = []
729 ; layout = []
730 ; maxy = max_int
731 ; tilelru = Queue.create ()
732 ; pagemap = Hashtbl.create 10
733 ; tilemap = Hashtbl.create 10
734 ; pdims = []
735 ; pagecount = 0
736 ; currently = Idle
737 ; mstate = Mnone
738 ; rects = []
739 ; rects1 = []
740 ; text = ""
741 ; mode = View
742 ; winstate = []
743 ; searchpattern = ""
744 ; outlines = [||]
745 ; bookmarks = []
746 ; path = ""
747 ; password = ""
748 ; nameddest = ""
749 ; geomcmds = firstgeomcmds
750 ; hists =
751 { nav = cbnew 10 emptyanchor
752 ; pat = cbnew 10 ""
753 ; pag = cbnew 10 ""
754 ; sel = cbnew 10 ""
756 ; memused = 0
757 ; gen = 0
758 ; throttle = None
759 ; autoscroll = None
760 ; ghyll = noghyll
761 ; help = makehelp ()
762 ; docinfo = []
763 ; texid = None
764 ; prevzoom = (1.0, 0)
765 ; progress = -1.0
766 ; uioh = nouioh
767 ; redisplay = true
768 ; mpos = (-1, -1)
769 ; keystate = KSnone
770 ; glinks = false
771 ; prevcolumns = None
772 ; winw = -1
773 ; winh = -1
774 ; reprf = noreprf
775 ; origin = ""
776 ; roam = noroam
777 ; bzoom = false
778 ; traw = Raw.create_static `float 8
779 ; vraw = Raw.create_static `float 8
783 let hscrollh () =
784 if (conf.scrollb land scrollbhv = 0)
785 || (state.x = 0 && state.w <= state.winw - conf.scrollbw)
786 then 0
787 else conf.scrollbw
790 let vscrollw () =
791 if (conf.scrollb land scrollbvv = 0)
792 then 0
793 else conf.scrollbw
796 let wadjsb w = w - vscrollw ();;
798 let setfontsize n =
799 fstate.fontsize <- n;
800 fstate.wwidth <- measurestr fstate.fontsize "w";
801 fstate.maxrows <- (state.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
804 let vlog fmt =
805 if conf.verbose
806 then
807 Printf.kprintf prerr_endline fmt
808 else
809 Printf.kprintf ignore fmt
812 let launchpath () =
813 if emptystr conf.pathlauncher
814 then print_endline state.path
815 else (
816 let re = Str.regexp "%s" in
817 let command = Str.global_replace re state.path conf.pathlauncher in
818 try popen command []
819 with exn ->
820 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
821 flush stderr;
825 module Ne = struct
826 type 'a t = | Res of 'a | Exn of exn;;
828 let pipe () =
829 try Res (Unix.pipe ())
830 with exn -> Exn exn
833 let clo fd f =
834 try tempfailureretry Unix.close fd
835 with exn -> f (exntos exn)
838 let dup fd =
839 try Res (tempfailureretry Unix.dup fd)
840 with exn -> Exn exn
843 let dup2 fd1 fd2 =
844 try Res (tempfailureretry (Unix.dup2 fd1) fd2)
845 with exn -> Exn exn
847 end;;
849 let redirectstderr () =
850 let clofail what errmsg = dolog "failed to close %s: %s" what errmsg in
851 if conf.redirectstderr
852 then
853 match Ne.pipe () with
854 | Ne.Exn exn ->
855 dolog "failed to create stderr redirection pipes: %s" (exntos exn)
857 | Ne.Res (r, w) ->
858 begin match Ne.dup Unix.stderr with
859 | Ne.Exn exn ->
860 dolog "failed to dup stderr: %s" (exntos exn);
861 Ne.clo r (clofail "pipe/r");
862 Ne.clo w (clofail "pipe/w");
864 | Ne.Res dupstderr ->
865 begin match Ne.dup2 w Unix.stderr with
866 | Ne.Exn exn ->
867 dolog "failed to dup2 to stderr: %s" (exntos exn);
868 Ne.clo dupstderr (clofail "stderr duplicate");
869 Ne.clo r (clofail "redir pipe/r");
870 Ne.clo w (clofail "redir pipe/w");
872 | Ne.Res () ->
873 state.stderr <- dupstderr;
874 state.errfd <- Some r;
875 end;
877 else (
878 state.newerrmsgs <- false;
879 begin match state.errfd with
880 | Some fd ->
881 begin match Ne.dup2 state.stderr Unix.stderr with
882 | Ne.Exn exn ->
883 dolog "failed to dup2 original stderr: %s" (exntos exn)
884 | Ne.Res () ->
885 Ne.clo fd (clofail "dup of stderr");
886 state.errfd <- None;
887 end;
888 | None -> ()
889 end;
890 prerr_string (Buffer.contents state.errmsgs);
891 flush stderr;
892 Buffer.clear state.errmsgs;
896 module G =
897 struct
898 let postRedisplay who =
899 if conf.verbose
900 then prerr_endline ("redisplay for " ^ who);
901 state.redisplay <- true;
903 end;;
905 let getopaque pageno =
906 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
907 with Not_found -> None
910 let putopaque pageno opaque =
911 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
914 let pagetranslatepoint l x y =
915 let dy = y - l.pagedispy in
916 let y = dy + l.pagey in
917 let dx = x - l.pagedispx in
918 let x = dx + l.pagex in
919 (x, y);
922 let onppundermouse g x y d =
923 let rec f = function
924 | l :: rest ->
925 begin match getopaque l.pageno with
926 | Some opaque ->
927 let x0 = l.pagedispx in
928 let x1 = x0 + l.pagevw in
929 let y0 = l.pagedispy in
930 let y1 = y0 + l.pagevh in
931 if y >= y0 && y <= y1 && x >= x0 && x <= x1
932 then
933 let px, py = pagetranslatepoint l x y in
934 match g opaque l px py with
935 | Some res -> res
936 | None -> f rest
937 else f rest
938 | _ ->
939 f rest
941 | [] -> d
943 f state.layout
946 let getunder x y =
947 let g opaque l px py =
948 if state.bzoom
949 then (
950 match rectofblock opaque px py with
951 | Some a ->
952 let rect = (a.(0),a.(2),a.(1),a.(2),a.(1),a.(3),a.(0),a.(3)) in
953 state.rects <- [l.pageno, l.pageno mod 3, rect];
954 G.postRedisplay "getunder";
955 | None -> ()
957 match whatsunder opaque px py with
958 | Unone -> None
959 | under -> Some under
961 onppundermouse g x y Unone
964 let unproject x y =
965 let g opaque l x y =
966 match unproject opaque x y with
967 | Some (x, y) -> Some (Some (l.pageno, x, y))
968 | None -> None
970 onppundermouse g x y None;
973 let showtext c s =
974 state.text <- Printf.sprintf "%c%s" c s;
975 G.postRedisplay "showtext";
978 let paxunder x y =
979 let g opaque l px py =
980 if markunder opaque px py conf.paxmark
981 then (
982 Some (fun () ->
983 match getopaque l.pageno with
984 | None -> ()
985 | Some opaque ->
986 match Ne.pipe () with
987 | Ne.Exn exn ->
988 showtext '!'
989 (Printf.sprintf
990 "can not create mark pipe: %s"
991 (exntos exn));
992 | Ne.Res (r, w) ->
993 let doclose what fd =
994 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
996 begin try
997 popen conf.paxcmd [r, 0; w, -1];
998 with exn ->
999 doclose "paxunder pipe/w" w;
1000 dolog "can not execute %S: %s" conf.paxcmd (exntos exn);
1001 end;
1002 copysel w opaque false;
1003 G.postRedisplay "paxunder";
1004 doclose "paxunder pipe/r" r;
1007 else None
1009 G.postRedisplay "paxunder";
1010 if conf.paxmark = Mark_page
1011 then
1012 List.iter (fun l ->
1013 match getopaque l.pageno with
1014 | None -> ()
1015 | Some opaque -> clearmark opaque) state.layout;
1016 state.roam <-
1017 onppundermouse g x y (fun () -> showtext '!' "Whoopsie daisy");
1020 let selstring s =
1021 match Ne.pipe () with
1022 | Ne.Exn exn ->
1023 showtext '!' (Printf.sprintf "pipe failed: %s" (exntos exn))
1024 | Ne.Res (r, w) ->
1025 let clo cap fd =
1026 Ne.clo fd (fun msg ->
1027 showtext '!' (Printf.sprintf "failed to close %s: %s" cap msg)
1030 let popened =
1031 try popen conf.selcmd [r, 0; w, -1]; true
1032 with exn ->
1033 clo "selstring pipe/w" w;
1034 showtext '!'
1035 (Printf.sprintf "failed to execute %s: %s"
1036 conf.selcmd (exntos exn));
1037 false
1039 if popened
1040 then (
1042 let l = String.length s in
1043 let n = tempfailureretry (Unix.write w s 0) l in
1044 if n != l
1045 then
1046 showtext '!'
1047 (Printf.sprintf
1048 "failed to write %d characters to sel pipe, wrote %d"
1051 with exn ->
1052 showtext '!'
1053 (Printf.sprintf "failed to write to sel pipe: %s"
1054 (exntos exn)
1057 else dolog "%s" s;
1058 clo "selstring pipe/r" r;
1061 let undertext = function
1062 | Unone -> "none"
1063 | Ulinkuri s -> s
1064 | Ulinkgoto (pageno, _) -> Printf.sprintf "%s: page %d" state.path (pageno+1)
1065 | Utext s -> "font: " ^ s
1066 | Uunexpected s -> "unexpected: " ^ s
1067 | Ulaunch s -> "launch: " ^ s
1068 | Unamed s -> "named: " ^ s
1069 | Uremote (filename, pageno) ->
1070 Printf.sprintf "%s: page %d" filename (pageno+1)
1071 | Uremotedest (filename, destname) ->
1072 Printf.sprintf "%s: destination %S" filename destname
1075 let updateunder x y =
1076 match getunder x y with
1077 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
1078 | Ulinkuri uri ->
1079 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
1080 Wsi.setcursor Wsi.CURSOR_INFO
1081 | Ulinkgoto (pageno, _) ->
1082 if conf.underinfo
1083 then showtext 'p' ("age: " ^ string_of_int (pageno+1));
1084 Wsi.setcursor Wsi.CURSOR_INFO
1085 | Utext s ->
1086 if conf.underinfo then showtext 'f' ("ont: " ^ s);
1087 Wsi.setcursor Wsi.CURSOR_TEXT
1088 | Uunexpected s ->
1089 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
1090 Wsi.setcursor Wsi.CURSOR_INHERIT
1091 | Ulaunch s ->
1092 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
1093 Wsi.setcursor Wsi.CURSOR_INHERIT
1094 | Unamed s ->
1095 if conf.underinfo then showtext 'n' ("amed: " ^ s);
1096 Wsi.setcursor Wsi.CURSOR_INHERIT
1097 | Uremote (filename, pageno) ->
1098 if conf.underinfo then showtext 'r'
1099 (Printf.sprintf "emote: %s (%d)" filename (pageno+1));
1100 Wsi.setcursor Wsi.CURSOR_INFO
1101 | Uremotedest (filename, destname) ->
1102 if conf.underinfo then showtext 'r'
1103 (Printf.sprintf "emote destination: %s (%S)" filename destname);
1104 Wsi.setcursor Wsi.CURSOR_INFO
1107 let showlinktype under =
1108 if conf.underinfo
1109 then
1110 match under with
1111 | Unone -> ()
1112 | under ->
1113 let s = undertext under in
1114 showtext ' ' s
1117 let addchar s c =
1118 let b = Buffer.create (String.length s + 1) in
1119 Buffer.add_string b s;
1120 Buffer.add_char b c;
1121 Buffer.contents b;
1124 module type TextEnumType =
1126 type t
1127 val name : string
1128 val names : string array
1129 end;;
1131 module TextEnumMake (Ten : TextEnumType) =
1132 struct
1133 let names = Ten.names;;
1134 let to_int (t : Ten.t) = Obj.magic t;;
1135 let to_string t = names.(to_int t);;
1136 let of_int n : Ten.t = Obj.magic n;;
1137 let of_string s =
1138 let rec find i =
1139 if i = Array.length names
1140 then failwith ("invalid " ^ Ten.name ^ ": " ^ s)
1141 else (
1142 if Ten.names.(i) = s
1143 then of_int i
1144 else find (i+1)
1146 in find 0;;
1147 end;;
1149 module CSTE = TextEnumMake (struct
1150 type t = colorspace;;
1151 let name = "colorspace";;
1152 let names = [|"rgb"; "bgr"; "gray"|];;
1153 end);;
1155 module MTE = TextEnumMake (struct
1156 type t = mark;;
1157 let name = "mark";;
1158 let names = [|"page"; "block"; "line"; "word"|];;
1159 end);;
1161 module FMTE = TextEnumMake (struct
1162 type t= fitmodel;;
1163 let name = "fitmodel";;
1164 let names = [|"width"; "proportional"; "page"|];;
1165 end);;
1167 let intentry_with_suffix text key =
1168 let c =
1169 if key >= 32 && key < 127
1170 then Char.chr key
1171 else '\000'
1173 match Char.lowercase c with
1174 | '0' .. '9' ->
1175 let text = addchar text c in
1176 TEcont text
1178 | 'k' | 'm' | 'g' ->
1179 let text = addchar text c in
1180 TEcont text
1182 | _ ->
1183 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
1184 TEcont text
1187 let multicolumns_to_string (n, a, b) =
1188 if a = 0 && b = 0
1189 then Printf.sprintf "%d" n
1190 else Printf.sprintf "%d,%d,%d" n a b;
1193 let multicolumns_of_string s =
1195 (int_of_string s, 0, 0)
1196 with _ ->
1197 Scanf.sscanf s "%u,%u,%u" (fun n a b ->
1198 if a > 1 || b > 1
1199 then failwith "subtly broken"; (n, a, b)
1203 let readcmd fd =
1204 let s = "xxxx" in
1205 let n = tempfailureretry (Unix.read fd s 0) 4 in
1206 if n != 4 then error "incomplete read(len) = %d" n;
1207 let len = 0
1208 lor (Char.code s.[0] lsl 24)
1209 lor (Char.code s.[1] lsl 16)
1210 lor (Char.code s.[2] lsl 8)
1211 lor (Char.code s.[3] lsl 0)
1213 let s = String.create len in
1214 let n = tempfailureretry (Unix.read fd s 0) len in
1215 if n != len then error "incomplete read(data) %d vs %d" n len;
1219 let btod b = if b then 1 else 0;;
1221 let wcmd fmt =
1222 let b = Buffer.create 16 in
1223 Buffer.add_string b "llll";
1224 Printf.kbprintf
1225 (fun b ->
1226 let s = Buffer.contents b in
1227 let n = String.length s in
1228 let len = n - 4 in
1229 (* dolog "wcmd %S" (String.sub s 4 len); *)
1230 s.[0] <- Char.chr ((len lsr 24) land 0xff);
1231 s.[1] <- Char.chr ((len lsr 16) land 0xff);
1232 s.[2] <- Char.chr ((len lsr 8) land 0xff);
1233 s.[3] <- Char.chr (len land 0xff);
1234 let n' = tempfailureretry (Unix.write state.sw s 0) n in
1235 if n' != n then error "write failed %d vs %d" n' n;
1236 ) b fmt;
1239 let calcips h =
1240 let d = state.winh - h in
1241 max conf.interpagespace ((d + 1) / 2)
1244 let rowyh (c, coverA, coverB) b n =
1245 if c = 1 || (n < coverA || n >= state.pagecount - coverB)
1246 then
1247 let _, _, vy, (_, _, h, _) = b.(n) in
1248 (vy, h)
1249 else
1250 let n' = n - coverA in
1251 let d = n' mod c in
1252 let s = n - d in
1253 let e = min state.pagecount (s + c) in
1254 let rec find m miny maxh = if m = e then miny, maxh else
1255 let _, _, y, (_, _, h, _) = b.(m) in
1256 let miny = min miny y in
1257 let maxh = max maxh h in
1258 find (m+1) miny maxh
1259 in find s max_int 0
1262 let calcheight () =
1263 match conf.columns with
1264 | Cmulti ((_, _, _) as cl, b) ->
1265 if Array.length b > 0
1266 then
1267 let y, h = rowyh cl b (Array.length b - 1) in
1268 y + h + (if conf.presentation then calcips h else 0)
1269 else 0
1270 | Csingle b ->
1271 if Array.length b > 0
1272 then
1273 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1274 y + h + (if conf.presentation then calcips h else 0)
1275 else 0
1276 | Csplit (_, b) ->
1277 if Array.length b > 0
1278 then
1279 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
1280 y + h
1281 else 0
1284 let getpageywh pageno =
1285 let pageno = bound pageno 0 (state.pagecount-1) in
1286 match conf.columns with
1287 | Csingle b ->
1288 if Array.length b = 0
1289 then 0, 0, 0
1290 else
1291 let (_, _, y, (_, w, h, _)) = b.(pageno) in
1292 let y =
1293 if conf.presentation
1294 then y - calcips h
1295 else y
1297 y, w, h
1298 | Cmulti (cl, b) ->
1299 if Array.length b = 0
1300 then 0, 0, 0
1301 else
1302 let y, h = rowyh cl b pageno in
1303 let (_, _, _, (_, w, _, _)) = b.(pageno) in
1304 let y =
1305 if conf.presentation
1306 then y - calcips h
1307 else y
1309 y, w, h
1310 | Csplit (c, b) ->
1311 if Array.length b = 0
1312 then 0, 0, 0
1313 else
1314 let n = pageno*c in
1315 let (_, _, y, (_, w, h, _)) = b.(n) in
1316 y, w / c, h
1319 let getpageyh pageno =
1320 let y,_,h = getpageywh pageno in
1321 y, h;
1324 let getpagedim pageno =
1325 let rec f ppdim l =
1326 match l with
1327 | (n, _, _, _) as pdim :: rest ->
1328 if n >= pageno
1329 then (if n = pageno then pdim else ppdim)
1330 else f pdim rest
1332 | [] -> ppdim
1334 f (-1, -1, -1, -1) state.pdims
1337 let getpagey pageno = fst (getpageyh pageno);;
1339 let nogeomcmds cmds =
1340 match cmds with
1341 | s, [] -> emptystr s
1342 | _ -> false
1345 let page_of_y y =
1346 let ((c, coverA, coverB) as cl), b =
1347 match conf.columns with
1348 | Csingle b -> (1, 0, 0), b
1349 | Cmulti (c, b) -> c, b
1350 | Csplit (_, b) -> (1, 0, 0), b
1352 if Array.length b = 0
1353 then -1
1354 else
1355 let rec bsearch nmin nmax =
1356 if nmin > nmax
1357 then bound nmin 0 (state.pagecount-1)
1358 else
1359 let n = (nmax + nmin) / 2 in
1360 let vy, h = rowyh cl b n in
1361 let y0, y1 =
1362 if conf.presentation
1363 then
1364 let ips = calcips h in
1365 let y0 = vy - ips in
1366 let y1 = vy + h + ips in
1367 y0, y1
1368 else (
1369 if n = 0
1370 then 0, vy + h + conf.interpagespace
1371 else
1372 let y0 = vy - conf.interpagespace in
1373 y0, y0 + h + conf.interpagespace
1376 if y >= y0 && y < y1
1377 then (
1378 if c = 1
1379 then n
1380 else (
1381 if n > coverA
1382 then
1383 if n < state.pagecount - coverB
1384 then ((n-coverA)/c)*c + coverA
1385 else n
1386 else n
1389 else (
1390 if y > y0
1391 then bsearch (n+1) nmax
1392 else bsearch nmin (n-1)
1395 bsearch 0 (state.pagecount-1);
1398 let layoutN ((columns, coverA, coverB), b) y sh =
1399 let sh = sh - (hscrollh ()) in
1400 let rec fold accu n =
1401 if n = Array.length b
1402 then accu
1403 else
1404 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1405 if (vy - y) > sh &&
1406 (n = coverA - 1
1407 || n = state.pagecount - coverB
1408 || (n - coverA) mod columns = columns - 1)
1409 then accu
1410 else
1411 let accu =
1412 if vy + h > y
1413 then
1414 let pagey = max 0 (y - vy) in
1415 let pagedispy = if pagey > 0 then 0 else vy - y in
1416 let pagedispx, pagex =
1417 let pdx =
1418 if n = coverA - 1 || n = state.pagecount - coverB
1419 then state.x + (wadjsb state.winw - w) / 2
1420 else dx + xoff + state.x
1422 if pdx < 0
1423 then 0, -pdx
1424 else pdx, 0
1426 let pagevw =
1427 let vw = wadjsb state.winw - pagedispx in
1428 let pw = w - pagex in
1429 min vw pw
1431 let pagevh = min (h - pagey) (sh - pagedispy) in
1432 if pagevw > 0 && pagevh > 0
1433 then
1434 let e =
1435 { pageno = n
1436 ; pagedimno = pdimno
1437 ; pagew = w
1438 ; pageh = h
1439 ; pagex = pagex
1440 ; pagey = pagey
1441 ; pagevw = pagevw
1442 ; pagevh = pagevh
1443 ; pagedispx = pagedispx
1444 ; pagedispy = pagedispy
1445 ; pagecol = 0
1448 e :: accu
1449 else
1450 accu
1451 else
1452 accu
1454 fold accu (n+1)
1456 if Array.length b = 0
1457 then []
1458 else List.rev (fold [] (page_of_y y))
1461 let layoutS (columns, b) y sh =
1462 let sh = sh - hscrollh () in
1463 let rec fold accu n =
1464 if n = Array.length b
1465 then accu
1466 else
1467 let pdimno, px, vy, (_, pagew, pageh, xoff) = b.(n) in
1468 if (vy - y) > sh
1469 then accu
1470 else
1471 let accu =
1472 if vy + pageh > y
1473 then
1474 let x = xoff + state.x in
1475 let pagey = max 0 (y - vy) in
1476 let pagedispy = if pagey > 0 then 0 else vy - y in
1477 let pagedispx, pagex =
1478 if px = 0
1479 then (
1480 if x < 0
1481 then 0, -x
1482 else x, 0
1484 else (
1485 let px = px - x in
1486 if px < 0
1487 then -px, 0
1488 else 0, px
1491 let pagecolw = pagew/columns in
1492 let pagedispx =
1493 if pagecolw < state.winw
1494 then pagedispx + ((wadjsb state.winw - pagecolw) / 2)
1495 else pagedispx
1497 let pagevw =
1498 let vw = wadjsb state.winw - pagedispx in
1499 let pw = pagew - pagex in
1500 min vw pw
1502 let pagevw = min pagevw pagecolw in
1503 let pagevh = min (pageh - pagey) (sh - pagedispy) in
1504 if pagevw > 0 && pagevh > 0
1505 then
1506 let e =
1507 { pageno = n/columns
1508 ; pagedimno = pdimno
1509 ; pagew = pagew
1510 ; pageh = pageh
1511 ; pagex = pagex
1512 ; pagey = pagey
1513 ; pagevw = pagevw
1514 ; pagevh = pagevh
1515 ; pagedispx = pagedispx
1516 ; pagedispy = pagedispy
1517 ; pagecol = n mod columns
1520 e :: accu
1521 else
1522 accu
1523 else
1524 accu
1526 fold accu (n+1)
1528 List.rev (fold [] 0)
1531 let layout y sh =
1532 if nogeomcmds state.geomcmds
1533 then
1534 match conf.columns with
1535 | Csingle b -> layoutN ((1, 0, 0), b) y sh
1536 | Cmulti c -> layoutN c y sh
1537 | Csplit s -> layoutS s y sh
1538 else []
1541 let clamp incr =
1542 let y = state.y + incr in
1543 let y = max 0 y in
1544 let y = min y (state.maxy - (if conf.maxhfit then state.winh else 0)) in
1548 let itertiles l f =
1549 let tilex = l.pagex mod conf.tilew in
1550 let tiley = l.pagey mod conf.tileh in
1552 let col = l.pagex / conf.tilew in
1553 let row = l.pagey / conf.tileh in
1555 let rec rowloop row y0 dispy h =
1556 if h = 0
1557 then ()
1558 else (
1559 let dh = conf.tileh - y0 in
1560 let dh = min h dh in
1561 let rec colloop col x0 dispx w =
1562 if w = 0
1563 then ()
1564 else (
1565 let dw = conf.tilew - x0 in
1566 let dw = min w dw in
1568 f col row dispx dispy x0 y0 dw dh;
1569 colloop (col+1) 0 (dispx+dw) (w-dw)
1572 colloop col tilex l.pagedispx l.pagevw;
1573 rowloop (row+1) 0 (dispy+dh) (h-dh)
1576 if l.pagevw > 0 && l.pagevh > 0
1577 then rowloop row tiley l.pagedispy l.pagevh;
1580 let gettileopaque l col row =
1581 let key =
1582 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1584 try Some (Hashtbl.find state.tilemap key)
1585 with Not_found -> None
1588 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1589 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1590 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1593 let filledrect x0 y0 x1 y1 =
1594 GlArray.disable `texture_coord;
1595 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
1596 GlArray.vertex `two state.vraw;
1597 GlArray.draw_arrays `triangle_strip 0 4;
1598 GlArray.enable `texture_coord;
1601 let linerect x0 y0 x1 y1 =
1602 GlArray.disable `texture_coord;
1603 Raw.sets_float state.vraw ~pos:0 [| x0; y0; x0; y1; x1; y1; x1; y0 |];
1604 GlArray.vertex `two state.vraw;
1605 GlArray.draw_arrays `line_loop 0 4;
1606 GlArray.enable `texture_coord;
1609 let drawtiles l color =
1610 GlDraw.color color;
1611 begintiles ();
1612 let f col row x y tilex tiley w h =
1613 match gettileopaque l col row with
1614 | Some (opaque, _, t) ->
1615 let params = x, y, w, h, tilex, tiley in
1616 if conf.invert
1617 then (
1618 Gl.enable `blend;
1619 GlFunc.blend_func `zero `one_minus_src_color;
1621 drawtile params opaque;
1622 if conf.invert
1623 then Gl.disable `blend;
1624 if conf.debug
1625 then (
1626 endtiles ();
1627 let s = Printf.sprintf
1628 "%d[%d,%d] %f sec"
1629 l.pageno col row t
1631 let w = measurestr fstate.fontsize s in
1632 GlDraw.color (0.0, 0.0, 0.0);
1633 filledrect (float (x-2))
1634 (float (y-2))
1635 (float (x+2) +. w)
1636 (float (y + fstate.fontsize + 2));
1637 GlDraw.color (1.0, 1.0, 1.0);
1638 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1639 begintiles ();
1642 | None ->
1643 endtiles ();
1644 let w =
1645 let lw = wadjsb state.winw - x in
1646 min lw w
1647 and h =
1648 let lh = state.winh - y in
1649 min lh h
1651 if conf.invert
1652 then (
1653 Gl.enable `blend;
1654 GlFunc.blend_func `zero `one_minus_src_color;
1656 begin match state.texid with
1657 | Some id ->
1658 Gl.enable `texture_2d;
1659 GlTex.bind_texture `texture_2d id;
1660 let x0 = float x
1661 and y0 = float y
1662 and x1 = float (x+w)
1663 and y1 = float (y+h) in
1665 let tw = float w /. 16.0
1666 and th = float h /. 16.0 in
1667 let tx0 = float tilex /. 16.0
1668 and ty0 = float tiley /. 16.0 in
1669 let tx1 = tx0 +. tw
1670 and ty1 = ty0 +. th in
1671 Raw.sets_float state.vraw ~pos:0
1672 [| x0; y0; x0; y1; x1; y0; x1; y1 |];
1673 Raw.sets_float state.traw ~pos:0
1674 [| tx0; ty0; tx0; ty1; tx1; ty0; tx1; ty1 |];
1675 GlArray.vertex `two state.vraw;
1676 GlArray.tex_coord `two state.traw;
1677 GlArray.draw_arrays `triangle_strip 0 4;
1679 | None ->
1680 GlDraw.color (1.0, 1.0, 1.0);
1681 filledrect (float x) (float y) (float (x+w)) (float (y+h));
1682 end;
1683 if w > 128 && h > fstate.fontsize + 10
1684 then (
1685 let c = if conf.invert then 1.0 else 0.0 in
1686 GlDraw.color (c, c, c);
1687 let c, r =
1688 if conf.verbose
1689 then (col*conf.tilew, row*conf.tileh)
1690 else col, row
1692 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1694 GlDraw.color color;
1695 begintiles ();
1697 itertiles l f;
1698 endtiles ();
1701 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1703 let tilevisible1 l x y =
1704 let ax0 = l.pagex
1705 and ax1 = l.pagex + l.pagevw
1706 and ay0 = l.pagey
1707 and ay1 = l.pagey + l.pagevh in
1709 let bx0 = x
1710 and by0 = y in
1711 let bx1 = min (bx0 + conf.tilew) l.pagew
1712 and by1 = min (by0 + conf.tileh) l.pageh in
1714 let rx0 = max ax0 bx0
1715 and ry0 = max ay0 by0
1716 and rx1 = min ax1 bx1
1717 and ry1 = min ay1 by1 in
1719 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1720 nonemptyintersection
1723 let tilevisible layout n x y =
1724 let rec findpageinlayout m = function
1725 | l :: rest when l.pageno = n ->
1726 tilevisible1 l x y || (
1727 match conf.columns with
1728 | Csplit (c, _) when c > m -> findpageinlayout (m+1) rest
1729 | _ -> false
1731 | _ :: rest -> findpageinlayout 0 rest
1732 | [] -> false
1734 findpageinlayout 0 layout;
1737 let tileready l x y =
1738 tilevisible1 l x y &&
1739 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1742 let tilepage n p layout =
1743 let rec loop = function
1744 | l :: rest ->
1745 if l.pageno = n
1746 then
1747 let f col row _ _ _ _ _ _ =
1748 if state.currently = Idle
1749 then
1750 match gettileopaque l col row with
1751 | Some _ -> ()
1752 | None ->
1753 let x = col*conf.tilew
1754 and y = row*conf.tileh in
1755 let w =
1756 let w = l.pagew - x in
1757 min w conf.tilew
1759 let h =
1760 let h = l.pageh - y in
1761 min h conf.tileh
1763 let pbo =
1764 if conf.usepbo
1765 then getpbo w h conf.colorspace
1766 else "0"
1768 wcmd "tile %s %d %d %d %d %s" p x y w h pbo;
1769 state.currently <-
1770 Tiling (
1771 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1772 conf.tilew, conf.tileh
1775 itertiles l f;
1776 else
1777 loop rest
1779 | [] -> ()
1781 if nogeomcmds state.geomcmds
1782 then loop layout;
1785 let preloadlayout y =
1786 let y = if y < state.winh then 0 else y - state.winh in
1787 let h = state.winh*3 in
1788 layout y h;
1791 let load pages =
1792 let rec loop pages =
1793 if state.currently != Idle
1794 then ()
1795 else
1796 match pages with
1797 | l :: rest ->
1798 begin match getopaque l.pageno with
1799 | None ->
1800 wcmd "page %d %d" l.pageno l.pagedimno;
1801 state.currently <- Loading (l, state.gen);
1802 | Some opaque ->
1803 tilepage l.pageno opaque pages;
1804 loop rest
1805 end;
1806 | _ -> ()
1808 if nogeomcmds state.geomcmds
1809 then loop pages
1812 let preload pages =
1813 load pages;
1814 if conf.preload && state.currently = Idle
1815 then load (preloadlayout state.y);
1818 let layoutready layout =
1819 let rec fold all ls =
1820 all && match ls with
1821 | l :: rest ->
1822 let seen = ref false in
1823 let allvisible = ref true in
1824 let foo col row _ _ _ _ _ _ =
1825 seen := true;
1826 allvisible := !allvisible &&
1827 begin match gettileopaque l col row with
1828 | Some _ -> true
1829 | None -> false
1832 itertiles l foo;
1833 fold (!seen && !allvisible) rest
1834 | [] -> true
1836 let alltilesvisible = fold true layout in
1837 alltilesvisible;
1840 let gotoy y =
1841 let y = bound y 0 state.maxy in
1842 let y, layout, proceed =
1843 match conf.maxwait with
1844 | Some time when state.ghyll == noghyll ->
1845 begin match state.throttle with
1846 | None ->
1847 let layout = layout y state.winh in
1848 let ready = layoutready layout in
1849 if not ready
1850 then (
1851 load layout;
1852 state.throttle <- Some (layout, y, now ());
1854 else G.postRedisplay "gotoy showall (None)";
1855 y, layout, ready
1856 | Some (_, _, started) ->
1857 let dt = now () -. started in
1858 if dt > time
1859 then (
1860 state.throttle <- None;
1861 let layout = layout y state.winh in
1862 load layout;
1863 G.postRedisplay "maxwait";
1864 y, layout, true
1866 else -1, [], false
1869 | _ ->
1870 let layout = layout y state.winh in
1871 if not !wtmode || layoutready layout
1872 then G.postRedisplay "gotoy ready";
1873 y, layout, true
1875 if proceed
1876 then (
1877 state.y <- y;
1878 state.layout <- layout;
1879 begin match state.mode with
1880 | LinkNav (Ltexact (pageno, linkno)) ->
1881 let rec loop = function
1882 | [] ->
1883 state.mode <- LinkNav (Ltgendir 0)
1884 | l :: _ when l.pageno = pageno ->
1885 begin match getopaque pageno with
1886 | None ->
1887 state.mode <- LinkNav (Ltgendir 0)
1888 | Some opaque ->
1889 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1890 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1891 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1892 then state.mode <- LinkNav (Ltgendir 0)
1894 | _ :: rest -> loop rest
1896 loop layout
1897 | _ -> ()
1898 end;
1899 begin match state.mode with
1900 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1901 if not (pagevisible layout pageno)
1902 then (
1903 match state.layout with
1904 | [] -> ()
1905 | l :: _ ->
1906 state.mode <- Birdseye (
1907 conf, leftx, l.pageno, hooverpageno, anchor
1910 | LinkNav (Ltgendir dir as lt) ->
1911 let linknav =
1912 let rec loop = function
1913 | [] -> lt
1914 | l :: rest ->
1915 match getopaque l.pageno with
1916 | None -> loop rest
1917 | Some opaque ->
1918 let link =
1919 let ld =
1920 if dir = 0
1921 then LDfirstvisible (l.pagex, l.pagey, dir)
1922 else (
1923 if dir > 0 then LDfirst else LDlast
1926 findlink opaque ld
1928 match link with
1929 | Lnotfound -> loop rest
1930 | Lfound n ->
1931 showlinktype (getlink opaque n);
1932 Ltexact (l.pageno, n)
1934 loop state.layout
1936 state.mode <- LinkNav linknav
1937 | _ -> ()
1938 end;
1939 preload layout;
1941 state.ghyll <- noghyll;
1942 if conf.updatecurs
1943 then (
1944 let mx, my = state.mpos in
1945 updateunder mx my;
1949 let conttiling pageno opaque =
1950 tilepage pageno opaque
1951 (if conf.preload then preloadlayout state.y else state.layout)
1954 let gotoy_and_clear_text y =
1955 if not conf.verbose then state.text <- "";
1956 gotoy y;
1959 let getanchor1 l =
1960 let top =
1961 let coloff = l.pagecol * l.pageh in
1962 float (l.pagey + coloff) /. float l.pageh
1964 let dtop =
1965 if l.pagedispy = 0
1966 then
1968 else (
1969 if conf.presentation
1970 then float l.pagedispy /. float (calcips l.pageh)
1971 else float l.pagedispy /. float conf.interpagespace
1974 (l.pageno, top, dtop)
1977 let getanchor () =
1978 match state.layout with
1979 | l :: _ -> getanchor1 l
1980 | [] ->
1981 let n = page_of_y state.y in
1982 if n = -1
1983 then state.anchor
1984 else
1985 let y, h = getpageyh n in
1986 let dy = y - state.y in
1987 let dtop =
1988 if conf.presentation
1989 then
1990 let ips = calcips h in
1991 float (dy + ips) /. float ips
1992 else
1993 float dy /. float conf.interpagespace
1995 (n, 0.0, dtop)
1998 let getanchory (n, top, dtop) =
1999 let y, h = getpageyh n in
2000 if conf.presentation
2001 then
2002 let ips = calcips h in
2003 y + truncate (top*.float h -. dtop*.float ips) + ips;
2004 else
2005 y + truncate (top*.float h -. dtop*.float conf.interpagespace)
2008 let gotoanchor anchor =
2009 gotoy (getanchory anchor);
2012 let addnav () =
2013 cbput state.hists.nav (getanchor ());
2016 let getnav dir =
2017 let anchor = cbgetc state.hists.nav dir in
2018 getanchory anchor;
2021 let gotoghyll y =
2022 let scroll f n a b =
2023 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
2024 let snake f a b =
2025 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
2026 if f < a
2027 then s (float f /. float a)
2028 else (
2029 if f > b
2030 then 1.0 -. s ((float (f-b) /. float (n-b)))
2031 else 1.0
2034 snake f a b
2035 and summa n a b =
2036 let ins = float a *. 0.5
2037 and outs = float (n-b) *. 0.5 in
2038 let ones = b - a in
2039 ins +. outs +. float ones
2041 let rec set (_N, _A, _B) y sy =
2042 let sum = summa _N _A _B in
2043 let dy = float (y - sy) in
2044 state.ghyll <- (
2045 let rec gf n y1 o =
2046 if n >= _N
2047 then state.ghyll <- noghyll
2048 else
2049 let go n =
2050 let s = scroll n _N _A _B in
2051 let y1 = y1 +. ((s *. dy) /. sum) in
2052 gotoy_and_clear_text (truncate y1);
2053 state.ghyll <- gf (n+1) y1;
2055 match o with
2056 | None -> go n
2057 | Some y' -> set (_N/2, 1, 1) y' state.y
2059 gf 0 (float state.y)
2062 match conf.ghyllscroll with
2063 | None ->
2064 gotoy_and_clear_text y
2065 | Some nab ->
2066 if state.ghyll == noghyll
2067 then set nab y state.y
2068 else state.ghyll (Some y)
2071 let gotopage n top =
2072 let y, h = getpageyh n in
2073 let y = y + (truncate (top *. float h)) in
2074 gotoghyll y
2077 let gotopage1 n top =
2078 let y = getpagey n in
2079 let y = y + top in
2080 gotoghyll y
2083 let invalidate s f =
2084 state.layout <- [];
2085 state.pdims <- [];
2086 state.rects <- [];
2087 state.rects1 <- [];
2088 match state.geomcmds with
2089 | ps, [] when emptystr ps ->
2090 f ();
2091 state.geomcmds <- s, [];
2093 | ps, [] ->
2094 state.geomcmds <- ps, [s, f];
2096 | ps, (s', _) :: rest when s' = s ->
2097 state.geomcmds <- ps, ((s, f) :: rest);
2099 | ps, cmds ->
2100 state.geomcmds <- ps, ((s, f) :: cmds);
2103 let flushpages () =
2104 Hashtbl.iter (fun _ opaque ->
2105 wcmd "freepage %s" opaque;
2106 ) state.pagemap;
2107 Hashtbl.clear state.pagemap;
2110 let flushtiles () =
2111 if not (Queue.is_empty state.tilelru)
2112 then (
2113 Queue.iter (fun (k, p, s) ->
2114 wcmd "freetile %s" p;
2115 state.memused <- state.memused - s;
2116 Hashtbl.remove state.tilemap k;
2117 ) state.tilelru;
2118 state.uioh#infochanged Memused;
2119 Queue.clear state.tilelru;
2121 load state.layout;
2124 let stateh h =
2125 let h = truncate (float h*.conf.zoom) in
2126 let d = conf.interpagespace lsl (if conf.presentation then 1 else 0) in
2127 h - d
2130 let opendoc path password =
2131 state.path <- path;
2132 state.password <- password;
2133 state.gen <- state.gen + 1;
2134 state.docinfo <- [];
2136 flushpages ();
2137 setaalevel conf.aalevel;
2138 let titlepath =
2139 if emptystr state.origin
2140 then path
2141 else state.origin
2143 Wsi.settitle ("llpp " ^ (mbtoutf8 (Filename.basename titlepath)));
2144 wcmd "open %d %d %s\000%s\000" (btod !wtmode) (btod !cxack) path password;
2145 invalidate "reqlayout"
2146 (fun () ->
2147 wcmd "reqlayout %d %d %d %s\000"
2148 conf.angle (FMTE.to_int conf.fitmodel)
2149 (stateh state.winh) state.nameddest
2153 let reload () =
2154 state.anchor <- getanchor ();
2155 opendoc state.path state.password;
2158 let scalecolor c =
2159 let c = c *. conf.colorscale in
2160 (c, c, c);
2163 let scalecolor2 (r, g, b) =
2164 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
2167 let docolumns = function
2168 | Csingle _ ->
2169 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2170 let rec loop pageno pdimno pdim y ph pdims =
2171 if pageno = state.pagecount
2172 then ()
2173 else
2174 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2175 match pdims with
2176 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2177 pdimno+1, pdim, rest
2178 | _ ->
2179 pdimno, pdim, pdims
2181 let x = max 0 (((wadjsb state.winw - w) / 2) - xoff) in
2182 let y = y +
2183 (if conf.presentation
2184 then (if pageno = 0 then calcips h else calcips ph + calcips h)
2185 else (if pageno = 0 then 0 else conf.interpagespace)
2188 a.(pageno) <- (pdimno, x, y, pdim);
2189 loop (pageno+1) pdimno pdim (y + h) h pdims
2191 loop 0 ~-1 (-1,-1,-1,-1) 0 0 state.pdims;
2192 conf.columns <- Csingle a;
2194 | Cmulti ((columns, coverA, coverB), _) ->
2195 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
2196 let rec loop pageno pdimno pdim x y rowh pdims =
2197 let rec fixrow m = if m = pageno then () else
2198 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
2199 if h < rowh
2200 then (
2201 let y = y + (rowh - h) / 2 in
2202 a.(m) <- (pdimno, x, y, pdim);
2204 fixrow (m+1)
2206 if pageno = state.pagecount
2207 then fixrow (((pageno - 1) / columns) * columns)
2208 else
2209 let pdimno, ((_, w, h, xoff) as pdim), pdims =
2210 match pdims with
2211 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2212 pdimno+1, pdim, rest
2213 | _ ->
2214 pdimno, pdim, pdims
2216 let x, y, rowh' =
2217 if pageno = coverA - 1 || pageno = state.pagecount - coverB
2218 then (
2219 let x = (wadjsb state.winw - w) / 2 in
2220 let ips =
2221 if conf.presentation then calcips h else conf.interpagespace in
2222 x, y + ips + rowh, h
2224 else (
2225 if (pageno - coverA) mod columns = 0
2226 then (
2227 let x = max 0 (wadjsb state.winw - state.w) / 2 in
2228 let y =
2229 if conf.presentation
2230 then
2231 let ips = calcips h in
2232 y + (if pageno = 0 then 0 else calcips rowh + ips)
2233 else
2234 y + (if pageno = 0 then 0 else conf.interpagespace)
2236 x, y + rowh, h
2238 else x, y, max rowh h
2241 let y =
2242 if pageno > 1 && (pageno - coverA) mod columns = 0
2243 then (
2244 let y =
2245 if pageno = columns && conf.presentation
2246 then (
2247 let ips = calcips rowh in
2248 for i = 0 to pred columns
2250 let (pdimno, x, y, pdim) = a.(i) in
2251 a.(i) <- (pdimno, x, y+ips, pdim)
2252 done;
2253 y+ips;
2255 else y
2257 fixrow (pageno - columns);
2260 else y
2262 a.(pageno) <- (pdimno, x, y, pdim);
2263 let x = x + w + xoff*2 + conf.interpagespace in
2264 loop (pageno+1) pdimno pdim x y rowh' pdims
2266 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
2267 conf.columns <- Cmulti ((columns, coverA, coverB), a);
2269 | Csplit (c, _) ->
2270 let a = Array.make (state.pagecount*c) (-1, -1, -1, (-1, -1, -1, -1)) in
2271 let rec loop pageno pdimno pdim y pdims =
2272 if pageno = state.pagecount
2273 then ()
2274 else
2275 let pdimno, ((_, w, h, _) as pdim), pdims =
2276 match pdims with
2277 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
2278 pdimno+1, pdim, rest
2279 | _ ->
2280 pdimno, pdim, pdims
2282 let cw = w / c in
2283 let rec loop1 n x y =
2284 if n = c then y else (
2285 a.(pageno*c + n) <- (pdimno, x, y, pdim);
2286 loop1 (n+1) (x+cw) (y + h + conf.interpagespace)
2289 let y = loop1 0 0 y in
2290 loop (pageno+1) pdimno pdim y pdims
2292 loop 0 ~-1 (-1,-1,-1,-1) 0 state.pdims;
2293 conf.columns <- Csplit (c, a);
2296 let represent () =
2297 docolumns conf.columns;
2298 state.maxy <- calcheight ();
2299 if state.reprf == noreprf
2300 then (
2301 match state.mode with
2302 | Birdseye (_, _, pageno, _, _) ->
2303 let y, h = getpageyh pageno in
2304 let top = (state.winh - h) / 2 in
2305 gotoy (max 0 (y - top))
2306 | _ -> gotoanchor state.anchor
2308 else (
2309 state.reprf ();
2310 state.reprf <- noreprf;
2314 let reshape w h =
2315 GlDraw.viewport 0 0 w h;
2316 let firsttime = state.geomcmds == firstgeomcmds in
2317 if not firsttime && nogeomcmds state.geomcmds
2318 then state.anchor <- getanchor ();
2320 state.winw <- w;
2321 let w = wadjsb (truncate (float w *. conf.zoom)) in
2322 let w = max w 2 in
2323 state.winh <- h;
2324 setfontsize fstate.fontsize;
2325 GlMat.mode `modelview;
2326 GlMat.load_identity ();
2328 GlMat.mode `projection;
2329 GlMat.load_identity ();
2330 GlMat.rotate ~x:1.0 ~angle:180.0 ();
2331 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
2332 GlMat.scale3 (2.0 /. float state.winw, 2.0 /. float state.winh, 1.0);
2334 let relx =
2335 if conf.zoom <= 1.0
2336 then 0.0
2337 else float state.x /. float state.w
2339 invalidate "geometry"
2340 (fun () ->
2341 state.w <- w;
2342 if not firsttime
2343 then state.x <- truncate (relx *. float w);
2344 let w =
2345 match conf.columns with
2346 | Csingle _ -> w
2347 | Cmulti ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
2348 | Csplit (c, _) -> w * c
2350 wcmd "geometry %d %d %d"
2351 w (stateh h) (FMTE.to_int conf.fitmodel)
2355 let enttext () =
2356 let len = String.length state.text in
2357 let drawstring s =
2358 let hscrollh =
2359 match state.mode with
2360 | Textentry _ | View | LinkNav _ ->
2361 let h, _, _ = state.uioh#scrollpw in
2363 | _ -> 0
2365 let rect x w =
2366 filledrect x (float (state.winh - (fstate.fontsize + 4) - hscrollh))
2367 (x+.w) (float (state.winh - hscrollh))
2370 let w = float (wadjsb state.winw - 1) in
2371 if state.progress >= 0.0 && state.progress < 1.0
2372 then (
2373 GlDraw.color (0.3, 0.3, 0.3);
2374 let w1 = w *. state.progress in
2375 rect 0.0 w1;
2376 GlDraw.color (0.0, 0.0, 0.0);
2377 rect w1 (w-.w1)
2379 else (
2380 GlDraw.color (0.0, 0.0, 0.0);
2381 rect 0.0 w;
2384 GlDraw.color (1.0, 1.0, 1.0);
2385 drawstring fstate.fontsize
2386 (if len > 0 then 8 else 2) (state.winh - hscrollh - 5) s;
2388 let s =
2389 match state.mode with
2390 | Textentry ((prefix, text, _, _, _, _), _) ->
2391 let s =
2392 if len > 0
2393 then
2394 Printf.sprintf "%s%s_ [%s]" prefix text state.text
2395 else
2396 Printf.sprintf "%s%s_" prefix text
2400 | _ -> state.text
2402 let s =
2403 if state.newerrmsgs
2404 then (
2405 if not (istextentry state.mode) && state.uioh#eformsgs
2406 then
2407 let s1 = "(press 'e' to review error messasges)" in
2408 if nonemptystr s then s ^ " " ^ s1 else s1
2409 else s
2411 else s
2413 if nonemptystr s
2414 then drawstring s
2417 let gctiles () =
2418 let len = Queue.length state.tilelru in
2419 let layout = lazy (
2420 match state.throttle with
2421 | None ->
2422 if conf.preload
2423 then preloadlayout state.y
2424 else state.layout
2425 | Some (layout, _, _) ->
2426 layout
2427 ) in
2428 let rec loop qpos =
2429 if state.memused <= conf.memlimit
2430 then ()
2431 else (
2432 if qpos < len
2433 then
2434 let (k, p, s) as lruitem = Queue.pop state.tilelru in
2435 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
2436 let (_, pw, ph, _) = getpagedim n in
2438 gen = state.gen
2439 && colorspace = conf.colorspace
2440 && angle = conf.angle
2441 && pagew = pw
2442 && pageh = ph
2443 && (
2444 let x = col*conf.tilew
2445 and y = row*conf.tileh in
2446 tilevisible (Lazy.force_val layout) n x y
2448 then Queue.push lruitem state.tilelru
2449 else (
2450 freepbo p;
2451 wcmd "freetile %s" p;
2452 state.memused <- state.memused - s;
2453 state.uioh#infochanged Memused;
2454 Hashtbl.remove state.tilemap k;
2456 loop (qpos+1)
2459 loop 0
2462 let logcurrently = function
2463 | Idle -> dolog "Idle"
2464 | Loading (l, gen) ->
2465 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
2466 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
2467 dolog
2468 "Tiling %d[%d,%d] page=%s cs=%s angle"
2469 l.pageno col row pageopaque
2470 (CSTE.to_string colorspace)
2472 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
2473 angle gen conf.angle state.gen
2474 tilew tileh
2475 conf.tilew conf.tileh
2477 | Outlining _ ->
2478 dolog "outlining"
2481 let splitatspace =
2482 let r = Str.regexp " " in
2483 fun s -> Str.bounded_split r s 2;
2486 let onpagerect pageno f =
2487 let b =
2488 match conf.columns with
2489 | Cmulti (_, b) -> b
2490 | Csingle b -> b
2491 | Csplit (_, b) -> b
2493 if pageno >= 0 && pageno < Array.length b
2494 then
2495 let (_, _, _, (w, h, _, _)) = b.(pageno) in
2496 f w h
2499 let gotopagexy1 pageno x y =
2500 let _,w1,h1,leftx = getpagedim pageno in
2501 let top = y /. (float h1) in
2502 let left = x /. (float w1) in
2503 let py, w, h = getpageywh pageno in
2504 let wh = state.winh - hscrollh () in
2505 let x = left *. (float w) in
2506 let x = leftx + state.x + truncate x in
2507 let sx =
2508 if x < 0 || x >= wadjsb state.winw
2509 then state.x - x
2510 else state.x
2512 let pdy = truncate (top *. float h) in
2513 let y' = py + pdy in
2514 let dy = y' - state.y in
2515 let sy =
2516 if x != state.x || not (dy > 0 && dy < wh)
2517 then (
2518 if conf.presentation
2519 then
2520 if abs (py - y') > wh
2521 then y'
2522 else py
2523 else y';
2525 else state.y
2527 if state.x != sx || state.y != sy
2528 then (
2529 let x, y =
2530 if !wtmode
2531 then (
2532 let ww = wadjsb state.winw in
2533 let qx = sx / ww
2534 and qy = pdy / wh in
2535 let x = qx * ww
2536 and y = py + qy * wh in
2537 let x = if -x + ww > w1 then -(w1-ww) else x
2538 and y' = if y + wh > state.maxy then state.maxy - wh else y in
2539 let y =
2540 if conf.presentation
2541 then
2542 if abs (py - y') > wh
2543 then y'
2544 else py
2545 else y';
2547 (x, y)
2549 else (sx, sy)
2551 state.x <- x;
2552 gotoy_and_clear_text y;
2554 else gotoy_and_clear_text state.y;
2557 let gotopagexy pageno x y =
2558 match state.mode with
2559 | Birdseye _ -> gotopage pageno 0.0
2560 | _ -> gotopagexy1 pageno x y
2563 let act cmds =
2564 (* dolog "%S" cmds; *)
2565 let cl = splitatspace cmds in
2566 let scan s fmt f =
2567 try Scanf.sscanf s fmt f
2568 with exn ->
2569 dolog "error processing '%S': %s" cmds (exntos exn);
2570 exit 1
2572 let addoutline outline =
2573 match state.currently with
2574 | Outlining outlines ->
2575 state.currently <- Outlining (outline :: outlines)
2576 | Idle -> state.currently <- Outlining [outline]
2577 | currently ->
2578 dolog "invalid outlining state";
2579 logcurrently currently
2581 match cl with
2582 | "clear" :: [] ->
2583 state.uioh#infochanged Pdim;
2584 state.pdims <- [];
2586 | "clearrects" :: [] ->
2587 state.rects <- state.rects1;
2588 G.postRedisplay "clearrects";
2590 | "continue" :: args :: [] ->
2591 let n = scan args "%u" (fun n -> n) in
2592 state.pagecount <- n;
2593 begin match state.currently with
2594 | Outlining l ->
2595 state.currently <- Idle;
2596 state.outlines <- Array.of_list (List.rev l)
2597 | _ -> ()
2598 end;
2600 let cur, cmds = state.geomcmds in
2601 if emptystr cur
2602 then failwith "umpossible";
2604 begin match List.rev cmds with
2605 | [] ->
2606 state.geomcmds <- "", [];
2607 represent ();
2608 | (s, f) :: rest ->
2609 f ();
2610 state.geomcmds <- s, List.rev rest;
2611 end;
2612 if conf.maxwait = None && not !wtmode
2613 then G.postRedisplay "continue";
2615 | "title" :: args :: [] ->
2616 Wsi.settitle args
2618 | "msg" :: args :: [] ->
2619 showtext ' ' args
2621 | "vmsg" :: args :: [] ->
2622 if conf.verbose
2623 then showtext ' ' args
2625 | "emsg" :: args :: [] ->
2626 Buffer.add_string state.errmsgs args;
2627 state.newerrmsgs <- true;
2628 G.postRedisplay "error message"
2630 | "progress" :: args :: [] ->
2631 let progress, text =
2632 scan args "%f %n"
2633 (fun f pos ->
2634 f, String.sub args pos (String.length args - pos))
2636 state.text <- text;
2637 state.progress <- progress;
2638 G.postRedisplay "progress"
2640 | "firstmatch" :: args :: [] ->
2641 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2642 scan args "%u %d %f %f %f %f %f %f %f %f"
2643 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2644 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2646 let y = (getpagey pageno) + truncate y0 in
2647 addnav ();
2648 gotoy y;
2649 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2651 | "match" :: args :: [] ->
2652 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2653 scan args "%u %d %f %f %f %f %f %f %f %f"
2654 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2655 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2657 state.rects1 <-
2658 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2660 | "page" :: args :: [] ->
2661 let pageopaque, t = scan args "%s %f" (fun p t -> p, t) in
2662 begin match state.currently with
2663 | Loading (l, gen) ->
2664 vlog "page %d took %f sec" l.pageno t;
2665 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2666 begin match state.throttle with
2667 | None ->
2668 let preloadedpages =
2669 if conf.preload
2670 then preloadlayout state.y
2671 else state.layout
2673 let evict () =
2674 let set =
2675 List.fold_left (fun s l -> IntSet.add l.pageno s)
2676 IntSet.empty preloadedpages
2678 let evictedpages =
2679 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2680 if not (IntSet.mem pageno set)
2681 then (
2682 wcmd "freepage %s" opaque;
2683 key :: accu
2685 else accu
2686 ) state.pagemap []
2688 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2690 evict ();
2691 state.currently <- Idle;
2692 if gen = state.gen
2693 then (
2694 tilepage l.pageno pageopaque state.layout;
2695 load state.layout;
2696 load preloadedpages;
2697 if pagevisible state.layout l.pageno
2698 && layoutready state.layout
2699 then G.postRedisplay "page";
2702 | Some (layout, _, _) ->
2703 state.currently <- Idle;
2704 tilepage l.pageno pageopaque layout;
2705 load state.layout
2706 end;
2708 | _ ->
2709 dolog "Inconsistent loading state";
2710 logcurrently state.currently;
2711 exit 1
2714 | "tile" :: args :: [] ->
2715 let (x, y, opaque, size, t) =
2716 scan args "%u %u %s %u %f"
2717 (fun x y p size t -> (x, y, p, size, t))
2719 begin match state.currently with
2720 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2721 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2723 unmappbo opaque;
2724 if tilew != conf.tilew || tileh != conf.tileh
2725 then (
2726 wcmd "freetile %s" opaque;
2727 state.currently <- Idle;
2728 load state.layout;
2730 else (
2731 puttileopaque l col row gen cs angle opaque size t;
2732 state.memused <- state.memused + size;
2733 state.uioh#infochanged Memused;
2734 gctiles ();
2735 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2736 opaque, size) state.tilelru;
2738 let layout =
2739 match state.throttle with
2740 | None -> state.layout
2741 | Some (layout, _, _) -> layout
2744 state.currently <- Idle;
2745 if gen = state.gen
2746 && conf.colorspace = cs
2747 && conf.angle = angle
2748 && tilevisible layout l.pageno x y
2749 then conttiling l.pageno pageopaque;
2751 begin match state.throttle with
2752 | None ->
2753 preload state.layout;
2754 if gen = state.gen
2755 && conf.colorspace = cs
2756 && conf.angle = angle
2757 && tilevisible state.layout l.pageno x y
2758 && (not !wtmode || layoutready state.layout)
2759 then G.postRedisplay "tile nothrottle";
2761 | Some (layout, y, _) ->
2762 let ready = layoutready layout in
2763 if ready
2764 then (
2765 state.y <- y;
2766 state.layout <- layout;
2767 state.throttle <- None;
2768 G.postRedisplay "throttle";
2770 else load layout;
2771 end;
2774 | _ ->
2775 dolog "Inconsistent tiling state";
2776 logcurrently state.currently;
2777 exit 1
2780 | "pdim" :: args :: [] ->
2781 let (n, w, h, _) as pdim =
2782 scan args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2784 let pdim =
2785 match conf.fitmodel, conf.columns with
2786 | (FitPage | FitProportional), Csplit _ -> (n, w, h, 0)
2787 | _ -> pdim
2789 state.uioh#infochanged Pdim;
2790 state.pdims <- pdim :: state.pdims
2792 | "o" :: args :: [] ->
2793 let (l, n, t, h, pos) =
2794 scan args "%u %u %d %u %n"
2795 (fun l n t h pos -> l, n, t, h, pos)
2797 let s = String.sub args pos (String.length args - pos) in
2798 addoutline (s, l, Oanchor (n, float t /. float h, 0.0))
2800 | "ou" :: args :: [] ->
2801 let (l, len, pos) = scan args "%u %u %n" (fun l len pos -> l, len, pos) in
2802 let s = String.sub args pos len in
2803 let pos2 = pos + len + 1 in
2804 let uri = String.sub args pos2 (String.length args - pos2) in
2805 addoutline (s, l, Ouri uri)
2807 | "on" :: args :: [] ->
2808 let (l, pos) = scan args "%u %n" (fun l pos -> l, pos) in
2809 let s = String.sub args pos (String.length args - pos) in
2810 addoutline (s, l, Onone)
2812 | "a" :: args :: [] ->
2813 let (n, l, t) =
2814 scan args "%u %d %d" (fun n l t -> n, l, t)
2816 state.reprf <- (fun () -> gotopagexy n (float l) (float t))
2818 | "info" :: args :: [] ->
2819 state.docinfo <- (1, args) :: state.docinfo
2821 | "infoend" :: [] ->
2822 state.uioh#infochanged Docinfo;
2823 state.docinfo <- List.rev state.docinfo
2825 | _ ->
2826 error "unknown cmd `%S'" cmds
2829 let onhist cb =
2830 let rc = cb.rc in
2831 let action = function
2832 | HCprev -> cbget cb ~-1
2833 | HCnext -> cbget cb 1
2834 | HCfirst -> cbget cb ~-(cb.rc)
2835 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2836 and cancel () = cb.rc <- rc
2837 in (action, cancel)
2840 let search pattern forward =
2841 match conf.columns with
2842 | Csplit _ ->
2843 showtext '!' "searching does not work properly in split columns mode"
2844 | _ ->
2845 if nonemptystr pattern
2846 then
2847 let pn, py =
2848 match state.layout with
2849 | [] -> 0, 0
2850 | l :: _ ->
2851 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2853 wcmd "search %d %d %d %d,%s\000"
2854 (btod conf.icase) pn py (btod forward) pattern;
2857 let intentry text key =
2858 let c =
2859 if key >= 32 && key < 127
2860 then Char.chr key
2861 else '\000'
2863 match c with
2864 | '0' .. '9' ->
2865 let text = addchar text c in
2866 TEcont text
2868 | _ ->
2869 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2870 TEcont text
2873 let linknentry text key =
2874 let c =
2875 if key >= 32 && key < 127
2876 then Char.chr key
2877 else '\000'
2879 match c with
2880 | 'a' .. 'z' ->
2881 let text = addchar text c in
2882 TEcont text
2884 | _ ->
2885 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2886 TEcont text
2889 let linkndone f s =
2890 if nonemptystr s
2891 then (
2892 let n =
2893 let l = String.length s in
2894 let rec loop pos n = if pos = l then n else
2895 let m = Char.code s.[pos] - (if pos = 0 && l > 1 then 96 else 97) in
2896 loop (pos+1) (n*26 + m)
2897 in loop 0 0
2899 let rec loop n = function
2900 | [] -> ()
2901 | l :: rest ->
2902 match getopaque l.pageno with
2903 | None -> loop n rest
2904 | Some opaque ->
2905 let m = getlinkcount opaque in
2906 if n < m
2907 then (
2908 let under = getlink opaque n in
2909 f under
2911 else loop (n-m) rest
2913 loop n state.layout;
2917 let textentry text key =
2918 if key land 0xff00 = 0xff00
2919 then TEcont text
2920 else TEcont (text ^ toutf8 key)
2923 let reqlayout angle fitmodel =
2924 match state.throttle with
2925 | None ->
2926 if nogeomcmds state.geomcmds
2927 then state.anchor <- getanchor ();
2928 conf.angle <- angle mod 360;
2929 if conf.angle != 0
2930 then (
2931 match state.mode with
2932 | LinkNav _ -> state.mode <- View
2933 | _ -> ()
2935 conf.fitmodel <- fitmodel;
2936 invalidate "reqlayout"
2937 (fun () ->
2938 wcmd "reqlayout %d %d %d"
2939 conf.angle (FMTE.to_int conf.fitmodel) (stateh state.winh)
2941 | _ -> ()
2944 let settrim trimmargins trimfuzz =
2945 if nogeomcmds state.geomcmds
2946 then state.anchor <- getanchor ();
2947 conf.trimmargins <- trimmargins;
2948 conf.trimfuzz <- trimfuzz;
2949 let x0, y0, x1, y1 = trimfuzz in
2950 invalidate "settrim"
2951 (fun () ->
2952 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2953 flushpages ();
2956 let setzoom zoom =
2957 match state.throttle with
2958 | None ->
2959 let zoom = max 0.0001 zoom in
2960 if zoom <> conf.zoom
2961 then (
2962 state.prevzoom <- (conf.zoom, state.x);
2963 conf.zoom <- zoom;
2964 reshape state.winw state.winh;
2965 state.text <- Printf.sprintf "zoom is now %-5.2f" (zoom *. 100.0);
2968 | Some (layout, y, started) ->
2969 let time =
2970 match conf.maxwait with
2971 | None -> 0.0
2972 | Some t -> t
2974 let dt = now () -. started in
2975 if dt > time
2976 then (
2977 state.y <- y;
2978 load layout;
2982 let setcolumns mode columns coverA coverB =
2983 state.prevcolumns <- Some (conf.columns, conf.zoom);
2984 if columns < 0
2985 then (
2986 if isbirdseye mode
2987 then showtext '!' "split mode doesn't work in bird's eye"
2988 else (
2989 conf.columns <- Csplit (-columns, [||]);
2990 state.x <- 0;
2991 conf.zoom <- 1.0;
2994 else (
2995 if columns < 2
2996 then (
2997 conf.columns <- Csingle [||];
2998 state.x <- 0;
2999 setzoom 1.0;
3001 else (
3002 conf.columns <- Cmulti ((columns, coverA, coverB), [||]);
3003 conf.zoom <- 1.0;
3006 reshape state.winw state.winh;
3009 let enterbirdseye () =
3010 let zoom = float conf.thumbw /. float state.winw in
3011 let birdseyepageno =
3012 let cy = state.winh / 2 in
3013 let fold = function
3014 | [] -> 0
3015 | l :: rest ->
3016 let rec fold best = function
3017 | [] -> best.pageno
3018 | l :: rest ->
3019 let d = cy - (l.pagedispy + l.pagevh/2)
3020 and dbest = cy - (best.pagedispy + best.pagevh/2) in
3021 if abs d < abs dbest
3022 then fold l rest
3023 else best.pageno
3024 in fold l rest
3026 fold state.layout
3028 state.mode <- Birdseye (
3029 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
3031 conf.zoom <- zoom;
3032 conf.presentation <- false;
3033 conf.interpagespace <- 10;
3034 conf.hlinks <- false;
3035 conf.fitmodel <- FitProportional;
3036 state.x <- 0;
3037 state.mstate <- Mnone;
3038 conf.maxwait <- None;
3039 conf.columns <- (
3040 match conf.beyecolumns with
3041 | Some c ->
3042 conf.zoom <- 1.0;
3043 Cmulti ((c, 0, 0), [||])
3044 | None -> Csingle [||]
3046 Wsi.setcursor Wsi.CURSOR_INHERIT;
3047 if conf.verbose
3048 then
3049 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
3050 (100.0*.zoom)
3051 else
3052 state.text <- ""
3054 reshape state.winw state.winh;
3057 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
3058 state.mode <- View;
3059 conf.zoom <- c.zoom;
3060 conf.presentation <- c.presentation;
3061 conf.interpagespace <- c.interpagespace;
3062 conf.maxwait <- c.maxwait;
3063 conf.hlinks <- c.hlinks;
3064 conf.fitmodel <- c.fitmodel;
3065 conf.beyecolumns <- (
3066 match conf.columns with
3067 | Cmulti ((c, _, _), _) -> Some c
3068 | Csingle _ -> None
3069 | Csplit _ -> failwith "leaving bird's eye split mode"
3071 conf.columns <- (
3072 match c.columns with
3073 | Cmulti (c, _) -> Cmulti (c, [||])
3074 | Csingle _ -> Csingle [||]
3075 | Csplit (c, _) -> Csplit (c, [||])
3077 if conf.verbose
3078 then
3079 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
3080 (100.0*.conf.zoom)
3082 reshape state.winw state.winh;
3083 state.anchor <- if goback then anchor else (pageno, 0.0, 1.0);
3084 state.x <- leftx;
3087 let togglebirdseye () =
3088 match state.mode with
3089 | Birdseye vals -> leavebirdseye vals true
3090 | View -> enterbirdseye ()
3091 | _ -> ()
3094 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3095 let pageno = max 0 (pageno - incr) in
3096 let rec loop = function
3097 | [] -> gotopage1 pageno 0
3098 | l :: _ when l.pageno = pageno ->
3099 if l.pagedispy >= 0 && l.pagey = 0
3100 then G.postRedisplay "upbirdseye"
3101 else gotopage1 pageno 0
3102 | _ :: rest -> loop rest
3104 loop state.layout;
3105 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
3108 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
3109 let pageno = min (state.pagecount - 1) (pageno + incr) in
3110 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
3111 let rec loop = function
3112 | [] ->
3113 let y, h = getpageyh pageno in
3114 let dy = (y - state.y) - (state.winh - h - conf.interpagespace) in
3115 gotoy (clamp dy)
3116 | l :: _ when l.pageno = pageno ->
3117 if l.pagevh != l.pageh
3118 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
3119 else G.postRedisplay "downbirdseye"
3120 | _ :: rest -> loop rest
3122 loop state.layout
3125 let boundastep h step =
3126 if step < 0
3127 then bound step ~-h 0
3128 else bound step 0 h
3131 let optentry mode _ key =
3132 let btos b = if b then "on" else "off" in
3133 if key >= 32 && key < 127
3134 then
3135 let c = Char.chr key in
3136 match c with
3137 | 's' ->
3138 let ondone s =
3139 try conf.scrollstep <- int_of_string s with exc ->
3140 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3142 TEswitch ("scroll step: ", "", None, intentry, ondone, true)
3144 | 'A' ->
3145 let ondone s =
3147 conf.autoscrollstep <- boundastep state.winh (int_of_string s);
3148 if state.autoscroll <> None
3149 then state.autoscroll <- Some conf.autoscrollstep
3150 with exc ->
3151 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3153 TEswitch ("auto scroll step: ", "", None, intentry, ondone, true)
3155 | 'C' ->
3156 let ondone s =
3158 let n, a, b = multicolumns_of_string s in
3159 setcolumns mode n a b;
3160 with exc ->
3161 state.text <- Printf.sprintf "bad columns `%s': %s" s (exntos exc)
3163 TEswitch ("columns: ", "", None, textentry, ondone, true)
3165 | 'Z' ->
3166 let ondone s =
3168 let zoom = float (int_of_string s) /. 100.0 in
3169 setzoom zoom
3170 with exc ->
3171 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3173 TEswitch ("zoom: ", "", None, intentry, ondone, true)
3175 | 't' ->
3176 let ondone s =
3178 conf.thumbw <- bound (int_of_string s) 2 4096;
3179 state.text <-
3180 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
3181 begin match mode with
3182 | Birdseye beye ->
3183 leavebirdseye beye false;
3184 enterbirdseye ();
3185 | _ -> ();
3187 with exc ->
3188 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3190 TEswitch ("thumbnail width: ", "", None, intentry, ondone, true)
3192 | 'R' ->
3193 let ondone s =
3194 match try
3195 Some (int_of_string s)
3196 with exc ->
3197 state.text <- Printf.sprintf "bad integer `%s': %s"
3198 s (exntos exc);
3199 None
3200 with
3201 | Some angle -> reqlayout angle conf.fitmodel
3202 | None -> ()
3204 TEswitch ("rotation: ", "", None, intentry, ondone, true)
3206 | 'i' ->
3207 conf.icase <- not conf.icase;
3208 TEdone ("case insensitive search " ^ (btos conf.icase))
3210 | 'p' ->
3211 conf.preload <- not conf.preload;
3212 gotoy state.y;
3213 TEdone ("preload " ^ (btos conf.preload))
3215 | 'v' ->
3216 conf.verbose <- not conf.verbose;
3217 TEdone ("verbose " ^ (btos conf.verbose))
3219 | 'd' ->
3220 conf.debug <- not conf.debug;
3221 TEdone ("debug " ^ (btos conf.debug))
3223 | 'h' ->
3224 conf.maxhfit <- not conf.maxhfit;
3225 state.maxy <- calcheight ();
3226 TEdone ("maxhfit " ^ (btos conf.maxhfit))
3228 | 'c' ->
3229 conf.crophack <- not conf.crophack;
3230 TEdone ("crophack " ^ btos conf.crophack)
3232 | 'a' ->
3233 let s =
3234 match conf.maxwait with
3235 | None ->
3236 conf.maxwait <- Some infinity;
3237 "always wait for page to complete"
3238 | Some _ ->
3239 conf.maxwait <- None;
3240 "show placeholder if page is not ready"
3242 TEdone s
3244 | 'f' ->
3245 conf.underinfo <- not conf.underinfo;
3246 TEdone ("underinfo " ^ btos conf.underinfo)
3248 | 'P' ->
3249 conf.savebmarks <- not conf.savebmarks;
3250 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
3252 | 'S' ->
3253 let ondone s =
3255 let pageno, py =
3256 match state.layout with
3257 | [] -> 0, 0
3258 | l :: _ ->
3259 l.pageno, l.pagey
3261 conf.interpagespace <- int_of_string s;
3262 docolumns conf.columns;
3263 state.maxy <- calcheight ();
3264 let y = getpagey pageno in
3265 gotoy (y + py)
3266 with exc ->
3267 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc)
3269 TEswitch ("vertical margin: ", "", None, intentry, ondone, true)
3271 | 'l' ->
3272 let fm =
3273 match conf.fitmodel with
3274 | FitProportional -> FitWidth
3275 | _ -> FitProportional
3277 reqlayout conf.angle fm;
3278 TEdone ("proportional display " ^ btos (fm == FitProportional))
3280 | 'T' ->
3281 settrim (not conf.trimmargins) conf.trimfuzz;
3282 TEdone ("trim margins " ^ btos conf.trimmargins)
3284 | 'I' ->
3285 conf.invert <- not conf.invert;
3286 TEdone ("invert colors " ^ btos conf.invert)
3288 | 'x' ->
3289 let ondone s =
3290 cbput state.hists.sel s;
3291 conf.selcmd <- s;
3293 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
3294 textentry, ondone, true)
3296 | 'M' ->
3297 if conf.pax == None
3298 then conf.pax <- Some (ref (0.0, 0, 0))
3299 else conf.pax <- None;
3300 TEdone ("PAX " ^ btos (conf.pax != None))
3302 | _ ->
3303 state.text <- Printf.sprintf "bad option %d `%c'" key c;
3304 TEstop
3305 else
3306 TEcont state.text
3309 class type lvsource = object
3310 method getitemcount : int
3311 method getitem : int -> (string * int)
3312 method hasaction : int -> bool
3313 method exit :
3314 uioh:uioh ->
3315 cancel:bool ->
3316 active:int ->
3317 first:int ->
3318 pan:int ->
3319 qsearch:string ->
3320 uioh option
3321 method getactive : int
3322 method getfirst : int
3323 method getqsearch : string
3324 method setqsearch : string -> unit
3325 method getpan : int
3326 end;;
3328 class virtual lvsourcebase = object
3329 val mutable m_active = 0
3330 val mutable m_first = 0
3331 val mutable m_qsearch = ""
3332 val mutable m_pan = 0
3333 method getactive = m_active
3334 method getfirst = m_first
3335 method getqsearch = m_qsearch
3336 method getpan = m_pan
3337 method setqsearch s = m_qsearch <- s
3338 end;;
3340 let withoutlastutf8 s =
3341 let len = String.length s in
3342 if len = 0
3343 then s
3344 else
3345 let rec find pos =
3346 if pos = 0
3347 then pos
3348 else
3349 let b = Char.code s.[pos] in
3350 if b land 0b11000000 = 0b11000000
3351 then pos
3352 else find (pos-1)
3354 let first =
3355 if Char.code s.[len-1] land 0x80 = 0
3356 then len-1
3357 else find (len-1)
3359 String.sub s 0 first;
3362 let textentrykeyboard
3363 key _mask ((c, text, opthist, onkey, ondone, cancelonempty), onleave) =
3364 let key =
3365 if key >= 0xffb0 && key <= 0xffb9
3366 then key - 0xffb0 + 48 else key
3368 let enttext te =
3369 state.mode <- Textentry (te, onleave);
3370 state.text <- "";
3371 enttext ();
3372 G.postRedisplay "textentrykeyboard enttext";
3374 let histaction cmd =
3375 match opthist with
3376 | None -> ()
3377 | Some (action, _) ->
3378 state.mode <- Textentry (
3379 (c, action cmd, opthist, onkey, ondone, cancelonempty), onleave
3381 G.postRedisplay "textentry histaction"
3383 match key with
3384 | 0xff08 -> (* backspace *)
3385 if emptystr text && cancelonempty
3386 then (
3387 onleave Cancel;
3388 G.postRedisplay "textentrykeyboard after cancel";
3390 else
3391 let s = withoutlastutf8 text in
3392 enttext (c, s, opthist, onkey, ondone, cancelonempty)
3394 | 0xff0d | 0xff8d -> (* (kp) enter *)
3395 ondone text;
3396 onleave Confirm;
3397 G.postRedisplay "textentrykeyboard after confirm"
3399 | 0xff52 | 0xff97 -> histaction HCprev (* (kp) up *)
3400 | 0xff54 | 0xff99 -> histaction HCnext (* (kp) down *)
3401 | 0xff50 | 0xff95 -> histaction HCfirst (* (kp) home) *)
3402 | 0xff57 | 0xff9c -> histaction HClast (* (kp) end *)
3404 | 0xff1b -> (* escape*)
3405 if emptystr text
3406 then (
3407 begin match opthist with
3408 | None -> ()
3409 | Some (_, onhistcancel) -> onhistcancel ()
3410 end;
3411 onleave Cancel;
3412 state.text <- "";
3413 G.postRedisplay "textentrykeyboard after cancel2"
3415 else (
3416 enttext (c, "", opthist, onkey, ondone, cancelonempty)
3419 | 0xff9f | 0xffff -> () (* delete *)
3421 | _ when key != 0
3422 && key land 0xff00 != 0xff00 (* keyboard *)
3423 && key land 0xfe00 != 0xfe00 (* xkb *)
3424 && key land 0xfd00 != 0xfd00 (* 3270 *)
3426 begin match onkey text key with
3427 | TEdone text ->
3428 ondone text;
3429 onleave Confirm;
3430 G.postRedisplay "textentrykeyboard after confirm2";
3432 | TEcont text ->
3433 enttext (c, text, opthist, onkey, ondone, cancelonempty);
3435 | TEstop ->
3436 onleave Cancel;
3437 G.postRedisplay "textentrykeyboard after cancel3"
3439 | TEswitch te ->
3440 state.mode <- Textentry (te, onleave);
3441 G.postRedisplay "textentrykeyboard switch";
3442 end;
3444 | _ ->
3445 vlog "unhandled key %s" (Wsi.keyname key)
3448 let firstof first active =
3449 if first > active || abs (first - active) > fstate.maxrows - 1
3450 then max 0 (active - (fstate.maxrows/2))
3451 else first
3454 let calcfirst first active =
3455 if active > first
3456 then
3457 let rows = active - first in
3458 if rows > fstate.maxrows then active - fstate.maxrows else first
3459 else active
3462 let scrollph y maxy =
3463 let sh = float (maxy + state.winh) /. float state.winh in
3464 let sh = float state.winh /. sh in
3465 let sh = max sh (float conf.scrollh) in
3467 let percent = float y /. float maxy in
3468 let position = (float state.winh -. sh) *. percent in
3470 let position =
3471 if position +. sh > float state.winh
3472 then float state.winh -. sh
3473 else position
3475 position, sh;
3478 let coe s = (s :> uioh);;
3480 class listview ~(source:lvsource) ~trusted ~modehash =
3481 object (self)
3482 val m_pan = source#getpan
3483 val m_first = source#getfirst
3484 val m_active = source#getactive
3485 val m_qsearch = source#getqsearch
3486 val m_prev_uioh = state.uioh
3488 method private elemunder y =
3489 let n = y / (fstate.fontsize+1) in
3490 if m_first + n < source#getitemcount
3491 then (
3492 if source#hasaction (m_first + n)
3493 then Some (m_first + n)
3494 else None
3496 else None
3498 method display =
3499 Gl.enable `blend;
3500 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
3501 GlDraw.color (0., 0., 0.) ~alpha:0.85;
3502 filledrect 0. 0. (float state.winw) (float state.winh);
3503 GlDraw.color (1., 1., 1.);
3504 Gl.enable `texture_2d;
3505 let fs = fstate.fontsize in
3506 let nfs = fs + 1 in
3507 let ww = fstate.wwidth in
3508 let tabw = 30.0*.ww in
3509 let itemcount = source#getitemcount in
3510 let rec loop row =
3511 if (row - m_first) > fstate.maxrows
3512 then ()
3513 else (
3514 if row >= 0 && row < itemcount
3515 then (
3516 let (s, level) = source#getitem row in
3517 let y = (row - m_first) * nfs in
3518 let x = 5.0 +. float (level + m_pan) *. ww in
3519 if row = m_active
3520 then (
3521 Gl.disable `texture_2d;
3522 let alpha = if source#hasaction row then 0.9 else 0.3 in
3523 GlDraw.color (1., 1., 1.) ~alpha;
3524 linerect 1. (float (y + 1))
3525 (float (state.winw - conf.scrollbw - 1)) (float (y + fs + 3));
3526 GlDraw.color (1., 1., 1.);
3527 Gl.enable `texture_2d;
3530 let drawtabularstring s =
3531 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
3532 if trusted
3533 then
3534 let tabpos = try String.index s '\t' with Not_found -> -1 in
3535 if tabpos > 0
3536 then
3537 let len = String.length s - tabpos - 1 in
3538 let s1 = String.sub s 0 tabpos
3539 and s2 = String.sub s (tabpos + 1) len in
3540 let nx = drawstr x s1 in
3541 let sw = nx -. x in
3542 let x = x +. (max tabw sw) in
3543 drawstr x s2
3544 else
3545 drawstr x s
3546 else
3547 drawstr x s
3549 let _ = drawtabularstring s in
3550 loop (row+1)
3554 loop m_first;
3555 Gl.disable `blend;
3556 Gl.disable `texture_2d;
3558 method updownlevel incr =
3559 let len = source#getitemcount in
3560 let curlevel =
3561 if m_active >= 0 && m_active < len
3562 then snd (source#getitem m_active)
3563 else -1
3565 let rec flow i =
3566 if i = len then i-1 else if i = -1 then 0 else
3567 let _, l = source#getitem i in
3568 if l != curlevel then i else flow (i+incr)
3570 let active = flow m_active in
3571 let first = calcfirst m_first active in
3572 G.postRedisplay "outline updownlevel";
3573 {< m_active = active; m_first = first >}
3575 method private key1 key mask =
3576 let set1 active first qsearch =
3577 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
3579 let search active pattern incr =
3580 let active = if active = -1 then m_first else active in
3581 let dosearch re =
3582 let rec loop n =
3583 if n >= 0 && n < source#getitemcount
3584 then (
3585 let s, _ = source#getitem n in
3587 (try ignore (Str.search_forward re s 0); true
3588 with Not_found -> false)
3589 then Some n
3590 else loop (n + incr)
3592 else None
3594 loop active
3597 let re = Str.regexp_case_fold pattern in
3598 dosearch re
3599 with Failure s ->
3600 state.text <- s;
3601 None
3603 let itemcount = source#getitemcount in
3604 let find start incr =
3605 let rec find i =
3606 if i = -1 || i = itemcount
3607 then -1
3608 else (
3609 if source#hasaction i
3610 then i
3611 else find (i + incr)
3614 find start
3616 let set active first =
3617 let first = bound first 0 (itemcount - fstate.maxrows) in
3618 state.text <- "";
3619 coe {< m_active = active; m_first = first; m_qsearch = "" >}
3621 let navigate incr =
3622 let isvisible first n = n >= first && n - first <= fstate.maxrows in
3623 let active, first =
3624 let incr1 = if incr > 0 then 1 else -1 in
3625 if isvisible m_first m_active
3626 then
3627 let next =
3628 let next = m_active + incr in
3629 let next =
3630 if next < 0 || next >= itemcount
3631 then -1
3632 else find next incr1
3634 if abs (m_active - next) > fstate.maxrows
3635 then -1
3636 else next
3638 if next = -1
3639 then
3640 let first = m_first + incr in
3641 let first = bound first 0 (itemcount - fstate.maxrows) in
3642 let next =
3643 let next = m_active + incr in
3644 let next = bound next 0 (itemcount - 1) in
3645 find next ~-incr1
3647 let active =
3648 if next = -1
3649 then m_active
3650 else (
3651 if isvisible first next
3652 then next
3653 else m_active
3656 active, first
3657 else
3658 let first = min next m_first in
3659 let first =
3660 if abs (next - first) > fstate.maxrows
3661 then first + incr
3662 else first
3664 next, first
3665 else
3666 let first = m_first + incr in
3667 let first = bound first 0 (itemcount - 1) in
3668 let active =
3669 let next = m_active + incr in
3670 let next = bound next 0 (itemcount - 1) in
3671 let next = find next incr1 in
3672 let active =
3673 if next = -1 || abs (m_active - first) > fstate.maxrows
3674 then (
3675 let active = if m_active = -1 then next else m_active in
3676 active
3678 else next
3680 if isvisible first active
3681 then active
3682 else -1
3684 active, first
3686 G.postRedisplay "listview navigate";
3687 set active first;
3689 match key with
3690 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3691 let incr = if key = 0x72 then -1 else 1 in
3692 let active, first =
3693 match search (m_active + incr) m_qsearch incr with
3694 | None ->
3695 state.text <- m_qsearch ^ " [not found]";
3696 m_active, m_first
3697 | Some active ->
3698 state.text <- m_qsearch;
3699 active, firstof m_first active
3701 G.postRedisplay "listview ctrl-r/s";
3702 set1 active first m_qsearch;
3704 | 0xff63 when Wsi.withctrl mask -> (* ctrl-insert *)
3705 if m_active >= 0 && m_active < source#getitemcount
3706 then (
3707 let s, _ = source#getitem m_active in
3708 selstring s;
3710 coe self
3712 | 0xff08 -> (* backspace *)
3713 if emptystr m_qsearch
3714 then coe self
3715 else (
3716 let qsearch = withoutlastutf8 m_qsearch in
3717 if emptystr qsearch
3718 then (
3719 state.text <- "";
3720 G.postRedisplay "listview empty qsearch";
3721 set1 m_active m_first "";
3723 else
3724 let active, first =
3725 match search m_active qsearch ~-1 with
3726 | None ->
3727 state.text <- qsearch ^ " [not found]";
3728 m_active, m_first
3729 | Some active ->
3730 state.text <- qsearch;
3731 active, firstof m_first active
3733 G.postRedisplay "listview backspace qsearch";
3734 set1 active first qsearch
3737 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3738 let pattern = m_qsearch ^ toutf8 key in
3739 let active, first =
3740 match search m_active pattern 1 with
3741 | None ->
3742 state.text <- pattern ^ " [not found]";
3743 m_active, m_first
3744 | Some active ->
3745 state.text <- pattern;
3746 active, firstof m_first active
3748 G.postRedisplay "listview qsearch add";
3749 set1 active first pattern;
3751 | 0xff1b -> (* escape *)
3752 state.text <- "";
3753 if emptystr m_qsearch
3754 then (
3755 G.postRedisplay "list view escape";
3756 begin
3757 match
3758 source#exit (coe self) true m_active m_first m_pan m_qsearch
3759 with
3760 | None -> m_prev_uioh
3761 | Some uioh -> uioh
3764 else (
3765 G.postRedisplay "list view kill qsearch";
3766 source#setqsearch "";
3767 coe {< m_qsearch = "" >}
3770 | 0xff0d | 0xff8d -> (* (kp) enter *)
3771 state.text <- "";
3772 let self = {< m_qsearch = "" >} in
3773 source#setqsearch "";
3774 let opt =
3775 G.postRedisplay "listview enter";
3776 if m_active >= 0 && m_active < source#getitemcount
3777 then (
3778 source#exit (coe self) false m_active m_first m_pan "";
3780 else (
3781 source#exit (coe self) true m_active m_first m_pan "";
3784 begin match opt with
3785 | None -> m_prev_uioh
3786 | Some uioh -> uioh
3789 | 0xff9f | 0xffff -> (* (kp) delete *)
3790 coe self
3792 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
3793 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
3794 | 0xff55 | 0xff9a -> navigate ~-(fstate.maxrows) (* (kp) prior *)
3795 | 0xff56 | 0xff9b -> navigate fstate.maxrows (* (kp) next *)
3797 | 0xff53 | 0xff98 -> (* (kp) right *)
3798 state.text <- "";
3799 G.postRedisplay "listview right";
3800 coe {< m_pan = m_pan - 1 >}
3802 | 0xff51 | 0xff96 -> (* (kp) left *)
3803 state.text <- "";
3804 G.postRedisplay "listview left";
3805 coe {< m_pan = m_pan + 1 >}
3807 | 0xff50 | 0xff95 -> (* (kp) home *)
3808 let active = find 0 1 in
3809 G.postRedisplay "listview home";
3810 set active 0;
3812 | 0xff57 | 0xff9c -> (* (kp) end *)
3813 let first = max 0 (itemcount - fstate.maxrows) in
3814 let active = find (itemcount - 1) ~-1 in
3815 G.postRedisplay "listview end";
3816 set active first;
3818 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3819 coe self
3821 | _ ->
3822 dolog "listview unknown key %#x" key; coe self
3824 method key key mask =
3825 match state.mode with
3826 | Textentry te -> textentrykeyboard key mask te; coe self
3827 | _ -> self#key1 key mask
3829 method button button down x y _ =
3830 let opt =
3831 match button with
3832 | 1 when x > state.winw - conf.scrollbw ->
3833 G.postRedisplay "listview scroll";
3834 if down
3835 then
3836 let _, position, sh = self#scrollph in
3837 if y > truncate position && y < truncate (position +. sh)
3838 then (
3839 state.mstate <- Mscrolly;
3840 Some (coe self)
3842 else
3843 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3844 let first = truncate (s *. float source#getitemcount) in
3845 let first = min source#getitemcount first in
3846 Some (coe {< m_first = first; m_active = first >})
3847 else (
3848 state.mstate <- Mnone;
3849 Some (coe self);
3851 | 1 when not down ->
3852 begin match self#elemunder y with
3853 | Some n ->
3854 G.postRedisplay "listview click";
3855 source#exit
3856 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3857 | _ ->
3858 Some (coe self)
3860 | n when (n == 4 || n == 5) && not down ->
3861 let len = source#getitemcount in
3862 let first =
3863 if n = 5 && m_first + fstate.maxrows >= len
3864 then
3865 m_first
3866 else
3867 let first = m_first + (if n == 4 then -1 else 1) in
3868 bound first 0 (len - 1)
3870 G.postRedisplay "listview wheel";
3871 Some (coe {< m_first = first >})
3872 | n when (n = 6 || n = 7) && not down ->
3873 let inc = if n = 7 then -1 else 1 in
3874 G.postRedisplay "listview hwheel";
3875 Some (coe {< m_pan = m_pan + inc >})
3876 | _ ->
3877 Some (coe self)
3879 match opt with
3880 | None -> m_prev_uioh
3881 | Some uioh -> uioh
3883 method multiclick _ x y = self#button 1 true x y
3885 method motion _ y =
3886 match state.mstate with
3887 | Mscrolly ->
3888 let s = float (max 0 (y - conf.scrollh)) /. float state.winh in
3889 let first = truncate (s *. float source#getitemcount) in
3890 let first = min source#getitemcount first in
3891 G.postRedisplay "listview motion";
3892 coe {< m_first = first; m_active = first >}
3893 | _ -> coe self
3895 method pmotion x y =
3896 if x < state.winw - conf.scrollbw
3897 then
3898 let n =
3899 match self#elemunder y with
3900 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3901 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3903 let o =
3904 if n != m_active
3905 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3906 else self
3908 coe o
3909 else (
3910 Wsi.setcursor Wsi.CURSOR_INHERIT;
3911 coe self
3914 method infochanged _ = ()
3916 method scrollpw = (0, 0.0, 0.0)
3917 method scrollph =
3918 let nfs = fstate.fontsize + 1 in
3919 let y = m_first * nfs in
3920 let itemcount = source#getitemcount in
3921 let maxi = max 0 (itemcount - fstate.maxrows) in
3922 let maxy = maxi * nfs in
3923 let p, h = scrollph y maxy in
3924 conf.scrollbw, p, h
3926 method modehash = modehash
3927 method eformsgs = false
3928 end;;
3930 class outlinelistview ~source =
3931 let settext autonarrow s =
3932 if autonarrow
3933 then state.text <- "[" ^ s ^ "]"
3934 else state.text <- s
3936 object (self)
3937 inherit listview
3938 ~source:(source :> lvsource)
3939 ~trusted:false
3940 ~modehash:(findkeyhash conf "outline")
3941 as super
3943 val m_autonarrow = false
3945 method key key mask =
3946 let maxrows =
3947 if emptystr state.text
3948 then fstate.maxrows
3949 else fstate.maxrows - 2
3951 let calcfirst first active =
3952 if active > first
3953 then
3954 let rows = active - first in
3955 if rows > maxrows then active - maxrows else first
3956 else active
3958 let navigate incr =
3959 let active = m_active + incr in
3960 let active = bound active 0 (source#getitemcount - 1) in
3961 let first = calcfirst m_first active in
3962 G.postRedisplay "outline navigate";
3963 coe {< m_active = active; m_first = first >}
3965 let navscroll first =
3966 let active =
3967 let dist = m_active - first in
3968 if dist < 0
3969 then first
3970 else (
3971 if dist < maxrows
3972 then m_active
3973 else first + maxrows
3976 G.postRedisplay "outline navscroll";
3977 coe {< m_first = first; m_active = active >}
3979 let ctrl = Wsi.withctrl mask in
3980 match key with
3981 | 97 when ctrl -> (* ctrl-a *)
3982 if m_autonarrow
3983 then source#denarrow
3984 else source#narrow m_qsearch;
3985 settext (not m_autonarrow) m_qsearch;
3986 G.postRedisplay "toggle auto narrowing";
3987 coe {< m_first = 0; m_active = 0; m_autonarrow = not m_autonarrow >}
3989 | 47 when emptystr m_qsearch && not m_autonarrow -> (* / *)
3990 settext true "";
3991 G.postRedisplay "toggle auto narrowing";
3992 coe {< m_first = 0; m_active = 0; m_autonarrow = true >}
3994 | 110 when ctrl -> (* ctrl-n *)
3995 source#narrow m_qsearch;
3996 if not m_autonarrow
3997 then source#add_narrow_pattern m_qsearch;
3998 G.postRedisplay "outline ctrl-n";
3999 coe {< m_first = 0; m_active = 0 >}
4001 | 115 when ctrl -> (* ctrl-s *)
4002 let active = source#calcactive (getanchor ()) in
4003 let first = firstof m_first active in
4004 G.postRedisplay "outline ctrl-s";
4005 coe {< m_first = first; m_active = active >}
4007 | 117 when ctrl -> (* ctrl-u *)
4008 source#del_narrow_pattern;
4009 let pattern = source#renarrow in
4010 G.postRedisplay "outline ctrl-u";
4011 let text =
4012 if emptystr pattern then "" else "Narrowed to " ^ pattern
4014 settext m_autonarrow text;
4015 coe {< m_first = 0; m_active = 0; m_qsearch = "" >}
4017 | 108 when ctrl -> (* ctrl-l *)
4018 let first = max 0 (m_active - (fstate.maxrows / 2)) in
4019 G.postRedisplay "outline ctrl-l";
4020 coe {< m_first = first >}
4022 | 0xff1b -> (* escape *)
4023 let o = super#key key mask in
4024 if m_autonarrow
4025 then (
4026 if nonemptystr m_qsearch
4027 then (
4028 source#add_narrow_pattern m_qsearch;
4029 settext true "";
4034 | 0xff0d | 0xff8d when m_autonarrow -> (* (kp) enter *)
4035 if nonemptystr m_qsearch
4036 then source#add_narrow_pattern m_qsearch;
4037 super#key key mask
4039 | key when m_autonarrow && (key != 0 && key land 0xff00 != 0xff00) ->
4040 let pattern = m_qsearch ^ toutf8 key in
4041 G.postRedisplay "outlinelistview autonarrow add";
4042 source#narrow pattern;
4043 settext true pattern;
4044 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4046 | key when m_autonarrow && key = 0xff08 -> (* backspace *)
4047 if emptystr m_qsearch
4048 then coe self
4049 else
4050 let pattern = withoutlastutf8 m_qsearch in
4051 G.postRedisplay "outlinelistview autonarrow backspace";
4052 ignore (source#renarrow);
4053 source#narrow pattern;
4054 settext true pattern;
4055 coe {< m_first = 0; m_active = 0; m_qsearch = pattern >}
4057 | 0xff9f | 0xffff -> (* (kp) delete *)
4058 source#remove m_active;
4059 G.postRedisplay "outline delete";
4060 let active = max 0 (m_active-1) in
4061 coe {< m_first = firstof m_first active;
4062 m_active = active >}
4064 | 0xff52 | 0xff97 when ctrl -> (* ctrl-(kp) up *)
4065 navscroll (max 0 (m_first - 1))
4067 | 0xff54 | 0xff99 when ctrl -> (* ctrl-(kp) down *)
4068 navscroll (min (source#getitemcount - 1) (m_first + 1))
4070 | 0xff52 | 0xff97 -> navigate ~-1 (* (kp) up *)
4071 | 0xff54 | 0xff99 -> navigate 1 (* (kp) down *)
4072 | 0xff55 | 0xff9a -> (* (kp) prior *)
4073 navigate ~-(fstate.maxrows)
4074 | 0xff56 | 0xff9b -> (* (kp) next *)
4075 navigate fstate.maxrows
4077 | 0xff53 | 0xff98 -> (* [ctrl-] (kp) right *)
4078 let o =
4079 if ctrl
4080 then (
4081 G.postRedisplay "outline ctrl right";
4082 {< m_pan = m_pan + 1 >}
4084 else self#updownlevel 1
4086 coe o
4088 | 0xff51 | 0xff96 -> (* [ctrl-] (kp) left *)
4089 let o =
4090 if ctrl
4091 then (
4092 G.postRedisplay "outline ctrl left";
4093 {< m_pan = m_pan - 1 >}
4095 else self#updownlevel ~-1
4097 coe o
4099 | 0xff50 | 0xff95 -> (* (kp) home *)
4100 G.postRedisplay "outline home";
4101 coe {< m_first = 0; m_active = 0 >}
4103 | 0xff57 | 0xff9c -> (* (kp) end *)
4104 let active = source#getitemcount - 1 in
4105 let first = max 0 (active - fstate.maxrows) in
4106 G.postRedisplay "outline end";
4107 coe {< m_active = active; m_first = first >}
4109 | _ -> super#key key mask
4112 let gotounder under =
4113 let getpath filename =
4114 let path =
4115 if nonemptystr filename
4116 then
4117 if Filename.is_relative filename
4118 then
4119 let dir = Filename.dirname state.path in
4120 let dir =
4121 if Filename.is_implicit dir
4122 then Filename.concat (Sys.getcwd ()) dir
4123 else dir
4125 Filename.concat dir filename
4126 else filename
4127 else ""
4129 if Sys.file_exists path
4130 then path
4131 else ""
4133 match under with
4134 | Ulinkgoto (pageno, top) ->
4135 if pageno >= 0
4136 then (
4137 addnav ();
4138 gotopage1 pageno top;
4141 | Ulinkuri s ->
4142 gotouri s
4144 | Uremote (filename, pageno) ->
4145 let path = getpath filename in
4146 if nonemptystr path
4147 then (
4148 if conf.riani
4149 then
4150 let command = Printf.sprintf "%s -page %d %S" !selfexec pageno path in
4151 try popen command []
4152 with exn ->
4153 Printf.eprintf "failed to execute `%s': %s\n" command (exntos exn);
4154 flush stderr;
4155 else
4156 let anchor = getanchor () in
4157 let ranchor = state.path, state.password, anchor, state.origin in
4158 state.origin <- "";
4159 state.anchor <- (pageno, 0.0, 0.0);
4160 state.ranchors <- ranchor :: state.ranchors;
4161 opendoc path "";
4163 else showtext '!' ("Could not find " ^ filename)
4165 | Uremotedest (filename, destname) ->
4166 let path = getpath filename in
4167 if nonemptystr path
4168 then (
4169 if conf.riani
4170 then
4171 let command = !selfexec ^ " " ^ path ^ " -dest " ^ destname in
4172 try popen command []
4173 with exn ->
4174 Printf.eprintf
4175 "failed to execute `%s': %s\n" command (exntos exn);
4176 flush stderr;
4177 else
4178 let anchor = getanchor () in
4179 let ranchor = state.path, state.password, anchor, state.origin in
4180 state.origin <- "";
4181 state.nameddest <- destname;
4182 state.ranchors <- ranchor :: state.ranchors;
4183 opendoc path "";
4185 else showtext '!' ("Could not find " ^ filename)
4187 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4190 let gotooutline (_, _, kind) =
4191 match kind with
4192 | Onone -> ()
4193 | Oanchor anchor ->
4194 let (pageno, y, _) = anchor in
4195 let y = getanchory
4196 (if conf.presentation then (pageno, y, 1.0) else anchor)
4198 gotoghyll y
4199 | Ouri uri -> gotounder (Ulinkuri uri)
4200 | Olaunch cmd -> gotounder (Ulaunch cmd)
4201 | Oremote remote -> gotounder (Uremote remote)
4202 | Oremotedest remotedest -> gotounder (Uremotedest remotedest)
4205 let outlinesource usebookmarks =
4206 let empty = [||] in
4207 (object (self)
4208 inherit lvsourcebase
4209 val mutable m_items = empty
4210 val mutable m_orig_items = empty
4211 val mutable m_narrow_patterns = []
4212 val mutable m_hadremovals = false
4214 method getitemcount =
4215 Array.length m_items + (if m_hadremovals then 1 else 0)
4217 method getitem n =
4218 if n == Array.length m_items && m_hadremovals
4219 then
4220 ("[Confirm removal]", 0)
4221 else
4222 let s, n, _ = m_items.(n) in
4223 (s, n)
4225 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4226 ignore (uioh, first, qsearch);
4227 let confrimremoval = m_hadremovals && active = Array.length m_items in
4228 let items =
4229 if m_narrow_patterns = []
4230 then m_orig_items
4231 else m_items
4233 if not cancel
4234 then (
4235 if not confrimremoval
4236 then (
4237 gotooutline m_items.(active);
4238 m_items <- items;
4240 else (
4241 state.bookmarks <- Array.to_list m_items;
4242 m_orig_items <- m_items;
4245 else m_items <- items;
4246 m_pan <- pan;
4247 None
4249 method hasaction _ = true
4251 method greetmsg =
4252 if Array.length m_items != Array.length m_orig_items
4253 then
4254 let s =
4255 match m_narrow_patterns with
4256 | one :: [] -> one
4257 | many -> String.concat "\xe2\x80\xa6" (List.rev many)
4259 "Narrowed to " ^ s ^ " (ctrl-u to restore)"
4260 else ""
4262 method narrow pattern =
4263 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
4264 match reopt with
4265 | None -> ()
4266 | Some re ->
4267 let rec loop accu n =
4268 if n = -1
4269 then m_items <- Array.of_list accu
4270 else
4271 let (s, _, _) as o = m_items.(n) in
4272 let accu =
4273 if (try ignore (Str.search_forward re s 0); true
4274 with Not_found -> false)
4275 then o :: accu
4276 else accu
4278 loop accu (n-1)
4280 loop [] (Array.length m_items - 1)
4282 method denarrow =
4283 m_orig_items <- (
4284 if usebookmarks
4285 then Array.of_list state.bookmarks
4286 else state.outlines
4288 m_items <- m_orig_items
4290 method remove m =
4291 if usebookmarks
4292 then
4293 if m >= 0 && m < Array.length m_items
4294 then (
4295 m_hadremovals <- true;
4296 m_items <- Array.init (Array.length m_items - 1) (fun n ->
4297 let n = if n >= m then n+1 else n in
4298 m_items.(n)
4302 method add_narrow_pattern pattern =
4303 m_narrow_patterns <- pattern :: m_narrow_patterns
4305 method del_narrow_pattern =
4306 match m_narrow_patterns with
4307 | _ :: rest -> m_narrow_patterns <- rest
4308 | [] -> ()
4310 method renarrow =
4311 self#denarrow;
4312 match m_narrow_patterns with
4313 | pattern :: [] -> self#narrow pattern; pattern
4314 | list ->
4315 List.fold_left (fun accu pattern ->
4316 self#narrow pattern;
4317 pattern ^ "\xe2\x80\xa6" ^ accu) "" list
4319 method calcactive anchor =
4320 let rely = getanchory anchor in
4321 let rec loop n best bestd =
4322 if n = Array.length m_items
4323 then best
4324 else
4325 let _, _, kind = m_items.(n) in
4326 match kind with
4327 | Oanchor anchor ->
4328 let orely = getanchory anchor in
4329 let d = abs (orely - rely) in
4330 if d < bestd
4331 then loop (n+1) n d
4332 else loop (n+1) best bestd
4333 | Onone | Oremote _ | Olaunch _ | Oremotedest _ | Ouri _ ->
4334 loop (n+1) best bestd
4336 loop 0 ~-1 max_int
4338 method reset anchor items =
4339 m_hadremovals <- false;
4340 if m_orig_items == empty
4341 then (
4342 m_orig_items <- items;
4343 if m_narrow_patterns == []
4344 then m_items <- items;
4346 let active = self#calcactive anchor in
4347 m_active <- active;
4348 m_first <- firstof m_first active
4349 end)
4352 let enterselector usebookmarks =
4353 let source = outlinesource usebookmarks in
4354 fun errmsg ->
4355 let outlines =
4356 if usebookmarks
4357 then Array.of_list state.bookmarks
4358 else state.outlines
4360 if Array.length outlines = 0
4361 then (
4362 showtext ' ' errmsg;
4364 else (
4365 state.text <- source#greetmsg;
4366 Wsi.setcursor Wsi.CURSOR_INHERIT;
4367 let anchor = getanchor () in
4368 source#reset anchor outlines;
4369 state.uioh <- coe (new outlinelistview ~source);
4370 G.postRedisplay "enter selector";
4374 let enteroutlinemode =
4375 let f = enterselector false in
4376 fun ()-> f "Document has no outline";
4379 let enterbookmarkmode =
4380 let f = enterselector true in
4381 fun () -> f "Document has no bookmarks (yet)";
4384 let color_of_string s =
4385 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
4386 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
4390 let color_to_string (r, g, b) =
4391 let r = truncate (r *. 256.0)
4392 and g = truncate (g *. 256.0)
4393 and b = truncate (b *. 256.0) in
4394 Printf.sprintf "%d/%d/%d" r g b
4397 let irect_of_string s =
4398 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
4401 let irect_to_string (x0,y0,x1,y1) =
4402 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
4405 let makecheckers () =
4406 (* Based on lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
4407 following to say:
4408 converted by Issac Trotts. July 25, 2002 *)
4409 let image = GlPix.create `ubyte ~format:`luminance ~width:2 ~height:2 in
4410 Raw.sets_string (GlPix.to_raw image) ~pos:0 "\255\200\200\255";
4411 let id = GlTex.gen_texture () in
4412 GlTex.bind_texture `texture_2d id;
4413 GlPix.store (`unpack_alignment 1);
4414 GlTex.image2d image;
4415 List.iter (GlTex.parameter ~target:`texture_2d)
4416 [ `mag_filter `nearest; `min_filter `nearest ];
4420 let setcheckers enabled =
4421 match state.texid with
4422 | None ->
4423 if enabled then state.texid <- Some (makecheckers ())
4425 | Some texid ->
4426 if not enabled
4427 then (
4428 GlTex.delete_texture texid;
4429 state.texid <- None;
4433 let int_of_string_with_suffix s =
4434 let l = String.length s in
4435 let s1, shift =
4436 if l > 1
4437 then
4438 let suffix = Char.lowercase s.[l-1] in
4439 match suffix with
4440 | 'k' -> String.sub s 0 (l-1), 10
4441 | 'm' -> String.sub s 0 (l-1), 20
4442 | 'g' -> String.sub s 0 (l-1), 30
4443 | _ -> s, 0
4444 else s, 0
4446 let n = int_of_string s1 in
4447 let m = n lsl shift in
4448 if m < 0 || m < n
4449 then raise (Failure "value too large")
4450 else m
4453 let string_with_suffix_of_int n =
4454 if n = 0
4455 then "0"
4456 else
4457 let units = [(30, "G"); (20, "M"); (10, "K")] in
4458 let prettyint n =
4459 let rec loop s n =
4460 let h = n mod 1000 in
4461 let n = n / 1000 in
4462 if n = 0
4463 then string_of_int h ^ s
4464 else (
4465 let s = Printf.sprintf "_%03d%s" h s in
4466 loop s n
4469 loop "" n
4471 let rec find = function
4472 | [] -> prettyint n
4473 | (shift, suffix) :: rest ->
4474 if (n land ((1 lsl shift) - 1)) = 0
4475 then prettyint (n lsr shift) ^ suffix
4476 else find rest
4478 find units
4481 let defghyllscroll = (40, 8, 32);;
4482 let fastghyllscroll = (5,1,2);;
4483 let neatghyllscroll = (10,1,9);;
4484 let ghyllscroll_of_string s =
4485 match s with
4486 | "default" -> Some defghyllscroll
4487 | "fast" -> Some (5,1,2)
4488 | "neat" -> Some (10,1,9)
4489 | "" | "none" -> None
4490 | _ ->
4491 let (n,a,b) as nab =
4492 Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b) in
4493 if n <= a || n <= b || a >= b
4494 then error "invalid ghyll N(%d),A(%d),B(%d) (N <= A, A < B, N <= B)"
4495 n a b;
4496 Some nab
4499 let ghyllscroll_to_string ((n, a, b) as nab) =
4500 (**) if nab = defghyllscroll then "default"
4501 else if nab = fastghyllscroll then "fast"
4502 else if nab = neatghyllscroll then "neat"
4503 else Printf.sprintf "%d,%d,%d" n a b;
4506 let describe_location () =
4507 let fn = page_of_y state.y in
4508 let ln = page_of_y (state.y + state.winh - hscrollh () - 1) in
4509 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
4510 let percent =
4511 if maxy <= 0
4512 then 100.
4513 else (100. *. (float state.y /. float maxy))
4515 if fn = ln
4516 then
4517 Printf.sprintf "page %d of %d [%.2f%%]"
4518 (fn+1) state.pagecount percent
4519 else
4520 Printf.sprintf
4521 "pages %d-%d of %d [%.2f%%]"
4522 (fn+1) (ln+1) state.pagecount percent
4525 let setpresentationmode v =
4526 let n = page_of_y state.y in
4527 state.anchor <- (n, 0.0, 1.0);
4528 conf.presentation <- v;
4529 if conf.fitmodel = FitPage
4530 then reqlayout conf.angle conf.fitmodel;
4531 represent ();
4534 let enterinfomode =
4535 let btos b = if b then "\xe2\x88\x9a" else "" in
4536 let showextended = ref false in
4537 let leave mode = function
4538 | Confirm -> state.mode <- mode
4539 | Cancel -> state.mode <- mode in
4540 let src =
4541 (object
4542 val mutable m_first_time = true
4543 val mutable m_l = []
4544 val mutable m_a = [||]
4545 val mutable m_prev_uioh = nouioh
4546 val mutable m_prev_mode = View
4548 inherit lvsourcebase
4550 method reset prev_mode prev_uioh =
4551 m_a <- Array.of_list (List.rev m_l);
4552 m_l <- [];
4553 m_prev_mode <- prev_mode;
4554 m_prev_uioh <- prev_uioh;
4555 if m_first_time
4556 then (
4557 let rec loop n =
4558 if n >= Array.length m_a
4559 then ()
4560 else
4561 match m_a.(n) with
4562 | _, _, _, Action _ -> m_active <- n
4563 | _ -> loop (n+1)
4565 loop 0;
4566 m_first_time <- false;
4569 method int name get set =
4570 m_l <-
4571 (name, `int get, 1, Action (
4572 fun u ->
4573 let ondone s =
4574 try set (int_of_string s)
4575 with exn ->
4576 state.text <- Printf.sprintf "bad integer `%s': %s"
4577 s (exntos exn)
4579 state.text <- "";
4580 let te = name ^ ": ", "", None, intentry, ondone, true in
4581 state.mode <- Textentry (te, leave m_prev_mode);
4583 )) :: m_l
4585 method int_with_suffix name get set =
4586 m_l <-
4587 (name, `intws get, 1, Action (
4588 fun u ->
4589 let ondone s =
4590 try set (int_of_string_with_suffix s)
4591 with exn ->
4592 state.text <- Printf.sprintf "bad integer `%s': %s"
4593 s (exntos exn)
4595 state.text <- "";
4596 let te =
4597 name ^ ": ", "", None, intentry_with_suffix, ondone, true
4599 state.mode <- Textentry (te, leave m_prev_mode);
4601 )) :: m_l
4603 method bool ?(offset=1) ?(btos=btos) name get set =
4604 m_l <-
4605 (name, `bool (btos, get), offset, Action (
4606 fun u ->
4607 let v = get () in
4608 set (not v);
4610 )) :: m_l
4612 method color name get set =
4613 m_l <-
4614 (name, `color get, 1, Action (
4615 fun u ->
4616 let invalid = (nan, nan, nan) in
4617 let ondone s =
4618 let c =
4619 try color_of_string s
4620 with exn ->
4621 state.text <- Printf.sprintf "bad color `%s': %s"
4622 s (exntos exn);
4623 invalid
4625 if c <> invalid
4626 then set c;
4628 let te = name ^ ": ", "", None, textentry, ondone, true in
4629 state.text <- color_to_string (get ());
4630 state.mode <- Textentry (te, leave m_prev_mode);
4632 )) :: m_l
4634 method string name get set =
4635 m_l <-
4636 (name, `string get, 1, Action (
4637 fun u ->
4638 let ondone s = set s in
4639 let te = name ^ ": ", "", None, textentry, ondone, true in
4640 state.mode <- Textentry (te, leave m_prev_mode);
4642 )) :: m_l
4644 method colorspace name get set =
4645 m_l <-
4646 (name, `string get, 1, Action (
4647 fun _ ->
4648 let source =
4649 (object
4650 inherit lvsourcebase
4652 initializer
4653 m_active <- CSTE.to_int conf.colorspace;
4654 m_first <- 0;
4656 method getitemcount =
4657 Array.length CSTE.names
4658 method getitem n =
4659 (CSTE.names.(n), 0)
4660 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4661 ignore (uioh, first, pan, qsearch);
4662 if not cancel then set active;
4663 None
4664 method hasaction _ = true
4665 end)
4667 state.text <- "";
4668 let modehash = findkeyhash conf "info" in
4669 coe (new listview ~source ~trusted:true ~modehash)
4670 )) :: m_l
4672 method paxmark name get set =
4673 m_l <-
4674 (name, `string get, 1, Action (
4675 fun _ ->
4676 let source =
4677 (object
4678 inherit lvsourcebase
4680 initializer
4681 m_active <- MTE.to_int conf.paxmark;
4682 m_first <- 0;
4684 method getitemcount = Array.length MTE.names
4685 method getitem n = (MTE.names.(n), 0)
4686 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4687 ignore (uioh, first, pan, qsearch);
4688 if not cancel then set active;
4689 None
4690 method hasaction _ = true
4691 end)
4693 state.text <- "";
4694 let modehash = findkeyhash conf "info" in
4695 coe (new listview ~source ~trusted:true ~modehash)
4696 )) :: m_l
4698 method fitmodel name get set =
4699 m_l <-
4700 (name, `string get, 1, Action (
4701 fun _ ->
4702 let source =
4703 (object
4704 inherit lvsourcebase
4706 initializer
4707 m_active <- FMTE.to_int conf.fitmodel;
4708 m_first <- 0;
4710 method getitemcount = Array.length FMTE.names
4711 method getitem n = (FMTE.names.(n), 0)
4712 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4713 ignore (uioh, first, pan, qsearch);
4714 if not cancel then set active;
4715 None
4716 method hasaction _ = true
4717 end)
4719 state.text <- "";
4720 let modehash = findkeyhash conf "info" in
4721 coe (new listview ~source ~trusted:true ~modehash)
4722 )) :: m_l
4724 method caption s offset =
4725 m_l <- (s, `empty, offset, Noaction) :: m_l
4727 method caption2 s f offset =
4728 m_l <- (s, `string f, offset, Noaction) :: m_l
4730 method getitemcount = Array.length m_a
4732 method getitem n =
4733 let tostr = function
4734 | `int f -> string_of_int (f ())
4735 | `intws f -> string_with_suffix_of_int (f ())
4736 | `string f -> f ()
4737 | `color f -> color_to_string (f ())
4738 | `bool (btos, f) -> btos (f ())
4739 | `empty -> ""
4741 let name, t, offset, _ = m_a.(n) in
4742 ((let s = tostr t in
4743 if nonemptystr s
4744 then Printf.sprintf "%s\t%s" name s
4745 else name),
4746 offset)
4748 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4749 let uiohopt =
4750 if not cancel
4751 then (
4752 m_qsearch <- qsearch;
4753 let uioh =
4754 match m_a.(active) with
4755 | _, _, _, Action f -> f uioh
4756 | _ -> uioh
4758 Some uioh
4760 else None
4762 m_active <- active;
4763 m_first <- first;
4764 m_pan <- pan;
4765 uiohopt
4767 method hasaction n =
4768 match m_a.(n) with
4769 | _, _, _, Action _ -> true
4770 | _ -> false
4771 end)
4773 let rec fillsrc prevmode prevuioh =
4774 let sep () = src#caption "" 0 in
4775 let colorp name get set =
4776 src#string name
4777 (fun () -> color_to_string (get ()))
4778 (fun v ->
4780 let c = color_of_string v in
4781 set c
4782 with exn ->
4783 state.text <- Printf.sprintf "bad color `%s': %s" v (exntos exn)
4786 let oldmode = state.mode in
4787 let birdseye = isbirdseye state.mode in
4789 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
4791 src#bool "presentation mode"
4792 (fun () -> conf.presentation)
4793 (fun v -> setpresentationmode v);
4795 src#bool "ignore case in searches"
4796 (fun () -> conf.icase)
4797 (fun v -> conf.icase <- v);
4799 src#bool "preload"
4800 (fun () -> conf.preload)
4801 (fun v -> conf.preload <- v);
4803 src#bool "highlight links"
4804 (fun () -> conf.hlinks)
4805 (fun v -> conf.hlinks <- v);
4807 src#bool "under info"
4808 (fun () -> conf.underinfo)
4809 (fun v -> conf.underinfo <- v);
4811 src#bool "persistent bookmarks"
4812 (fun () -> conf.savebmarks)
4813 (fun v -> conf.savebmarks <- v);
4815 src#fitmodel "fit model"
4816 (fun () -> FMTE.to_string conf.fitmodel)
4817 (fun v -> reqlayout conf.angle (FMTE.of_int v));
4819 src#bool "trim margins"
4820 (fun () -> conf.trimmargins)
4821 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
4823 src#bool "persistent location"
4824 (fun () -> conf.jumpback)
4825 (fun v -> conf.jumpback <- v);
4827 sep ();
4828 src#int "inter-page space"
4829 (fun () -> conf.interpagespace)
4830 (fun n ->
4831 conf.interpagespace <- n;
4832 docolumns conf.columns;
4833 let pageno, py =
4834 match state.layout with
4835 | [] -> 0, 0
4836 | l :: _ ->
4837 l.pageno, l.pagey
4839 state.maxy <- calcheight ();
4840 let y = getpagey pageno in
4841 gotoy (y + py)
4844 src#int "page bias"
4845 (fun () -> conf.pagebias)
4846 (fun v -> conf.pagebias <- v);
4848 src#int "scroll step"
4849 (fun () -> conf.scrollstep)
4850 (fun n -> conf.scrollstep <- n);
4852 src#int "horizontal scroll step"
4853 (fun () -> conf.hscrollstep)
4854 (fun v -> conf.hscrollstep <- v);
4856 src#int "auto scroll step"
4857 (fun () ->
4858 match state.autoscroll with
4859 | Some step -> step
4860 | _ -> conf.autoscrollstep)
4861 (fun n ->
4862 let n = boundastep state.winh n in
4863 if state.autoscroll <> None
4864 then state.autoscroll <- Some n;
4865 conf.autoscrollstep <- n);
4867 src#int "zoom"
4868 (fun () -> truncate (conf.zoom *. 100.))
4869 (fun v -> setzoom ((float v) /. 100.));
4871 src#int "rotation"
4872 (fun () -> conf.angle)
4873 (fun v -> reqlayout v conf.fitmodel);
4875 src#int "scroll bar width"
4876 (fun () -> conf.scrollbw)
4877 (fun v ->
4878 conf.scrollbw <- v;
4879 reshape state.winw state.winh;
4882 src#int "scroll handle height"
4883 (fun () -> conf.scrollh)
4884 (fun v -> conf.scrollh <- v;);
4886 src#int "thumbnail width"
4887 (fun () -> conf.thumbw)
4888 (fun v ->
4889 conf.thumbw <- min 4096 v;
4890 match oldmode with
4891 | Birdseye beye ->
4892 leavebirdseye beye false;
4893 enterbirdseye ()
4894 | _ -> ()
4897 let mode = state.mode in
4898 src#string "columns"
4899 (fun () ->
4900 match conf.columns with
4901 | Csingle _ -> "1"
4902 | Cmulti (multi, _) -> multicolumns_to_string multi
4903 | Csplit (count, _) -> "-" ^ string_of_int count
4905 (fun v ->
4906 let n, a, b = multicolumns_of_string v in
4907 setcolumns mode n a b);
4909 sep ();
4910 src#caption "Pixmap cache" 0;
4911 src#int_with_suffix "size (advisory)"
4912 (fun () -> conf.memlimit)
4913 (fun v -> conf.memlimit <- v);
4915 src#caption2 "used"
4916 (fun () -> Printf.sprintf "%s bytes, %d tiles"
4917 (string_with_suffix_of_int state.memused)
4918 (Hashtbl.length state.tilemap)) 1;
4920 sep ();
4921 src#caption "Layout" 0;
4922 src#caption2 "Dimension"
4923 (fun () ->
4924 Printf.sprintf "%dx%d (virtual %dx%d)"
4925 state.winw state.winh
4926 state.w state.maxy)
4928 if conf.debug
4929 then
4930 src#caption2 "Position" (fun () ->
4931 Printf.sprintf "%dx%d" state.x state.y
4933 else
4934 src#caption2 "Position" (fun () -> describe_location ()) 1
4937 sep ();
4938 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4939 "Save these parameters as global defaults at exit"
4940 (fun () -> conf.bedefault)
4941 (fun v -> conf.bedefault <- v)
4944 sep ();
4945 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4946 src#bool ~offset:0 ~btos "Extended parameters"
4947 (fun () -> !showextended)
4948 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4949 if !showextended
4950 then (
4951 src#bool "checkers"
4952 (fun () -> conf.checkers)
4953 (fun v -> conf.checkers <- v; setcheckers v);
4954 src#bool "update cursor"
4955 (fun () -> conf.updatecurs)
4956 (fun v -> conf.updatecurs <- v);
4957 src#bool "verbose"
4958 (fun () -> conf.verbose)
4959 (fun v -> conf.verbose <- v);
4960 src#bool "invert colors"
4961 (fun () -> conf.invert)
4962 (fun v -> conf.invert <- v);
4963 src#bool "max fit"
4964 (fun () -> conf.maxhfit)
4965 (fun v -> conf.maxhfit <- v);
4966 src#bool "redirect stderr"
4967 (fun () -> conf.redirectstderr)
4968 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4969 src#bool "pax mode"
4970 (fun () -> conf.pax != None)
4971 (fun v ->
4972 if v
4973 then conf.pax <- Some (ref (now (), 0, 0))
4974 else conf.pax <- None);
4975 src#string "uri launcher"
4976 (fun () -> conf.urilauncher)
4977 (fun v -> conf.urilauncher <- v);
4978 src#string "path launcher"
4979 (fun () -> conf.pathlauncher)
4980 (fun v -> conf.pathlauncher <- v);
4981 src#string "tile size"
4982 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4983 (fun v ->
4985 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4986 conf.tilew <- max 64 w;
4987 conf.tileh <- max 64 h;
4988 flushtiles ();
4989 with exn ->
4990 state.text <- Printf.sprintf "bad tile size `%s': %s"
4991 v (exntos exn)
4993 src#int "texture count"
4994 (fun () -> conf.texcount)
4995 (fun v ->
4996 if realloctexts v
4997 then conf.texcount <- v
4998 else showtext '!' " Failed to set texture count please retry later"
5000 src#int "slice height"
5001 (fun () -> conf.sliceheight)
5002 (fun v ->
5003 conf.sliceheight <- v;
5004 wcmd "sliceh %d" conf.sliceheight;
5006 src#int "anti-aliasing level"
5007 (fun () -> conf.aalevel)
5008 (fun v ->
5009 conf.aalevel <- bound v 0 8;
5010 state.anchor <- getanchor ();
5011 opendoc state.path state.password;
5013 src#string "page scroll scaling factor"
5014 (fun () -> string_of_float conf.pgscale)
5015 (fun v ->
5017 let s = float_of_string v in
5018 conf.pgscale <- s
5019 with exn ->
5020 state.text <- Printf.sprintf
5021 "bad page scroll scaling factor `%s': %s" v (exntos exn)
5024 src#int "ui font size"
5025 (fun () -> fstate.fontsize)
5026 (fun v -> setfontsize (bound v 5 100));
5027 src#int "hint font size"
5028 (fun () -> conf.hfsize)
5029 (fun v -> conf.hfsize <- bound v 5 100);
5030 colorp "background color"
5031 (fun () -> conf.bgcolor)
5032 (fun v -> conf.bgcolor <- v);
5033 src#bool "crop hack"
5034 (fun () -> conf.crophack)
5035 (fun v -> conf.crophack <- v);
5036 src#string "trim fuzz"
5037 (fun () -> irect_to_string conf.trimfuzz)
5038 (fun v ->
5040 conf.trimfuzz <- irect_of_string v;
5041 if conf.trimmargins
5042 then settrim true conf.trimfuzz;
5043 with exn ->
5044 state.text <- Printf.sprintf "bad irect `%s': %s" v (exntos exn)
5046 src#string "throttle"
5047 (fun () ->
5048 match conf.maxwait with
5049 | None -> "show place holder if page is not ready"
5050 | Some time ->
5051 if time = infinity
5052 then "wait for page to fully render"
5053 else
5054 "wait " ^ string_of_float time
5055 ^ " seconds before showing placeholder"
5057 (fun v ->
5059 let f = float_of_string v in
5060 if f <= 0.0
5061 then conf.maxwait <- None
5062 else conf.maxwait <- Some f
5063 with exn ->
5064 state.text <- Printf.sprintf "bad time `%s': %s" v (exntos exn)
5066 src#string "ghyll scroll"
5067 (fun () ->
5068 match conf.ghyllscroll with
5069 | None -> ""
5070 | Some nab -> ghyllscroll_to_string nab
5072 (fun v ->
5073 try conf.ghyllscroll <- ghyllscroll_of_string v
5074 with exn ->
5075 state.text <- Printf.sprintf "bad ghyll `%s': %s" v (exntos exn)
5077 src#string "selection command"
5078 (fun () -> conf.selcmd)
5079 (fun v -> conf.selcmd <- v);
5080 src#string "synctex command"
5081 (fun () -> conf.stcmd)
5082 (fun v -> conf.stcmd <- v);
5083 src#string "pax command"
5084 (fun () -> conf.paxcmd)
5085 (fun v -> conf.paxcmd <- v);
5086 src#colorspace "color space"
5087 (fun () -> CSTE.to_string conf.colorspace)
5088 (fun v ->
5089 conf.colorspace <- CSTE.of_int v;
5090 wcmd "cs %d" v;
5091 load state.layout;
5093 src#paxmark "pax mark method"
5094 (fun () -> MTE.to_string conf.paxmark)
5095 (fun v -> conf.paxmark <- MTE.of_int v);
5096 if pbousable ()
5097 then
5098 src#bool "use PBO"
5099 (fun () -> conf.usepbo)
5100 (fun v -> conf.usepbo <- v);
5101 src#bool "mouse wheel scrolls pages"
5102 (fun () -> conf.wheelbypage)
5103 (fun v -> conf.wheelbypage <- v);
5104 src#bool "open remote links in a new instance"
5105 (fun () -> conf.riani)
5106 (fun v -> conf.riani <- v);
5109 sep ();
5110 src#caption "Document" 0;
5111 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
5112 src#caption2 "Pages"
5113 (fun () -> string_of_int state.pagecount) 1;
5114 src#caption2 "Dimensions"
5115 (fun () -> string_of_int (List.length state.pdims)) 1;
5116 if conf.trimmargins
5117 then (
5118 sep ();
5119 src#caption "Trimmed margins" 0;
5120 src#caption2 "Dimensions"
5121 (fun () -> string_of_int (List.length state.pdims)) 1;
5124 sep ();
5125 src#caption "OpenGL" 0;
5126 src#caption (Printf.sprintf "Vendor\t%s" (GlMisc.get_string `vendor)) 1;
5127 src#caption (Printf.sprintf "Renderer\t%s" (GlMisc.get_string `renderer)) 1;
5129 sep ();
5130 src#caption "Location" 0;
5131 if nonemptystr state.origin
5132 then src#caption ("Orign\t" ^ mbtoutf8 state.origin) 1;
5133 src#caption ("Path\t" ^ mbtoutf8 state.path) 1;
5135 src#reset prevmode prevuioh;
5137 fun () ->
5138 state.text <- "";
5139 let prevmode = state.mode
5140 and prevuioh = state.uioh in
5141 fillsrc prevmode prevuioh;
5142 let source = (src :> lvsource) in
5143 let modehash = findkeyhash conf "info" in
5144 state.uioh <- coe (object (self)
5145 inherit listview ~source ~trusted:true ~modehash as super
5146 val mutable m_prevmemused = 0
5147 method infochanged = function
5148 | Memused ->
5149 if m_prevmemused != state.memused
5150 then (
5151 m_prevmemused <- state.memused;
5152 G.postRedisplay "memusedchanged";
5154 | Pdim -> G.postRedisplay "pdimchanged"
5155 | Docinfo -> fillsrc prevmode prevuioh
5157 method key key mask =
5158 if not (Wsi.withctrl mask)
5159 then
5160 match key with
5161 | 0xff51 | 0xff96 -> coe (self#updownlevel ~-1) (* (kp) left *)
5162 | 0xff53 | 0xff98 -> coe (self#updownlevel 1) (* (kp) right *)
5163 | _ -> super#key key mask
5164 else super#key key mask
5165 end);
5166 G.postRedisplay "info";
5169 let enterhelpmode =
5170 let source =
5171 (object
5172 inherit lvsourcebase
5173 method getitemcount = Array.length state.help
5174 method getitem n =
5175 let s, l, _ = state.help.(n) in
5176 (s, l)
5178 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5179 let optuioh =
5180 if not cancel
5181 then (
5182 m_qsearch <- qsearch;
5183 match state.help.(active) with
5184 | _, _, Action f -> Some (f uioh)
5185 | _ -> Some (uioh)
5187 else None
5189 m_active <- active;
5190 m_first <- first;
5191 m_pan <- pan;
5192 optuioh
5194 method hasaction n =
5195 match state.help.(n) with
5196 | _, _, Action _ -> true
5197 | _ -> false
5199 initializer
5200 m_active <- -1
5201 end)
5202 in fun () ->
5203 let modehash = findkeyhash conf "help" in
5204 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
5205 G.postRedisplay "help";
5208 let entermsgsmode =
5209 let msgsource =
5210 let re = Str.regexp "[\r\n]" in
5211 (object
5212 inherit lvsourcebase
5213 val mutable m_items = [||]
5215 method getitemcount = 1 + Array.length m_items
5217 method getitem n =
5218 if n = 0
5219 then "[Clear]", 0
5220 else m_items.(n-1), 0
5222 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
5223 ignore uioh;
5224 if not cancel
5225 then (
5226 if active = 0
5227 then Buffer.clear state.errmsgs;
5228 m_qsearch <- qsearch;
5230 m_active <- active;
5231 m_first <- first;
5232 m_pan <- pan;
5233 None
5235 method hasaction n =
5236 n = 0
5238 method reset =
5239 state.newerrmsgs <- false;
5240 let l = Str.split re (Buffer.contents state.errmsgs) in
5241 m_items <- Array.of_list l
5243 initializer
5244 m_active <- 0
5245 end)
5246 in fun () ->
5247 state.text <- "";
5248 msgsource#reset;
5249 let source = (msgsource :> lvsource) in
5250 let modehash = findkeyhash conf "listview" in
5251 state.uioh <- coe (object
5252 inherit listview ~source ~trusted:false ~modehash as super
5253 method display =
5254 if state.newerrmsgs
5255 then msgsource#reset;
5256 super#display
5257 end);
5258 G.postRedisplay "msgs";
5261 let quickbookmark ?title () =
5262 match state.layout with
5263 | [] -> ()
5264 | l :: _ ->
5265 let title =
5266 match title with
5267 | None ->
5268 let sec = Unix.gettimeofday () in
5269 let tm = Unix.localtime sec in
5270 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
5271 (l.pageno+1)
5272 tm.Unix.tm_mday
5273 tm.Unix.tm_mon
5274 (tm.Unix.tm_year + 1900)
5275 tm.Unix.tm_hour
5276 tm.Unix.tm_min
5277 | Some title -> title
5279 state.bookmarks <- (title, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5282 let setautoscrollspeed step goingdown =
5283 let incr = max 1 ((abs step) / 2) in
5284 let incr = if goingdown then incr else -incr in
5285 let astep = boundastep state.winh (step + incr) in
5286 state.autoscroll <- Some astep;
5289 let canpan () =
5290 match conf.columns with
5291 | Csplit _ -> true
5292 | _ -> state.x != 0 || conf.zoom > 1.0
5295 let panbound x = bound x (-state.w) (wadjsb state.winw);;
5297 let existsinrow pageno (columns, coverA, coverB) p =
5298 let last = ((pageno - coverA) mod columns) + columns in
5299 let rec any = function
5300 | [] -> false
5301 | l :: rest ->
5302 if l.pageno = coverA - 1 || l.pageno = state.pagecount - coverB
5303 then p l
5304 else (
5305 if not (p l)
5306 then (if l.pageno = last then false else any rest)
5307 else true
5310 any state.layout
5313 let nextpage () =
5314 match state.layout with
5315 | [] ->
5316 let pageno = page_of_y state.y in
5317 gotoghyll (getpagey (pageno+1))
5318 | l :: rest ->
5319 match conf.columns with
5320 | Csingle _ ->
5321 if conf.presentation && rest == [] && l.pageh > l.pagey + l.pagevh
5322 then
5323 let y = clamp (pgscale state.winh) in
5324 gotoghyll y
5325 else
5326 let pageno = min (l.pageno+1) (state.pagecount-1) in
5327 gotoghyll (getpagey pageno)
5328 | Cmulti ((c, _, _) as cl, _) ->
5329 if conf.presentation
5330 && (existsinrow l.pageno cl
5331 (fun l -> l.pageh > l.pagey + l.pagevh))
5332 then
5333 let y = clamp (pgscale state.winh) in
5334 gotoghyll y
5335 else
5336 let pageno = min (l.pageno+c) (state.pagecount-1) in
5337 gotoghyll (getpagey pageno)
5338 | Csplit (n, _) ->
5339 if l.pageno < state.pagecount - 1 || l.pagecol < n - 1
5340 then
5341 let pagey, pageh = getpageyh l.pageno in
5342 let pagey = pagey + pageh * l.pagecol in
5343 let ips = if l.pagecol = 0 then 0 else conf.interpagespace in
5344 gotoghyll (pagey + pageh + ips)
5347 let prevpage () =
5348 match state.layout with
5349 | [] ->
5350 let pageno = page_of_y state.y in
5351 gotoghyll (getpagey (pageno-1))
5352 | l :: _ ->
5353 match conf.columns with
5354 | Csingle _ ->
5355 if conf.presentation && l.pagey != 0
5356 then
5357 gotoghyll (clamp (pgscale ~-(state.winh)))
5358 else
5359 let pageno = max 0 (l.pageno-1) in
5360 gotoghyll (getpagey pageno)
5361 | Cmulti ((c, _, coverB) as cl, _) ->
5362 if conf.presentation &&
5363 (existsinrow l.pageno cl (fun l -> l.pagey != 0))
5364 then
5365 gotoghyll (clamp (pgscale ~-(state.winh)))
5366 else
5367 let decr =
5368 if l.pageno = state.pagecount - coverB
5369 then 1
5370 else c
5372 let pageno = max 0 (l.pageno-decr) in
5373 gotoghyll (getpagey pageno)
5374 | Csplit (n, _) ->
5375 let y =
5376 if l.pagecol = 0
5377 then
5378 if l.pageno = 0
5379 then l.pagey
5380 else
5381 let pageno = max 0 (l.pageno-1) in
5382 let pagey, pageh = getpageyh pageno in
5383 pagey + (n-1)*pageh
5384 else
5385 let pagey, pageh = getpageyh l.pageno in
5386 pagey + pageh * (l.pagecol-1) - conf.interpagespace
5388 gotoghyll y
5391 let viewkeyboard key mask =
5392 let enttext te =
5393 let mode = state.mode in
5394 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
5395 state.text <- "";
5396 enttext ();
5397 G.postRedisplay "view:enttext"
5399 let ctrl = Wsi.withctrl mask in
5400 let key =
5401 if key >= 0xffb0 && key < 0xffb9 then key - 0xffb0 + 48 else key
5403 match key with
5404 | 81 -> (* Q *)
5405 exit 0
5407 | 0xff63 -> (* insert *)
5408 if conf.angle mod 360 = 0 && not (isbirdseye state.mode)
5409 then (
5410 state.mode <- LinkNav (Ltgendir 0);
5411 gotoy state.y;
5413 else showtext '!' "Keyboard link navigation does not work under rotation"
5415 | 0xff1b | 113 -> (* escape / q *)
5416 begin match state.mstate with
5417 | Mzoomrect _ ->
5418 state.mstate <- Mnone;
5419 Wsi.setcursor Wsi.CURSOR_INHERIT;
5420 G.postRedisplay "kill zoom rect";
5421 | _ ->
5422 begin match state.mode with
5423 | LinkNav _ ->
5424 state.mode <- View;
5425 G.postRedisplay "esc leave linknav"
5426 | _ ->
5427 match state.ranchors with
5428 | [] -> raise Quit
5429 | (path, password, anchor, origin) :: rest ->
5430 state.ranchors <- rest;
5431 state.anchor <- anchor;
5432 state.origin <- origin;
5433 state.nameddest <- "";
5434 opendoc path password
5435 end;
5436 end;
5438 | 0xff08 -> (* backspace *)
5439 gotoghyll (getnav ~-1)
5441 | 111 -> (* o *)
5442 enteroutlinemode ()
5444 | 117 -> (* u *)
5445 state.rects <- [];
5446 state.text <- "";
5447 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
5448 G.postRedisplay "dehighlight";
5450 | 47 | 63 -> (* / ? *)
5451 let ondone isforw s =
5452 cbput state.hists.pat s;
5453 state.searchpattern <- s;
5454 search s isforw
5456 let s = String.create 1 in
5457 s.[0] <- Char.chr key;
5458 enttext (s, "", Some (onhist state.hists.pat),
5459 textentry, ondone (key = 47), true)
5461 | 43 | 0xffab | 61 when ctrl -> (* ctrl-+ or ctrl-= *)
5462 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
5463 setzoom (conf.zoom +. incr)
5465 | 43 | 0xffab -> (* + *)
5466 let ondone s =
5467 let n =
5468 try int_of_string s with exc ->
5469 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5470 max_int
5472 if n != max_int
5473 then (
5474 conf.pagebias <- n;
5475 state.text <- "page bias is now " ^ string_of_int n;
5478 enttext ("page bias: ", "", None, intentry, ondone, true)
5480 | 45 | 0xffad when ctrl -> (* ctrl-- *)
5481 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
5482 setzoom (max 0.01 (conf.zoom -. decr))
5484 | 45 | 0xffad -> (* - *)
5485 let ondone msg = state.text <- msg in
5486 enttext (
5487 "option [acfhilpstvxACFPRSZTISM]: ", "", None,
5488 optentry state.mode, ondone, true
5491 | 48 when ctrl -> (* ctrl-0 *)
5492 if conf.zoom = 1.0
5493 then (
5494 state.x <- 0;
5495 gotoy state.y
5497 else setzoom 1.0
5499 | (49 | 50) when ctrl && conf.fitmodel != FitPage -> (* ctrl-1/2 *)
5500 let cols =
5501 match conf.columns with
5502 | Csingle _ | Cmulti _ -> 1
5503 | Csplit (n, _) -> n
5505 let h = state.winh -
5506 conf.interpagespace lsl (if conf.presentation then 1 else 0)
5508 let zoom = zoomforh state.winw h (vscrollw ()) cols in
5509 if zoom > 0.0 && (key = 50 || zoom < 1.0)
5510 then setzoom zoom
5512 | 51 when ctrl -> (* ctrl-3 *)
5513 let fm =
5514 match conf.fitmodel with
5515 | FitWidth -> FitProportional
5516 | FitProportional -> FitPage
5517 | FitPage -> FitWidth
5519 state.text <- "fit model: " ^ FMTE.to_string fm;
5520 reqlayout conf.angle fm
5522 | 0xffc6 -> (* f9 *)
5523 togglebirdseye ()
5525 | 57 when ctrl -> (* ctrl-9 *)
5526 togglebirdseye ()
5528 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
5529 when not ctrl -> (* 0..9 *)
5530 let ondone s =
5531 let n =
5532 try int_of_string s with exc ->
5533 state.text <- Printf.sprintf "bad integer `%s': %s" s (exntos exc);
5536 if n >= 0
5537 then (
5538 addnav ();
5539 cbput state.hists.pag (string_of_int n);
5540 gotopage1 (n + conf.pagebias - 1) 0;
5543 let pageentry text key =
5544 match Char.unsafe_chr key with
5545 | 'g' -> TEdone text
5546 | _ -> intentry text key
5548 let text = "x" in text.[0] <- Char.chr key;
5549 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone, true)
5551 | 98 -> (* b *)
5552 conf.scrollb <- if conf.scrollb = 0 then (scrollbvv lor scrollbhv) else 0;
5553 reshape state.winw state.winh;
5555 | 66 -> (* B *)
5556 state.bzoom <- not state.bzoom;
5557 state.rects <- [];
5558 showtext ' ' ("block zoom " ^ if state.bzoom then "on" else "off")
5560 | 108 -> (* l *)
5561 conf.hlinks <- not conf.hlinks;
5562 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
5563 G.postRedisplay "toggle highlightlinks";
5565 | 70 -> (* F *)
5566 state.glinks <- true;
5567 let mode = state.mode in
5568 state.mode <- Textentry (
5569 (":", "", None, linknentry, linkndone gotounder, false),
5570 (fun _ ->
5571 state.glinks <- false;
5572 state.mode <- mode)
5574 state.text <- "";
5575 G.postRedisplay "view:linkent(F)"
5577 | 121 -> (* y *)
5578 state.glinks <- true;
5579 let mode = state.mode in
5580 state.mode <- Textentry (
5582 ":", "", None, linknentry, linkndone (fun under ->
5583 selstring (undertext under);
5584 ), false
5586 fun _ ->
5587 state.glinks <- false;
5588 state.mode <- mode
5590 state.text <- "";
5591 G.postRedisplay "view:linkent"
5593 | 97 -> (* a *)
5594 begin match state.autoscroll with
5595 | Some step ->
5596 conf.autoscrollstep <- step;
5597 state.autoscroll <- None
5598 | None ->
5599 if conf.autoscrollstep = 0
5600 then state.autoscroll <- Some 1
5601 else state.autoscroll <- Some conf.autoscrollstep
5604 | 112 when ctrl -> (* ctrl-p *)
5605 launchpath ()
5607 | 80 -> (* P *)
5608 setpresentationmode (not conf.presentation);
5609 showtext ' ' ("presentation mode " ^
5610 if conf.presentation then "on" else "off");
5612 | 102 -> (* f *)
5613 if List.mem Wsi.Fullscreen state.winstate
5614 then Wsi.reshape conf.cwinw conf.cwinh
5615 else Wsi.fullscreen ()
5617 | 112 | 78 -> (* p|N *)
5618 search state.searchpattern false
5620 | 110 | 0xffc0 -> (* n|F3 *)
5621 search state.searchpattern true
5623 | 116 -> (* t *)
5624 begin match state.layout with
5625 | [] -> ()
5626 | l :: _ ->
5627 gotoghyll (getpagey l.pageno)
5630 | 32 -> (* space *)
5631 nextpage ()
5633 | 0xff9f | 0xffff -> (* delete *)
5634 prevpage ()
5636 | 61 -> (* = *)
5637 showtext ' ' (describe_location ());
5639 | 119 -> (* w *)
5640 begin match state.layout with
5641 | [] -> ()
5642 | l :: _ ->
5643 Wsi.reshape (l.pagew + vscrollw ()) l.pageh;
5644 G.postRedisplay "w"
5647 | 39 -> (* ' *)
5648 enterbookmarkmode ()
5650 | 104 | 0xffbe -> (* h|F1 *)
5651 enterhelpmode ()
5653 | 105 -> (* i *)
5654 enterinfomode ()
5656 | 101 when Buffer.length state.errmsgs > 0 -> (* e *)
5657 entermsgsmode ()
5659 | 109 -> (* m *)
5660 let ondone s =
5661 match state.layout with
5662 | l :: _ ->
5663 if nonemptystr s
5664 then
5665 state.bookmarks <-
5666 (s, 0, Oanchor (getanchor1 l)) :: state.bookmarks
5667 | _ -> ()
5669 enttext ("bookmark: ", "", None, textentry, ondone, true)
5671 | 126 -> (* ~ *)
5672 quickbookmark ();
5673 showtext ' ' "Quick bookmark added";
5675 | 122 -> (* z *)
5676 begin match state.layout with
5677 | l :: _ ->
5678 let rect = getpdimrect l.pagedimno in
5679 let w, h =
5680 if conf.crophack
5681 then
5682 (truncate (1.8 *. (rect.(1) -. rect.(0))),
5683 truncate (1.2 *. (rect.(3) -. rect.(0))))
5684 else
5685 (truncate (rect.(1) -. rect.(0)),
5686 truncate (rect.(3) -. rect.(0)))
5688 let w = truncate ((float w)*.conf.zoom)
5689 and h = truncate ((float h)*.conf.zoom) in
5690 if w != 0 && h != 0
5691 then (
5692 state.anchor <- getanchor ();
5693 Wsi.reshape (w + vscrollw ()) (h + conf.interpagespace)
5695 G.postRedisplay "z";
5697 | [] -> ()
5700 | 120 -> (* x *)
5701 state.roam ();
5702 state.roam <- noroam;
5703 | 60 | 62 -> (* < > *)
5704 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.fitmodel
5706 | 91 | 93 -> (* [ ] *)
5707 conf.colorscale <-
5708 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
5710 G.postRedisplay "brightness";
5712 | 99 when state.mode = View -> (* [alt-]c *)
5713 if Wsi.withalt mask
5714 then (
5715 if conf.zoom > 1.0
5716 then
5717 let m = (wadjsb state.winw - state.w) / 2 in
5718 state.x <- m;
5719 gotoy_and_clear_text state.y
5721 else
5722 let (c, a, b), z =
5723 match state.prevcolumns with
5724 | None -> (1, 0, 0), 1.0
5725 | Some (columns, z) ->
5726 let cab =
5727 match columns with
5728 | Csplit (c, _) -> -c, 0, 0
5729 | Cmulti ((c, a, b), _) -> c, a, b
5730 | Csingle _ -> 1, 0, 0
5732 cab, z
5734 setcolumns View c a b;
5735 setzoom z
5737 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask
5738 -> (* ctrl-shift- (kp) [up|down] *)
5739 let zoom, x = state.prevzoom in
5740 setzoom zoom;
5741 state.x <- x;
5743 | 107 | 0xff52 | 0xff97 -> (* k (kp) up *)
5744 begin match state.autoscroll with
5745 | None ->
5746 begin match state.mode with
5747 | Birdseye beye -> upbirdseye 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 prevpage ()
5754 else gotoy_and_clear_text (clamp (-conf.scrollstep))
5757 | Some n ->
5758 setautoscrollspeed n false
5761 | 106 | 0xff54 | 0xff99 -> (* j (kp) down *)
5762 begin match state.autoscroll with
5763 | None ->
5764 begin match state.mode with
5765 | Birdseye beye -> downbirdseye 1 beye
5766 | _ ->
5767 if ctrl
5768 then gotoy_and_clear_text (clamp (state.winh/2))
5769 else (
5770 if not (Wsi.withshift mask) && conf.presentation
5771 then nextpage ()
5772 else gotoy_and_clear_text (clamp conf.scrollstep)
5775 | Some n ->
5776 setautoscrollspeed n true
5779 | 0xff51 | 0xff53 | 0xff96 | 0xff98
5780 when not (Wsi.withalt mask) -> (* (kp) left / right *)
5781 if canpan ()
5782 then
5783 let dx =
5784 if ctrl
5785 then state.winw / 2
5786 else conf.hscrollstep
5788 let dx = if key = 0xff51 || key = 0xff96 then dx else -dx in
5789 state.x <- panbound (state.x + dx);
5790 gotoy_and_clear_text state.y
5791 else (
5792 state.text <- "";
5793 G.postRedisplay "left/right"
5796 | 0xff55 | 0xff9a -> (* (kp) prior *)
5797 let y =
5798 if ctrl
5799 then
5800 match state.layout with
5801 | [] -> state.y
5802 | l :: _ -> state.y - l.pagey
5803 else
5804 clamp (pgscale (-state.winh))
5806 gotoghyll y
5808 | 0xff56 | 0xff9b -> (* (kp) next *)
5809 let y =
5810 if ctrl
5811 then
5812 match List.rev state.layout with
5813 | [] -> state.y
5814 | l :: _ -> getpagey l.pageno
5815 else
5816 clamp (pgscale state.winh)
5818 gotoghyll y
5820 | 103 | 0xff50 | 0xff95 -> (* g (kp) home *)
5821 gotoghyll 0
5822 | 71 | 0xff57 | 0xff9c -> (* G (kp) end *)
5823 gotoghyll (clamp state.maxy)
5825 | 0xff53 | 0xff98
5826 when Wsi.withalt mask -> (* alt-(kp) right *)
5827 gotoghyll (getnav 1)
5828 | 0xff51 | 0xff96
5829 when Wsi.withalt mask -> (* alt-(kp) left *)
5830 gotoghyll (getnav ~-1)
5832 | 114 -> (* r *)
5833 reload ()
5835 | 118 when conf.debug -> (* v *)
5836 state.rects <- [];
5837 List.iter (fun l ->
5838 match getopaque l.pageno with
5839 | None -> ()
5840 | Some opaque ->
5841 let x0, y0, x1, y1 = pagebbox opaque in
5842 let a,b = float x0, float y0 in
5843 let c,d = float x1, float y0 in
5844 let e,f = float x1, float y1 in
5845 let h,j = float x0, float y1 in
5846 let rect = (a,b,c,d,e,f,h,j) in
5847 debugrect rect;
5848 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
5849 ) state.layout;
5850 G.postRedisplay "v";
5852 | _ ->
5853 vlog "huh? %s" (Wsi.keyname key)
5856 let linknavkeyboard key mask linknav =
5857 let getpage pageno =
5858 let rec loop = function
5859 | [] -> None
5860 | l :: _ when l.pageno = pageno -> Some l
5861 | _ :: rest -> loop rest
5862 in loop state.layout
5864 let doexact (pageno, n) =
5865 match getopaque pageno, getpage pageno with
5866 | Some opaque, Some l ->
5867 if key = 0xff0d || key = 0xff8d (* (kp)enter *)
5868 then
5869 let under = getlink opaque n in
5870 G.postRedisplay "link gotounder";
5871 gotounder under;
5872 state.mode <- View;
5873 else
5874 let opt, dir =
5875 match key with
5876 | 0xff50 -> (* home *)
5877 Some (findlink opaque LDfirst), -1
5879 | 0xff57 -> (* end *)
5880 Some (findlink opaque LDlast), 1
5882 | 0xff51 -> (* left *)
5883 Some (findlink opaque (LDleft n)), -1
5885 | 0xff53 -> (* right *)
5886 Some (findlink opaque (LDright n)), 1
5888 | 0xff52 -> (* up *)
5889 Some (findlink opaque (LDup n)), -1
5891 | 0xff54 -> (* down *)
5892 Some (findlink opaque (LDdown n)), 1
5894 | _ -> None, 0
5896 let pwl l dir =
5897 begin match findpwl l.pageno dir with
5898 | Pwlnotfound -> ()
5899 | Pwl pageno ->
5900 let notfound dir =
5901 state.mode <- LinkNav (Ltgendir dir);
5902 let y, h = getpageyh pageno in
5903 let y =
5904 if dir < 0
5905 then y + h - state.winh
5906 else y
5908 gotoy y
5910 begin match getopaque pageno, getpage pageno with
5911 | Some opaque, Some _ ->
5912 let link =
5913 let ld = if dir > 0 then LDfirst else LDlast in
5914 findlink opaque ld
5916 begin match link with
5917 | Lfound m ->
5918 showlinktype (getlink opaque m);
5919 state.mode <- LinkNav (Ltexact (pageno, m));
5920 G.postRedisplay "linknav jpage";
5921 | _ -> notfound dir
5922 end;
5923 | _ -> notfound dir
5924 end;
5925 end;
5927 begin match opt with
5928 | Some Lnotfound -> pwl l dir;
5929 | Some (Lfound m) ->
5930 if m = n
5931 then pwl l dir
5932 else (
5933 let _, y0, _, y1 = getlinkrect opaque m in
5934 if y0 < l.pagey
5935 then gotopage1 l.pageno y0
5936 else (
5937 let d = fstate.fontsize + 1 in
5938 if y1 - l.pagey > l.pagevh - d
5939 then gotopage1 l.pageno (y1 - state.winh - hscrollh () + d)
5940 else G.postRedisplay "linknav";
5942 showlinktype (getlink opaque m);
5943 state.mode <- LinkNav (Ltexact (l.pageno, m));
5946 | None -> viewkeyboard key mask
5947 end;
5948 | _ -> viewkeyboard key mask
5950 if key = 0xff63
5951 then (
5952 state.mode <- View;
5953 G.postRedisplay "leave linknav"
5955 else
5956 match linknav with
5957 | Ltgendir _ -> viewkeyboard key mask
5958 | Ltexact exact -> doexact exact
5961 let keyboard key mask =
5962 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
5963 then wcmd "interrupt"
5964 else state.uioh <- state.uioh#key key mask
5967 let birdseyekeyboard key mask
5968 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
5969 let incr =
5970 match conf.columns with
5971 | Csingle _ -> 1
5972 | Cmulti ((c, _, _), _) -> c
5973 | Csplit _ -> failwith "bird's eye split mode"
5975 let pgh layout = List.fold_left (fun m l -> max l.pageh m) state.winh layout in
5976 match key with
5977 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
5978 let y, h = getpageyh pageno in
5979 let top = (state.winh - h) / 2 in
5980 gotoy (max 0 (y - top))
5981 | 0xff0d (* enter *)
5982 | 0xff8d -> leavebirdseye beye false (* kp enter *)
5983 | 0xff1b -> leavebirdseye beye true (* escape *)
5984 | 0xff52 -> upbirdseye incr beye (* up *)
5985 | 0xff54 -> downbirdseye incr beye (* down *)
5986 | 0xff51 -> upbirdseye 1 beye (* left *)
5987 | 0xff53 -> downbirdseye 1 beye (* right *)
5989 | 0xff55 -> (* prior *)
5990 begin match state.layout with
5991 | l :: _ ->
5992 if l.pagey != 0
5993 then (
5994 state.mode <- Birdseye (
5995 oconf, leftx, l.pageno, hooverpageno, anchor
5997 gotopage1 l.pageno 0;
5999 else (
6000 let layout = layout (state.y-state.winh) (pgh state.layout) in
6001 match layout with
6002 | [] -> gotoy (clamp (-state.winh))
6003 | l :: _ ->
6004 state.mode <- Birdseye (
6005 oconf, leftx, l.pageno, hooverpageno, anchor
6007 gotopage1 l.pageno 0
6010 | [] -> gotoy (clamp (-state.winh))
6011 end;
6013 | 0xff56 -> (* next *)
6014 begin match List.rev state.layout with
6015 | l :: _ ->
6016 let layout = layout (state.y + (pgh state.layout)) state.winh in
6017 begin match layout with
6018 | [] ->
6019 let incr = l.pageh - l.pagevh in
6020 if incr = 0
6021 then (
6022 state.mode <-
6023 Birdseye (
6024 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
6026 G.postRedisplay "birdseye pagedown";
6028 else gotoy (clamp (incr + conf.interpagespace*2));
6030 | l :: _ ->
6031 state.mode <-
6032 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
6033 gotopage1 l.pageno 0;
6036 | [] -> gotoy (clamp state.winh)
6037 end;
6039 | 0xff50 -> (* home *)
6040 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
6041 gotopage1 0 0
6043 | 0xff57 -> (* end *)
6044 let pageno = state.pagecount - 1 in
6045 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
6046 if not (pagevisible state.layout pageno)
6047 then
6048 let h =
6049 match List.rev state.pdims with
6050 | [] -> state.winh
6051 | (_, _, h, _) :: _ -> h
6053 gotoy (max 0 (getpagey pageno - (state.winh - h - conf.interpagespace)))
6054 else G.postRedisplay "birdseye end";
6055 | _ -> viewkeyboard key mask
6058 let drawpage l =
6059 let color =
6060 match state.mode with
6061 | Textentry _ -> scalecolor 0.4
6062 | LinkNav _
6063 | View -> scalecolor 1.0
6064 | Birdseye (_, _, pageno, hooverpageno, _) ->
6065 if l.pageno = hooverpageno
6066 then scalecolor 0.9
6067 else (
6068 if l.pageno = pageno
6069 then scalecolor 1.0
6070 else scalecolor 0.8
6073 drawtiles l color;
6076 let postdrawpage l linkindexbase =
6077 match getopaque l.pageno with
6078 | Some opaque ->
6079 if tileready l l.pagex l.pagey
6080 then
6081 let x = l.pagedispx - l.pagex
6082 and y = l.pagedispy - l.pagey in
6083 let hlmask =
6084 match conf.columns with
6085 | Csingle _ | Cmulti _ ->
6086 (if conf.hlinks then 1 else 0)
6087 + (if state.glinks
6088 && not (isbirdseye state.mode) then 2 else 0)
6089 | _ -> 0
6091 let s =
6092 match state.mode with
6093 | Textentry ((_, s, _, _, _, _), _) when state.glinks -> s
6094 | _ -> ""
6096 postprocess opaque hlmask x y (linkindexbase, s, conf.hfsize);
6097 else 0
6098 | _ -> 0
6101 let scrollindicator () =
6102 let sbw, ph, sh = state.uioh#scrollph in
6103 let sbh, pw, sw = state.uioh#scrollpw in
6105 GlDraw.color (0.64, 0.64, 0.64);
6106 filledrect
6107 (float (state.winw - sbw)) 0.
6108 (float state.winw) (float state.winh)
6110 filledrect
6111 0. (float (state.winh - sbh))
6112 (float (wadjsb state.winw - 1)) (float state.winh)
6114 GlDraw.color (0.0, 0.0, 0.0);
6116 filledrect
6117 (float (state.winw - sbw)) ph
6118 (float state.winw) (ph +. sh)
6120 filledrect
6121 pw (float (state.winh - sbh))
6122 (pw +. sw) (float state.winh)
6126 let showsel () =
6127 match state.mstate with
6128 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
6131 | Msel ((x0, y0), (x1, y1)) ->
6132 let rec loop = function
6133 | l :: ls ->
6134 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
6135 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
6136 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
6137 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
6138 then
6139 match getopaque l.pageno with
6140 | Some opaque ->
6141 let x0, y0 = pagetranslatepoint l x0 y0 in
6142 let x1, y1 = pagetranslatepoint l x1 y1 in
6143 seltext opaque (x0, y0, x1, y1);
6144 | _ -> ()
6145 else loop ls
6146 | [] -> ()
6148 loop state.layout
6151 let showrects = function [] -> () | rects ->
6152 Gl.enable `blend;
6153 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
6154 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6155 List.iter
6156 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
6157 List.iter (fun l ->
6158 if l.pageno = pageno
6159 then (
6160 let dx = float (l.pagedispx - l.pagex) in
6161 let dy = float (l.pagedispy - l.pagey) in
6162 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
6163 Raw.sets_float state.vraw ~pos:0
6164 [| x0+.dx; y0+.dy;
6165 x1+.dx; y1+.dy;
6166 x3+.dx; y3+.dy;
6167 x2+.dx; y2+.dy |];
6168 GlArray.vertex `two state.vraw;
6169 GlArray.draw_arrays `triangle_strip 0 4;
6171 ) state.layout
6172 ) rects
6174 Gl.disable `blend;
6177 let display () =
6178 GlClear.color (scalecolor2 conf.bgcolor);
6179 GlClear.clear [`color];
6180 List.iter drawpage state.layout;
6181 let rects =
6182 match state.mode with
6183 | LinkNav (Ltexact (pageno, linkno)) ->
6184 begin match getopaque pageno with
6185 | Some opaque ->
6186 let x0, y0, x1, y1 = getlinkrect opaque linkno in
6187 (pageno, 5, (
6188 float x0, float y0,
6189 float x1, float y0,
6190 float x1, float y1,
6191 float x0, float y1)
6192 ) :: state.rects
6193 | None -> state.rects
6195 | _ -> state.rects
6197 showrects rects;
6198 let rec postloop linkindexbase = function
6199 | l :: rest ->
6200 let linkindexbase = linkindexbase + postdrawpage l linkindexbase in
6201 postloop linkindexbase rest
6202 | [] -> ()
6204 showsel ();
6205 postloop 0 state.layout;
6206 state.uioh#display;
6207 begin match state.mstate with
6208 | Mzoomrect ((x0, y0), (x1, y1)) ->
6209 Gl.enable `blend;
6210 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
6211 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
6212 filledrect (float x0) (float y0) (float x1) (float y1);
6213 Gl.disable `blend;
6214 | _ -> ()
6215 end;
6216 enttext ();
6217 scrollindicator ();
6218 Wsi.swapb ();
6221 let zoomrect x y x1 y1 =
6222 let x0 = min x x1
6223 and x1 = max x x1
6224 and y0 = min y y1 in
6225 gotoy (state.y + y0);
6226 state.anchor <- getanchor ();
6227 let zoom = (float state.w) /. float (x1 - x0) in
6228 let margin =
6229 match conf.fitmodel, conf.columns with
6230 | FitPage, Csplit _ ->
6231 onppundermouse (fun _ l _ _ -> Some l.pagedispx) x0 y0 x0
6233 | _, _ ->
6234 let adjw = wadjsb state.winw in
6235 if state.w < adjw
6236 then (adjw - state.w) / 2
6237 else 0
6239 state.x <- (state.x + margin) - x0;
6240 setzoom zoom;
6241 Wsi.setcursor Wsi.CURSOR_INHERIT;
6242 state.mstate <- Mnone;
6245 let zoomblock x y =
6246 let g opaque l px py =
6247 match rectofblock opaque px py with
6248 | Some a ->
6249 let x0 = a.(0) -. 20. in
6250 let x1 = a.(1) +. 20. in
6251 let y0 = a.(2) -. 20. in
6252 let zoom = (float state.w) /. (x1 -. x0) in
6253 let pagey = getpagey l.pageno in
6254 gotoy_and_clear_text (pagey + truncate y0);
6255 state.anchor <- getanchor ();
6256 let margin = (state.w - l.pagew)/2 in
6257 state.x <- -truncate x0 - margin;
6258 setzoom zoom;
6259 None
6260 | None -> None
6262 match conf.columns with
6263 | Csplit _ ->
6264 showtext '!' "block zooming does not work properly in split columns mode"
6265 | _ -> onppundermouse g x y ()
6268 let scrollx x =
6269 let winw = wadjsb state.winw - 1 in
6270 let s = float x /. float winw in
6271 let destx = truncate (float (state.w + winw) *. s) in
6272 state.x <- winw - destx;
6273 gotoy_and_clear_text state.y;
6274 state.mstate <- Mscrollx;
6277 let scrolly y =
6278 let s = float y /. float state.winh in
6279 let desty = truncate (float (state.maxy - state.winh) *. s) in
6280 gotoy_and_clear_text desty;
6281 state.mstate <- Mscrolly;
6284 let viewmulticlick clicks x y mask =
6285 let g opaque l px py =
6286 let mark =
6287 match clicks with
6288 | 2 -> Mark_word
6289 | 3 -> Mark_line
6290 | 4 -> Mark_block
6291 | _ -> Mark_page
6293 if markunder opaque px py mark
6294 then (
6295 Some (fun () ->
6296 match getopaque l.pageno with
6297 | None -> ()
6298 | Some opaque ->
6299 match Ne.pipe () with
6300 | Ne.Exn exn ->
6301 showtext '!'
6302 (Printf.sprintf
6303 "can not create mark pipe: %s"
6304 (exntos exn));
6305 | Ne.Res (r, w) ->
6306 let dopipe cmd =
6307 let doclose what fd =
6308 Ne.clo fd (fun msg -> dolog "%s close failed: %s" what msg)
6310 begin try
6311 popen cmd [r, 0; w, -1];
6312 with exn ->
6313 doclose "viewmulticlick pipe/w" w;
6314 dolog "can not execute %S: %s" cmd (exntos exn);
6315 end;
6316 copysel w opaque false;
6317 G.postRedisplay "viewmulticlick";
6318 doclose "viewmulticlick pipe/r" r;
6320 if Wsi.withctrl mask
6321 then state.roam <- (fun () -> dopipe conf.paxcmd)
6322 else dopipe conf.selcmd
6325 else None
6327 G.postRedisplay "viewmulticlick";
6328 onppundermouse g x y (fun () -> showtext '!' "Daisy whoopsie") ();
6331 let viewmouse button down x y mask =
6332 match button with
6333 | n when (n == 4 || n == 5) && not down ->
6334 if Wsi.withctrl mask
6335 then (
6336 match state.mstate with
6337 | Mzoom (oldn, i) ->
6338 if oldn = n
6339 then (
6340 if i = 2
6341 then
6342 let incr =
6343 match n with
6344 | 5 ->
6345 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
6346 | _ ->
6347 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
6349 let zoom = conf.zoom -. incr in
6350 setzoom zoom;
6351 state.mstate <- Mzoom (n, 0);
6352 else
6353 state.mstate <- Mzoom (n, i+1);
6355 else state.mstate <- Mzoom (n, 0)
6357 | _ -> state.mstate <- Mzoom (n, 0)
6359 else (
6360 match state.autoscroll with
6361 | Some step -> setautoscrollspeed step (n=4)
6362 | None ->
6363 if conf.wheelbypage || conf.presentation
6364 then (
6365 if n = 4
6366 then prevpage ()
6367 else nextpage ()
6369 else
6370 let incr =
6371 if n = 4
6372 then -conf.scrollstep
6373 else conf.scrollstep
6375 let incr = incr * 2 in
6376 let y = clamp incr in
6377 gotoy_and_clear_text y
6380 | n when (n = 6 || n = 7) && not down && canpan () ->
6381 state.x <-
6382 panbound (state.x + (if n = 7 then -2 else 2) * conf.hscrollstep);
6383 gotoy_and_clear_text state.y
6385 | 1 when Wsi.withshift mask ->
6386 state.mstate <- Mnone;
6387 if not down
6388 then (
6389 match unproject x y with
6390 | Some (pageno, ux, uy) ->
6391 let cmd = Printf.sprintf
6392 "%s %s %d %d %d"
6393 conf.stcmd state.path pageno ux uy
6395 popen cmd []
6396 | None -> ()
6399 | 1 when Wsi.withctrl mask ->
6400 if down
6401 then (
6402 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6403 state.mstate <- Mpan (x, y)
6405 else
6406 state.mstate <- Mnone
6408 | 3 ->
6409 if down
6410 then (
6411 Wsi.setcursor Wsi.CURSOR_CYCLE;
6412 let p = (x, y) in
6413 state.mstate <- Mzoomrect (p, p)
6415 else (
6416 match state.mstate with
6417 | Mzoomrect ((x0, y0), _) ->
6418 if abs (x-x0) > 10 && abs (y - y0) > 10
6419 then zoomrect x0 y0 x y
6420 else (
6421 state.mstate <- Mnone;
6422 Wsi.setcursor Wsi.CURSOR_INHERIT;
6423 G.postRedisplay "kill accidental zoom rect";
6425 | _ ->
6426 Wsi.setcursor Wsi.CURSOR_INHERIT;
6427 state.mstate <- Mnone
6430 | 1 when x > state.winw - vscrollw () ->
6431 if down
6432 then
6433 let _, position, sh = state.uioh#scrollph in
6434 if y > truncate position && y < truncate (position +. sh)
6435 then state.mstate <- Mscrolly
6436 else scrolly y
6437 else
6438 state.mstate <- Mnone
6440 | 1 when y > state.winh - hscrollh () ->
6441 if down
6442 then
6443 let _, position, sw = state.uioh#scrollpw in
6444 if x > truncate position && x < truncate (position +. sw)
6445 then state.mstate <- Mscrollx
6446 else scrollx x
6447 else
6448 state.mstate <- Mnone
6450 | 1 when state.bzoom -> if not down then zoomblock x y
6452 | 1 ->
6453 let dest = if down then getunder x y else Unone in
6454 begin match dest with
6455 | Ulinkgoto _
6456 | Ulinkuri _
6457 | Uremote _ | Uremotedest _
6458 | Uunexpected _ | Ulaunch _ | Unamed _ ->
6459 gotounder dest
6461 | Unone when down ->
6462 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
6463 state.mstate <- Mpan (x, y);
6465 | Unone | Utext _ ->
6466 if down
6467 then (
6468 if conf.angle mod 360 = 0
6469 then (
6470 state.mstate <- Msel ((x, y), (x, y));
6471 G.postRedisplay "mouse select";
6474 else (
6475 match state.mstate with
6476 | Mnone -> ()
6478 | Mzoom _ | Mscrollx | Mscrolly ->
6479 state.mstate <- Mnone
6481 | Mzoomrect ((x0, y0), _) ->
6482 zoomrect x0 y0 x y
6484 | Mpan _ ->
6485 Wsi.setcursor Wsi.CURSOR_INHERIT;
6486 state.mstate <- Mnone
6488 | Msel ((x0, y0), (x1, y1)) ->
6489 let rec loop = function
6490 | [] -> ()
6491 | l :: rest ->
6492 let inside =
6493 let a0 = l.pagedispy in
6494 let a1 = a0 + l.pagevh in
6495 let b0 = l.pagedispx in
6496 let b1 = b0 + l.pagevw in
6497 ((y0 >= a0 && y0 <= a1) || (y1 >= a0 && y1 <= a1))
6498 && ((x0 >= b0 && x0 <= b1) || (x1 >= b0 && x1 <= b1))
6500 if inside
6501 then
6502 match getopaque l.pageno with
6503 | Some opaque ->
6504 begin
6505 match Ne.pipe () with
6506 | Ne.Exn exn ->
6507 showtext '!'
6508 (Printf.sprintf
6509 "can not create sel pipe: %s"
6510 (exntos exn));
6511 | Ne.Res (r, w) ->
6512 let doclose what fd =
6513 Ne.clo fd (fun msg ->
6514 dolog "%s close failed: %s" what msg)
6516 begin try
6517 popen conf.selcmd [r, 0; w, -1];
6518 with exn ->
6519 dolog "can not execute %S: %s"
6520 conf.selcmd (exntos exn);
6521 doclose "Msel pipe/w" w;
6522 end;
6523 copysel w opaque true;
6524 G.postRedisplay "copysel";
6525 doclose "Msel pipe/r" r;
6527 | None -> ()
6528 else loop rest
6530 loop state.layout;
6531 Wsi.setcursor Wsi.CURSOR_INHERIT;
6532 state.mstate <- Mnone;
6536 | _ -> ()
6539 let birdseyemouse button down x y mask
6540 (conf, leftx, _, hooverpageno, anchor) =
6541 match button with
6542 | 1 when down ->
6543 let rec loop = function
6544 | [] -> ()
6545 | l :: rest ->
6546 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6547 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6548 then (
6549 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
6551 else loop rest
6553 loop state.layout
6554 | 3 -> ()
6555 | _ -> viewmouse button down x y mask
6558 let uioh = object
6559 method display = ()
6561 method key key mask =
6562 begin match state.mode with
6563 | Textentry textentry -> textentrykeyboard key mask textentry
6564 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
6565 | View -> viewkeyboard key mask
6566 | LinkNav linknav -> linknavkeyboard key mask linknav
6567 end;
6568 state.uioh
6570 method button button bstate x y mask =
6571 begin match state.mode with
6572 | LinkNav _
6573 | View -> viewmouse button bstate x y mask
6574 | Birdseye beye -> birdseyemouse button bstate x y mask beye
6575 | Textentry _ -> ()
6576 end;
6577 state.uioh
6579 method multiclick clicks x y mask =
6580 begin match state.mode with
6581 | LinkNav _
6582 | View -> viewmulticlick clicks x y mask
6583 | Birdseye _
6584 | Textentry _ -> ()
6585 end;
6586 state.uioh
6588 method motion x y =
6589 begin match state.mode with
6590 | Textentry _ -> ()
6591 | View | Birdseye _ | LinkNav _ ->
6592 match state.mstate with
6593 | Mzoom _ | Mnone -> ()
6595 | Mpan (x0, y0) ->
6596 let dx = x - x0
6597 and dy = y0 - y in
6598 state.mstate <- Mpan (x, y);
6599 if canpan ()
6600 then state.x <- panbound (state.x + dx);
6601 let y = clamp dy in
6602 gotoy_and_clear_text y
6604 | Msel (a, _) ->
6605 state.mstate <- Msel (a, (x, y));
6606 G.postRedisplay "motion select";
6608 | Mscrolly ->
6609 let y = min state.winh (max 0 y) in
6610 scrolly y
6612 | Mscrollx ->
6613 let x = min state.winw (max 0 x) in
6614 scrollx x
6616 | Mzoomrect (p0, _) ->
6617 state.mstate <- Mzoomrect (p0, (x, y));
6618 G.postRedisplay "motion zoomrect";
6619 end;
6620 state.uioh
6622 method pmotion x y =
6623 begin match state.mode with
6624 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
6625 let rec loop = function
6626 | [] ->
6627 if hooverpageno != -1
6628 then (
6629 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
6630 G.postRedisplay "pmotion birdseye no hoover";
6632 | l :: rest ->
6633 if y > l.pagedispy && y < l.pagedispy + l.pagevh
6634 && x > l.pagedispx && x < l.pagedispx + l.pagevw
6635 then (
6636 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
6637 G.postRedisplay "pmotion birdseye hoover";
6639 else loop rest
6641 loop state.layout
6643 | Textentry _ -> ()
6645 | LinkNav _
6646 | View ->
6647 match state.mstate with
6648 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
6650 | Mnone ->
6651 updateunder x y;
6652 match conf.pax with
6653 | None -> ()
6654 | Some r ->
6655 let past, _, _ = !r in
6656 let now = now () in
6657 let delta = now -. past in
6658 if delta > 0.01
6659 then paxunder x y
6660 else r := (now, x, y)
6661 end;
6662 state.uioh
6664 method infochanged _ = ()
6666 method scrollph =
6667 let maxy = state.maxy - (if conf.maxhfit then state.winh else 0) in
6668 let p, h =
6669 if maxy = 0
6670 then 0.0, float state.winh
6671 else scrollph state.y maxy
6673 vscrollw (), p, h
6675 method scrollpw =
6676 let winw = wadjsb state.winw in
6677 let fwinw = float winw in
6678 let sw =
6679 let sw = fwinw /. float state.w in
6680 let sw = fwinw *. sw in
6681 max sw (float conf.scrollh)
6683 let position =
6684 let maxx = state.w + winw in
6685 let x = winw - state.x in
6686 let percent = float x /. float maxx in
6687 (fwinw -. sw) *. percent
6689 hscrollh (), position, sw
6691 method modehash =
6692 let modename =
6693 match state.mode with
6694 | LinkNav _ -> "links"
6695 | Textentry _ -> "textentry"
6696 | Birdseye _ -> "birdseye"
6697 | View -> "view"
6699 findkeyhash conf modename
6701 method eformsgs = true
6702 end;;
6704 module Config =
6705 struct
6706 open Parser
6708 let fontpath = ref "";;
6710 module KeyMap =
6711 Map.Make (struct type t = (int * int) let compare = compare end);;
6713 let unent s =
6714 let l = String.length s in
6715 let b = Buffer.create l in
6716 unent b s 0 l;
6717 Buffer.contents b;
6720 let home =
6721 try Sys.getenv "HOME"
6722 with exn ->
6723 prerr_endline
6724 ("Can not determine home directory location: " ^ exntos exn);
6728 let modifier_of_string = function
6729 | "alt" -> Wsi.altmask
6730 | "shift" -> Wsi.shiftmask
6731 | "ctrl" | "control" -> Wsi.ctrlmask
6732 | "meta" -> Wsi.metamask
6733 | _ -> 0
6736 let key_of_string =
6737 let r = Str.regexp "-" in
6738 fun s ->
6739 let elems = Str.full_split r s in
6740 let f n k m =
6741 let g s =
6742 let m1 = modifier_of_string s in
6743 if m1 = 0
6744 then (Wsi.namekey s, m)
6745 else (k, m lor m1)
6746 in function
6747 | Str.Delim s when n land 1 = 0 -> g s
6748 | Str.Text s -> g s
6749 | Str.Delim _ -> (k, m)
6751 let rec loop n k m = function
6752 | [] -> (k, m)
6753 | x :: xs ->
6754 let k, m = f n k m x in
6755 loop (n+1) k m xs
6757 loop 0 0 0 elems
6760 let keys_of_string =
6761 let r = Str.regexp "[ \t]" in
6762 fun s ->
6763 let elems = Str.split r s in
6764 List.map key_of_string elems
6767 let copykeyhashes c =
6768 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
6771 let config_of c attrs =
6772 let apply c k v =
6774 match k with
6775 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
6776 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
6777 | "case-insensitive-search" -> { c with icase = bool_of_string v }
6778 | "preload" -> { c with preload = bool_of_string v }
6779 | "page-bias" -> { c with pagebias = int_of_string v }
6780 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
6781 | "horizontal-scroll-step" ->
6782 { c with hscrollstep = max (int_of_string v) 1 }
6783 | "auto-scroll-step" ->
6784 { c with autoscrollstep = max 0 (int_of_string v) }
6785 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
6786 | "crop-hack" -> { c with crophack = bool_of_string v }
6787 | "throttle" ->
6788 let mw =
6789 match String.lowercase v with
6790 | "true" -> Some infinity
6791 | "false" -> None
6792 | f -> Some (float_of_string f)
6794 { c with maxwait = mw}
6795 | "highlight-links" -> { c with hlinks = bool_of_string v }
6796 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
6797 | "vertical-margin" ->
6798 { c with interpagespace = max 0 (int_of_string v) }
6799 | "zoom" ->
6800 let zoom = float_of_string v /. 100. in
6801 let zoom = max zoom 0.0 in
6802 { c with zoom = zoom }
6803 | "presentation" -> { c with presentation = bool_of_string v }
6804 | "rotation-angle" -> { c with angle = int_of_string v }
6805 | "width" -> { c with cwinw = max 20 (int_of_string v) }
6806 | "height" -> { c with cwinh = max 20 (int_of_string v) }
6807 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
6808 | "proportional-display" ->
6809 let fm =
6810 if bool_of_string v
6811 then FitProportional
6812 else FitWidth
6814 { c with fitmodel = fm }
6815 | "fit-model" -> { c with fitmodel = FMTE.of_string v }
6816 | "pixmap-cache-size" ->
6817 { c with memlimit = max 2 (int_of_string_with_suffix v) }
6818 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
6819 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
6820 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
6821 | "persistent-location" -> { c with jumpback = bool_of_string v }
6822 | "background-color" -> { c with bgcolor = color_of_string v }
6823 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
6824 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
6825 | "mupdf-store-size" ->
6826 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
6827 | "checkers" -> { c with checkers = bool_of_string v }
6828 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
6829 | "trim-margins" -> { c with trimmargins = bool_of_string v }
6830 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
6831 | "uri-launcher" -> { c with urilauncher = unent v }
6832 | "path-launcher" -> { c with pathlauncher = unent v }
6833 | "color-space" -> { c with colorspace = CSTE.of_string v }
6834 | "invert-colors" -> { c with invert = bool_of_string v }
6835 | "brightness" -> { c with colorscale = float_of_string v }
6836 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
6837 | "ghyllscroll" -> { c with ghyllscroll = ghyllscroll_of_string v }
6838 | "columns" ->
6839 let (n, _, _) as nab = multicolumns_of_string v in
6840 if n < 0
6841 then { c with columns = Csplit (-n, [||]) }
6842 else { c with columns = Cmulti (nab, [||]) }
6843 | "birds-eye-columns" ->
6844 { c with beyecolumns = Some (max (int_of_string v) 2) }
6845 | "selection-command" -> { c with selcmd = unent v }
6846 | "synctex-command" -> { c with stcmd = unent v }
6847 | "pax-command" -> { c with paxcmd = unent v }
6848 | "update-cursor" -> { c with updatecurs = bool_of_string v }
6849 | "hint-font-size" -> { c with hfsize = bound (int_of_string v) 5 100 }
6850 | "page-scroll-scale" -> { c with pgscale = float_of_string v }
6851 | "use-pbo" -> { c with usepbo = bool_of_string v }
6852 | "wheel-scrolls-pages" -> { c with wheelbypage = bool_of_string v }
6853 | "horizontal-scrollbar-visible" ->
6854 let b =
6855 if bool_of_string v
6856 then c.scrollb lor scrollbhv
6857 else c.scrollb land (lnot scrollbhv)
6859 { c with scrollb = b }
6860 | "vertical-scrollbar-visible" ->
6861 let b =
6862 if bool_of_string v
6863 then c.scrollb lor scrollbvv
6864 else c.scrollb land (lnot scrollbvv)
6866 { c with scrollb = b }
6867 | "remote-in-a-new-instance" -> { c with riani = bool_of_string v }
6868 | "point-and-x" ->
6869 { c with pax =
6870 if bool_of_string v
6871 then Some (ref (0.0, 0, 0))
6872 else None }
6873 | "point-and-x-mark" -> { c with paxmark = MTE.of_string v }
6874 | _ -> c
6875 with exn ->
6876 prerr_endline ("Error processing attribute (`" ^
6877 k ^ "'=`" ^ v ^ "'): " ^ exntos exn);
6880 let rec fold c = function
6881 | [] -> c
6882 | (k, v) :: rest ->
6883 let c = apply c k v in
6884 fold c rest
6886 fold { c with keyhashes = copykeyhashes c } attrs;
6889 let fromstring f pos n v d =
6890 try f v
6891 with exn ->
6892 dolog "Error processing attribute (%S=%S) at %d\n%s"
6893 n v pos (exntos exn)
6898 let bookmark_of attrs =
6899 let rec fold title page rely visy = function
6900 | ("title", v) :: rest -> fold v page rely visy rest
6901 | ("page", v) :: rest -> fold title v rely visy rest
6902 | ("rely", v) :: rest -> fold title page v visy rest
6903 | ("visy", v) :: rest -> fold title page rely v rest
6904 | _ :: rest -> fold title page rely visy rest
6905 | [] -> title, page, rely, visy
6907 fold "invalid" "0" "0" "0" attrs
6910 let doc_of attrs =
6911 let rec fold path page rely pan visy = function
6912 | ("path", v) :: rest -> fold v page rely pan visy rest
6913 | ("page", v) :: rest -> fold path v rely pan visy rest
6914 | ("rely", v) :: rest -> fold path page v pan visy rest
6915 | ("pan", v) :: rest -> fold path page rely v visy rest
6916 | ("visy", v) :: rest -> fold path page rely pan v rest
6917 | _ :: rest -> fold path page rely pan visy rest
6918 | [] -> path, page, rely, pan, visy
6920 fold "" "0" "0" "0" "0" attrs
6923 let map_of attrs =
6924 let rec fold rs ls = function
6925 | ("out", v) :: rest -> fold v ls rest
6926 | ("in", v) :: rest -> fold rs v rest
6927 | _ :: rest -> fold ls rs rest
6928 | [] -> ls, rs
6930 fold "" "" attrs
6933 let setconf dst src =
6934 dst.scrollbw <- src.scrollbw;
6935 dst.scrollh <- src.scrollh;
6936 dst.icase <- src.icase;
6937 dst.preload <- src.preload;
6938 dst.pagebias <- src.pagebias;
6939 dst.verbose <- src.verbose;
6940 dst.scrollstep <- src.scrollstep;
6941 dst.maxhfit <- src.maxhfit;
6942 dst.crophack <- src.crophack;
6943 dst.autoscrollstep <- src.autoscrollstep;
6944 dst.maxwait <- src.maxwait;
6945 dst.hlinks <- src.hlinks;
6946 dst.underinfo <- src.underinfo;
6947 dst.interpagespace <- src.interpagespace;
6948 dst.zoom <- src.zoom;
6949 dst.presentation <- src.presentation;
6950 dst.angle <- src.angle;
6951 dst.cwinw <- src.cwinw;
6952 dst.cwinh <- src.cwinh;
6953 dst.savebmarks <- src.savebmarks;
6954 dst.memlimit <- src.memlimit;
6955 dst.fitmodel <- src.fitmodel;
6956 dst.texcount <- src.texcount;
6957 dst.sliceheight <- src.sliceheight;
6958 dst.thumbw <- src.thumbw;
6959 dst.jumpback <- src.jumpback;
6960 dst.bgcolor <- src.bgcolor;
6961 dst.tilew <- src.tilew;
6962 dst.tileh <- src.tileh;
6963 dst.mustoresize <- src.mustoresize;
6964 dst.checkers <- src.checkers;
6965 dst.aalevel <- src.aalevel;
6966 dst.trimmargins <- src.trimmargins;
6967 dst.trimfuzz <- src.trimfuzz;
6968 dst.urilauncher <- src.urilauncher;
6969 dst.colorspace <- src.colorspace;
6970 dst.invert <- src.invert;
6971 dst.colorscale <- src.colorscale;
6972 dst.redirectstderr <- src.redirectstderr;
6973 dst.ghyllscroll <- src.ghyllscroll;
6974 dst.columns <- src.columns;
6975 dst.beyecolumns <- src.beyecolumns;
6976 dst.selcmd <- src.selcmd;
6977 dst.updatecurs <- src.updatecurs;
6978 dst.pathlauncher <- src.pathlauncher;
6979 dst.keyhashes <- copykeyhashes src;
6980 dst.hfsize <- src.hfsize;
6981 dst.hscrollstep <- src.hscrollstep;
6982 dst.pgscale <- src.pgscale;
6983 dst.usepbo <- src.usepbo;
6984 dst.wheelbypage <- src.wheelbypage;
6985 dst.stcmd <- src.stcmd;
6986 dst.paxcmd <- src.paxcmd;
6987 dst.scrollb <- src.scrollb;
6988 dst.riani <- src.riani;
6989 dst.paxmark <- src.paxmark;
6990 dst.pax <-
6991 if src.pax = None
6992 then None
6993 else Some ((ref (0.0, 0, 0)));
6996 let get s =
6997 let h = Hashtbl.create 10 in
6998 let dc = { defconf with angle = defconf.angle } in
6999 let rec toplevel v t spos _ =
7000 match t with
7001 | Vdata | Vcdata | Vend -> v
7002 | Vopen ("llppconfig", _, closed) ->
7003 if closed
7004 then v
7005 else { v with f = llppconfig }
7006 | Vopen _ ->
7007 error "unexpected subelement at top level" s spos
7008 | Vclose _ -> error "unexpected close at top level" s spos
7010 and llppconfig v t spos _ =
7011 match t with
7012 | Vdata | Vcdata -> v
7013 | Vend -> error "unexpected end of input in llppconfig" s spos
7014 | Vopen ("defaults", attrs, closed) ->
7015 let c = config_of dc attrs in
7016 setconf dc c;
7017 if closed
7018 then v
7019 else { v with f = defaults }
7021 | Vopen ("ui-font", attrs, closed) ->
7022 let rec getsize size = function
7023 | [] -> size
7024 | ("size", v) :: rest ->
7025 let size =
7026 fromstring int_of_string spos "size" v fstate.fontsize in
7027 getsize size rest
7028 | l -> getsize size l
7030 fstate.fontsize <- getsize fstate.fontsize attrs;
7031 if closed
7032 then v
7033 else { v with f = uifont (Buffer.create 10) }
7035 | Vopen ("doc", attrs, closed) ->
7036 let pathent, spage, srely, span, svisy = doc_of attrs in
7037 let path = unent pathent
7038 and pageno = fromstring int_of_string spos "page" spage 0
7039 and rely = fromstring float_of_string spos "rely" srely 0.0
7040 and pan = fromstring int_of_string spos "pan" span 0
7041 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7042 let c = config_of dc attrs in
7043 let anchor = (pageno, rely, visy) in
7044 if closed
7045 then (Hashtbl.add h path (c, [], pan, anchor); v)
7046 else { v with f = doc path pan anchor c [] }
7048 | Vopen _ ->
7049 error "unexpected subelement in llppconfig" s spos
7051 | Vclose "llppconfig" -> { v with f = toplevel }
7052 | Vclose _ -> error "unexpected close in llppconfig" s spos
7054 and defaults v t spos _ =
7055 match t with
7056 | Vdata | Vcdata -> v
7057 | Vend -> error "unexpected end of input in defaults" s spos
7058 | Vopen ("keymap", attrs, closed) ->
7059 let modename =
7060 try List.assoc "mode" attrs
7061 with Not_found -> "global" in
7062 if closed
7063 then v
7064 else
7065 let ret keymap =
7066 let h = findkeyhash dc modename in
7067 KeyMap.iter (Hashtbl.replace h) keymap;
7068 defaults
7070 { v with f = pkeymap ret KeyMap.empty }
7072 | Vopen (_, _, _) ->
7073 error "unexpected subelement in defaults" s spos
7075 | Vclose "defaults" ->
7076 { v with f = llppconfig }
7078 | Vclose _ -> error "unexpected close in defaults" s spos
7080 and uifont b v t spos epos =
7081 match t with
7082 | Vdata | Vcdata ->
7083 Buffer.add_substring b s spos (epos - spos);
7085 | Vopen (_, _, _) ->
7086 error "unexpected subelement in ui-font" s spos
7087 | Vclose "ui-font" ->
7088 if emptystr !fontpath
7089 then fontpath := Buffer.contents b;
7090 { v with f = llppconfig }
7091 | Vclose _ -> error "unexpected close in ui-font" s spos
7092 | Vend -> error "unexpected end of input in ui-font" s spos
7094 and doc path pan anchor c bookmarks v t spos _ =
7095 match t with
7096 | Vdata | Vcdata -> v
7097 | Vend -> error "unexpected end of input in doc" s spos
7098 | Vopen ("bookmarks", _, closed) ->
7099 if closed
7100 then v
7101 else { v with f = pbookmarks path pan anchor c bookmarks }
7103 | Vopen ("keymap", attrs, closed) ->
7104 let modename =
7105 try List.assoc "mode" attrs
7106 with Not_found -> "global"
7108 if closed
7109 then v
7110 else
7111 let ret keymap =
7112 let h = findkeyhash c modename in
7113 KeyMap.iter (Hashtbl.replace h) keymap;
7114 doc path pan anchor c bookmarks
7116 { v with f = pkeymap ret KeyMap.empty }
7118 | Vopen (_, _, _) ->
7119 error "unexpected subelement in doc" s spos
7121 | Vclose "doc" ->
7122 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
7123 { v with f = llppconfig }
7125 | Vclose _ -> error "unexpected close in doc" s spos
7127 and pkeymap ret keymap v t spos _ =
7128 match t with
7129 | Vdata | Vcdata -> v
7130 | Vend -> error "unexpected end of input in keymap" s spos
7131 | Vopen ("map", attrs, closed) ->
7132 let r, l = map_of attrs in
7133 let kss = fromstring keys_of_string spos "in" r [] in
7134 let lss = fromstring keys_of_string spos "out" l [] in
7135 let keymap =
7136 match kss with
7137 | [] -> keymap
7138 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
7139 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
7141 if closed
7142 then { v with f = pkeymap ret keymap }
7143 else
7144 let f () = v in
7145 { v with f = skip "map" f }
7147 | Vopen _ ->
7148 error "unexpected subelement in keymap" s spos
7150 | Vclose "keymap" ->
7151 { v with f = ret keymap }
7153 | Vclose _ -> error "unexpected close in keymap" s spos
7155 and pbookmarks path pan anchor c bookmarks v t spos _ =
7156 match t with
7157 | Vdata | Vcdata -> v
7158 | Vend -> error "unexpected end of input in bookmarks" s spos
7159 | Vopen ("item", attrs, closed) ->
7160 let titleent, spage, srely, svisy = bookmark_of attrs in
7161 let page = fromstring int_of_string spos "page" spage 0
7162 and rely = fromstring float_of_string spos "rely" srely 0.0
7163 and visy = fromstring float_of_string spos "visy" svisy 0.0 in
7164 let bookmarks =
7165 (unent titleent, 0, Oanchor (page, rely, visy)) :: bookmarks
7167 if closed
7168 then { v with f = pbookmarks path pan anchor c bookmarks }
7169 else
7170 let f () = v in
7171 { v with f = skip "item" f }
7173 | Vopen _ ->
7174 error "unexpected subelement in bookmarks" s spos
7176 | Vclose "bookmarks" ->
7177 { v with f = doc path pan anchor c bookmarks }
7179 | Vclose _ -> error "unexpected close in bookmarks" s spos
7181 and skip tag f v t spos _ =
7182 match t with
7183 | Vdata | Vcdata -> v
7184 | Vend ->
7185 error ("unexpected end of input in skipped " ^ tag) s spos
7186 | Vopen (tag', _, closed) ->
7187 if closed
7188 then v
7189 else
7190 let f' () = { v with f = skip tag f } in
7191 { v with f = skip tag' f' }
7192 | Vclose ctag ->
7193 if tag = ctag
7194 then f ()
7195 else error ("unexpected close in skipped " ^ tag) s spos
7198 parse { f = toplevel; accu = () } s;
7199 h, dc;
7202 let do_load f ic =
7204 let len = in_channel_length ic in
7205 let s = String.create len in
7206 really_input ic s 0 len;
7207 f s;
7208 with
7209 | Parse_error (msg, s, pos) ->
7210 let subs = subs s pos in
7211 Utils.error "parse error: %s: at %d [..%s..]" msg pos subs
7213 | exn ->
7214 failwith ("config load error: " ^ exntos exn)
7217 let defconfpath =
7218 let dir =
7220 let dir = Filename.concat home ".config" in
7221 if Sys.is_directory dir then dir else home
7222 with _ -> home
7224 Filename.concat dir "llpp.conf"
7227 let confpath = ref defconfpath;;
7229 let load1 f =
7230 if Sys.file_exists !confpath
7231 then
7232 match
7233 (try Some (open_in_bin !confpath)
7234 with exn ->
7235 prerr_endline
7236 ("Error opening configuration file `" ^ !confpath ^ "': " ^
7237 exntos exn);
7238 None
7240 with
7241 | Some ic ->
7242 let success =
7244 f (do_load get ic)
7245 with exn ->
7246 prerr_endline
7247 ("Error loading configuration from `" ^ !confpath ^ "': " ^
7248 exntos exn);
7249 false
7251 close_in ic;
7252 success
7254 | None -> false
7255 else
7256 f (Hashtbl.create 0, defconf)
7259 let load () =
7260 let f (h, dc) =
7261 let pc, pb, px, pa =
7263 let key =
7264 if emptystr state.origin
7265 then state.path
7266 else state.origin
7268 Hashtbl.find h (Filename.basename key)
7269 with Not_found -> dc, [], 0, emptyanchor
7271 setconf defconf dc;
7272 setconf conf pc;
7273 state.bookmarks <- pb;
7274 state.x <- px;
7275 if conf.jumpback
7276 then state.anchor <- pa;
7277 cbput state.hists.nav pa;
7278 true
7280 load1 f
7283 let add_attrs bb always dc c =
7284 let ob s a b =
7285 if always || a != b
7286 then Printf.bprintf bb "\n %s='%b'" s a
7287 and op s a b =
7288 if always || a <> b
7289 then Printf.bprintf bb "\n %s='%b'" s (a != None)
7290 and oi s a b =
7291 if always || a != b
7292 then Printf.bprintf bb "\n %s='%d'" s a
7293 and oI s a b =
7294 if always || a != b
7295 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
7296 and oz s a b =
7297 if always || a <> b
7298 then Printf.bprintf bb "\n %s='%g'" s (a*.100.)
7299 and oF s a b =
7300 if always || a <> b
7301 then Printf.bprintf bb "\n %s='%f'" s a
7302 and oc s a b =
7303 if always || a <> b
7304 then
7305 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
7306 and oC s a b =
7307 if always || a <> b
7308 then
7309 Printf.bprintf bb "\n %s='%s'" s (CSTE.to_string a)
7310 and oR s a b =
7311 if always || a <> b
7312 then
7313 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
7314 and os s a b =
7315 if always || a <> b
7316 then
7317 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
7318 and og s a b =
7319 if always || a <> b
7320 then
7321 match a with
7322 | Some (_N, _A, _B) ->
7323 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
7324 | None ->
7325 match b with
7326 | None -> ()
7327 | _ ->
7328 Printf.bprintf bb "\n %s='none'" s
7329 and oW s a b =
7330 if always || a <> b
7331 then
7332 let v =
7333 match a with
7334 | None -> "false"
7335 | Some f ->
7336 if f = infinity
7337 then "true"
7338 else string_of_float f
7340 Printf.bprintf bb "\n %s='%s'" s v
7341 and oco s a b =
7342 if always || a <> b
7343 then
7344 match a with
7345 | Cmulti ((n, a, b), _) when n > 1 ->
7346 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
7347 | Csplit (n, _) when n > 1 ->
7348 Printf.bprintf bb "\n %s='%d'" s ~-n
7349 | _ -> ()
7350 and obeco s a b =
7351 if always || a <> b
7352 then
7353 match a with
7354 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
7355 | _ -> ()
7356 and oFm s a b =
7357 if always || a <> b
7358 then
7359 Printf.bprintf bb "\n %s='%s'" s (FMTE.to_string a)
7360 and oSv s a b m =
7361 if always || a <> b
7362 then
7363 Printf.bprintf bb "\n %s='%b'" s (a land m != 0)
7364 and oPm s a b =
7365 if always || a <> b
7366 then
7367 Printf.bprintf bb "\n %s='%s'" s (MTE.to_string a)
7369 oi "width" c.cwinw dc.cwinw;
7370 oi "height" c.cwinh dc.cwinh;
7371 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
7372 oi "scroll-handle-height" c.scrollh dc.scrollh;
7373 oSv "horizontal-scrollbar-visible" c.scrollb dc.scrollb scrollbhv;
7374 oSv "vertical-scrollbar-visible" c.scrollb dc.scrollb scrollbvv;
7375 ob "case-insensitive-search" c.icase dc.icase;
7376 ob "preload" c.preload dc.preload;
7377 oi "page-bias" c.pagebias dc.pagebias;
7378 oi "scroll-step" c.scrollstep dc.scrollstep;
7379 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
7380 ob "max-height-fit" c.maxhfit dc.maxhfit;
7381 ob "crop-hack" c.crophack dc.crophack;
7382 oW "throttle" c.maxwait dc.maxwait;
7383 ob "highlight-links" c.hlinks dc.hlinks;
7384 ob "under-cursor-info" c.underinfo dc.underinfo;
7385 oi "vertical-margin" c.interpagespace dc.interpagespace;
7386 oz "zoom" c.zoom dc.zoom;
7387 ob "presentation" c.presentation dc.presentation;
7388 oi "rotation-angle" c.angle dc.angle;
7389 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
7390 oFm "fit-model" c.fitmodel dc.fitmodel;
7391 oI "pixmap-cache-size" c.memlimit dc.memlimit;
7392 oi "tex-count" c.texcount dc.texcount;
7393 oi "slice-height" c.sliceheight dc.sliceheight;
7394 oi "thumbnail-width" c.thumbw dc.thumbw;
7395 ob "persistent-location" c.jumpback dc.jumpback;
7396 oc "background-color" c.bgcolor dc.bgcolor;
7397 oi "tile-width" c.tilew dc.tilew;
7398 oi "tile-height" c.tileh dc.tileh;
7399 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
7400 ob "checkers" c.checkers dc.checkers;
7401 oi "aalevel" c.aalevel dc.aalevel;
7402 ob "trim-margins" c.trimmargins dc.trimmargins;
7403 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
7404 os "uri-launcher" c.urilauncher dc.urilauncher;
7405 os "path-launcher" c.pathlauncher dc.pathlauncher;
7406 oC "color-space" c.colorspace dc.colorspace;
7407 ob "invert-colors" c.invert dc.invert;
7408 oF "brightness" c.colorscale dc.colorscale;
7409 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
7410 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
7411 oco "columns" c.columns dc.columns;
7412 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
7413 os "selection-command" c.selcmd dc.selcmd;
7414 os "synctex-command" c.stcmd dc.stcmd;
7415 os "pax-command" c.paxcmd dc.paxcmd;
7416 ob "update-cursor" c.updatecurs dc.updatecurs;
7417 oi "hint-font-size" c.hfsize dc.hfsize;
7418 oi "horizontal-scroll-step" c.hscrollstep dc.hscrollstep;
7419 oF "page-scroll-scale" c.pgscale dc.pgscale;
7420 ob "use-pbo" c.usepbo dc.usepbo;
7421 ob "wheel-scrolls-pages" c.wheelbypage dc.wheelbypage;
7422 ob "remote-in-a-new-instance" c.riani dc.riani;
7423 op "point-and-x" c.pax dc.pax;
7424 oPm "point-and-x-mark" c.paxmark dc.paxmark;
7427 let keymapsbuf always dc c =
7428 let bb = Buffer.create 16 in
7429 let rec loop = function
7430 | [] -> ()
7431 | (modename, h) :: rest ->
7432 let dh = findkeyhash dc modename in
7433 if always || h <> dh
7434 then (
7435 if Hashtbl.length h > 0
7436 then (
7437 if Buffer.length bb > 0
7438 then Buffer.add_char bb '\n';
7439 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
7440 Hashtbl.iter (fun i o ->
7441 let isdifferent = always ||
7443 let dO = Hashtbl.find dh i in
7444 dO <> o
7445 with Not_found -> true
7447 if isdifferent
7448 then
7449 let addkm (k, m) =
7450 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
7451 if Wsi.withalt m then Buffer.add_string bb "alt-";
7452 if Wsi.withshift m then Buffer.add_string bb "shift-";
7453 if Wsi.withmeta m then Buffer.add_string bb "meta-";
7454 Buffer.add_string bb (Wsi.keyname k);
7456 let addkms l =
7457 let rec loop = function
7458 | [] -> ()
7459 | km :: [] -> addkm km
7460 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
7462 loop l
7464 Buffer.add_string bb "<map in='";
7465 addkm i;
7466 match o with
7467 | KMinsrt km ->
7468 Buffer.add_string bb "' out='";
7469 addkm km;
7470 Buffer.add_string bb "'/>\n"
7472 | KMinsrl kms ->
7473 Buffer.add_string bb "' out='";
7474 addkms kms;
7475 Buffer.add_string bb "'/>\n"
7477 | KMmulti (ins, kms) ->
7478 Buffer.add_char bb ' ';
7479 addkms ins;
7480 Buffer.add_string bb "' out='";
7481 addkms kms;
7482 Buffer.add_string bb "'/>\n"
7483 ) h;
7484 Buffer.add_string bb "</keymap>";
7487 loop rest
7489 loop c.keyhashes;
7493 let save () =
7494 let uifontsize = fstate.fontsize in
7495 let bb = Buffer.create 32768 in
7496 let relx = float state.x /. float state.winw in
7497 let w, h, x =
7498 let cx w = truncate (relx *. float w) in
7499 List.fold_left
7500 (fun (w, h, x) ws ->
7501 match ws with
7502 | Wsi.Fullscreen -> (conf.cwinw, conf.cwinh, cx conf.cwinw)
7503 | Wsi.MaxVert -> (w, conf.cwinh, x)
7504 | Wsi.MaxHorz -> (conf.cwinw, h, cx conf.cwinw)
7506 (state.winw, state.winh, state.x) state.winstate
7508 conf.cwinw <- w;
7509 conf.cwinh <- h;
7510 let f (h, dc) =
7511 let dc = if conf.bedefault then conf else dc in
7512 Buffer.add_string bb "<llppconfig>\n";
7514 if nonemptystr !fontpath
7515 then
7516 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
7517 uifontsize
7518 !fontpath
7519 else (
7520 if uifontsize <> 14
7521 then
7522 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
7525 Buffer.add_string bb "<defaults";
7526 add_attrs bb true dc dc;
7527 let kb = keymapsbuf true dc dc in
7528 if Buffer.length kb > 0
7529 then (
7530 Buffer.add_string bb ">\n";
7531 Buffer.add_buffer bb kb;
7532 Buffer.add_string bb "\n</defaults>\n";
7534 else Buffer.add_string bb "/>\n";
7536 let adddoc path pan anchor c bookmarks =
7537 if bookmarks == [] && c = dc && anchor = emptyanchor
7538 then ()
7539 else (
7540 Printf.bprintf bb "<doc path='%s'"
7541 (enent path 0 (String.length path));
7543 if anchor <> emptyanchor
7544 then (
7545 let n, rely, visy = anchor in
7546 Printf.bprintf bb " page='%d'" n;
7547 if rely > 1e-6
7548 then
7549 Printf.bprintf bb " rely='%f'" rely
7551 if abs_float visy > 1e-6
7552 then
7553 Printf.bprintf bb " visy='%f'" visy
7557 if pan != 0
7558 then Printf.bprintf bb " pan='%d'" pan;
7560 add_attrs bb false dc c;
7561 let kb = keymapsbuf false dc c in
7563 begin match bookmarks with
7564 | [] ->
7565 if Buffer.length kb > 0
7566 then (
7567 Buffer.add_string bb ">\n";
7568 Buffer.add_buffer bb kb;
7569 Buffer.add_string bb "\n</doc>\n";
7571 else Buffer.add_string bb "/>\n"
7572 | _ ->
7573 Buffer.add_string bb ">\n<bookmarks>\n";
7574 List.iter (fun (title, _, kind) ->
7575 begin match kind with
7576 | Oanchor (page, rely, visy) ->
7577 Printf.bprintf bb
7578 "<item title='%s' page='%d'"
7579 (enent title 0 (String.length title))
7580 page
7582 if rely > 1e-6
7583 then
7584 Printf.bprintf bb " rely='%f'" rely
7586 if abs_float visy > 1e-6
7587 then
7588 Printf.bprintf bb " visy='%f'" visy
7590 | Onone | Ouri _ | Oremote _ | Oremotedest _ | Olaunch _ ->
7591 failwith "unexpected link in bookmarks"
7592 end;
7593 Buffer.add_string bb "/>\n";
7594 ) bookmarks;
7595 Buffer.add_string bb "</bookmarks>";
7596 if Buffer.length kb > 0
7597 then (
7598 Buffer.add_string bb "\n";
7599 Buffer.add_buffer bb kb;
7601 Buffer.add_string bb "\n</doc>\n";
7602 end;
7606 let pan, conf =
7607 match state.mode with
7608 | Birdseye (c, pan, _, _, _) ->
7609 let beyecolumns =
7610 match conf.columns with
7611 | Cmulti ((c, _, _), _) -> Some c
7612 | Csingle _ -> None
7613 | Csplit _ -> None
7614 and columns =
7615 match c.columns with
7616 | Cmulti (c, _) -> Cmulti (c, [||])
7617 | Csingle _ -> Csingle [||]
7618 | Csplit _ -> failwith "quit from bird's eye while split"
7620 pan, { c with beyecolumns = beyecolumns; columns = columns }
7621 | _ -> x, conf
7623 let basename = Filename.basename
7624 (if emptystr state.origin then state.path else state.origin)
7626 adddoc basename pan (getanchor ())
7627 (let conf =
7628 let autoscrollstep =
7629 match state.autoscroll with
7630 | Some step -> step
7631 | None -> conf.autoscrollstep
7633 match state.mode with
7634 | Birdseye (bc, _, _, _, _) ->
7635 { conf with
7636 zoom = bc.zoom;
7637 presentation = bc.presentation;
7638 interpagespace = bc.interpagespace;
7639 maxwait = bc.maxwait;
7640 autoscrollstep = autoscrollstep }
7641 | _ -> { conf with autoscrollstep = autoscrollstep }
7642 in conf)
7643 (if conf.savebmarks then state.bookmarks else []);
7645 Hashtbl.iter (fun path (c, bookmarks, x, anchor) ->
7646 if basename <> path
7647 then adddoc path x anchor c bookmarks
7648 ) h;
7649 Buffer.add_string bb "</llppconfig>\n";
7650 true;
7652 if load1 f && Buffer.length bb > 0
7653 then
7655 let tmp = !confpath ^ ".tmp" in
7656 let oc = open_out_bin tmp in
7657 Buffer.output_buffer oc bb;
7658 close_out oc;
7659 Unix.rename tmp !confpath;
7660 with exn ->
7661 prerr_endline
7662 ("error while saving configuration: " ^ exntos exn)
7664 end;;
7666 let adderrmsg src msg =
7667 Buffer.add_string state.errmsgs msg;
7668 state.newerrmsgs <- true;
7669 G.postRedisplay src
7672 let adderrfmt src fmt =
7673 Format.kprintf (fun s -> adderrmsg src s) fmt;
7676 let ract cmds =
7677 let cl = splitatspace cmds in
7678 let scan s fmt f =
7679 try Scanf.sscanf s fmt f
7680 with exn ->
7681 adderrfmt "remote exec"
7682 "error processing '%S': %s\n" cmds (exntos exn)
7684 match cl with
7685 | "reload" :: [] -> reload ()
7686 | "goto" :: args :: [] ->
7687 scan args "%u %f %f"
7688 (fun pageno x y ->
7689 let cmd, _ = state.geomcmds in
7690 if emptystr cmd
7691 then gotopagexy pageno x y
7692 else
7693 let f prevf () =
7694 gotopagexy pageno x y;
7695 prevf ()
7697 state.reprf <- f state.reprf
7699 | "goto1" :: args :: [] -> scan args "%u %f" gotopage
7700 | "gotor" :: args :: [] ->
7701 scan args "%S %u"
7702 (fun filename pageno -> gotounder (Uremote (filename, pageno)))
7703 | "gotord" :: args :: [] ->
7704 scan args "%S %S"
7705 (fun filename dest -> gotounder (Uremotedest (filename, dest)))
7706 | "rect" :: args :: [] ->
7707 scan args "%u %u %f %f %f %f"
7708 (fun pageno color x0 y0 x1 y1 ->
7709 onpagerect pageno (fun w h ->
7710 let _,w1,h1,_ = getpagedim pageno in
7711 let sw = float w1 /. float w
7712 and sh = float h1 /. float h in
7713 let x0s = x0 *. sw
7714 and x1s = x1 *. sw
7715 and y0s = y0 *. sh
7716 and y1s = y1 *. sh in
7717 let rect = (x0s,y0s,x1s,y0s,x1s,y1s,x0s,y1s) in
7718 debugrect rect;
7719 state.rects <- (pageno, color, rect) :: state.rects;
7720 G.postRedisplay "rect";
7723 | "activatewin" :: [] -> Wsi.activatewin ()
7724 | "quit" :: [] -> raise Quit
7725 | _ ->
7726 adderrfmt "remote command"
7727 "error processing remote command: %S\n" cmds;
7730 let remote =
7731 let scratch = String.create 80 in
7732 let buf = Buffer.create 80 in
7733 fun fd ->
7734 let rec tempfr () =
7735 try Some (Unix.read fd scratch 0 80)
7736 with
7737 | Unix.Unix_error (Unix.EAGAIN, _, _) -> None
7738 | Unix.Unix_error (Unix.EINTR, _, _) -> tempfr ()
7739 | exn -> raise exn
7741 match tempfr () with
7742 | None -> Some fd
7743 | Some n ->
7744 if n = 0
7745 then (
7746 Unix.close fd;
7747 if Buffer.length buf > 0
7748 then (
7749 let s = Buffer.contents buf in
7750 Buffer.clear buf;
7751 ract s;
7753 None
7755 else
7756 let rec eat ppos =
7757 let nlpos =
7759 let pos = String.index_from scratch ppos '\n' in
7760 if pos >= n then -1 else pos
7761 with Not_found -> -1
7763 if nlpos >= 0
7764 then (
7765 Buffer.add_substring buf scratch ppos (nlpos-ppos);
7766 let s = Buffer.contents buf in
7767 Buffer.clear buf;
7768 ract s;
7769 eat (nlpos+1);
7771 else (
7772 Buffer.add_substring buf scratch ppos (n-ppos);
7773 Some fd
7775 in eat 0
7778 let remoteopen path =
7779 try Some (Unix.openfile path [Unix.O_NONBLOCK; Unix.O_RDONLY] 0o0)
7780 with exn ->
7781 adderrfmt "remoteopen" "error opening %S: %s" path (exntos exn);
7782 None
7785 let () =
7786 let trimcachepath = ref "" in
7787 let rcmdpath = ref "" in
7788 let pageno = ref None in
7789 selfexec := Sys.executable_name;
7790 Arg.parse
7791 (Arg.align
7792 [("-p", Arg.String (fun s -> state.password <- s),
7793 "<password> Set password");
7795 ("-f", Arg.String
7796 (fun s ->
7797 Config.fontpath := s;
7798 selfexec := !selfexec ^ " -f " ^ Filename.quote s;
7800 "<path> Set path to the user interface font");
7802 ("-c", Arg.String
7803 (fun s ->
7804 selfexec := !selfexec ^ " -c " ^ Filename.quote s;
7805 Config.confpath := s),
7806 "<path> Set path to the configuration file");
7808 ("-page", Arg.Int (fun pageno1 -> pageno := Some (pageno1-1)),
7809 "<page-number> Jump to page");
7811 ("-tcf", Arg.String (fun s -> trimcachepath := s),
7812 "<path> Set path to the trim cache file");
7814 ("-dest", Arg.String (fun s -> state.nameddest <- s),
7815 "<named-destination> Set named destination");
7817 ("-wtmode", Arg.Set wtmode, " Operate in wt mode");
7818 ("-cxack", Arg.Set cxack, " Cut corners");
7820 ("-remote", Arg.String (fun s -> rcmdpath := s),
7821 "<path> Set path to the remote commands source");
7823 ("-origin", Arg.String (fun s -> state.origin <- s),
7824 "<original-path> Set original path");
7826 ("-v", Arg.Unit (fun () ->
7827 Printf.printf
7828 "%s\nconfiguration path: %s\n"
7829 (version ())
7830 Config.defconfpath
7832 exit 0), " Print version and exit");
7835 (fun s -> state.path <- s)
7836 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
7838 if !wtmode
7839 then selfexec := !selfexec ^ " -wtmode";
7841 if emptystr state.path
7842 then (prerr_endline "file name missing"; exit 1);
7844 if not (Config.load ())
7845 then prerr_endline "failed to load configuration";
7846 begin match !pageno with
7847 | Some pageno -> state.anchor <- (pageno, 0.0, 0.0)
7848 | None -> ()
7849 end;
7851 let wsfd, winw, winh = Wsi.init (object (self)
7852 val mutable m_hack = false
7853 val mutable m_clicks = 0
7854 val mutable m_click_x = 0
7855 val mutable m_click_y = 0
7856 val mutable m_lastclicktime = infinity
7858 method private cleanup =
7859 Hashtbl.iter (fun _ opaque -> clearmark opaque) state.pagemap;
7860 method expose = if not m_hack then G.postRedisplay "expose"
7861 method visible = self#cleanup; G.postRedisplay "visible"
7862 method display = m_hack <- false; display ()
7863 method reshape w h =
7864 self#cleanup;
7865 m_hack <- w < state.winw && h < state.winh;
7866 reshape w h
7867 method mouse b d x y m =
7868 if d
7869 then (
7870 (* http://blogs.msdn.com/b/oldnewthing/archive/2004/10/18/243925.aspx *)
7871 m_click_x <- x;
7872 m_click_y <- y;
7873 if b = 1
7874 then (
7875 let t = now () in
7876 if abs x - m_click_x > 10
7877 || abs y - m_click_y > 10
7878 || abs_float (t -. m_lastclicktime) > 0.3
7879 then m_clicks <- 0;
7880 m_clicks <- m_clicks + 1;
7881 m_lastclicktime <- t;
7882 if m_clicks = 1
7883 then (
7884 self#cleanup;
7885 state.uioh <- state.uioh#button b d x y m;
7887 else state.uioh <- state.uioh#multiclick m_clicks x y m
7889 else (
7890 self#cleanup;
7891 m_lastclicktime <- infinity;
7892 state.uioh <- state.uioh#button b d x y m
7895 else (
7896 state.uioh <- state.uioh#button b d x y m
7898 method motion x y =
7899 state.mpos <- (x, y);
7900 state.uioh <- state.uioh#motion x y
7901 method pmotion x y =
7902 state.mpos <- (x, y);
7903 state.uioh <- state.uioh#pmotion x y
7904 method key k m =
7905 self#cleanup;
7906 let mascm = m land (
7907 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
7908 ) in
7909 match state.keystate with
7910 | KSnone ->
7911 let km = k, mascm in
7912 begin
7913 match
7914 let modehash = state.uioh#modehash in
7915 try Hashtbl.find modehash km
7916 with Not_found ->
7917 try Hashtbl.find (findkeyhash conf "global") km
7918 with Not_found -> KMinsrt (k, m)
7919 with
7920 | KMinsrt (k, m) -> keyboard k m
7921 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
7922 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
7924 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
7925 List.iter (fun (k, m) -> keyboard k m) insrt;
7926 state.keystate <- KSnone
7927 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
7928 state.keystate <- KSinto (keys, insrt)
7929 | _ ->
7930 state.keystate <- KSnone
7932 method enter x y =
7933 state.mpos <- (x, y);
7934 state.uioh <- state.uioh#pmotion x y
7935 method leave = state.mpos <- (-1, -1)
7936 method winstate wsl = state.winstate <- wsl; m_hack <- false
7937 method quit = raise Quit
7938 end) conf.cwinw conf.cwinh (platform = Posx) in
7940 state.wsfd <- wsfd;
7942 if not (
7943 List.exists GlMisc.check_extension
7944 [ "GL_ARB_texture_rectangle"
7945 ; "GL_EXT_texture_recangle"
7946 ; "GL_NV_texture_rectangle" ]
7948 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
7950 if (
7951 let r = GlMisc.get_string `renderer in
7952 let p = "Mesa DRI Intel(" in
7953 let l = String.length p in
7954 String.length r > l && String.sub r 0 l = p
7956 then (
7957 defconf.sliceheight <- 1024;
7958 defconf.texcount <- 32;
7959 defconf.usepbo <- true;
7962 let cr, sw =
7963 match Ne.pipe () with
7964 | Ne.Exn exn ->
7965 Printf.eprintf "pipe/crsw failed: %s" (exntos exn);
7966 exit 1
7967 | Ne.Res rw -> rw
7968 and sr, cw =
7969 match Ne.pipe () with
7970 | Ne.Exn exn ->
7971 Printf.eprintf "pipe/srcw failed: %s" (exntos exn);
7972 exit 1
7973 | Ne.Res rw -> rw
7976 cloexec cr;
7977 cloexec sw;
7978 cloexec sr;
7979 cloexec cw;
7981 setcheckers conf.checkers;
7982 redirectstderr ();
7983 if conf.redirectstderr
7984 then
7985 at_exit (fun () ->
7986 let s = Buffer.contents state.errmsgs ^
7987 (match state.errfd with
7988 | Some fd ->
7989 let s = String.create (80*24) in
7990 let n =
7992 let r, _, _ = Unix.select [fd] [] [] 0.0 in
7993 if List.mem fd r
7994 then Unix.read fd s 0 (String.length s)
7995 else 0
7996 with _ -> 0
7998 if n = 0
7999 then ""
8000 else String.sub s 0 n
8001 | None -> ""
8004 try ignore (Unix.write state.stderr s 0 (String.length s))
8005 with exn -> print_endline (exntos exn)
8009 init (cr, cw) (
8010 conf.angle, conf.fitmodel, (conf.trimmargins, conf.trimfuzz),
8011 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
8012 !Config.fontpath, !trimcachepath,
8013 GlMisc.check_extension "GL_ARB_pixel_buffer_object"
8015 List.iter GlArray.enable [`texture_coord; `vertex];
8016 state.sr <- sr;
8017 state.sw <- sw;
8018 state.text <- "Opening " ^ (mbtoutf8 state.path);
8019 reshape winw winh;
8020 opendoc state.path state.password;
8021 state.uioh <- uioh;
8022 display ();
8023 Wsi.mapwin ();
8024 Sys.set_signal Sys.sighup (Sys.Signal_handle (fun _ -> reload ()));
8025 let optrfd =
8026 ref (
8027 if nonemptystr !rcmdpath
8028 then remoteopen !rcmdpath
8029 else None
8033 let rec loop deadline =
8034 let r =
8035 match state.errfd with
8036 | None -> [state.sr; state.wsfd]
8037 | Some fd -> [state.sr; state.wsfd; fd]
8039 let r =
8040 match !optrfd with
8041 | None -> r
8042 | Some fd -> fd :: r
8044 if state.redisplay
8045 then (
8046 state.redisplay <- false;
8047 display ();
8049 let timeout =
8050 let now = now () in
8051 if deadline > now
8052 then (
8053 if deadline = infinity
8054 then ~-.1.0
8055 else max 0.0 (deadline -. now)
8057 else 0.0
8059 let r, _, _ =
8060 try Unix.select r [] [] timeout
8061 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
8063 begin match r with
8064 | [] ->
8065 state.ghyll None;
8066 let newdeadline =
8067 if state.ghyll == noghyll
8068 then
8069 match state.autoscroll with
8070 | Some step when step != 0 ->
8071 let y = state.y + step in
8072 let y =
8073 if y < 0
8074 then state.maxy
8075 else if y >= state.maxy then 0 else y
8077 gotoy y;
8078 if state.mode = View
8079 then state.text <- "";
8080 deadline +. 0.01
8081 | _ -> infinity
8082 else deadline +. 0.01
8084 loop newdeadline
8086 | l ->
8087 let rec checkfds = function
8088 | [] -> ()
8089 | fd :: rest when fd = state.sr ->
8090 let cmd = readcmd state.sr in
8091 act cmd;
8092 checkfds rest
8094 | fd :: rest when fd = state.wsfd ->
8095 Wsi.readresp fd;
8096 checkfds rest
8098 | fd :: rest when Some fd = !optrfd ->
8099 begin match remote fd with
8100 | None -> optrfd := remoteopen !rcmdpath;
8101 | opt -> optrfd := opt
8102 end;
8103 checkfds rest
8105 | fd :: rest ->
8106 let s = String.create 80 in
8107 let n = tempfailureretry (Unix.read fd s 0) 80 in
8108 if conf.redirectstderr
8109 then (
8110 Buffer.add_substring state.errmsgs s 0 n;
8111 state.newerrmsgs <- true;
8112 state.redisplay <- true;
8114 else (
8115 prerr_string (String.sub s 0 n);
8116 flush stderr;
8118 checkfds rest
8120 checkfds l;
8121 let newdeadline =
8122 let deadline1 =
8123 if deadline = infinity
8124 then now () +. 0.01
8125 else deadline
8127 match state.autoscroll with
8128 | Some step when step != 0 -> deadline1
8129 | _ -> if state.ghyll == noghyll then infinity else deadline1
8131 loop newdeadline
8132 end;
8135 loop infinity;
8136 with Quit ->
8137 Config.save ();