Fix grave slinks related bug(s)
[llpp.git] / main.ml
blob4e65ea46b9dad349216a7f345436bfa8a90a13f1
1 exception Quit;;
3 type under =
4 | Unone
5 | Ulinkuri of string
6 | Ulinkgoto of (int * int)
7 | Utext of facename
8 | Uunexpected of string
9 | Ulaunch of string
10 | Unamed of string
11 | Uremote of (string * int)
12 and facename = string;;
14 let dolog fmt = Printf.kprintf prerr_endline fmt;;
15 let now = Unix.gettimeofday;;
17 type params = (angle * proportional * trimparams
18 * texcount * sliceheight * memsize
19 * colorspace * fontpath)
20 and pageno = int
21 and width = int
22 and height = int
23 and leftx = int
24 and opaque = string
25 and recttype = int
26 and pixmapsize = int
27 and angle = int
28 and proportional = bool
29 and trimmargins = bool
30 and interpagespace = int
31 and texcount = int
32 and sliceheight = int
33 and gen = int
34 and top = float
35 and fontpath = string
36 and memsize = int
37 and aalevel = int
38 and irect = (int * int * int * int)
39 and trimparams = (trimmargins * irect)
40 and colorspace = | Rgb | Bgr | Gray
43 type link =
44 | Lnotfound
45 | Lfound of (int * int * int * int * int)
46 and linkdir =
47 | LDfirst
48 | LDlast
49 | LDfirstvisible of (int * int * int)
50 | LDleft of int
51 | LDright of int
52 | LDdown of int
53 | LDup of int
56 type pagewithlinks =
57 | Pwlnotfound
58 | Pwl of int
61 type keymap =
62 | KMinsrt of key
63 | KMinsrl of key list
64 | KMmulti of key list * key list
65 and key = int * int
66 and keyhash = (key, keymap) Hashtbl.t
67 and keystate =
68 | KSnone
69 | KSinto of (key list * key list)
72 type platform = | Punknown | Plinux | Posx | Psun | Pfreebsd
73 | Pdragonflybsd | Popenbsd | Pnetbsd | Pcygwin;;
75 type pipe = (Unix.file_descr * Unix.file_descr);;
77 external init : pipe -> params -> unit = "ml_init";;
78 external seltext : string -> (int * int * int * int) -> unit = "ml_seltext";;
79 external copysel : string -> opaque -> unit = "ml_copysel";;
80 external getpdimrect : int -> float array = "ml_getpdimrect";;
81 external whatsunder : string -> int -> int -> under = "ml_whatsunder";;
82 external zoomforh : int -> int -> int -> float = "ml_zoom_for_height";;
83 external drawstr : int -> int -> int -> string -> float = "ml_draw_string";;
84 external measurestr : int -> string -> float = "ml_measure_string";;
85 external getmaxw : unit -> float = "ml_getmaxw";;
86 external postprocess : opaque -> bool -> int -> int -> unit = "ml_postprocess";;
87 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
88 external platform : unit -> platform = "ml_platform";;
89 external setaalevel : int -> unit = "ml_setaalevel";;
90 external realloctexts : int -> bool = "ml_realloctexts";;
91 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
92 external findlink : opaque -> linkdir -> link = "ml_findlink";;
93 external getlink : opaque -> int -> under = "ml_getlink";;
94 external findpwl: int -> int -> pagewithlinks = "ml_find_page_with_links"
96 let platform_to_string = function
97 | Punknown -> "unknown"
98 | Plinux -> "Linux"
99 | Posx -> "OSX"
100 | Psun -> "Sun"
101 | Pfreebsd -> "FreeBSD"
102 | Pdragonflybsd -> "DragonflyBSD"
103 | Popenbsd -> "OpenBSD"
104 | Pnetbsd -> "NetBSD"
105 | Pcygwin -> "Cygwin"
108 let platform = platform ();;
110 type x = int
111 and y = int
112 and tilex = int
113 and tiley = int
114 and tileparams = (x * y * width * height * tilex * tiley)
117 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
119 type mpos = int * int
120 and mstate =
121 | Msel of (mpos * mpos)
122 | Mpan of mpos
123 | Mscrolly | Mscrollx
124 | Mzoom of (int * int)
125 | Mzoomrect of (mpos * mpos)
126 | Mnone
129 type textentry = string * string * onhist option * onkey * ondone
130 and onkey = string -> int -> te
131 and ondone = string -> unit
132 and histcancel = unit -> unit
133 and onhist = ((histcmd -> string) * histcancel)
134 and histcmd = HCnext | HCprev | HCfirst | HClast
135 and te =
136 | TEstop
137 | TEdone of string
138 | TEcont of string
139 | TEswitch of textentry
142 type 'a circbuf =
143 { store : 'a array
144 ; mutable rc : int
145 ; mutable wc : int
146 ; mutable len : int
150 let bound v minv maxv =
151 max minv (min maxv v);
154 let cbnew n v =
155 { store = Array.create n v
156 ; rc = 0
157 ; wc = 0
158 ; len = 0
162 let drawstring size x y s =
163 Gl.enable `blend;
164 Gl.enable `texture_2d;
165 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
166 ignore (drawstr size x y s);
167 Gl.disable `blend;
168 Gl.disable `texture_2d;
171 let drawstring1 size x y s =
172 drawstr size x y s;
175 let drawstring2 size x y fmt =
176 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
179 let cbcap b = Array.length b.store;;
181 let cbput b v =
182 let cap = cbcap b in
183 b.store.(b.wc) <- v;
184 b.wc <- (b.wc + 1) mod cap;
185 b.rc <- b.wc;
186 b.len <- min (b.len + 1) cap;
189 let cbempty b = b.len = 0;;
191 let cbgetg b circular dir =
192 if cbempty b
193 then b.store.(0)
194 else
195 let rc = b.rc + dir in
196 let rc =
197 if circular
198 then (
199 if rc = -1
200 then b.len-1
201 else (
202 if rc = b.len
203 then 0
204 else rc
207 else max 0 (min rc (b.len-1))
209 b.rc <- rc;
210 b.store.(rc);
213 let cbget b = cbgetg b false;;
214 let cbgetc b = cbgetg b true;;
216 type page =
217 { pageno : int
218 ; pagedimno : int
219 ; pagew : int
220 ; pageh : int
221 ; pagex : int
222 ; pagey : int
223 ; pagevw : int
224 ; pagevh : int
225 ; pagedispx : int
226 ; pagedispy : int
230 let debugl l =
231 dolog "l %d dim=%d {" l.pageno l.pagedimno;
232 dolog " WxH %dx%d" l.pagew l.pageh;
233 dolog " vWxH %dx%d" l.pagevw l.pagevh;
234 dolog " pagex,y %d,%d" l.pagex l.pagey;
235 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
236 dolog "}";
239 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
240 dolog "rect {";
241 dolog " x0,y0=(% f, % f)" x0 y0;
242 dolog " x1,y1=(% f, % f)" x1 y1;
243 dolog " x2,y2=(% f, % f)" x2 y2;
244 dolog " x3,y3=(% f, % f)" x3 y3;
245 dolog "}";
248 type columns =
249 multicol * ((pdimno * x * y * (pageno * width * height * leftx)) array)
250 and multicol = columncount * covercount * covercount
251 and pdimno = int
252 and columncount = int
253 and covercount = int;;
255 type conf =
256 { mutable scrollbw : int
257 ; mutable scrollh : int
258 ; mutable icase : bool
259 ; mutable preload : bool
260 ; mutable pagebias : int
261 ; mutable verbose : bool
262 ; mutable debug : bool
263 ; mutable scrollstep : int
264 ; mutable maxhfit : bool
265 ; mutable crophack : bool
266 ; mutable autoscrollstep : int
267 ; mutable maxwait : float option
268 ; mutable hlinks : bool
269 ; mutable underinfo : bool
270 ; mutable interpagespace : interpagespace
271 ; mutable zoom : float
272 ; mutable presentation : bool
273 ; mutable angle : angle
274 ; mutable winw : int
275 ; mutable winh : int
276 ; mutable savebmarks : bool
277 ; mutable proportional : proportional
278 ; mutable trimmargins : trimmargins
279 ; mutable trimfuzz : irect
280 ; mutable memlimit : memsize
281 ; mutable texcount : texcount
282 ; mutable sliceheight : sliceheight
283 ; mutable thumbw : width
284 ; mutable jumpback : bool
285 ; mutable bgcolor : float * float * float
286 ; mutable bedefault : bool
287 ; mutable scrollbarinpm : bool
288 ; mutable tilew : int
289 ; mutable tileh : int
290 ; mutable mustoresize : memsize
291 ; mutable checkers : bool
292 ; mutable aalevel : int
293 ; mutable urilauncher : string
294 ; mutable pathlauncher : string
295 ; mutable colorspace : colorspace
296 ; mutable invert : bool
297 ; mutable colorscale : float
298 ; mutable redirectstderr : bool
299 ; mutable ghyllscroll : (int * int * int) option
300 ; mutable columns : columns option
301 ; mutable beyecolumns : columncount option
302 ; mutable selcmd : string
303 ; mutable updatecurs : bool
304 ; mutable keyhashes : (string * keyhash) list
308 type anchor = pageno * top;;
310 type outline = string * int * anchor;;
312 type rect = float * float * float * float * float * float * float * float;;
314 type tile = opaque * pixmapsize * elapsed
315 and elapsed = float;;
316 type pagemapkey = pageno * gen;;
317 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
318 and row = int
319 and col = int;;
321 let emptyanchor = (0, 0.0);;
323 type infochange = | Memused | Docinfo | Pdim;;
325 class type uioh = object
326 method display : unit
327 method key : int -> int -> uioh
328 method button : int -> bool -> int -> int -> int -> uioh
329 method motion : int -> int -> uioh
330 method pmotion : int -> int -> uioh
331 method infochanged : infochange -> unit
332 method scrollpw : (int * float * float)
333 method scrollph : (int * float * float)
334 method modehash : keyhash
335 end;;
337 type mode =
338 | Birdseye of (conf * leftx * pageno * pageno * anchor)
339 | Textentry of (textentry * onleave)
340 | View
341 | LinkNav of linktarget
342 and onleave = leavetextentrystatus -> unit
343 and leavetextentrystatus = | Cancel | Confirm
344 and helpitem = string * int * action
345 and action =
346 | Noaction
347 | Action of (uioh -> uioh)
348 and linktarget =
349 | Ltexact of ((pageno * int) * (int * int * int * int))
350 | Ltgendir of int
353 let isbirdseye = function Birdseye _ -> true | _ -> false;;
354 let istextentry = function Textentry _ -> true | _ -> false;;
356 type currently =
357 | Idle
358 | Loading of (page * gen)
359 | Tiling of (
360 page * opaque * colorspace * angle * gen * col * row * width * height
362 | Outlining of outline list
365 let emptykeyhash = Hashtbl.create 0;;
366 let nouioh : uioh = object (self)
367 method display = ()
368 method key _ _ = self
369 method button _ _ _ _ _ = self
370 method motion _ _ = self
371 method pmotion _ _ = self
372 method infochanged _ = ()
373 method scrollpw = (0, nan, nan)
374 method scrollph = (0, nan, nan)
375 method modehash = emptykeyhash
376 end;;
378 type state =
379 { mutable sr : Unix.file_descr
380 ; mutable sw : Unix.file_descr
381 ; mutable wsfd : Unix.file_descr
382 ; mutable errfd : Unix.file_descr option
383 ; mutable stderr : Unix.file_descr
384 ; mutable errmsgs : Buffer.t
385 ; mutable newerrmsgs : bool
386 ; mutable w : int
387 ; mutable x : int
388 ; mutable y : int
389 ; mutable scrollw : int
390 ; mutable hscrollh : int
391 ; mutable anchor : anchor
392 ; mutable ranchors : (string * string * anchor) list
393 ; mutable maxy : int
394 ; mutable layout : page list
395 ; pagemap : (pagemapkey, opaque) Hashtbl.t
396 ; tilemap : (tilemapkey, tile) Hashtbl.t
397 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
398 ; mutable pdims : (pageno * width * height * leftx) list
399 ; mutable pagecount : int
400 ; mutable currently : currently
401 ; mutable mstate : mstate
402 ; mutable searchpattern : string
403 ; mutable rects : (pageno * recttype * rect) list
404 ; mutable rects1 : (pageno * recttype * rect) list
405 ; mutable text : string
406 ; mutable fullscreen : (width * height) option
407 ; mutable mode : mode
408 ; mutable uioh : uioh
409 ; mutable outlines : outline array
410 ; mutable bookmarks : outline list
411 ; mutable path : string
412 ; mutable password : string
413 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
414 ; mutable memused : memsize
415 ; mutable gen : gen
416 ; mutable throttle : (page list * int * float) option
417 ; mutable autoscroll : int option
418 ; mutable ghyll : (int option -> unit)
419 ; mutable help : helpitem array
420 ; mutable docinfo : (int * string) list
421 ; mutable texid : GlTex.texture_id option
422 ; hists : hists
423 ; mutable prevzoom : float
424 ; mutable progress : float
425 ; mutable redisplay : bool
426 ; mutable mpos : mpos
427 ; mutable keystate : keystate
429 and hists =
430 { pat : string circbuf
431 ; pag : string circbuf
432 ; nav : anchor circbuf
433 ; sel : string circbuf
437 let defconf =
438 { scrollbw = 7
439 ; scrollh = 12
440 ; icase = true
441 ; preload = true
442 ; pagebias = 0
443 ; verbose = false
444 ; debug = false
445 ; scrollstep = 24
446 ; maxhfit = true
447 ; crophack = false
448 ; autoscrollstep = 2
449 ; maxwait = None
450 ; hlinks = false
451 ; underinfo = false
452 ; interpagespace = 2
453 ; zoom = 1.0
454 ; presentation = false
455 ; angle = 0
456 ; winw = 900
457 ; winh = 900
458 ; savebmarks = true
459 ; proportional = true
460 ; trimmargins = false
461 ; trimfuzz = (0,0,0,0)
462 ; memlimit = 32 lsl 20
463 ; texcount = 256
464 ; sliceheight = 24
465 ; thumbw = 76
466 ; jumpback = true
467 ; bgcolor = (0.5, 0.5, 0.5)
468 ; bedefault = false
469 ; scrollbarinpm = true
470 ; tilew = 2048
471 ; tileh = 2048
472 ; mustoresize = 128 lsl 20
473 ; checkers = true
474 ; aalevel = 8
475 ; urilauncher =
476 (match platform with
477 | Plinux | Pfreebsd | Pdragonflybsd
478 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
479 | Posx -> "open \"%s\""
480 | Pcygwin -> "cygstart %s"
481 | Punknown -> "echo %s")
482 ; pathlauncher = "lp \"%s\""
483 ; selcmd =
484 (match platform with
485 | Plinux | Pfreebsd | Pdragonflybsd
486 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
487 | Posx -> "pbcopy"
488 | Pcygwin -> "wsel"
489 | Punknown -> "cat")
490 ; colorspace = Rgb
491 ; invert = false
492 ; colorscale = 1.0
493 ; redirectstderr = false
494 ; ghyllscroll = None
495 ; columns = None
496 ; beyecolumns = None
497 ; updatecurs = false
498 ; keyhashes =
499 let mk n = (n, Hashtbl.create 1) in
500 [ mk "global"
501 ; mk "info"
502 ; mk "help"
503 ; mk "outline"
504 ; mk "listview"
505 ; mk "birdseye"
506 ; mk "textentry"
507 ; mk "links"
512 let findkeyhash c name =
513 try List.assoc name c.keyhashes
514 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
517 let conf = { defconf with angle = defconf.angle };;
519 type fontstate =
520 { mutable fontsize : int
521 ; mutable wwidth : float
522 ; mutable maxrows : int
526 let fstate =
527 { fontsize = 14
528 ; wwidth = nan
529 ; maxrows = -1
533 let setfontsize n =
534 fstate.fontsize <- n;
535 fstate.wwidth <- measurestr fstate.fontsize "w";
536 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
539 let geturl s =
540 let colonpos = try String.index s ':' with Not_found -> -1 in
541 let len = String.length s in
542 if colonpos >= 0 && colonpos + 3 < len
543 then (
544 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
545 then
546 let schemestartpos =
547 try String.rindex_from s colonpos ' '
548 with Not_found -> -1
550 let scheme =
551 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
553 match scheme with
554 | "http" | "ftp" | "mailto" ->
555 let epos =
556 try String.index_from s colonpos ' '
557 with Not_found -> len
559 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
560 | _ -> ""
561 else ""
563 else ""
566 let popen =
567 let shell, farg = "/bin/sh", "-c" in
568 fun s ->
569 let args = [|shell; farg; s|] in
570 ignore (Unix.create_process shell args Unix.stdin Unix.stdout Unix.stderr)
573 let gotouri uri =
574 if String.length conf.urilauncher = 0
575 then print_endline uri
576 else (
577 let url = geturl uri in
578 if String.length url = 0
579 then print_endline uri
580 else
581 let re = Str.regexp "%s" in
582 let command = Str.global_replace re url conf.urilauncher in
583 try popen command
584 with exn ->
585 Printf.eprintf
586 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
587 flush stderr;
591 let version () =
592 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
593 (platform_to_string platform) Sys.word_size Sys.ocaml_version
596 let makehelp () =
597 let strings = version () :: "" :: Help.keys in
598 Array.of_list (
599 List.map (fun s ->
600 let url = geturl s in
601 if String.length url > 0
602 then (s, 0, Action (fun u -> gotouri url; u))
603 else (s, 0, Noaction)
604 ) strings);
607 let noghyll _ = ();;
608 let firstgeomcmds = "", [];;
610 let state =
611 { sr = Unix.stdin
612 ; sw = Unix.stdin
613 ; wsfd = Unix.stdin
614 ; errfd = None
615 ; stderr = Unix.stderr
616 ; errmsgs = Buffer.create 0
617 ; newerrmsgs = false
618 ; x = 0
619 ; y = 0
620 ; w = 0
621 ; scrollw = 0
622 ; hscrollh = 0
623 ; anchor = emptyanchor
624 ; ranchors = []
625 ; layout = []
626 ; maxy = max_int
627 ; tilelru = Queue.create ()
628 ; pagemap = Hashtbl.create 10
629 ; tilemap = Hashtbl.create 10
630 ; pdims = []
631 ; pagecount = 0
632 ; currently = Idle
633 ; mstate = Mnone
634 ; rects = []
635 ; rects1 = []
636 ; text = ""
637 ; mode = View
638 ; fullscreen = None
639 ; searchpattern = ""
640 ; outlines = [||]
641 ; bookmarks = []
642 ; path = ""
643 ; password = ""
644 ; geomcmds = firstgeomcmds
645 ; hists =
646 { nav = cbnew 10 (0, 0.0)
647 ; pat = cbnew 10 ""
648 ; pag = cbnew 10 ""
649 ; sel = cbnew 10 ""
651 ; memused = 0
652 ; gen = 0
653 ; throttle = None
654 ; autoscroll = None
655 ; ghyll = noghyll
656 ; help = makehelp ()
657 ; docinfo = []
658 ; texid = None
659 ; prevzoom = 1.0
660 ; progress = -1.0
661 ; uioh = nouioh
662 ; redisplay = true
663 ; mpos = (-1, -1)
664 ; keystate = KSnone
668 let vlog fmt =
669 if conf.verbose
670 then
671 Printf.kprintf prerr_endline fmt
672 else
673 Printf.kprintf ignore fmt
676 let launchpath () =
677 if String.length conf.pathlauncher = 0
678 then print_endline state.path
679 else (
680 let re = Str.regexp "%s" in
681 let command = Str.global_replace re state.path conf.pathlauncher in
682 try popen command
683 with exn ->
684 Printf.eprintf
685 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
686 flush stderr;
690 let redirectstderr () =
691 if conf.redirectstderr
692 then
693 let rfd, wfd = Unix.pipe () in
694 state.stderr <- Unix.dup Unix.stderr;
695 state.errfd <- Some rfd;
696 Unix.dup2 wfd Unix.stderr;
697 else (
698 state.newerrmsgs <- false;
699 begin match state.errfd with
700 | Some fd ->
701 Unix.close fd;
702 Unix.dup2 state.stderr Unix.stderr;
703 state.errfd <- None;
704 | None -> ()
705 end;
706 prerr_string (Buffer.contents state.errmsgs);
707 flush stderr;
708 Buffer.clear state.errmsgs;
712 module G =
713 struct
714 let postRedisplay who =
715 if conf.verbose
716 then prerr_endline ("redisplay for " ^ who);
717 state.redisplay <- true;
719 end;;
721 let getopaque pageno =
722 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
723 with Not_found -> None
726 let putopaque pageno opaque =
727 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
730 let pagetranslatepoint l x y =
731 let dy = y - l.pagedispy in
732 let y = dy + l.pagey in
733 let dx = x - l.pagedispx in
734 let x = dx + l.pagex in
735 (x, y);
738 let getunder x y =
739 let rec f = function
740 | l :: rest ->
741 begin match getopaque l.pageno with
742 | Some opaque ->
743 let x0 = l.pagedispx in
744 let x1 = x0 + l.pagevw in
745 let y0 = l.pagedispy in
746 let y1 = y0 + l.pagevh in
747 if y >= y0 && y <= y1 && x >= x0 && x <= x1
748 then
749 let px, py = pagetranslatepoint l x y in
750 match whatsunder opaque px py with
751 | Unone -> f rest
752 | under -> under
753 else f rest
754 | _ ->
755 f rest
757 | [] -> Unone
759 f state.layout
762 let showtext c s =
763 state.text <- Printf.sprintf "%c%s" c s;
764 G.postRedisplay "showtext";
767 let updateunder x y =
768 match getunder x y with
769 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
770 | Ulinkuri uri ->
771 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
772 Wsi.setcursor Wsi.CURSOR_INFO
773 | Ulinkgoto (page, _) ->
774 if conf.underinfo
775 then showtext 'p' ("age: " ^ string_of_int (page+1));
776 Wsi.setcursor Wsi.CURSOR_INFO
777 | Utext s ->
778 if conf.underinfo then showtext 'f' ("ont: " ^ s);
779 Wsi.setcursor Wsi.CURSOR_TEXT
780 | Uunexpected s ->
781 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
782 Wsi.setcursor Wsi.CURSOR_INHERIT
783 | Ulaunch s ->
784 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
785 Wsi.setcursor Wsi.CURSOR_INHERIT
786 | Unamed s ->
787 if conf.underinfo then showtext 'n' ("amed: " ^ s);
788 Wsi.setcursor Wsi.CURSOR_INHERIT
789 | Uremote (filename, pageno) ->
790 if conf.underinfo then showtext 'r'
791 (Printf.sprintf "emote: %s (%d)" filename pageno);
792 Wsi.setcursor Wsi.CURSOR_INFO
795 let showlinktype under =
796 if conf.underinfo
797 then
798 match under with
799 | Unone -> ()
800 | Ulinkuri uri ->
801 showtext 'u' ("ri: " ^ uri)
802 | Ulinkgoto (page, _) ->
803 showtext 'p' ("age: " ^ string_of_int (page+1));
804 | Utext s ->
805 showtext 'f' ("ont: " ^ s);
806 | Uunexpected s ->
807 showtext 'u' ("nexpected: " ^ s);
808 | Ulaunch s ->
809 showtext 'l' ("aunch: " ^ s);
810 | Unamed s ->
811 showtext 'n' ("amed: " ^ s);
812 | Uremote (filename, pageno) ->
813 showtext 'r' (Printf.sprintf "emote: %s (%d)" filename pageno);
816 let addchar s c =
817 let b = Buffer.create (String.length s + 1) in
818 Buffer.add_string b s;
819 Buffer.add_char b c;
820 Buffer.contents b;
823 let colorspace_of_string s =
824 match String.lowercase s with
825 | "rgb" -> Rgb
826 | "bgr" -> Bgr
827 | "gray" -> Gray
828 | _ -> failwith "invalid colorspace"
831 let int_of_colorspace = function
832 | Rgb -> 0
833 | Bgr -> 1
834 | Gray -> 2
837 let colorspace_of_int = function
838 | 0 -> Rgb
839 | 1 -> Bgr
840 | 2 -> Gray
841 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
844 let colorspace_to_string = function
845 | Rgb -> "rgb"
846 | Bgr -> "bgr"
847 | Gray -> "gray"
850 let intentry_with_suffix text key =
851 let c =
852 if key >= 32 && key < 127
853 then Char.chr key
854 else '\000'
856 match Char.lowercase c with
857 | '0' .. '9' ->
858 let text = addchar text c in
859 TEcont text
861 | 'k' | 'm' | 'g' ->
862 let text = addchar text c in
863 TEcont text
865 | _ ->
866 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
867 TEcont text
870 let columns_to_string (n, a, b) =
871 if a = 0 && b = 0
872 then Printf.sprintf "%d" n
873 else Printf.sprintf "%d,%d,%d" n a b;
876 let columns_of_string s =
878 (int_of_string s, 0, 0)
879 with _ ->
880 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
883 let readcmd fd =
884 let s = "xxxx" in
885 let n = Unix.read fd s 0 4 in
886 if n != 4 then failwith "incomplete read(len)";
887 let len = 0
888 lor (Char.code s.[0] lsl 24)
889 lor (Char.code s.[1] lsl 16)
890 lor (Char.code s.[2] lsl 8)
891 lor (Char.code s.[3] lsl 0)
893 let s = String.create len in
894 let n = Unix.read fd s 0 len in
895 if n != len then failwith "incomplete read(data)";
899 let btod b = if b then 1 else 0;;
901 let wcmd fmt =
902 let b = Buffer.create 16 in
903 Buffer.add_string b "llll";
904 Printf.kbprintf
905 (fun b ->
906 let s = Buffer.contents b in
907 let n = String.length s in
908 let len = n - 4 in
909 (* dolog "wcmd %S" (String.sub s 4 len); *)
910 s.[0] <- Char.chr ((len lsr 24) land 0xff);
911 s.[1] <- Char.chr ((len lsr 16) land 0xff);
912 s.[2] <- Char.chr ((len lsr 8) land 0xff);
913 s.[3] <- Char.chr (len land 0xff);
914 let n' = Unix.write state.sw s 0 n in
915 if n' != n then failwith "write failed";
916 ) b fmt;
919 let calcips h =
920 if conf.presentation
921 then
922 let d = conf.winh - h in
923 max 0 ((d + 1) / 2)
924 else
925 conf.interpagespace
928 let calcheight () =
929 let rec f pn ph pi fh l =
930 match l with
931 | (n, _, h, _) :: rest ->
932 let ips = calcips h in
933 let fh =
934 if conf.presentation
935 then fh+ips
936 else (
937 if isbirdseye state.mode && pn = 0
938 then fh + ips
939 else fh
942 let fh = fh + ((n - pn) * (ph + pi)) in
943 f n h ips fh rest;
945 | [] ->
946 let inc =
947 if conf.presentation || (isbirdseye state.mode && pn = 0)
948 then 0
949 else -pi
951 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
952 max 0 fh
954 let fh = f 0 0 0 0 state.pdims in
958 let calcheight () =
959 match conf.columns with
960 | None -> calcheight ()
961 | Some (_, b) ->
962 if Array.length b > 0
963 then
964 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
965 y + h
966 else 0
969 let getpageyh pageno =
970 let rec f pn ph pi y l =
971 match l with
972 | (n, _, h, _) :: rest ->
973 let ips = calcips h in
974 if n >= pageno
975 then
976 let h = if n = pageno then h else ph in
977 if conf.presentation && n = pageno
978 then
979 y + (pageno - pn) * (ph + pi) + pi, h
980 else
981 y + (pageno - pn) * (ph + pi), h
982 else
983 let y = y + (if conf.presentation then pi else 0) in
984 let y = y + (n - pn) * (ph + pi) in
985 f n h ips y rest
987 | [] ->
988 y + (pageno - pn) * (ph + pi), ph
990 f 0 0 0 0 state.pdims
993 let getpageyh pageno =
994 match conf.columns with
995 | None -> getpageyh pageno
996 | Some (_, b) ->
997 let (_, _, y, (_, _, h, _)) = b.(pageno) in
998 y, h
1001 let getpagedim pageno =
1002 let rec f ppdim l =
1003 match l with
1004 | (n, _, _, _) as pdim :: rest ->
1005 if n >= pageno
1006 then (if n = pageno then pdim else ppdim)
1007 else f pdim rest
1009 | [] -> ppdim
1011 f (-1, -1, -1, -1) state.pdims
1014 let getpagey pageno = fst (getpageyh pageno);;
1016 let nogeomcmds cmds =
1017 match cmds with
1018 | s, [] -> String.length s = 0
1019 | _ -> false
1022 let layout1 y sh =
1023 let sh = sh - state.hscrollh in
1024 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
1025 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
1026 match pdims with
1027 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
1028 let ips = calcips h in
1029 let yinc =
1030 if conf.presentation || (isbirdseye state.mode && pageno = 0)
1031 then ips
1032 else 0
1034 (w, h, ips, xoff), rest, pdimno + 1, yinc
1035 | _ ->
1036 prev, pdims, pdimno, 0
1038 let dy = dy + yinc in
1039 let py = py + yinc in
1040 if pageno = state.pagecount || dy >= sh
1041 then
1042 accu
1043 else
1044 let vy = y + dy in
1045 if py + h <= vy - yinc
1046 then
1047 let py = py + h + ips in
1048 let dy = max 0 (py - y) in
1049 f ~pageno:(pageno+1)
1050 ~pdimno
1051 ~prev:curr
1054 ~pdims:rest
1055 ~accu
1056 else
1057 let pagey = vy - py in
1058 let pagevh = h - pagey in
1059 let pagevh = min (sh - dy) pagevh in
1060 let off = if yinc > 0 then py - vy else 0 in
1061 let py = py + h + ips in
1062 let pagex, dx =
1063 let xoff = xoff +
1064 if state.w < conf.winw - state.scrollw
1065 then (conf.winw - state.scrollw - state.w) / 2
1066 else 0
1068 let dispx = xoff + state.x in
1069 if dispx < 0
1070 then (-dispx, 0)
1071 else (0, dispx)
1073 let pagevw =
1074 let lw = w - pagex in
1075 min lw (conf.winw - state.scrollw)
1077 let e =
1078 { pageno = pageno
1079 ; pagedimno = pdimno
1080 ; pagew = w
1081 ; pageh = h
1082 ; pagex = pagex
1083 ; pagey = pagey + off
1084 ; pagevw = pagevw
1085 ; pagevh = pagevh - off
1086 ; pagedispx = dx
1087 ; pagedispy = dy + off
1090 let accu = e :: accu in
1091 f ~pageno:(pageno+1)
1092 ~pdimno
1093 ~prev:curr
1095 ~dy:(dy+pagevh+ips)
1096 ~pdims:rest
1097 ~accu
1099 if nogeomcmds state.geomcmds
1100 then (
1101 let accu =
1103 ~pageno:0
1104 ~pdimno:~-1
1105 ~prev:(0,0,0,0)
1106 ~py:0
1107 ~dy:0
1108 ~pdims:state.pdims
1109 ~accu:[]
1111 List.rev accu
1113 else
1117 let layoutN ((columns, coverA, coverB), b) y sh =
1118 let sh = sh - state.hscrollh in
1119 let rec fold accu n =
1120 if n = Array.length b
1121 then accu
1122 else
1123 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1124 if (vy - y) > sh &&
1125 (n = coverA - 1
1126 || n = state.pagecount - coverB
1127 || (n - coverA) mod columns = columns - 1)
1128 then accu
1129 else
1130 let accu =
1131 if vy + h > y
1132 then
1133 let pagey = max 0 (y - vy) in
1134 let pagedispy = if pagey > 0 then 0 else vy - y in
1135 let pagedispx, pagex, pagevw =
1136 let pdx =
1137 if n = coverA - 1 || n = state.pagecount - coverB
1138 then state.x + (conf.winw - state.scrollw - w) / 2
1139 else dx + xoff + state.x
1141 if pdx < 0
1142 then 0, -pdx, w + pdx
1143 else pdx, 0, min (conf.winw - state.scrollw) w
1145 let pagevh = min (h - pagey) (sh - pagedispy) in
1146 if pagedispx < conf.winw - state.scrollw && pagevw > 0 && pagevh > 0
1147 then
1148 let e =
1149 { pageno = n
1150 ; pagedimno = pdimno
1151 ; pagew = w
1152 ; pageh = h
1153 ; pagex = pagex
1154 ; pagey = pagey
1155 ; pagevw = pagevw
1156 ; pagevh = pagevh
1157 ; pagedispx = pagedispx
1158 ; pagedispy = pagedispy
1161 e :: accu
1162 else
1163 accu
1164 else
1165 accu
1167 fold accu (n+1)
1169 if nogeomcmds state.geomcmds
1170 then List.rev (fold [] 0)
1171 else []
1174 let layout y sh =
1175 match conf.columns with
1176 | None -> layout1 y sh
1177 | Some c -> layoutN c y sh
1180 let clamp incr =
1181 let y = state.y + incr in
1182 let y = max 0 y in
1183 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1187 let itertiles l f =
1188 let tilex = l.pagex mod conf.tilew in
1189 let tiley = l.pagey mod conf.tileh in
1191 let col = l.pagex / conf.tilew in
1192 let row = l.pagey / conf.tileh in
1194 let vw =
1195 let a = l.pagew - l.pagex in
1196 let b = conf.winw - state.scrollw in
1197 min a b
1198 and vh = l.pagevh in
1200 let rec rowloop row y0 dispy h =
1201 if h = 0
1202 then ()
1203 else (
1204 let dh = conf.tileh - y0 in
1205 let dh = min h dh in
1206 let rec colloop col x0 dispx w =
1207 if w = 0
1208 then ()
1209 else (
1210 let dw = conf.tilew - x0 in
1211 let dw = min w dw in
1213 f col row dispx dispy x0 y0 dw dh;
1214 colloop (col+1) 0 (dispx+dw) (w-dw)
1217 colloop col tilex l.pagedispx vw;
1218 rowloop (row+1) 0 (dispy+dh) (h-dh)
1221 if vw > 0 && vh > 0
1222 then rowloop row tiley l.pagedispy vh;
1225 let gettileopaque l col row =
1226 let key =
1227 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1229 try Some (Hashtbl.find state.tilemap key)
1230 with Not_found -> None
1233 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1234 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1235 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1238 let drawtiles l color =
1239 GlDraw.color color;
1240 let f col row x y tilex tiley w h =
1241 match gettileopaque l col row with
1242 | Some (opaque, _, t) ->
1243 let params = x, y, w, h, tilex, tiley in
1244 if conf.invert
1245 then (
1246 Gl.enable `blend;
1247 GlFunc.blend_func `zero `one_minus_src_color;
1249 drawtile params opaque;
1250 if conf.invert
1251 then Gl.disable `blend;
1252 if conf.debug
1253 then (
1254 let s = Printf.sprintf
1255 "%d[%d,%d] %f sec"
1256 l.pageno col row t
1258 let w = measurestr fstate.fontsize s in
1259 GlMisc.push_attrib [`current];
1260 GlDraw.color (0.0, 0.0, 0.0);
1261 GlDraw.rect
1262 (float (x-2), float (y-2))
1263 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1264 GlDraw.color (1.0, 1.0, 1.0);
1265 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1266 GlMisc.pop_attrib ();
1269 | _ ->
1270 let w =
1271 let lw = conf.winw - state.scrollw - x in
1272 min lw w
1273 and h =
1274 let lh = conf.winh - y in
1275 min lh h
1277 Gl.enable `texture_2d;
1278 begin match state.texid with
1279 | Some id ->
1280 GlTex.bind_texture `texture_2d id;
1281 let x0 = float x
1282 and y0 = float y
1283 and x1 = float (x+w)
1284 and y1 = float (y+h) in
1286 let tw = float w /. 64.0
1287 and th = float h /. 64.0 in
1288 let tx0 = float tilex /. 64.0
1289 and ty0 = float tiley /. 64.0 in
1290 let tx1 = tx0 +. tw
1291 and ty1 = ty0 +. th in
1292 GlDraw.begins `quads;
1293 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1294 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1295 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1296 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1297 GlDraw.ends ();
1299 Gl.disable `texture_2d;
1300 | None ->
1301 GlDraw.color (1.0, 1.0, 1.0);
1302 GlDraw.rect
1303 (float x, float y)
1304 (float (x+w), float (y+h));
1305 end;
1306 if w > 128 && h > fstate.fontsize + 10
1307 then (
1308 GlDraw.color (0.0, 0.0, 0.0);
1309 let c, r =
1310 if conf.verbose
1311 then (col*conf.tilew, row*conf.tileh)
1312 else col, row
1314 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1316 GlDraw.color color;
1318 itertiles l f
1321 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1323 let tilevisible1 l x y =
1324 let ax0 = l.pagex
1325 and ax1 = l.pagex + l.pagevw
1326 and ay0 = l.pagey
1327 and ay1 = l.pagey + l.pagevh in
1329 let bx0 = x
1330 and by0 = y in
1331 let bx1 = min (bx0 + conf.tilew) l.pagew
1332 and by1 = min (by0 + conf.tileh) l.pageh in
1334 let rx0 = max ax0 bx0
1335 and ry0 = max ay0 by0
1336 and rx1 = min ax1 bx1
1337 and ry1 = min ay1 by1 in
1339 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1340 nonemptyintersection
1343 let tilevisible layout n x y =
1344 let rec findpageinlayout = function
1345 | l :: _ when l.pageno = n -> tilevisible1 l x y
1346 | _ :: rest -> findpageinlayout rest
1347 | [] -> false
1349 findpageinlayout layout
1352 let tileready l x y =
1353 tilevisible1 l x y &&
1354 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1357 let tilepage n p layout =
1358 let rec loop = function
1359 | l :: rest ->
1360 if l.pageno = n
1361 then
1362 let f col row _ _ _ _ _ _ =
1363 if state.currently = Idle
1364 then
1365 match gettileopaque l col row with
1366 | Some _ -> ()
1367 | None ->
1368 let x = col*conf.tilew
1369 and y = row*conf.tileh in
1370 let w =
1371 let w = l.pagew - x in
1372 min w conf.tilew
1374 let h =
1375 let h = l.pageh - y in
1376 min h conf.tileh
1378 wcmd "tile %s %d %d %d %d" p x y w h;
1379 state.currently <-
1380 Tiling (
1381 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1382 conf.tilew, conf.tileh
1385 itertiles l f;
1386 else
1387 loop rest
1389 | [] -> ()
1391 if nogeomcmds state.geomcmds
1392 then loop layout;
1395 let preloadlayout visiblepages =
1396 let presentation = conf.presentation in
1397 let interpagespace = conf.interpagespace in
1398 let maxy = state.maxy in
1399 conf.presentation <- false;
1400 conf.interpagespace <- 0;
1401 state.maxy <- calcheight ();
1402 let y =
1403 match visiblepages with
1404 | [] -> 0
1405 | l :: _ -> getpagey l.pageno + l.pagey
1407 let y = if y < conf.winh then 0 else y - conf.winh in
1408 let h = state.y - y + conf.winh*3 in
1409 let pages = layout y h in
1410 conf.presentation <- presentation;
1411 conf.interpagespace <- interpagespace;
1412 state.maxy <- maxy;
1413 pages;
1416 let load pages =
1417 let rec loop pages =
1418 if state.currently != Idle
1419 then ()
1420 else
1421 match pages with
1422 | l :: rest ->
1423 begin match getopaque l.pageno with
1424 | None ->
1425 wcmd "page %d %d" l.pageno l.pagedimno;
1426 state.currently <- Loading (l, state.gen);
1427 | Some opaque ->
1428 tilepage l.pageno opaque pages;
1429 loop rest
1430 end;
1431 | _ -> ()
1433 if nogeomcmds state.geomcmds
1434 then loop pages
1437 let preload pages =
1438 load pages;
1439 if conf.preload && state.currently = Idle
1440 then load (preloadlayout pages);
1443 let layoutready layout =
1444 let rec fold all ls =
1445 all && match ls with
1446 | l :: rest ->
1447 let seen = ref false in
1448 let allvisible = ref true in
1449 let foo col row _ _ _ _ _ _ =
1450 seen := true;
1451 allvisible := !allvisible &&
1452 begin match gettileopaque l col row with
1453 | Some _ -> true
1454 | None -> false
1457 itertiles l foo;
1458 fold (!seen && !allvisible) rest
1459 | [] -> true
1461 let alltilesvisible = fold true layout in
1462 alltilesvisible;
1465 let gotoy y =
1466 let y = bound y 0 state.maxy in
1467 let y, layout, proceed =
1468 match conf.maxwait with
1469 | Some time when state.ghyll == noghyll ->
1470 begin match state.throttle with
1471 | None ->
1472 let layout = layout y conf.winh in
1473 let ready = layoutready layout in
1474 if not ready
1475 then (
1476 load layout;
1477 state.throttle <- Some (layout, y, now ());
1479 else G.postRedisplay "gotoy showall (None)";
1480 y, layout, ready
1481 | Some (_, _, started) ->
1482 let dt = now () -. started in
1483 if dt > time
1484 then (
1485 state.throttle <- None;
1486 let layout = layout y conf.winh in
1487 load layout;
1488 G.postRedisplay "maxwait";
1489 y, layout, true
1491 else -1, [], false
1494 | _ ->
1495 let layout = layout y conf.winh in
1496 if true || layoutready layout
1497 then G.postRedisplay "gotoy ready";
1498 y, layout, true
1500 if proceed
1501 then (
1502 state.y <- y;
1503 state.layout <- layout;
1504 begin match state.mode with
1505 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1506 if not (pagevisible layout pageno)
1507 then (
1508 match state.layout with
1509 | [] -> ()
1510 | l :: _ ->
1511 state.mode <- Birdseye (
1512 conf, leftx, l.pageno, hooverpageno, anchor
1515 | LinkNav (Ltgendir dir as lt) ->
1516 let linknav =
1517 let rec loop = function
1518 | [] -> lt
1519 | l :: rest ->
1520 match getopaque l.pageno with
1521 | None -> loop rest
1522 | Some opaque ->
1523 let link =
1524 let ld =
1525 if dir = 0
1526 then LDfirstvisible (l.pagex, l.pagey, dir)
1527 else (
1528 if dir > 0 then LDfirst else LDlast
1531 findlink opaque ld
1533 match link with
1534 | Lnotfound -> loop rest
1535 | Lfound (n, x0, y0, x1, y1) ->
1536 showlinktype (getlink opaque n);
1537 Ltexact ((l.pageno, n), (x0, y0, x1, y1))
1539 loop state.layout
1541 state.mode <- LinkNav linknav
1542 | _ -> ()
1543 end;
1544 preload layout;
1546 state.ghyll <- noghyll;
1547 if conf.updatecurs
1548 then (
1549 let mx, my = state.mpos in
1550 updateunder mx my;
1554 let conttiling pageno opaque =
1555 tilepage pageno opaque
1556 (if conf.preload then preloadlayout state.layout else state.layout)
1559 let gotoy_and_clear_text y =
1560 if not conf.verbose then state.text <- "";
1561 gotoy y;
1564 let getanchor () =
1565 match state.layout with
1566 | [] -> emptyanchor
1567 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1570 let getanchory (n, top) =
1571 let y, h = getpageyh n in
1572 y + (truncate (top *. float h));
1575 let gotoanchor anchor =
1576 gotoy (getanchory anchor);
1579 let addnav () =
1580 cbput state.hists.nav (getanchor ());
1583 let getnav dir =
1584 let anchor = cbgetc state.hists.nav dir in
1585 getanchory anchor;
1588 let gotoghyll y =
1589 let rec scroll f n a b =
1590 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1591 let snake f a b =
1592 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1593 if f < a
1594 then s (float f /. float a)
1595 else (
1596 if f > b
1597 then 1.0 -. s ((float (f-b) /. float (n-b)))
1598 else 1.0
1601 snake f a b
1602 and summa f n a b =
1603 (* courtesy:
1604 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1605 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1606 let iv1 = iv f in
1607 let ins = float a *. iv1
1608 and outs = float (n-b) *. iv1 in
1609 let ones = b - a in
1610 ins +. outs +. float ones
1612 let rec set (_N, _A, _B) y sy =
1613 let sum = summa 1.0 _N _A _B in
1614 let dy = float (y - sy) in
1615 state.ghyll <- (
1616 let rec gf n y1 o =
1617 if n >= _N
1618 then state.ghyll <- noghyll
1619 else
1620 let go n =
1621 let s = scroll n _N _A _B in
1622 let y1 = y1 +. ((s *. dy) /. sum) in
1623 gotoy_and_clear_text (truncate y1);
1624 state.ghyll <- gf (n+1) y1;
1626 match o with
1627 | None -> go n
1628 | Some y' -> set (_N/2, 0, 0) y' state.y
1630 gf 0 (float state.y)
1633 match conf.ghyllscroll with
1634 | None ->
1635 gotoy_and_clear_text y
1636 | Some nab ->
1637 if state.ghyll == noghyll
1638 then set nab y state.y
1639 else state.ghyll (Some y)
1642 let gotopage n top =
1643 let y, h = getpageyh n in
1644 let y = y + (truncate (top *. float h)) in
1645 gotoghyll y
1648 let gotopage1 n top =
1649 let y = getpagey n in
1650 let y = y + top in
1651 gotoghyll y
1654 let invalidate s f =
1655 state.layout <- [];
1656 state.pdims <- [];
1657 state.rects <- [];
1658 state.rects1 <- [];
1659 match state.geomcmds with
1660 | ps, [] when String.length ps = 0 ->
1661 f ();
1662 state.geomcmds <- s, [];
1664 | ps, [] ->
1665 state.geomcmds <- ps, [s, f];
1667 | ps, (s', _) :: rest when s' = s ->
1668 state.geomcmds <- ps, ((s, f) :: rest);
1670 | ps, cmds ->
1671 state.geomcmds <- ps, ((s, f) :: cmds);
1674 let opendoc path password =
1675 state.path <- path;
1676 state.password <- password;
1677 state.gen <- state.gen + 1;
1678 state.docinfo <- [];
1680 setaalevel conf.aalevel;
1681 Wsi.settitle ("llpp " ^ Filename.basename path);
1682 wcmd "open %s\000%s\000" path password;
1683 invalidate "reqlayout"
1684 (fun () ->
1685 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1688 let scalecolor c =
1689 let c = c *. conf.colorscale in
1690 (c, c, c);
1693 let scalecolor2 (r, g, b) =
1694 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1697 let represent () =
1698 let docolumns = function
1699 | None -> ()
1700 | Some ((columns, coverA, coverB), _) ->
1701 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1702 let rec loop pageno pdimno pdim x y rowh pdims =
1703 if pageno = state.pagecount
1704 then ()
1705 else
1706 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1707 match pdims with
1708 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1709 pdimno+1, pdim, rest
1710 | _ ->
1711 pdimno, pdim, pdims
1713 let x, y, rowh' =
1714 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1715 then (
1716 (conf.winw - state.scrollw - w) / 2,
1717 y + rowh + conf.interpagespace, h
1719 else (
1720 if (pageno - coverA) mod columns = 0
1721 then 0, y + rowh + conf.interpagespace, h
1722 else x, y, max rowh h
1725 let rec fixrow m = if m = pageno then () else
1726 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1727 if h < rowh
1728 then (
1729 let y = y + (rowh - h) / 2 in
1730 a.(m) <- (pdimno, x, y, pdim);
1732 fixrow (m+1)
1734 if pageno > 1 && (pageno - coverA) mod columns = 0
1735 then fixrow (pageno - columns);
1736 a.(pageno) <- (pdimno, x, y, pdim);
1737 let x = x + w + xoff*2 + conf.interpagespace in
1738 loop (pageno+1) pdimno pdim x y rowh' pdims
1740 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1741 conf.columns <- Some ((columns, coverA, coverB), a);
1743 docolumns conf.columns;
1744 state.maxy <- calcheight ();
1745 state.hscrollh <-
1746 if state.w <= conf.winw - state.scrollw
1747 then 0
1748 else state.scrollw
1750 match state.mode with
1751 | Birdseye (_, _, pageno, _, _) ->
1752 let y, h = getpageyh pageno in
1753 let top = (conf.winh - h) / 2 in
1754 gotoy (max 0 (y - top))
1755 | _ -> gotoanchor state.anchor
1758 let reshape w h =
1759 GlDraw.viewport 0 0 w h;
1760 if state.geomcmds != firstgeomcmds && nogeomcmds state.geomcmds
1761 then state.anchor <- getanchor ();
1763 conf.winw <- w;
1764 let w = truncate (float w *. conf.zoom) - state.scrollw in
1765 let w = max w 2 in
1766 conf.winh <- h;
1767 setfontsize fstate.fontsize;
1768 GlMat.mode `modelview;
1769 GlMat.load_identity ();
1771 GlMat.mode `projection;
1772 GlMat.load_identity ();
1773 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1774 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1775 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1777 let relx =
1778 if conf.zoom <= 1.0
1779 then 0.0
1780 else float state.x /. float state.w
1782 invalidate "geometry"
1783 (fun () ->
1784 state.w <- w;
1785 state.x <- truncate (relx *. float w);
1786 let w =
1787 match conf.columns with
1788 | None -> w
1789 | Some ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1791 wcmd "geometry %d %d" w h);
1794 let enttext () =
1795 let len = String.length state.text in
1796 let drawstring s =
1797 let hscrollh =
1798 match state.mode with
1799 | Textentry _
1800 | View -> state.hscrollh
1801 | _ -> 0
1803 let rect x w =
1804 GlDraw.rect
1805 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1806 (x+.w, float (conf.winh - hscrollh))
1809 let w = float (conf.winw - state.scrollw - 1) in
1810 if state.progress >= 0.0 && state.progress < 1.0
1811 then (
1812 GlDraw.color (0.3, 0.3, 0.3);
1813 let w1 = w *. state.progress in
1814 rect 0.0 w1;
1815 GlDraw.color (0.0, 0.0, 0.0);
1816 rect w1 (w-.w1)
1818 else (
1819 GlDraw.color (0.0, 0.0, 0.0);
1820 rect 0.0 w;
1823 GlDraw.color (1.0, 1.0, 1.0);
1824 drawstring fstate.fontsize
1825 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1827 let s =
1828 match state.mode with
1829 | Textentry ((prefix, text, _, _, _), _) ->
1830 let s =
1831 if len > 0
1832 then
1833 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1834 else
1835 Printf.sprintf "%s%s_" prefix text
1839 | _ -> state.text
1841 let s =
1842 if state.newerrmsgs
1843 then (
1844 if not (istextentry state.mode)
1845 then
1846 let s1 = "(press 'e' to review error messasges)" in
1847 if String.length s > 0 then s ^ " " ^ s1 else s1
1848 else s
1850 else s
1852 if String.length s > 0
1853 then drawstring s
1856 let gctiles () =
1857 let len = Queue.length state.tilelru in
1858 let rec loop qpos =
1859 if state.memused <= conf.memlimit
1860 then ()
1861 else (
1862 if qpos < len
1863 then
1864 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1865 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1866 let (_, pw, ph, _) = getpagedim n in
1868 gen = state.gen
1869 && colorspace = conf.colorspace
1870 && angle = conf.angle
1871 && pagew = pw
1872 && pageh = ph
1873 && (
1874 let layout =
1875 match state.throttle with
1876 | None ->
1877 if conf.preload
1878 then preloadlayout state.layout
1879 else state.layout
1880 | Some (layout, _, _) ->
1881 layout
1883 let x = col*conf.tilew
1884 and y = row*conf.tileh in
1885 tilevisible layout n x y
1887 then Queue.push lruitem state.tilelru
1888 else (
1889 wcmd "freetile %s" p;
1890 state.memused <- state.memused - s;
1891 state.uioh#infochanged Memused;
1892 Hashtbl.remove state.tilemap k;
1894 loop (qpos+1)
1897 loop 0
1900 let flushtiles () =
1901 Queue.iter (fun (k, p, s) ->
1902 wcmd "freetile %s" p;
1903 state.memused <- state.memused - s;
1904 state.uioh#infochanged Memused;
1905 Hashtbl.remove state.tilemap k;
1906 ) state.tilelru;
1907 Queue.clear state.tilelru;
1908 load state.layout;
1911 let logcurrently = function
1912 | Idle -> dolog "Idle"
1913 | Loading (l, gen) ->
1914 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1915 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1916 dolog
1917 "Tiling %d[%d,%d] page=%s cs=%s angle"
1918 l.pageno col row pageopaque
1919 (colorspace_to_string colorspace)
1921 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1922 angle gen conf.angle state.gen
1923 tilew tileh
1924 conf.tilew conf.tileh
1926 | Outlining _ ->
1927 dolog "outlining"
1930 let act cmds =
1931 (* dolog "%S" cmds; *)
1932 let op, args =
1933 let spacepos =
1934 try String.index cmds ' '
1935 with Not_found -> -1
1937 if spacepos = -1
1938 then cmds, ""
1939 else
1940 let l = String.length cmds in
1941 let op = String.sub cmds 0 spacepos in
1942 op, begin
1943 if l - spacepos < 2 then ""
1944 else String.sub cmds (spacepos+1) (l-spacepos-1)
1947 match op with
1948 | "clear" ->
1949 state.uioh#infochanged Pdim;
1950 state.pdims <- [];
1952 | "clearrects" ->
1953 state.rects <- state.rects1;
1954 G.postRedisplay "clearrects";
1956 | "continue" ->
1957 let n =
1958 try Scanf.sscanf args "%u" (fun n -> n)
1959 with exn ->
1960 dolog "error processing 'continue' %S: %s"
1961 cmds (Printexc.to_string exn);
1962 exit 1;
1964 state.pagecount <- n;
1965 begin match state.currently with
1966 | Outlining l ->
1967 state.currently <- Idle;
1968 state.outlines <- Array.of_list (List.rev l)
1969 | _ -> ()
1970 end;
1972 let cur, cmds = state.geomcmds in
1973 if String.length cur = 0
1974 then failwith "umpossible";
1976 begin match List.rev cmds with
1977 | [] ->
1978 state.geomcmds <- "", [];
1979 represent ();
1980 | (s, f) :: rest ->
1981 f ();
1982 state.geomcmds <- s, List.rev rest;
1983 end;
1984 if conf.maxwait = None
1985 then G.postRedisplay "continue";
1987 | "title" ->
1988 Wsi.settitle args
1990 | "msg" ->
1991 showtext ' ' args
1993 | "vmsg" ->
1994 if conf.verbose
1995 then showtext ' ' args
1997 | "progress" ->
1998 let progress, text =
2000 Scanf.sscanf args "%f %n"
2001 (fun f pos ->
2002 f, String.sub args pos (String.length args - pos))
2003 with exn ->
2004 dolog "error processing 'progress' %S: %s"
2005 cmds (Printexc.to_string exn);
2006 exit 1;
2008 state.text <- text;
2009 state.progress <- progress;
2010 G.postRedisplay "progress"
2012 | "firstmatch" ->
2013 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2015 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2016 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2017 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2018 with exn ->
2019 dolog "error processing 'firstmatch' %S: %s"
2020 cmds (Printexc.to_string exn);
2021 exit 1;
2023 let y = (getpagey pageno) + truncate y0 in
2024 addnav ();
2025 gotoy y;
2026 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2028 | "match" ->
2029 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2031 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2032 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2033 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2034 with exn ->
2035 dolog "error processing 'match' %S: %s"
2036 cmds (Printexc.to_string exn);
2037 exit 1;
2039 state.rects1 <-
2040 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2042 | "page" ->
2043 let pageopaque, t =
2045 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2046 with exn ->
2047 dolog "error processing 'page' %S: %s"
2048 cmds (Printexc.to_string exn);
2049 exit 1;
2051 begin match state.currently with
2052 | Loading (l, gen) ->
2053 vlog "page %d took %f sec" l.pageno t;
2054 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2055 begin match state.throttle with
2056 | None ->
2057 let preloadedpages =
2058 if conf.preload
2059 then preloadlayout state.layout
2060 else state.layout
2062 let evict () =
2063 let module IntSet =
2064 Set.Make (struct type t = int let compare = (-) end) in
2065 let set =
2066 List.fold_left (fun s l -> IntSet.add l.pageno s)
2067 IntSet.empty preloadedpages
2069 let evictedpages =
2070 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2071 if not (IntSet.mem pageno set)
2072 then (
2073 wcmd "freepage %s" opaque;
2074 key :: accu
2076 else accu
2077 ) state.pagemap []
2079 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2081 evict ();
2082 state.currently <- Idle;
2083 if gen = state.gen
2084 then (
2085 tilepage l.pageno pageopaque state.layout;
2086 load state.layout;
2087 load preloadedpages;
2088 if pagevisible state.layout l.pageno
2089 && layoutready state.layout
2090 then G.postRedisplay "page";
2093 | Some (layout, _, _) ->
2094 state.currently <- Idle;
2095 tilepage l.pageno pageopaque layout;
2096 load state.layout
2097 end;
2099 | _ ->
2100 dolog "Inconsistent loading state";
2101 logcurrently state.currently;
2102 exit 1
2105 | "tile" ->
2106 let (x, y, opaque, size, t) =
2108 Scanf.sscanf args "%u %u %s %u %f"
2109 (fun x y p size t -> (x, y, p, size, t))
2110 with exn ->
2111 dolog "error processing 'tile' %S: %s"
2112 cmds (Printexc.to_string exn);
2113 exit 1;
2115 begin match state.currently with
2116 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2117 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2119 if tilew != conf.tilew || tileh != conf.tileh
2120 then (
2121 wcmd "freetile %s" opaque;
2122 state.currently <- Idle;
2123 load state.layout;
2125 else (
2126 puttileopaque l col row gen cs angle opaque size t;
2127 state.memused <- state.memused + size;
2128 state.uioh#infochanged Memused;
2129 gctiles ();
2130 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2131 opaque, size) state.tilelru;
2133 let layout =
2134 match state.throttle with
2135 | None -> state.layout
2136 | Some (layout, _, _) -> layout
2139 state.currently <- Idle;
2140 if gen = state.gen
2141 && conf.colorspace = cs
2142 && conf.angle = angle
2143 && tilevisible layout l.pageno x y
2144 then conttiling l.pageno pageopaque;
2146 begin match state.throttle with
2147 | None ->
2148 preload state.layout;
2149 if gen = state.gen
2150 && conf.colorspace = cs
2151 && conf.angle = angle
2152 && tilevisible state.layout l.pageno x y
2153 then G.postRedisplay "tile nothrottle";
2155 | Some (layout, y, _) ->
2156 let ready = layoutready layout in
2157 if ready
2158 then (
2159 state.y <- y;
2160 state.layout <- layout;
2161 state.throttle <- None;
2162 G.postRedisplay "throttle";
2164 else load layout;
2165 end;
2168 | _ ->
2169 dolog "Inconsistent tiling state";
2170 logcurrently state.currently;
2171 exit 1
2174 | "pdim" ->
2175 let pdim =
2177 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2178 with exn ->
2179 dolog "error processing 'pdim' %S: %s"
2180 cmds (Printexc.to_string exn);
2181 exit 1;
2183 state.uioh#infochanged Pdim;
2184 state.pdims <- pdim :: state.pdims
2186 | "o" ->
2187 let (l, n, t, h, pos) =
2189 Scanf.sscanf args "%u %u %d %u %n"
2190 (fun l n t h pos -> l, n, t, h, pos)
2191 with exn ->
2192 dolog "error processing 'o' %S: %s"
2193 cmds (Printexc.to_string exn);
2194 exit 1;
2196 let s = String.sub args pos (String.length args - pos) in
2197 let outline = (s, l, (n, float t /. float h)) in
2198 begin match state.currently with
2199 | Outlining outlines ->
2200 state.currently <- Outlining (outline :: outlines)
2201 | Idle ->
2202 state.currently <- Outlining [outline]
2203 | currently ->
2204 dolog "invalid outlining state";
2205 logcurrently currently
2208 | "info" ->
2209 state.docinfo <- (1, args) :: state.docinfo
2211 | "infoend" ->
2212 state.uioh#infochanged Docinfo;
2213 state.docinfo <- List.rev state.docinfo
2215 | _ ->
2216 dolog "unknown cmd `%S'" cmds
2219 let onhist cb =
2220 let rc = cb.rc in
2221 let action = function
2222 | HCprev -> cbget cb ~-1
2223 | HCnext -> cbget cb 1
2224 | HCfirst -> cbget cb ~-(cb.rc)
2225 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2226 and cancel () = cb.rc <- rc
2227 in (action, cancel)
2230 let search pattern forward =
2231 if String.length pattern > 0
2232 then
2233 let pn, py =
2234 match state.layout with
2235 | [] -> 0, 0
2236 | l :: _ ->
2237 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2239 wcmd "search %d %d %d %d,%s\000"
2240 (btod conf.icase) pn py (btod forward) pattern;
2243 let intentry text key =
2244 let c =
2245 if key >= 32 && key < 127
2246 then Char.chr key
2247 else '\000'
2249 match c with
2250 | '0' .. '9' ->
2251 let text = addchar text c in
2252 TEcont text
2254 | _ ->
2255 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2256 TEcont text
2259 let textentry text key =
2260 if key land 0xff00 = 0xff00
2261 then TEcont text
2262 else TEcont (text ^ Wsi.toutf8 key)
2265 let reqlayout angle proportional =
2266 match state.throttle with
2267 | None ->
2268 if nogeomcmds state.geomcmds
2269 then state.anchor <- getanchor ();
2270 conf.angle <- angle mod 360;
2271 if conf.angle != 0
2272 then (
2273 match state.mode with
2274 | LinkNav _ -> state.mode <- View
2275 | _ -> ()
2277 conf.proportional <- proportional;
2278 invalidate "reqlayout"
2279 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2280 | _ -> ()
2283 let settrim trimmargins trimfuzz =
2284 if nogeomcmds state.geomcmds
2285 then state.anchor <- getanchor ();
2286 conf.trimmargins <- trimmargins;
2287 conf.trimfuzz <- trimfuzz;
2288 let x0, y0, x1, y1 = trimfuzz in
2289 invalidate "settrim"
2290 (fun () ->
2291 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2292 Hashtbl.iter (fun _ opaque ->
2293 wcmd "freepage %s" opaque;
2294 ) state.pagemap;
2295 Hashtbl.clear state.pagemap;
2298 let setzoom zoom =
2299 match state.throttle with
2300 | None ->
2301 let zoom = max 0.01 zoom in
2302 if zoom <> conf.zoom
2303 then (
2304 state.prevzoom <- conf.zoom;
2305 conf.zoom <- zoom;
2306 reshape conf.winw conf.winh;
2307 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2310 | Some (layout, y, started) ->
2311 let time =
2312 match conf.maxwait with
2313 | None -> 0.0
2314 | Some t -> t
2316 let dt = now () -. started in
2317 if dt > time
2318 then (
2319 state.y <- y;
2320 load layout;
2324 let setcolumns columns coverA coverB =
2325 if columns < 2
2326 then (
2327 conf.columns <- None;
2328 state.x <- 0;
2329 setzoom 1.0;
2331 else (
2332 conf.columns <- Some ((columns, coverA, coverB), [||]);
2333 conf.zoom <- 1.0;
2335 reshape conf.winw conf.winh;
2338 let enterbirdseye () =
2339 let zoom = float conf.thumbw /. float conf.winw in
2340 let birdseyepageno =
2341 let cy = conf.winh / 2 in
2342 let fold = function
2343 | [] -> 0
2344 | l :: rest ->
2345 let rec fold best = function
2346 | [] -> best.pageno
2347 | l :: rest ->
2348 let d = cy - (l.pagedispy + l.pagevh/2)
2349 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2350 if abs d < abs dbest
2351 then fold l rest
2352 else best.pageno
2353 in fold l rest
2355 fold state.layout
2357 state.mode <- Birdseye (
2358 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2360 conf.zoom <- zoom;
2361 conf.presentation <- false;
2362 conf.interpagespace <- 10;
2363 conf.hlinks <- false;
2364 state.x <- 0;
2365 state.mstate <- Mnone;
2366 conf.maxwait <- None;
2367 conf.columns <- (
2368 match conf.beyecolumns with
2369 | Some c ->
2370 conf.zoom <- 1.0;
2371 Some ((c, 0, 0), [||])
2372 | None -> None
2374 Wsi.setcursor Wsi.CURSOR_INHERIT;
2375 if conf.verbose
2376 then
2377 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2378 (100.0*.zoom)
2379 else
2380 state.text <- ""
2382 reshape conf.winw conf.winh;
2385 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2386 state.mode <- View;
2387 conf.zoom <- c.zoom;
2388 conf.presentation <- c.presentation;
2389 conf.interpagespace <- c.interpagespace;
2390 conf.maxwait <- c.maxwait;
2391 conf.hlinks <- c.hlinks;
2392 conf.beyecolumns <- (
2393 match conf.columns with
2394 | Some ((c, _, _), _) -> Some c
2395 | None -> None
2397 conf.columns <- (
2398 match c.columns with
2399 | Some (c, _) -> Some (c, [||])
2400 | None -> None
2402 state.x <- leftx;
2403 if conf.verbose
2404 then
2405 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2406 (100.0*.conf.zoom)
2408 reshape conf.winw conf.winh;
2409 state.anchor <- if goback then anchor else (pageno, 0.0);
2412 let togglebirdseye () =
2413 match state.mode with
2414 | Birdseye vals -> leavebirdseye vals true
2415 | View -> enterbirdseye ()
2416 | _ -> ()
2419 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2420 let pageno = max 0 (pageno - incr) in
2421 let rec loop = function
2422 | [] -> gotopage1 pageno 0
2423 | l :: _ when l.pageno = pageno ->
2424 if l.pagedispy >= 0 && l.pagey = 0
2425 then G.postRedisplay "upbirdseye"
2426 else gotopage1 pageno 0
2427 | _ :: rest -> loop rest
2429 loop state.layout;
2430 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2433 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2434 let pageno = min (state.pagecount - 1) (pageno + incr) in
2435 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2436 let rec loop = function
2437 | [] ->
2438 let y, h = getpageyh pageno in
2439 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2440 gotoy (clamp dy)
2441 | l :: _ when l.pageno = pageno ->
2442 if l.pagevh != l.pageh
2443 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2444 else G.postRedisplay "downbirdseye"
2445 | _ :: rest -> loop rest
2447 loop state.layout
2450 let optentry mode _ key =
2451 let btos b = if b then "on" else "off" in
2452 if key >= 32 && key < 127
2453 then
2454 let c = Char.chr key in
2455 match c with
2456 | 's' ->
2457 let ondone s =
2458 try conf.scrollstep <- int_of_string s with exc ->
2459 state.text <- Printf.sprintf "bad integer `%s': %s"
2460 s (Printexc.to_string exc)
2462 TEswitch ("scroll step: ", "", None, intentry, ondone)
2464 | 'A' ->
2465 let ondone s =
2467 conf.autoscrollstep <- int_of_string s;
2468 if state.autoscroll <> None
2469 then state.autoscroll <- Some conf.autoscrollstep
2470 with exc ->
2471 state.text <- Printf.sprintf "bad integer `%s': %s"
2472 s (Printexc.to_string exc)
2474 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2476 | 'C' ->
2477 let ondone s =
2479 let n, a, b = columns_of_string s in
2480 setcolumns n a b;
2481 with exc ->
2482 state.text <- Printf.sprintf "bad columns `%s': %s"
2483 s (Printexc.to_string exc)
2485 TEswitch ("columns: ", "", None, textentry, ondone)
2487 | 'Z' ->
2488 let ondone s =
2490 let zoom = float (int_of_string s) /. 100.0 in
2491 setzoom zoom
2492 with exc ->
2493 state.text <- Printf.sprintf "bad integer `%s': %s"
2494 s (Printexc.to_string exc)
2496 TEswitch ("zoom: ", "", None, intentry, ondone)
2498 | 't' ->
2499 let ondone s =
2501 conf.thumbw <- bound (int_of_string s) 2 4096;
2502 state.text <-
2503 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2504 begin match mode with
2505 | Birdseye beye ->
2506 leavebirdseye beye false;
2507 enterbirdseye ();
2508 | _ -> ();
2510 with exc ->
2511 state.text <- Printf.sprintf "bad integer `%s': %s"
2512 s (Printexc.to_string exc)
2514 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2516 | 'R' ->
2517 let ondone s =
2518 match try
2519 Some (int_of_string s)
2520 with exc ->
2521 state.text <- Printf.sprintf "bad integer `%s': %s"
2522 s (Printexc.to_string exc);
2523 None
2524 with
2525 | Some angle -> reqlayout angle conf.proportional
2526 | None -> ()
2528 TEswitch ("rotation: ", "", None, intentry, ondone)
2530 | 'i' ->
2531 conf.icase <- not conf.icase;
2532 TEdone ("case insensitive search " ^ (btos conf.icase))
2534 | 'p' ->
2535 conf.preload <- not conf.preload;
2536 gotoy state.y;
2537 TEdone ("preload " ^ (btos conf.preload))
2539 | 'v' ->
2540 conf.verbose <- not conf.verbose;
2541 TEdone ("verbose " ^ (btos conf.verbose))
2543 | 'd' ->
2544 conf.debug <- not conf.debug;
2545 TEdone ("debug " ^ (btos conf.debug))
2547 | 'h' ->
2548 conf.maxhfit <- not conf.maxhfit;
2549 state.maxy <-
2550 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2551 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2553 | 'c' ->
2554 conf.crophack <- not conf.crophack;
2555 TEdone ("crophack " ^ btos conf.crophack)
2557 | 'a' ->
2558 let s =
2559 match conf.maxwait with
2560 | None ->
2561 conf.maxwait <- Some infinity;
2562 "always wait for page to complete"
2563 | Some _ ->
2564 conf.maxwait <- None;
2565 "show placeholder if page is not ready"
2567 TEdone s
2569 | 'f' ->
2570 conf.underinfo <- not conf.underinfo;
2571 TEdone ("underinfo " ^ btos conf.underinfo)
2573 | 'P' ->
2574 conf.savebmarks <- not conf.savebmarks;
2575 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2577 | 'S' ->
2578 let ondone s =
2580 let pageno, py =
2581 match state.layout with
2582 | [] -> 0, 0
2583 | l :: _ ->
2584 l.pageno, l.pagey
2586 conf.interpagespace <- int_of_string s;
2587 state.maxy <- calcheight ();
2588 let y = getpagey pageno in
2589 gotoy (y + py)
2590 with exc ->
2591 state.text <- Printf.sprintf "bad integer `%s': %s"
2592 s (Printexc.to_string exc)
2594 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2596 | 'l' ->
2597 reqlayout conf.angle (not conf.proportional);
2598 TEdone ("proportional display " ^ btos conf.proportional)
2600 | 'T' ->
2601 settrim (not conf.trimmargins) conf.trimfuzz;
2602 TEdone ("trim margins " ^ btos conf.trimmargins)
2604 | 'I' ->
2605 conf.invert <- not conf.invert;
2606 TEdone ("invert colors " ^ btos conf.invert)
2608 | 'x' ->
2609 let ondone s =
2610 cbput state.hists.sel s;
2611 conf.selcmd <- s;
2613 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2614 textentry, ondone)
2616 | _ ->
2617 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2618 TEstop
2619 else
2620 TEcont state.text
2623 class type lvsource = object
2624 method getitemcount : int
2625 method getitem : int -> (string * int)
2626 method hasaction : int -> bool
2627 method exit :
2628 uioh:uioh ->
2629 cancel:bool ->
2630 active:int ->
2631 first:int ->
2632 pan:int ->
2633 qsearch:string ->
2634 uioh option
2635 method getactive : int
2636 method getfirst : int
2637 method getqsearch : string
2638 method setqsearch : string -> unit
2639 method getpan : int
2640 end;;
2642 class virtual lvsourcebase = object
2643 val mutable m_active = 0
2644 val mutable m_first = 0
2645 val mutable m_qsearch = ""
2646 val mutable m_pan = 0
2647 method getactive = m_active
2648 method getfirst = m_first
2649 method getqsearch = m_qsearch
2650 method getpan = m_pan
2651 method setqsearch s = m_qsearch <- s
2652 end;;
2654 let withoutlastutf8 s =
2655 let len = String.length s in
2656 if len = 0
2657 then s
2658 else
2659 let rec find pos =
2660 if pos = 0
2661 then pos
2662 else
2663 let b = Char.code s.[pos] in
2664 if b land 0b110000 = 0b11000000
2665 then find (pos-1)
2666 else pos-1
2668 let first =
2669 if Char.code s.[len-1] land 0x80 = 0
2670 then len-1
2671 else find (len-1)
2673 String.sub s 0 first;
2676 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2677 let enttext te =
2678 state.mode <- Textentry (te, onleave);
2679 state.text <- "";
2680 enttext ();
2681 G.postRedisplay "textentrykeyboard enttext";
2683 let histaction cmd =
2684 match opthist with
2685 | None -> ()
2686 | Some (action, _) ->
2687 state.mode <- Textentry (
2688 (c, action cmd, opthist, onkey, ondone), onleave
2690 G.postRedisplay "textentry histaction"
2692 match key with
2693 | 0xff08 -> (* backspace *)
2694 let s = withoutlastutf8 text in
2695 let len = String.length s in
2696 if len = 0
2697 then (
2698 onleave Cancel;
2699 G.postRedisplay "textentrykeyboard after cancel";
2701 else (
2702 enttext (c, s, opthist, onkey, ondone)
2705 | 0xff0d ->
2706 ondone text;
2707 onleave Confirm;
2708 G.postRedisplay "textentrykeyboard after confirm"
2710 | 0xff52 -> histaction HCprev
2711 | 0xff54 -> histaction HCnext
2712 | 0xff50 -> histaction HCfirst
2713 | 0xff57 -> histaction HClast
2715 | 0xff1b -> (* escape*)
2716 if String.length text = 0
2717 then (
2718 begin match opthist with
2719 | None -> ()
2720 | Some (_, onhistcancel) -> onhistcancel ()
2721 end;
2722 onleave Cancel;
2723 state.text <- "";
2724 G.postRedisplay "textentrykeyboard after cancel2"
2726 else (
2727 enttext (c, "", opthist, onkey, ondone)
2730 | 0xff9f | 0xffff -> () (* delete *)
2732 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2733 begin match onkey text key with
2734 | TEdone text ->
2735 ondone text;
2736 onleave Confirm;
2737 G.postRedisplay "textentrykeyboard after confirm2";
2739 | TEcont text ->
2740 enttext (c, text, opthist, onkey, ondone);
2742 | TEstop ->
2743 onleave Cancel;
2744 G.postRedisplay "textentrykeyboard after cancel3"
2746 | TEswitch te ->
2747 state.mode <- Textentry (te, onleave);
2748 G.postRedisplay "textentrykeyboard switch";
2749 end;
2751 | _ ->
2752 vlog "unhandled key %s" (Wsi.keyname key)
2755 let firstof first active =
2756 if first > active || abs (first - active) > fstate.maxrows - 1
2757 then max 0 (active - (fstate.maxrows/2))
2758 else first
2761 let calcfirst first active =
2762 if active > first
2763 then
2764 let rows = active - first in
2765 if rows > fstate.maxrows then active - fstate.maxrows else first
2766 else active
2769 let scrollph y maxy =
2770 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2771 let sh = float conf.winh /. sh in
2772 let sh = max sh (float conf.scrollh) in
2774 let percent =
2775 if y = state.maxy
2776 then 1.0
2777 else float y /. float maxy
2779 let position = (float conf.winh -. sh) *. percent in
2781 let position =
2782 if position +. sh > float conf.winh
2783 then float conf.winh -. sh
2784 else position
2786 position, sh;
2789 let coe s = (s :> uioh);;
2791 class listview ~(source:lvsource) ~trusted ~modehash =
2792 object (self)
2793 val m_pan = source#getpan
2794 val m_first = source#getfirst
2795 val m_active = source#getactive
2796 val m_qsearch = source#getqsearch
2797 val m_prev_uioh = state.uioh
2799 method private elemunder y =
2800 let n = y / (fstate.fontsize+1) in
2801 if m_first + n < source#getitemcount
2802 then (
2803 if source#hasaction (m_first + n)
2804 then Some (m_first + n)
2805 else None
2807 else None
2809 method display =
2810 Gl.enable `blend;
2811 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2812 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2813 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2814 GlDraw.color (1., 1., 1.);
2815 Gl.enable `texture_2d;
2816 let fs = fstate.fontsize in
2817 let nfs = fs + 1 in
2818 let ww = fstate.wwidth in
2819 let tabw = 30.0*.ww in
2820 let itemcount = source#getitemcount in
2821 let rec loop row =
2822 if (row - m_first) * nfs > conf.winh
2823 then ()
2824 else (
2825 if row >= 0 && row < itemcount
2826 then (
2827 let (s, level) = source#getitem row in
2828 let y = (row - m_first) * nfs in
2829 let x = 5.0 +. float (level + m_pan) *. ww in
2830 if row = m_active
2831 then (
2832 Gl.disable `texture_2d;
2833 GlDraw.polygon_mode `both `line;
2834 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2835 GlDraw.rect (1., float (y + 1))
2836 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2837 GlDraw.polygon_mode `both `fill;
2838 GlDraw.color (1., 1., 1.);
2839 Gl.enable `texture_2d;
2842 let drawtabularstring s =
2843 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2844 if trusted
2845 then
2846 let tabpos = try String.index s '\t' with Not_found -> -1 in
2847 if tabpos > 0
2848 then
2849 let len = String.length s - tabpos - 1 in
2850 let s1 = String.sub s 0 tabpos
2851 and s2 = String.sub s (tabpos + 1) len in
2852 let nx = drawstr x s1 in
2853 let sw = nx -. x in
2854 let x = x +. (max tabw sw) in
2855 drawstr x s2
2856 else
2857 drawstr x s
2858 else
2859 drawstr x s
2861 let _ = drawtabularstring s in
2862 loop (row+1)
2866 loop m_first;
2867 Gl.disable `blend;
2868 Gl.disable `texture_2d;
2870 method updownlevel incr =
2871 let len = source#getitemcount in
2872 let curlevel =
2873 if m_active >= 0 && m_active < len
2874 then snd (source#getitem m_active)
2875 else -1
2877 let rec flow i =
2878 if i = len then i-1 else if i = -1 then 0 else
2879 let _, l = source#getitem i in
2880 if l != curlevel then i else flow (i+incr)
2882 let active = flow m_active in
2883 let first = calcfirst m_first active in
2884 G.postRedisplay "outline updownlevel";
2885 {< m_active = active; m_first = first >}
2887 method private key1 key mask =
2888 let set1 active first qsearch =
2889 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2891 let search active pattern incr =
2892 let dosearch re =
2893 let rec loop n =
2894 if n >= 0 && n < source#getitemcount
2895 then (
2896 let s, _ = source#getitem n in
2898 (try ignore (Str.search_forward re s 0); true
2899 with Not_found -> false)
2900 then Some n
2901 else loop (n + incr)
2903 else None
2905 loop active
2908 let re = Str.regexp_case_fold pattern in
2909 dosearch re
2910 with Failure s ->
2911 state.text <- s;
2912 None
2914 let itemcount = source#getitemcount in
2915 let find start incr =
2916 let rec find i =
2917 if i = -1 || i = itemcount
2918 then -1
2919 else (
2920 if source#hasaction i
2921 then i
2922 else find (i + incr)
2925 find start
2927 let set active first =
2928 let first = bound first 0 (itemcount - fstate.maxrows) in
2929 state.text <- "";
2930 coe {< m_active = active; m_first = first >}
2932 let navigate incr =
2933 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2934 let active, first =
2935 let incr1 = if incr > 0 then 1 else -1 in
2936 if isvisible m_first m_active
2937 then
2938 let next =
2939 let next = m_active + incr in
2940 let next =
2941 if next < 0 || next >= itemcount
2942 then -1
2943 else find next incr1
2945 if next = -1 || abs (m_active - next) > fstate.maxrows
2946 then -1
2947 else next
2949 if next = -1
2950 then
2951 let first = m_first + incr in
2952 let first = bound first 0 (itemcount - 1) in
2953 let next =
2954 let next = m_active + incr in
2955 let next = bound next 0 (itemcount - 1) in
2956 find next ~-incr1
2958 let active = if next = -1 then m_active else next in
2959 active, first
2960 else
2961 let first = min next m_first in
2962 let first =
2963 if abs (next - first) > fstate.maxrows
2964 then first + incr
2965 else first
2967 next, first
2968 else
2969 let first = m_first + incr in
2970 let first = bound first 0 (itemcount - 1) in
2971 let active =
2972 let next = m_active + incr in
2973 let next = bound next 0 (itemcount - 1) in
2974 let next = find next incr1 in
2975 let active =
2976 if next = -1 || abs (m_active - first) > fstate.maxrows
2977 then (
2978 let active = if m_active = -1 then next else m_active in
2979 active
2981 else next
2983 if isvisible first active
2984 then active
2985 else -1
2987 active, first
2989 G.postRedisplay "listview navigate";
2990 set active first;
2992 match key with
2993 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
2994 let incr = if key = 0x72 then -1 else 1 in
2995 let active, first =
2996 match search (m_active + incr) m_qsearch incr with
2997 | None ->
2998 state.text <- m_qsearch ^ " [not found]";
2999 m_active, m_first
3000 | Some active ->
3001 state.text <- m_qsearch;
3002 active, firstof m_first active
3004 G.postRedisplay "listview ctrl-r/s";
3005 set1 active first m_qsearch;
3007 | 0xff08 -> (* backspace *)
3008 if String.length m_qsearch = 0
3009 then coe self
3010 else (
3011 let qsearch = withoutlastutf8 m_qsearch in
3012 let len = String.length qsearch in
3013 if len = 0
3014 then (
3015 state.text <- "";
3016 G.postRedisplay "listview empty qsearch";
3017 set1 m_active m_first "";
3019 else
3020 let active, first =
3021 match search m_active qsearch ~-1 with
3022 | None ->
3023 state.text <- qsearch ^ " [not found]";
3024 m_active, m_first
3025 | Some active ->
3026 state.text <- qsearch;
3027 active, firstof m_first active
3029 G.postRedisplay "listview backspace qsearch";
3030 set1 active first qsearch
3033 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3034 let pattern = m_qsearch ^ Wsi.toutf8 key in
3035 let active, first =
3036 match search m_active pattern 1 with
3037 | None ->
3038 state.text <- pattern ^ " [not found]";
3039 m_active, m_first
3040 | Some active ->
3041 state.text <- pattern;
3042 active, firstof m_first active
3044 G.postRedisplay "listview qsearch add";
3045 set1 active first pattern;
3047 | 0xff1b -> (* escape *)
3048 state.text <- "";
3049 if String.length m_qsearch = 0
3050 then (
3051 G.postRedisplay "list view escape";
3052 begin
3053 match
3054 source#exit (coe self) true m_active m_first m_pan m_qsearch
3055 with
3056 | None -> m_prev_uioh
3057 | Some uioh -> uioh
3060 else (
3061 G.postRedisplay "list view kill qsearch";
3062 source#setqsearch "";
3063 coe {< m_qsearch = "" >}
3066 | 0xff0d -> (* return *)
3067 state.text <- "";
3068 let self = {< m_qsearch = "" >} in
3069 source#setqsearch "";
3070 let opt =
3071 G.postRedisplay "listview enter";
3072 if m_active >= 0 && m_active < source#getitemcount
3073 then (
3074 source#exit (coe self) false m_active m_first m_pan "";
3076 else (
3077 source#exit (coe self) true m_active m_first m_pan "";
3080 begin match opt with
3081 | None -> m_prev_uioh
3082 | Some uioh -> uioh
3085 | 0xff9f | 0xffff -> (* delete *)
3086 coe self
3088 | 0xff52 -> navigate ~-1 (* up *)
3089 | 0xff54 -> navigate 1 (* down *)
3090 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3091 | 0xff56 -> navigate fstate.maxrows (* next *)
3093 | 0xff53 -> (* right *)
3094 state.text <- "";
3095 G.postRedisplay "listview right";
3096 coe {< m_pan = m_pan - 1 >}
3098 | 0xff51 -> (* left *)
3099 state.text <- "";
3100 G.postRedisplay "listview left";
3101 coe {< m_pan = m_pan + 1 >}
3103 | 0xff50 -> (* home *)
3104 let active = find 0 1 in
3105 G.postRedisplay "listview home";
3106 set active 0;
3108 | 0xff57 -> (* end *)
3109 let first = max 0 (itemcount - fstate.maxrows) in
3110 let active = find (itemcount - 1) ~-1 in
3111 G.postRedisplay "listview end";
3112 set active first;
3114 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3115 coe self
3117 | _ ->
3118 dolog "listview unknown key %#x" key; coe self
3120 method key key mask =
3121 match state.mode with
3122 | Textentry te -> textentrykeyboard key mask te; coe self
3123 | _ -> self#key1 key mask
3125 method button button down x y _ =
3126 let opt =
3127 match button with
3128 | 1 when x > conf.winw - conf.scrollbw ->
3129 G.postRedisplay "listview scroll";
3130 if down
3131 then
3132 let _, position, sh = self#scrollph in
3133 if y > truncate position && y < truncate (position +. sh)
3134 then (
3135 state.mstate <- Mscrolly;
3136 Some (coe self)
3138 else
3139 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3140 let first = truncate (s *. float source#getitemcount) in
3141 let first = min source#getitemcount first in
3142 Some (coe {< m_first = first; m_active = first >})
3143 else (
3144 state.mstate <- Mnone;
3145 Some (coe self);
3147 | 1 when not down ->
3148 begin match self#elemunder y with
3149 | Some n ->
3150 G.postRedisplay "listview click";
3151 source#exit
3152 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3153 | _ ->
3154 Some (coe self)
3156 | n when (n == 4 || n == 5) && not down ->
3157 let len = source#getitemcount in
3158 let first =
3159 if n = 5 && m_first + fstate.maxrows >= len
3160 then
3161 m_first
3162 else
3163 let first = m_first + (if n == 4 then -1 else 1) in
3164 bound first 0 (len - 1)
3166 G.postRedisplay "listview wheel";
3167 Some (coe {< m_first = first >})
3168 | _ ->
3169 Some (coe self)
3171 match opt with
3172 | None -> m_prev_uioh
3173 | Some uioh -> uioh
3175 method motion _ y =
3176 match state.mstate with
3177 | Mscrolly ->
3178 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3179 let first = truncate (s *. float source#getitemcount) in
3180 let first = min source#getitemcount first in
3181 G.postRedisplay "listview motion";
3182 coe {< m_first = first; m_active = first >}
3183 | _ -> coe self
3185 method pmotion x y =
3186 if x < conf.winw - conf.scrollbw
3187 then
3188 let n =
3189 match self#elemunder y with
3190 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3191 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3193 let o =
3194 if n != m_active
3195 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3196 else self
3198 coe o
3199 else (
3200 Wsi.setcursor Wsi.CURSOR_INHERIT;
3201 coe self
3204 method infochanged _ = ()
3206 method scrollpw = (0, 0.0, 0.0)
3207 method scrollph =
3208 let nfs = fstate.fontsize + 1 in
3209 let y = m_first * nfs in
3210 let itemcount = source#getitemcount in
3211 let maxi = max 0 (itemcount - fstate.maxrows) in
3212 let maxy = maxi * nfs in
3213 let p, h = scrollph y maxy in
3214 conf.scrollbw, p, h
3216 method modehash = modehash
3217 end;;
3219 class outlinelistview ~source =
3220 object (self)
3221 inherit listview
3222 ~source:(source :> lvsource)
3223 ~trusted:false
3224 ~modehash:(findkeyhash conf "outline")
3225 as super
3227 method key key mask =
3228 let calcfirst first active =
3229 if active > first
3230 then
3231 let rows = active - first in
3232 if rows > fstate.maxrows then active - fstate.maxrows else first
3233 else active
3235 let navigate incr =
3236 let active = m_active + incr in
3237 let active = bound active 0 (source#getitemcount - 1) in
3238 let first = calcfirst m_first active in
3239 G.postRedisplay "outline navigate";
3240 coe {< m_active = active; m_first = first >}
3242 let ctrl = Wsi.withctrl mask in
3243 match key with
3244 | 110 when ctrl -> (* ctrl-n *)
3245 source#narrow m_qsearch;
3246 G.postRedisplay "outline ctrl-n";
3247 coe {< m_first = 0; m_active = 0 >}
3249 | 117 when ctrl -> (* ctrl-u *)
3250 source#denarrow;
3251 G.postRedisplay "outline ctrl-u";
3252 state.text <- "";
3253 coe {< m_first = 0; m_active = 0 >}
3255 | 108 when ctrl -> (* ctrl-l *)
3256 let first = m_active - (fstate.maxrows / 2) in
3257 G.postRedisplay "outline ctrl-l";
3258 coe {< m_first = first >}
3260 | 0xff9f | 0xffff -> (* delete *)
3261 source#remove m_active;
3262 G.postRedisplay "outline delete";
3263 let active = max 0 (m_active-1) in
3264 coe {< m_first = firstof m_first active;
3265 m_active = active >}
3267 | 0xff52 -> navigate ~-1 (* up *)
3268 | 0xff54 -> navigate 1 (* down *)
3269 | 0xff55 -> (* prior *)
3270 navigate ~-(fstate.maxrows)
3271 | 0xff56 -> (* next *)
3272 navigate fstate.maxrows
3274 | 0xff53 -> (* [ctrl-]right *)
3275 let o =
3276 if ctrl
3277 then (
3278 G.postRedisplay "outline ctrl right";
3279 {< m_pan = m_pan + 1 >}
3281 else self#updownlevel 1
3283 coe o
3285 | 0xff51 -> (* [ctrl-]left *)
3286 let o =
3287 if ctrl
3288 then (
3289 G.postRedisplay "outline ctrl left";
3290 {< m_pan = m_pan - 1 >}
3292 else self#updownlevel ~-1
3294 coe o
3296 | 0xff50 -> (* home *)
3297 G.postRedisplay "outline home";
3298 coe {< m_first = 0; m_active = 0 >}
3300 | 0xff57 -> (* end *)
3301 let active = source#getitemcount - 1 in
3302 let first = max 0 (active - fstate.maxrows) in
3303 G.postRedisplay "outline end";
3304 coe {< m_active = active; m_first = first >}
3306 | _ -> super#key key mask
3309 let outlinesource usebookmarks =
3310 let empty = [||] in
3311 (object
3312 inherit lvsourcebase
3313 val mutable m_items = empty
3314 val mutable m_orig_items = empty
3315 val mutable m_prev_items = empty
3316 val mutable m_narrow_pattern = ""
3317 val mutable m_hadremovals = false
3319 method getitemcount =
3320 Array.length m_items + (if m_hadremovals then 1 else 0)
3322 method getitem n =
3323 if n == Array.length m_items && m_hadremovals
3324 then
3325 ("[Confirm removal]", 0)
3326 else
3327 let s, n, _ = m_items.(n) in
3328 (s, n)
3330 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3331 ignore (uioh, first, qsearch);
3332 let confrimremoval = m_hadremovals && active = Array.length m_items in
3333 let items =
3334 if String.length m_narrow_pattern = 0
3335 then m_orig_items
3336 else m_items
3338 if not cancel
3339 then (
3340 if not confrimremoval
3341 then(
3342 let _, _, anchor = m_items.(active) in
3343 gotoanchor anchor;
3344 m_items <- items;
3346 else (
3347 state.bookmarks <- Array.to_list m_items;
3348 m_orig_items <- m_items;
3351 else m_items <- items;
3352 m_pan <- pan;
3353 None
3355 method hasaction _ = true
3357 method greetmsg =
3358 if Array.length m_items != Array.length m_orig_items
3359 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3360 else ""
3362 method narrow pattern =
3363 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3364 match reopt with
3365 | None -> ()
3366 | Some re ->
3367 let rec loop accu n =
3368 if n = -1
3369 then (
3370 m_narrow_pattern <- pattern;
3371 m_items <- Array.of_list accu
3373 else
3374 let (s, _, _) as o = m_items.(n) in
3375 let accu =
3376 if (try ignore (Str.search_forward re s 0); true
3377 with Not_found -> false)
3378 then o :: accu
3379 else accu
3381 loop accu (n-1)
3383 loop [] (Array.length m_items - 1)
3385 method denarrow =
3386 m_orig_items <- (
3387 if usebookmarks
3388 then Array.of_list state.bookmarks
3389 else state.outlines
3391 m_items <- m_orig_items
3393 method remove m =
3394 if usebookmarks
3395 then
3396 if m >= 0 && m < Array.length m_items
3397 then (
3398 m_hadremovals <- true;
3399 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3400 let n = if n >= m then n+1 else n in
3401 m_items.(n)
3405 method reset anchor items =
3406 m_hadremovals <- false;
3407 if m_orig_items == empty || m_prev_items != items
3408 then (
3409 m_orig_items <- items;
3410 if String.length m_narrow_pattern = 0
3411 then m_items <- items;
3413 m_prev_items <- items;
3414 let rely = getanchory anchor in
3415 let active =
3416 let rec loop n best bestd =
3417 if n = Array.length m_items
3418 then best
3419 else
3420 let (_, _, anchor) = m_items.(n) in
3421 let orely = getanchory anchor in
3422 let d = abs (orely - rely) in
3423 if d < bestd
3424 then loop (n+1) n d
3425 else loop (n+1) best bestd
3427 loop 0 ~-1 max_int
3429 m_active <- active;
3430 m_first <- firstof m_first active
3431 end)
3434 let enterselector usebookmarks =
3435 let source = outlinesource usebookmarks in
3436 fun errmsg ->
3437 let outlines =
3438 if usebookmarks
3439 then Array.of_list state.bookmarks
3440 else state.outlines
3442 if Array.length outlines = 0
3443 then (
3444 showtext ' ' errmsg;
3446 else (
3447 state.text <- source#greetmsg;
3448 Wsi.setcursor Wsi.CURSOR_INHERIT;
3449 let anchor = getanchor () in
3450 source#reset anchor outlines;
3451 state.uioh <- coe (new outlinelistview ~source);
3452 G.postRedisplay "enter selector";
3456 let enteroutlinemode =
3457 let f = enterselector false in
3458 fun ()-> f "Document has no outline";
3461 let enterbookmarkmode =
3462 let f = enterselector true in
3463 fun () -> f "Document has no bookmarks (yet)";
3466 let color_of_string s =
3467 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3468 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3472 let color_to_string (r, g, b) =
3473 let r = truncate (r *. 256.0)
3474 and g = truncate (g *. 256.0)
3475 and b = truncate (b *. 256.0) in
3476 Printf.sprintf "%d/%d/%d" r g b
3479 let irect_of_string s =
3480 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3483 let irect_to_string (x0,y0,x1,y1) =
3484 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3487 let makecheckers () =
3488 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3489 following to say:
3490 converted by Issac Trotts. July 25, 2002 *)
3491 let image_height = 64
3492 and image_width = 64 in
3494 let make_image () =
3495 let image =
3496 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3498 for i = 0 to image_width - 1 do
3499 for j = 0 to image_height - 1 do
3500 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3501 (if (i land 8 ) lxor (j land 8) = 0
3502 then [|255;255;255|] else [|200;200;200|])
3503 done
3504 done;
3505 image
3507 let image = make_image () in
3508 let id = GlTex.gen_texture () in
3509 GlTex.bind_texture `texture_2d id;
3510 GlPix.store (`unpack_alignment 1);
3511 GlTex.image2d image;
3512 List.iter (GlTex.parameter ~target:`texture_2d)
3513 [ `wrap_s `repeat;
3514 `wrap_t `repeat;
3515 `mag_filter `nearest;
3516 `min_filter `nearest ];
3520 let setcheckers enabled =
3521 match state.texid with
3522 | None ->
3523 if enabled then state.texid <- Some (makecheckers ())
3525 | Some texid ->
3526 if not enabled
3527 then (
3528 GlTex.delete_texture texid;
3529 state.texid <- None;
3533 let int_of_string_with_suffix s =
3534 let l = String.length s in
3535 let s1, shift =
3536 if l > 1
3537 then
3538 let suffix = Char.lowercase s.[l-1] in
3539 match suffix with
3540 | 'k' -> String.sub s 0 (l-1), 10
3541 | 'm' -> String.sub s 0 (l-1), 20
3542 | 'g' -> String.sub s 0 (l-1), 30
3543 | _ -> s, 0
3544 else s, 0
3546 let n = int_of_string s1 in
3547 let m = n lsl shift in
3548 if m < 0 || m < n
3549 then raise (Failure "value too large")
3550 else m
3553 let string_with_suffix_of_int n =
3554 if n = 0
3555 then "0"
3556 else
3557 let n, s =
3558 if n = 0
3559 then 0, ""
3560 else (
3561 if n land ((1 lsl 20) - 1) = 0
3562 then n lsr 20, "M"
3563 else (
3564 if n land ((1 lsl 10) - 1) = 0
3565 then n lsr 10, "K"
3566 else n, ""
3570 let rec loop s n =
3571 let h = n mod 1000 in
3572 let n = n / 1000 in
3573 if n = 0
3574 then string_of_int h ^ s
3575 else (
3576 let s = Printf.sprintf "_%03d%s" h s in
3577 loop s n
3580 loop "" n ^ s;
3583 let defghyllscroll = (40, 8, 32);;
3584 let ghyllscroll_of_string s =
3585 let (n, a, b) as nab =
3586 if s = "default"
3587 then defghyllscroll
3588 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3590 if n <= a || n <= b || a >= b
3591 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3592 nab;
3595 let ghyllscroll_to_string ((n, a, b) as nab) =
3596 if nab = defghyllscroll
3597 then "default"
3598 else Printf.sprintf "%d,%d,%d" n a b;
3601 let describe_location () =
3602 let f (fn, _) l =
3603 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3605 let fn, ln = List.fold_left f (-1, -1) state.layout in
3606 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3607 let percent =
3608 if maxy <= 0
3609 then 100.
3610 else (100. *. (float state.y /. float maxy))
3612 if fn = ln
3613 then
3614 Printf.sprintf "page %d of %d [%.2f%%]"
3615 (fn+1) state.pagecount percent
3616 else
3617 Printf.sprintf
3618 "pages %d-%d of %d [%.2f%%]"
3619 (fn+1) (ln+1) state.pagecount percent
3622 let enterinfomode =
3623 let btos b = if b then "\xe2\x88\x9a" else "" in
3624 let showextended = ref false in
3625 let leave mode = function
3626 | Confirm -> state.mode <- mode
3627 | Cancel -> state.mode <- mode in
3628 let src =
3629 (object
3630 val mutable m_first_time = true
3631 val mutable m_l = []
3632 val mutable m_a = [||]
3633 val mutable m_prev_uioh = nouioh
3634 val mutable m_prev_mode = View
3636 inherit lvsourcebase
3638 method reset prev_mode prev_uioh =
3639 m_a <- Array.of_list (List.rev m_l);
3640 m_l <- [];
3641 m_prev_mode <- prev_mode;
3642 m_prev_uioh <- prev_uioh;
3643 if m_first_time
3644 then (
3645 let rec loop n =
3646 if n >= Array.length m_a
3647 then ()
3648 else
3649 match m_a.(n) with
3650 | _, _, _, Action _ -> m_active <- n
3651 | _ -> loop (n+1)
3653 loop 0;
3654 m_first_time <- false;
3657 method int name get set =
3658 m_l <-
3659 (name, `int get, 1, Action (
3660 fun u ->
3661 let ondone s =
3662 try set (int_of_string s)
3663 with exn ->
3664 state.text <- Printf.sprintf "bad integer `%s': %s"
3665 s (Printexc.to_string exn)
3667 state.text <- "";
3668 let te = name ^ ": ", "", None, intentry, ondone in
3669 state.mode <- Textentry (te, leave m_prev_mode);
3671 )) :: m_l
3673 method int_with_suffix name get set =
3674 m_l <-
3675 (name, `intws get, 1, Action (
3676 fun u ->
3677 let ondone s =
3678 try set (int_of_string_with_suffix s)
3679 with exn ->
3680 state.text <- Printf.sprintf "bad integer `%s': %s"
3681 s (Printexc.to_string exn)
3683 state.text <- "";
3684 let te =
3685 name ^ ": ", "", None, intentry_with_suffix, ondone
3687 state.mode <- Textentry (te, leave m_prev_mode);
3689 )) :: m_l
3691 method bool ?(offset=1) ?(btos=btos) name get set =
3692 m_l <-
3693 (name, `bool (btos, get), offset, Action (
3694 fun u ->
3695 let v = get () in
3696 set (not v);
3698 )) :: m_l
3700 method color name get set =
3701 m_l <-
3702 (name, `color get, 1, Action (
3703 fun u ->
3704 let invalid = (nan, nan, nan) in
3705 let ondone s =
3706 let c =
3707 try color_of_string s
3708 with exn ->
3709 state.text <- Printf.sprintf "bad color `%s': %s"
3710 s (Printexc.to_string exn);
3711 invalid
3713 if c <> invalid
3714 then set c;
3716 let te = name ^ ": ", "", None, textentry, ondone in
3717 state.text <- color_to_string (get ());
3718 state.mode <- Textentry (te, leave m_prev_mode);
3720 )) :: m_l
3722 method string name get set =
3723 m_l <-
3724 (name, `string get, 1, Action (
3725 fun u ->
3726 let ondone s = set s in
3727 let te = name ^ ": ", "", None, textentry, ondone in
3728 state.mode <- Textentry (te, leave m_prev_mode);
3730 )) :: m_l
3732 method colorspace name get set =
3733 m_l <-
3734 (name, `string get, 1, Action (
3735 fun _ ->
3736 let source =
3737 let vals = [| "rgb"; "bgr"; "gray" |] in
3738 (object
3739 inherit lvsourcebase
3741 initializer
3742 m_active <- int_of_colorspace conf.colorspace;
3743 m_first <- 0;
3745 method getitemcount = Array.length vals
3746 method getitem n = (vals.(n), 0)
3747 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3748 ignore (uioh, first, pan, qsearch);
3749 if not cancel then set active;
3750 None
3751 method hasaction _ = true
3752 end)
3754 state.text <- "";
3755 let modehash = findkeyhash conf "info" in
3756 coe (new listview ~source ~trusted:true ~modehash)
3757 )) :: m_l
3759 method caption s offset =
3760 m_l <- (s, `empty, offset, Noaction) :: m_l
3762 method caption2 s f offset =
3763 m_l <- (s, `string f, offset, Noaction) :: m_l
3765 method getitemcount = Array.length m_a
3767 method getitem n =
3768 let tostr = function
3769 | `int f -> string_of_int (f ())
3770 | `intws f -> string_with_suffix_of_int (f ())
3771 | `string f -> f ()
3772 | `color f -> color_to_string (f ())
3773 | `bool (btos, f) -> btos (f ())
3774 | `empty -> ""
3776 let name, t, offset, _ = m_a.(n) in
3777 ((let s = tostr t in
3778 if String.length s > 0
3779 then Printf.sprintf "%s\t%s" name s
3780 else name),
3781 offset)
3783 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3784 let uiohopt =
3785 if not cancel
3786 then (
3787 m_qsearch <- qsearch;
3788 let uioh =
3789 match m_a.(active) with
3790 | _, _, _, Action f -> f uioh
3791 | _ -> uioh
3793 Some uioh
3795 else None
3797 m_active <- active;
3798 m_first <- first;
3799 m_pan <- pan;
3800 uiohopt
3802 method hasaction n =
3803 match m_a.(n) with
3804 | _, _, _, Action _ -> true
3805 | _ -> false
3806 end)
3808 let rec fillsrc prevmode prevuioh =
3809 let sep () = src#caption "" 0 in
3810 let colorp name get set =
3811 src#string name
3812 (fun () -> color_to_string (get ()))
3813 (fun v ->
3815 let c = color_of_string v in
3816 set c
3817 with exn ->
3818 state.text <- Printf.sprintf "bad color `%s': %s"
3819 v (Printexc.to_string exn);
3822 let oldmode = state.mode in
3823 let birdseye = isbirdseye state.mode in
3825 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3827 src#bool "presentation mode"
3828 (fun () -> conf.presentation)
3829 (fun v ->
3830 conf.presentation <- v;
3831 state.anchor <- getanchor ();
3832 represent ());
3834 src#bool "ignore case in searches"
3835 (fun () -> conf.icase)
3836 (fun v -> conf.icase <- v);
3838 src#bool "preload"
3839 (fun () -> conf.preload)
3840 (fun v -> conf.preload <- v);
3842 src#bool "highlight links"
3843 (fun () -> conf.hlinks)
3844 (fun v -> conf.hlinks <- v);
3846 src#bool "under info"
3847 (fun () -> conf.underinfo)
3848 (fun v -> conf.underinfo <- v);
3850 src#bool "persistent bookmarks"
3851 (fun () -> conf.savebmarks)
3852 (fun v -> conf.savebmarks <- v);
3854 src#bool "proportional display"
3855 (fun () -> conf.proportional)
3856 (fun v -> reqlayout conf.angle v);
3858 src#bool "trim margins"
3859 (fun () -> conf.trimmargins)
3860 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3862 src#bool "persistent location"
3863 (fun () -> conf.jumpback)
3864 (fun v -> conf.jumpback <- v);
3866 sep ();
3867 src#int "inter-page space"
3868 (fun () -> conf.interpagespace)
3869 (fun n ->
3870 conf.interpagespace <- n;
3871 let pageno, py =
3872 match state.layout with
3873 | [] -> 0, 0
3874 | l :: _ ->
3875 l.pageno, l.pagey
3877 state.maxy <- calcheight ();
3878 let y = getpagey pageno in
3879 gotoy (y + py)
3882 src#int "page bias"
3883 (fun () -> conf.pagebias)
3884 (fun v -> conf.pagebias <- v);
3886 src#int "scroll step"
3887 (fun () -> conf.scrollstep)
3888 (fun n -> conf.scrollstep <- n);
3890 src#int "auto scroll step"
3891 (fun () ->
3892 match state.autoscroll with
3893 | Some step -> step
3894 | _ -> conf.autoscrollstep)
3895 (fun n ->
3896 if state.autoscroll <> None
3897 then state.autoscroll <- Some n;
3898 conf.autoscrollstep <- n);
3900 src#int "zoom"
3901 (fun () -> truncate (conf.zoom *. 100.))
3902 (fun v -> setzoom ((float v) /. 100.));
3904 src#int "rotation"
3905 (fun () -> conf.angle)
3906 (fun v -> reqlayout v conf.proportional);
3908 src#int "scroll bar width"
3909 (fun () -> state.scrollw)
3910 (fun v ->
3911 state.scrollw <- v;
3912 conf.scrollbw <- v;
3913 reshape conf.winw conf.winh;
3916 src#int "scroll handle height"
3917 (fun () -> conf.scrollh)
3918 (fun v -> conf.scrollh <- v;);
3920 src#int "thumbnail width"
3921 (fun () -> conf.thumbw)
3922 (fun v ->
3923 conf.thumbw <- min 4096 v;
3924 match oldmode with
3925 | Birdseye beye ->
3926 leavebirdseye beye false;
3927 enterbirdseye ()
3928 | _ -> ()
3931 src#string "columns"
3932 (fun () ->
3933 match conf.columns with
3934 | None -> "1"
3935 | Some (multicol, _) -> columns_to_string multicol)
3936 (fun v ->
3937 let n, a, b = columns_of_string v in
3938 setcolumns n a b);
3940 sep ();
3941 src#caption "Presentation mode" 0;
3942 src#bool "scrollbar visible"
3943 (fun () -> conf.scrollbarinpm)
3944 (fun v ->
3945 if v != conf.scrollbarinpm
3946 then (
3947 conf.scrollbarinpm <- v;
3948 if conf.presentation
3949 then (
3950 state.scrollw <- if v then conf.scrollbw else 0;
3951 reshape conf.winw conf.winh;
3956 sep ();
3957 src#caption "Pixmap cache" 0;
3958 src#int_with_suffix "size (advisory)"
3959 (fun () -> conf.memlimit)
3960 (fun v -> conf.memlimit <- v);
3962 src#caption2 "used"
3963 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3964 (string_with_suffix_of_int state.memused)
3965 (Hashtbl.length state.tilemap)) 1;
3967 sep ();
3968 src#caption "Layout" 0;
3969 src#caption2 "Dimension"
3970 (fun () ->
3971 Printf.sprintf "%dx%d (virtual %dx%d)"
3972 conf.winw conf.winh
3973 state.w state.maxy)
3975 if conf.debug
3976 then
3977 src#caption2 "Position" (fun () ->
3978 Printf.sprintf "%dx%d" state.x state.y
3980 else
3981 src#caption2 "Visible" (fun () -> describe_location ()) 1
3984 sep ();
3985 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3986 "Save these parameters as global defaults at exit"
3987 (fun () -> conf.bedefault)
3988 (fun v -> conf.bedefault <- v)
3991 sep ();
3992 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3993 src#bool ~offset:0 ~btos "Extended parameters"
3994 (fun () -> !showextended)
3995 (fun v -> showextended := v; fillsrc prevmode prevuioh);
3996 if !showextended
3997 then (
3998 src#bool "checkers"
3999 (fun () -> conf.checkers)
4000 (fun v -> conf.checkers <- v; setcheckers v);
4001 src#bool "update cursor"
4002 (fun () -> conf.updatecurs)
4003 (fun v -> conf.updatecurs <- v);
4004 src#bool "verbose"
4005 (fun () -> conf.verbose)
4006 (fun v -> conf.verbose <- v);
4007 src#bool "invert colors"
4008 (fun () -> conf.invert)
4009 (fun v -> conf.invert <- v);
4010 src#bool "max fit"
4011 (fun () -> conf.maxhfit)
4012 (fun v -> conf.maxhfit <- v);
4013 src#bool "redirect stderr"
4014 (fun () -> conf.redirectstderr)
4015 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4016 src#string "uri launcher"
4017 (fun () -> conf.urilauncher)
4018 (fun v -> conf.urilauncher <- v);
4019 src#string "path launcher"
4020 (fun () -> conf.pathlauncher)
4021 (fun v -> conf.pathlauncher <- v);
4022 src#string "tile size"
4023 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4024 (fun v ->
4026 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4027 conf.tileh <- max 64 w;
4028 conf.tilew <- max 64 h;
4029 flushtiles ();
4030 with exn ->
4031 state.text <- Printf.sprintf "bad tile size `%s': %s"
4032 v (Printexc.to_string exn));
4033 src#int "texture count"
4034 (fun () -> conf.texcount)
4035 (fun v ->
4036 if realloctexts v
4037 then conf.texcount <- v
4038 else showtext '!' " Failed to set texture count please retry later"
4040 src#int "slice height"
4041 (fun () -> conf.sliceheight)
4042 (fun v ->
4043 conf.sliceheight <- v;
4044 wcmd "sliceh %d" conf.sliceheight;
4046 src#int "anti-aliasing level"
4047 (fun () -> conf.aalevel)
4048 (fun v ->
4049 conf.aalevel <- bound v 0 8;
4050 state.anchor <- getanchor ();
4051 opendoc state.path state.password;
4053 src#int "ui font size"
4054 (fun () -> fstate.fontsize)
4055 (fun v -> setfontsize (bound v 5 100));
4056 colorp "background color"
4057 (fun () -> conf.bgcolor)
4058 (fun v -> conf.bgcolor <- v);
4059 src#bool "crop hack"
4060 (fun () -> conf.crophack)
4061 (fun v -> conf.crophack <- v);
4062 src#string "trim fuzz"
4063 (fun () -> irect_to_string conf.trimfuzz)
4064 (fun v ->
4066 conf.trimfuzz <- irect_of_string v;
4067 if conf.trimmargins
4068 then settrim true conf.trimfuzz;
4069 with exn ->
4070 state.text <- Printf.sprintf "bad irect `%s': %s"
4071 v (Printexc.to_string exn)
4073 src#string "throttle"
4074 (fun () ->
4075 match conf.maxwait with
4076 | None -> "show place holder if page is not ready"
4077 | Some time ->
4078 if time = infinity
4079 then "wait for page to fully render"
4080 else
4081 "wait " ^ string_of_float time
4082 ^ " seconds before showing placeholder"
4084 (fun v ->
4086 let f = float_of_string v in
4087 if f <= 0.0
4088 then conf.maxwait <- None
4089 else conf.maxwait <- Some f
4090 with exn ->
4091 state.text <- Printf.sprintf "bad time `%s': %s"
4092 v (Printexc.to_string exn)
4094 src#string "ghyll scroll"
4095 (fun () ->
4096 match conf.ghyllscroll with
4097 | None -> ""
4098 | Some nab -> ghyllscroll_to_string nab
4100 (fun v ->
4102 let gs =
4103 if String.length v = 0
4104 then None
4105 else Some (ghyllscroll_of_string v)
4107 conf.ghyllscroll <- gs
4108 with exn ->
4109 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4110 v (Printexc.to_string exn)
4112 src#string "selection command"
4113 (fun () -> conf.selcmd)
4114 (fun v -> conf.selcmd <- v);
4115 src#colorspace "color space"
4116 (fun () -> colorspace_to_string conf.colorspace)
4117 (fun v ->
4118 conf.colorspace <- colorspace_of_int v;
4119 wcmd "cs %d" v;
4120 load state.layout;
4124 sep ();
4125 src#caption "Document" 0;
4126 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4127 src#caption2 "Pages"
4128 (fun () -> string_of_int state.pagecount) 1;
4129 src#caption2 "Dimensions"
4130 (fun () -> string_of_int (List.length state.pdims)) 1;
4131 if conf.trimmargins
4132 then (
4133 sep ();
4134 src#caption "Trimmed margins" 0;
4135 src#caption2 "Dimensions"
4136 (fun () -> string_of_int (List.length state.pdims)) 1;
4139 src#reset prevmode prevuioh;
4141 fun () ->
4142 state.text <- "";
4143 let prevmode = state.mode
4144 and prevuioh = state.uioh in
4145 fillsrc prevmode prevuioh;
4146 let source = (src :> lvsource) in
4147 let modehash = findkeyhash conf "info" in
4148 state.uioh <- coe (object (self)
4149 inherit listview ~source ~trusted:true ~modehash as super
4150 val mutable m_prevmemused = 0
4151 method infochanged = function
4152 | Memused ->
4153 if m_prevmemused != state.memused
4154 then (
4155 m_prevmemused <- state.memused;
4156 G.postRedisplay "memusedchanged";
4158 | Pdim -> G.postRedisplay "pdimchanged"
4159 | Docinfo -> fillsrc prevmode prevuioh
4161 method key key mask =
4162 if not (Wsi.withctrl mask)
4163 then
4164 match key with
4165 | 0xff51 -> coe (self#updownlevel ~-1)
4166 | 0xff53 -> coe (self#updownlevel 1)
4167 | _ -> super#key key mask
4168 else super#key key mask
4169 end);
4170 G.postRedisplay "info";
4173 let enterhelpmode =
4174 let source =
4175 (object
4176 inherit lvsourcebase
4177 method getitemcount = Array.length state.help
4178 method getitem n =
4179 let s, n, _ = state.help.(n) in
4180 (s, n)
4182 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4183 let optuioh =
4184 if not cancel
4185 then (
4186 m_qsearch <- qsearch;
4187 match state.help.(active) with
4188 | _, _, Action f -> Some (f uioh)
4189 | _ -> Some (uioh)
4191 else None
4193 m_active <- active;
4194 m_first <- first;
4195 m_pan <- pan;
4196 optuioh
4198 method hasaction n =
4199 match state.help.(n) with
4200 | _, _, Action _ -> true
4201 | _ -> false
4203 initializer
4204 m_active <- -1
4205 end)
4206 in fun () ->
4207 let modehash = findkeyhash conf "help" in
4208 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4209 G.postRedisplay "help";
4212 let entermsgsmode =
4213 let msgsource =
4214 let re = Str.regexp "[\r\n]" in
4215 (object
4216 inherit lvsourcebase
4217 val mutable m_items = [||]
4219 method getitemcount = 1 + Array.length m_items
4221 method getitem n =
4222 if n = 0
4223 then "[Clear]", 0
4224 else m_items.(n-1), 0
4226 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4227 ignore uioh;
4228 if not cancel
4229 then (
4230 if active = 0
4231 then Buffer.clear state.errmsgs;
4232 m_qsearch <- qsearch;
4234 m_active <- active;
4235 m_first <- first;
4236 m_pan <- pan;
4237 None
4239 method hasaction n =
4240 n = 0
4242 method reset =
4243 state.newerrmsgs <- false;
4244 let l = Str.split re (Buffer.contents state.errmsgs) in
4245 m_items <- Array.of_list l
4247 initializer
4248 m_active <- 0
4249 end)
4250 in fun () ->
4251 state.text <- "";
4252 msgsource#reset;
4253 let source = (msgsource :> lvsource) in
4254 let modehash = findkeyhash conf "listview" in
4255 state.uioh <- coe (object
4256 inherit listview ~source ~trusted:false ~modehash as super
4257 method display =
4258 if state.newerrmsgs
4259 then msgsource#reset;
4260 super#display
4261 end);
4262 G.postRedisplay "msgs";
4265 let quickbookmark ?title () =
4266 match state.layout with
4267 | [] -> ()
4268 | l :: _ ->
4269 let title =
4270 match title with
4271 | None ->
4272 let sec = Unix.gettimeofday () in
4273 let tm = Unix.localtime sec in
4274 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4275 (l.pageno+1)
4276 tm.Unix.tm_mday
4277 tm.Unix.tm_mon
4278 (tm.Unix.tm_year + 1900)
4279 tm.Unix.tm_hour
4280 tm.Unix.tm_min
4281 | Some title -> title
4283 state.bookmarks <-
4284 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4285 :: state.bookmarks
4288 let doreshape w h =
4289 state.fullscreen <- None;
4290 Wsi.reshape w h;
4293 let setautoscrollspeed step goingdown =
4294 let incr = max 1 ((abs step) / 2) in
4295 let incr = if goingdown then incr else -incr in
4296 let astep = step + incr in
4297 state.autoscroll <- Some astep;
4300 let viewkeyboard key mask =
4301 let enttext te =
4302 let mode = state.mode in
4303 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4304 state.text <- "";
4305 enttext ();
4306 G.postRedisplay "view:enttext"
4308 let ctrl = Wsi.withctrl mask in
4309 match key with
4310 | 81 -> (* Q *)
4311 exit 0
4313 | 0xff63 -> (* insert *)
4314 if conf.angle mod 360 = 0
4315 then (
4316 state.mode <- LinkNav (Ltgendir 0);
4317 gotoy state.y;
4319 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4321 | 0xff1b | 113 -> (* escape / q *)
4322 begin match state.mstate with
4323 | Mzoomrect _ ->
4324 state.mstate <- Mnone;
4325 Wsi.setcursor Wsi.CURSOR_INHERIT;
4326 G.postRedisplay "kill zoom rect";
4327 | _ ->
4328 match state.ranchors with
4329 | [] -> raise Quit
4330 | (path, password, anchor) :: rest ->
4331 state.ranchors <- rest;
4332 state.anchor <- anchor;
4333 opendoc path password
4334 end;
4336 | 0xff08 -> (* backspace *)
4337 let y = getnav ~-1 in
4338 gotoy_and_clear_text y
4340 | 111 -> (* o *)
4341 enteroutlinemode ()
4343 | 117 -> (* u *)
4344 state.rects <- [];
4345 state.text <- "";
4346 G.postRedisplay "dehighlight";
4348 | 47 | 63 -> (* / ? *)
4349 let ondone isforw s =
4350 cbput state.hists.pat s;
4351 state.searchpattern <- s;
4352 search s isforw
4354 let s = String.create 1 in
4355 s.[0] <- Char.chr key;
4356 enttext (s, "", Some (onhist state.hists.pat),
4357 textentry, ondone (key = 47))
4359 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4360 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4361 setzoom (conf.zoom +. incr)
4363 | 43 | 0xffab -> (* + *)
4364 let ondone s =
4365 let n =
4366 try int_of_string s with exc ->
4367 state.text <- Printf.sprintf "bad integer `%s': %s"
4368 s (Printexc.to_string exc);
4369 max_int
4371 if n != max_int
4372 then (
4373 conf.pagebias <- n;
4374 state.text <- "page bias is now " ^ string_of_int n;
4377 enttext ("page bias: ", "", None, intentry, ondone)
4379 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4380 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4381 setzoom (max 0.01 (conf.zoom -. decr))
4383 | 45 | 0xffad -> (* - *)
4384 let ondone msg = state.text <- msg in
4385 enttext (
4386 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4387 optentry state.mode, ondone
4390 | 48 when ctrl -> (* ctrl-0 *)
4391 setzoom 1.0
4393 | 49 when ctrl -> (* 1 *)
4394 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4395 if zoom < 1.0
4396 then setzoom zoom
4398 | 0xffc6 -> (* f9 *)
4399 togglebirdseye ()
4401 | 57 when ctrl -> (* ctrl-9 *)
4402 togglebirdseye ()
4404 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4405 when not ctrl -> (* 0..9 *)
4406 let ondone s =
4407 let n =
4408 try int_of_string s with exc ->
4409 state.text <- Printf.sprintf "bad integer `%s': %s"
4410 s (Printexc.to_string exc);
4413 if n >= 0
4414 then (
4415 addnav ();
4416 cbput state.hists.pag (string_of_int n);
4417 gotopage1 (n + conf.pagebias - 1) 0;
4420 let pageentry text key =
4421 match Char.unsafe_chr key with
4422 | 'g' -> TEdone text
4423 | _ -> intentry text key
4425 let text = "x" in text.[0] <- Char.chr key;
4426 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4428 | 98 -> (* b *)
4429 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4430 reshape conf.winw conf.winh;
4432 | 108 -> (* l *)
4433 conf.hlinks <- not conf.hlinks;
4434 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4435 G.postRedisplay "toggle highlightlinks";
4437 | 97 -> (* a *)
4438 begin match state.autoscroll with
4439 | Some step ->
4440 conf.autoscrollstep <- step;
4441 state.autoscroll <- None
4442 | None ->
4443 if conf.autoscrollstep = 0
4444 then state.autoscroll <- Some 1
4445 else state.autoscroll <- Some conf.autoscrollstep
4448 | 80 -> (* P *)
4449 conf.presentation <- not conf.presentation;
4450 if conf.presentation
4451 then (
4452 if not conf.scrollbarinpm
4453 then state.scrollw <- 0;
4455 else
4456 state.scrollw <- conf.scrollbw;
4458 showtext ' ' ("presentation mode " ^
4459 if conf.presentation then "on" else "off");
4460 state.anchor <- getanchor ();
4461 represent ()
4463 | 102 -> (* f *)
4464 begin match state.fullscreen with
4465 | None ->
4466 state.fullscreen <- Some (conf.winw, conf.winh);
4467 Wsi.fullscreen ()
4468 | Some (w, h) ->
4469 state.fullscreen <- None;
4470 doreshape w h
4473 | 103 -> (* g *)
4474 gotoy_and_clear_text 0
4476 | 71 -> (* G *)
4477 gotopage1 (state.pagecount - 1) 0
4479 | 112 | 78 -> (* p|N *)
4480 search state.searchpattern false
4482 | 110 | 0xffc0 -> (* n|F3 *)
4483 search state.searchpattern true
4485 | 116 -> (* t *)
4486 begin match state.layout with
4487 | [] -> ()
4488 | l :: _ ->
4489 gotoy_and_clear_text (getpagey l.pageno)
4492 | 32 -> (* ' ' *)
4493 begin match List.rev state.layout with
4494 | [] -> ()
4495 | l :: _ ->
4496 let pageno = min (l.pageno+1) (state.pagecount-1) in
4497 gotoy_and_clear_text (getpagey pageno)
4500 | 0xff9f | 0xffff -> (* delete *)
4501 begin match state.layout with
4502 | [] -> ()
4503 | l :: _ ->
4504 let pageno = max 0 (l.pageno-1) in
4505 gotoy_and_clear_text (getpagey pageno)
4508 | 61 -> (* = *)
4509 showtext ' ' (describe_location ());
4511 | 119 -> (* w *)
4512 begin match state.layout with
4513 | [] -> ()
4514 | l :: _ ->
4515 doreshape (l.pagew + state.scrollw) l.pageh;
4516 G.postRedisplay "w"
4519 | 39 -> (* ' *)
4520 enterbookmarkmode ()
4522 | 104 | 0xffbe -> (* h|F1 *)
4523 enterhelpmode ()
4525 | 105 -> (* i *)
4526 enterinfomode ()
4528 | 101 when conf.redirectstderr -> (* e *)
4529 entermsgsmode ()
4531 | 109 -> (* m *)
4532 let ondone s =
4533 match state.layout with
4534 | l :: _ ->
4535 state.bookmarks <-
4536 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4537 :: state.bookmarks
4538 | _ -> ()
4540 enttext ("bookmark: ", "", None, textentry, ondone)
4542 | 126 -> (* ~ *)
4543 quickbookmark ();
4544 showtext ' ' "Quick bookmark added";
4546 | 122 -> (* z *)
4547 begin match state.layout with
4548 | l :: _ ->
4549 let rect = getpdimrect l.pagedimno in
4550 let w, h =
4551 if conf.crophack
4552 then
4553 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4554 truncate (1.2 *. (rect.(3) -. rect.(0))))
4555 else
4556 (truncate (rect.(1) -. rect.(0)),
4557 truncate (rect.(3) -. rect.(0)))
4559 let w = truncate ((float w)*.conf.zoom)
4560 and h = truncate ((float h)*.conf.zoom) in
4561 if w != 0 && h != 0
4562 then (
4563 state.anchor <- getanchor ();
4564 doreshape (w + state.scrollw) (h + conf.interpagespace)
4566 G.postRedisplay "z";
4568 | [] -> ()
4571 | 50 when ctrl -> (* ctrl-2 *)
4572 let maxw = getmaxw () in
4573 if maxw > 0.0
4574 then setzoom (maxw /. float conf.winw)
4576 | 60 | 62 -> (* < > *)
4577 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
4579 | 91 | 93 -> (* [ ] *)
4580 conf.colorscale <-
4581 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4583 G.postRedisplay "brightness";
4585 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
4586 setzoom state.prevzoom
4588 | 107 | 0xff52 -> (* k up *)
4589 begin match state.autoscroll with
4590 | None ->
4591 begin match state.mode with
4592 | Birdseye beye -> upbirdseye 1 beye
4593 | _ ->
4594 if ctrl
4595 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
4596 else gotoy_and_clear_text (clamp (-conf.scrollstep))
4598 | Some n ->
4599 setautoscrollspeed n false
4602 | 106 | 0xff54 -> (* j down *)
4603 begin match state.autoscroll with
4604 | None ->
4605 begin match state.mode with
4606 | Birdseye beye -> downbirdseye 1 beye
4607 | _ ->
4608 if ctrl
4609 then gotoy_and_clear_text (clamp (conf.winh/2))
4610 else gotoy_and_clear_text (clamp conf.scrollstep)
4612 | Some n ->
4613 setautoscrollspeed n true
4616 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
4617 if conf.zoom > 1.0
4618 then
4619 let dx =
4620 if ctrl
4621 then conf.winw / 2
4622 else 10
4624 let dx = if key = 0xff51 then dx else -dx in
4625 state.x <- state.x + dx;
4626 gotoy_and_clear_text state.y
4627 else (
4628 state.text <- "";
4629 G.postRedisplay "lef/right"
4632 | 0xff55 -> (* prior *)
4633 let y =
4634 if ctrl
4635 then
4636 match state.layout with
4637 | [] -> state.y
4638 | l :: _ -> state.y - l.pagey
4639 else
4640 clamp (-conf.winh)
4642 gotoghyll y
4644 | 0xff56 -> (* next *)
4645 let y =
4646 if ctrl
4647 then
4648 match List.rev state.layout with
4649 | [] -> state.y
4650 | l :: _ -> getpagey l.pageno
4651 else
4652 clamp conf.winh
4654 gotoghyll y
4656 | 0xff50 -> gotoghyll 0
4657 | 0xff57 -> gotoghyll (clamp state.maxy)
4658 | 0xff53 when Wsi.withalt mask ->
4659 gotoghyll (getnav ~-1)
4660 | 0xff51 when Wsi.withalt mask ->
4661 gotoghyll (getnav 1)
4663 | 114 -> (* r *)
4664 state.anchor <- getanchor ();
4665 opendoc state.path state.password
4667 | 76 -> (* L *)
4668 launchpath ()
4670 | 118 when conf.debug -> (* v *)
4671 state.rects <- [];
4672 List.iter (fun l ->
4673 match getopaque l.pageno with
4674 | None -> ()
4675 | Some opaque ->
4676 let x0, y0, x1, y1 = pagebbox opaque in
4677 let a,b = float x0, float y0 in
4678 let c,d = float x1, float y0 in
4679 let e,f = float x1, float y1 in
4680 let h,j = float x0, float y1 in
4681 let rect = (a,b,c,d,e,f,h,j) in
4682 debugrect rect;
4683 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4684 ) state.layout;
4685 G.postRedisplay "v";
4687 | _ ->
4688 vlog "huh? %s" (Wsi.keyname key)
4691 let gotounder = function
4692 | Ulinkgoto (pageno, top) ->
4693 if pageno >= 0
4694 then (
4695 addnav ();
4696 gotopage1 pageno top;
4699 | Ulinkuri s ->
4700 gotouri s
4702 | Uremote (filename, pageno) ->
4703 let path =
4704 if Sys.file_exists filename
4705 then filename
4706 else
4707 let dir = Filename.dirname state.path in
4708 let path = Filename.concat dir filename in
4709 if Sys.file_exists path
4710 then path
4711 else ""
4713 if String.length path > 0
4714 then (
4715 let anchor = getanchor () in
4716 let ranchor = state.path, state.password, anchor in
4717 state.anchor <- (pageno, 0.0);
4718 state.ranchors <- ranchor :: state.ranchors;
4719 opendoc path "";
4721 else showtext '!' ("Could not find " ^ filename)
4723 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4726 let linknavkeyboard key mask linknav =
4727 let getpage pageno =
4728 let rec loop = function
4729 | [] -> None
4730 | l :: _ when l.pageno = pageno -> Some l
4731 | _ :: rest -> loop rest
4732 in loop state.layout
4734 let doexact (pageno, n) =
4735 match getopaque pageno, getpage pageno with
4736 | Some opaque, Some l ->
4737 if key = 0xff0d
4738 then
4739 let under = getlink opaque n in
4740 G.postRedisplay "link gotounder";
4741 gotounder under;
4742 state.mode <- View;
4743 else
4744 let opt, dir =
4745 match key with
4746 | 0xff50 -> (* home *)
4747 Some (findlink opaque LDfirst), -1
4749 | 0xff57 -> (* end *)
4750 Some (findlink opaque LDlast), 1
4752 | 0xff51 -> (* left *)
4753 Some (findlink opaque (LDleft n)), -1
4755 | 0xff53 -> (* right *)
4756 Some (findlink opaque (LDright n)), 1
4758 | 0xff52 -> (* up *)
4759 Some (findlink opaque (LDup n)), -1
4761 | 0xff54 -> (* down *)
4762 Some (findlink opaque (LDdown n)), 1
4764 | _ -> None, 0
4766 let pwl l dir =
4767 begin match findpwl l.pageno dir with
4768 | Pwlnotfound -> ()
4769 | Pwl pageno ->
4770 let notfound dir =
4771 state.mode <- LinkNav (Ltgendir dir);
4772 let y, h = getpageyh pageno in
4773 let y =
4774 if dir < 0
4775 then y + h - conf.winh
4776 else y
4778 gotoy y
4780 begin match getopaque pageno, getpage pageno with
4781 | Some opaque, Some _ ->
4782 let link =
4783 let ld = if dir > 0 then LDfirst else LDlast in
4784 findlink opaque ld
4786 begin match link with
4787 | Lfound (m, x0, y0, x1, y1) ->
4788 let r = x0, y0, x1, y1 in
4789 showlinktype (getlink opaque m);
4790 state.mode <- LinkNav (Ltexact ((pageno, m), r));
4791 G.postRedisplay "linknav jpage";
4792 | _ -> notfound dir
4793 end;
4794 | _ -> notfound dir
4795 end;
4796 end;
4798 begin match opt with
4799 | Some Lnotfound -> pwl l dir;
4800 | Some (Lfound (m, x0, y0, x1, y1)) ->
4801 if m = n
4802 then pwl l dir
4803 else (
4804 if y0 < l.pagey
4805 then gotopage1 l.pageno y0
4806 else (
4807 let d = fstate.fontsize + 1 in
4808 if y1 - l.pagey > l.pagevh - d
4809 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
4810 else G.postRedisplay "linknav";
4812 let r = x0, y0, x1, y1 in
4813 showlinktype (getlink opaque m);
4814 state.mode <- LinkNav (Ltexact ((l.pageno, m), r));
4817 | None ->
4818 state.mode <- LinkNav (Ltgendir 0);
4819 viewkeyboard key mask
4820 end;
4821 | _ -> viewkeyboard key mask
4823 if key = 0xff63
4824 then (
4825 state.mode <- View;
4826 G.postRedisplay "leave linknav"
4828 else
4829 match linknav with
4830 | Ltgendir _ -> viewkeyboard key mask
4831 | Ltexact (exact, _) -> doexact exact
4834 let keyboard key mask =
4835 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
4836 then wcmd "interrupt"
4837 else state.uioh <- state.uioh#key key mask
4840 let birdseyekeyboard key mask
4841 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
4842 let incr =
4843 match conf.columns with
4844 | None -> 1
4845 | Some ((c, _, _), _) -> c
4847 match key with
4848 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
4849 let y, h = getpageyh pageno in
4850 let top = (conf.winh - h) / 2 in
4851 gotoy (max 0 (y - top))
4852 | 0xff0d -> leavebirdseye beye false
4853 | 0xff1b -> leavebirdseye beye true (* escape *)
4854 | 0xff52 -> upbirdseye incr beye (* prior *)
4855 | 0xff54 -> downbirdseye incr beye (* next *)
4856 | 0xff51 -> upbirdseye 1 beye (* up *)
4857 | 0xff53 -> downbirdseye 1 beye (* down *)
4859 | 0xff55 ->
4860 begin match state.layout with
4861 | l :: _ ->
4862 if l.pagey != 0
4863 then (
4864 state.mode <- Birdseye (
4865 oconf, leftx, l.pageno, hooverpageno, anchor
4867 gotopage1 l.pageno 0;
4869 else (
4870 let layout = layout (state.y-conf.winh) conf.winh in
4871 match layout with
4872 | [] -> gotoy (clamp (-conf.winh))
4873 | l :: _ ->
4874 state.mode <- Birdseye (
4875 oconf, leftx, l.pageno, hooverpageno, anchor
4877 gotopage1 l.pageno 0
4880 | [] -> gotoy (clamp (-conf.winh))
4881 end;
4883 | 0xff56 ->
4884 begin match List.rev state.layout with
4885 | l :: _ ->
4886 let layout = layout (state.y + conf.winh) conf.winh in
4887 begin match layout with
4888 | [] ->
4889 let incr = l.pageh - l.pagevh in
4890 if incr = 0
4891 then (
4892 state.mode <-
4893 Birdseye (
4894 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4896 G.postRedisplay "birdseye pagedown";
4898 else gotoy (clamp (incr + conf.interpagespace*2));
4900 | l :: _ ->
4901 state.mode <-
4902 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4903 gotopage1 l.pageno 0;
4906 | [] -> gotoy (clamp conf.winh)
4907 end;
4909 | 0xff50 ->
4910 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4911 gotopage1 0 0
4913 | 0xff57 ->
4914 let pageno = state.pagecount - 1 in
4915 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4916 if not (pagevisible state.layout pageno)
4917 then
4918 let h =
4919 match List.rev state.pdims with
4920 | [] -> conf.winh
4921 | (_, _, h, _) :: _ -> h
4923 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4924 else G.postRedisplay "birdseye end";
4925 | _ -> viewkeyboard key mask
4928 let drawpage l =
4929 let color =
4930 match state.mode with
4931 | Textentry _ -> scalecolor 0.4
4932 | LinkNav _
4933 | View -> scalecolor 1.0
4934 | Birdseye (_, _, pageno, hooverpageno, _) ->
4935 if l.pageno = hooverpageno
4936 then scalecolor 0.9
4937 else (
4938 if l.pageno = pageno
4939 then scalecolor 1.0
4940 else scalecolor 0.8
4943 drawtiles l color;
4944 begin match getopaque l.pageno with
4945 | Some opaque ->
4946 if tileready l l.pagex l.pagey
4947 then
4948 let x = l.pagedispx - l.pagex
4949 and y = l.pagedispy - l.pagey in
4950 postprocess opaque conf.hlinks x y;
4952 | _ -> ()
4953 end;
4956 let scrollindicator () =
4957 let sbw, ph, sh = state.uioh#scrollph in
4958 let sbh, pw, sw = state.uioh#scrollpw in
4960 GlDraw.color (0.64, 0.64, 0.64);
4961 GlDraw.rect
4962 (float (conf.winw - sbw), 0.)
4963 (float conf.winw, float conf.winh)
4965 GlDraw.rect
4966 (0., float (conf.winh - sbh))
4967 (float (conf.winw - state.scrollw - 1), float conf.winh)
4969 GlDraw.color (0.0, 0.0, 0.0);
4971 GlDraw.rect
4972 (float (conf.winw - sbw), ph)
4973 (float conf.winw, ph +. sh)
4975 GlDraw.rect
4976 (pw, float (conf.winh - sbh))
4977 (pw +. sw, float conf.winh)
4981 let showsel () =
4982 match state.mstate with
4983 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4986 | Msel ((x0, y0), (x1, y1)) ->
4987 let rec loop = function
4988 | l :: ls ->
4989 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4990 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
4991 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
4992 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
4993 then
4994 match getopaque l.pageno with
4995 | Some opaque ->
4996 let x0, y0 = pagetranslatepoint l x0 y0 in
4997 let x1, y1 = pagetranslatepoint l x1 y1 in
4998 seltext opaque (x0, y0, x1, y1);
4999 | _ -> ()
5000 else loop ls
5001 | [] -> ()
5003 loop state.layout
5006 let showrects rects =
5007 Gl.enable `blend;
5008 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5009 GlDraw.polygon_mode `both `fill;
5010 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5011 List.iter
5012 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5013 List.iter (fun l ->
5014 if l.pageno = pageno
5015 then (
5016 let dx = float (l.pagedispx - l.pagex) in
5017 let dy = float (l.pagedispy - l.pagey) in
5018 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5019 GlDraw.begins `quads;
5021 GlDraw.vertex2 (x0+.dx, y0+.dy);
5022 GlDraw.vertex2 (x1+.dx, y1+.dy);
5023 GlDraw.vertex2 (x2+.dx, y2+.dy);
5024 GlDraw.vertex2 (x3+.dx, y3+.dy);
5026 GlDraw.ends ();
5028 ) state.layout
5029 ) rects
5031 Gl.disable `blend;
5034 let display () =
5035 GlClear.color (scalecolor2 conf.bgcolor);
5036 GlClear.clear [`color];
5037 List.iter drawpage state.layout;
5038 let rects =
5039 match state.mode with
5040 | LinkNav (Ltexact ((pageno, _), (x0, y0, x1, y1))) ->
5041 (pageno, 5, (
5042 float x0, float y0,
5043 float x1, float y0,
5044 float x1, float y1,
5045 float x0, float y1)
5046 ) :: state.rects
5047 | _ -> state.rects
5049 showrects rects;
5050 showsel ();
5051 state.uioh#display;
5052 begin match state.mstate with
5053 | Mzoomrect ((x0, y0), (x1, y1)) ->
5054 Gl.enable `blend;
5055 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5056 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5057 GlDraw.rect (float x0, float y0)
5058 (float x1, float y1);
5059 Gl.disable `blend;
5060 | _ -> ()
5061 end;
5062 enttext ();
5063 scrollindicator ();
5064 Wsi.swapb ();
5067 let zoomrect x y x1 y1 =
5068 let x0 = min x x1
5069 and x1 = max x x1
5070 and y0 = min y y1 in
5071 gotoy (state.y + y0);
5072 state.anchor <- getanchor ();
5073 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5074 let margin =
5075 if state.w < conf.winw - state.scrollw
5076 then (conf.winw - state.scrollw - state.w) / 2
5077 else 0
5079 state.x <- (state.x + margin) - x0;
5080 setzoom zoom;
5081 Wsi.setcursor Wsi.CURSOR_INHERIT;
5082 state.mstate <- Mnone;
5085 let scrollx x =
5086 let winw = conf.winw - state.scrollw - 1 in
5087 let s = float x /. float winw in
5088 let destx = truncate (float (state.w + winw) *. s) in
5089 state.x <- winw - destx;
5090 gotoy_and_clear_text state.y;
5091 state.mstate <- Mscrollx;
5094 let scrolly y =
5095 let s = float y /. float conf.winh in
5096 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5097 gotoy_and_clear_text desty;
5098 state.mstate <- Mscrolly;
5101 let viewmouse button down x y mask =
5102 match button with
5103 | n when (n == 4 || n == 5) && not down ->
5104 if Wsi.withctrl mask
5105 then (
5106 match state.mstate with
5107 | Mzoom (oldn, i) ->
5108 if oldn = n
5109 then (
5110 if i = 2
5111 then
5112 let incr =
5113 match n with
5114 | 5 ->
5115 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5116 | _ ->
5117 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5119 let zoom = conf.zoom -. incr in
5120 setzoom zoom;
5121 state.mstate <- Mzoom (n, 0);
5122 else
5123 state.mstate <- Mzoom (n, i+1);
5125 else state.mstate <- Mzoom (n, 0)
5127 | _ -> state.mstate <- Mzoom (n, 0)
5129 else (
5130 match state.autoscroll with
5131 | Some step -> setautoscrollspeed step (n=4)
5132 | None ->
5133 let incr =
5134 if n = 4
5135 then -conf.scrollstep
5136 else conf.scrollstep
5138 let incr = incr * 2 in
5139 let y = clamp incr in
5140 gotoy_and_clear_text y
5143 | 1 when Wsi.withctrl mask ->
5144 if down
5145 then (
5146 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5147 state.mstate <- Mpan (x, y)
5149 else
5150 state.mstate <- Mnone
5152 | 3 ->
5153 if down
5154 then (
5155 Wsi.setcursor Wsi.CURSOR_CYCLE;
5156 let p = (x, y) in
5157 state.mstate <- Mzoomrect (p, p)
5159 else (
5160 match state.mstate with
5161 | Mzoomrect ((x0, y0), _) ->
5162 if abs (x-x0) > 10 && abs (y - y0) > 10
5163 then zoomrect x0 y0 x y
5164 else (
5165 state.mstate <- Mnone;
5166 Wsi.setcursor Wsi.CURSOR_INHERIT;
5167 G.postRedisplay "kill accidental zoom rect";
5169 | _ ->
5170 Wsi.setcursor Wsi.CURSOR_INHERIT;
5171 state.mstate <- Mnone
5174 | 1 when x > conf.winw - state.scrollw ->
5175 if down
5176 then
5177 let _, position, sh = state.uioh#scrollph in
5178 if y > truncate position && y < truncate (position +. sh)
5179 then state.mstate <- Mscrolly
5180 else scrolly y
5181 else
5182 state.mstate <- Mnone
5184 | 1 when y > conf.winh - state.hscrollh ->
5185 if down
5186 then
5187 let _, position, sw = state.uioh#scrollpw in
5188 if x > truncate position && x < truncate (position +. sw)
5189 then state.mstate <- Mscrollx
5190 else scrollx x
5191 else
5192 state.mstate <- Mnone
5194 | 1 ->
5195 let dest = if down then getunder x y else Unone in
5196 begin match dest with
5197 | Ulinkgoto (pageno, top) ->
5198 if pageno >= 0
5199 then (
5200 addnav ();
5201 gotopage1 pageno top;
5204 | Ulinkuri s ->
5205 gotouri s
5207 | Uremote (filename, pageno) ->
5208 let path =
5209 if Sys.file_exists filename
5210 then filename
5211 else
5212 let dir = Filename.dirname state.path in
5213 let path = Filename.concat dir filename in
5214 if Sys.file_exists path
5215 then path
5216 else ""
5218 if String.length path > 0
5219 then (
5220 let anchor = getanchor () in
5221 let ranchor = state.path, state.password, anchor in
5222 state.anchor <- (pageno, 0.0);
5223 state.ranchors <- ranchor :: state.ranchors;
5224 opendoc path "";
5226 else showtext '!' ("Could not find " ^ filename)
5228 | Uunexpected _ | Ulaunch _ | Unamed _ -> ()
5230 | Unone when down ->
5231 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5232 state.mstate <- Mpan (x, y);
5234 | Unone | Utext _ ->
5235 if down
5236 then (
5237 if conf.angle mod 360 = 0
5238 then (
5239 state.mstate <- Msel ((x, y), (x, y));
5240 G.postRedisplay "mouse select";
5243 else (
5244 match state.mstate with
5245 | Mnone -> ()
5247 | Mzoom _ | Mscrollx | Mscrolly ->
5248 state.mstate <- Mnone
5250 | Mzoomrect ((x0, y0), _) ->
5251 zoomrect x0 y0 x y
5253 | Mpan _ ->
5254 Wsi.setcursor Wsi.CURSOR_INHERIT;
5255 state.mstate <- Mnone
5257 | Msel ((_, y0), (_, y1)) ->
5258 let rec loop = function
5259 | [] -> ()
5260 | l :: rest ->
5261 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5262 || ((y1 >= l.pagedispy
5263 && y1 <= (l.pagedispy + l.pagevh)))
5264 then
5265 match getopaque l.pageno with
5266 | Some opaque ->
5267 copysel conf.selcmd opaque;
5268 G.postRedisplay "copysel"
5269 | _ -> ()
5270 else loop rest
5272 loop state.layout;
5273 Wsi.setcursor Wsi.CURSOR_INHERIT;
5274 state.mstate <- Mnone;
5278 | _ -> ()
5281 let birdseyemouse button down x y mask
5282 (conf, leftx, _, hooverpageno, anchor) =
5283 match button with
5284 | 1 when down ->
5285 let rec loop = function
5286 | [] -> ()
5287 | l :: rest ->
5288 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5289 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5290 then (
5291 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5293 else loop rest
5295 loop state.layout
5296 | 3 -> ()
5297 | _ -> viewmouse button down x y mask
5300 let mouse button down x y mask =
5301 state.uioh <- state.uioh#button button down x y mask;
5304 let motion ~x ~y =
5305 state.uioh <- state.uioh#motion x y
5308 let pmotion ~x ~y =
5309 state.uioh <- state.uioh#pmotion x y;
5312 let uioh = object
5313 method display = ()
5315 method key key mask =
5316 begin match state.mode with
5317 | Textentry textentry -> textentrykeyboard key mask textentry
5318 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5319 | View -> viewkeyboard key mask
5320 | LinkNav linknav -> linknavkeyboard key mask linknav
5321 end;
5322 state.uioh
5324 method button button bstate x y mask =
5325 begin match state.mode with
5326 | LinkNav _
5327 | View -> viewmouse button bstate x y mask
5328 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5329 | Textentry _ -> ()
5330 end;
5331 state.uioh
5333 method motion x y =
5334 begin match state.mode with
5335 | Textentry _ -> ()
5336 | View | Birdseye _ | LinkNav _ ->
5337 match state.mstate with
5338 | Mzoom _ | Mnone -> ()
5340 | Mpan (x0, y0) ->
5341 let dx = x - x0
5342 and dy = y0 - y in
5343 state.mstate <- Mpan (x, y);
5344 if conf.zoom > 1.0 then state.x <- state.x + dx;
5345 let y = clamp dy in
5346 gotoy_and_clear_text y
5348 | Msel (a, _) ->
5349 state.mstate <- Msel (a, (x, y));
5350 G.postRedisplay "motion select";
5352 | Mscrolly ->
5353 let y = min conf.winh (max 0 y) in
5354 scrolly y
5356 | Mscrollx ->
5357 let x = min conf.winw (max 0 x) in
5358 scrollx x
5360 | Mzoomrect (p0, _) ->
5361 state.mstate <- Mzoomrect (p0, (x, y));
5362 G.postRedisplay "motion zoomrect";
5363 end;
5364 state.uioh
5366 method pmotion x y =
5367 begin match state.mode with
5368 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5369 let rec loop = function
5370 | [] ->
5371 if hooverpageno != -1
5372 then (
5373 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5374 G.postRedisplay "pmotion birdseye no hoover";
5376 | l :: rest ->
5377 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5378 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5379 then (
5380 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5381 G.postRedisplay "pmotion birdseye hoover";
5383 else loop rest
5385 loop state.layout
5387 | Textentry _ -> ()
5389 | LinkNav _
5390 | View ->
5391 match state.mstate with
5392 | Mnone -> updateunder x y
5393 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5395 end;
5396 state.uioh
5398 method infochanged _ = ()
5400 method scrollph =
5401 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5402 let p, h = scrollph state.y maxy in
5403 state.scrollw, p, h
5405 method scrollpw =
5406 let winw = conf.winw - state.scrollw - 1 in
5407 let fwinw = float winw in
5408 let sw =
5409 let sw = fwinw /. float state.w in
5410 let sw = fwinw *. sw in
5411 max sw (float conf.scrollh)
5413 let position, sw =
5414 let f = state.w+winw in
5415 let r = float (winw-state.x) /. float f in
5416 let p = fwinw *. r in
5417 p-.sw/.2., sw
5419 let sw =
5420 if position +. sw > fwinw
5421 then fwinw -. position
5422 else sw
5424 state.hscrollh, position, sw
5426 method modehash =
5427 let modename =
5428 match state.mode with
5429 | LinkNav _ -> "links"
5430 | Textentry _ -> "textentry"
5431 | Birdseye _ -> "birdseye"
5432 | View -> "global"
5434 findkeyhash conf modename
5435 end;;
5437 module Config =
5438 struct
5439 open Parser
5441 let fontpath = ref "";;
5443 module KeyMap =
5444 Map.Make (struct type t = (int * int) let compare = compare end);;
5446 let unent s =
5447 let l = String.length s in
5448 let b = Buffer.create l in
5449 unent b s 0 l;
5450 Buffer.contents b;
5453 let home =
5454 try Sys.getenv "HOME"
5455 with exn ->
5456 prerr_endline
5457 ("Can not determine home directory location: " ^
5458 Printexc.to_string exn);
5462 let modifier_of_string = function
5463 | "alt" -> Wsi.altmask
5464 | "shift" -> Wsi.shiftmask
5465 | "ctrl" | "control" -> Wsi.ctrlmask
5466 | "meta" -> Wsi.metamask
5467 | _ -> 0
5470 let key_of_string =
5471 let r = Str.regexp "-" in
5472 fun s ->
5473 let elems = Str.full_split r s in
5474 let f n k m =
5475 let g s =
5476 let m1 = modifier_of_string s in
5477 if m1 = 0
5478 then (Wsi.namekey s, m)
5479 else (k, m lor m1)
5480 in function
5481 | Str.Delim s when n land 1 = 0 -> g s
5482 | Str.Text s -> g s
5483 | Str.Delim _ -> (k, m)
5485 let rec loop n k m = function
5486 | [] -> (k, m)
5487 | x :: xs ->
5488 let k, m = f n k m x in
5489 loop (n+1) k m xs
5491 loop 0 0 0 elems
5494 let keys_of_string =
5495 let r = Str.regexp "[ \t]" in
5496 fun s ->
5497 let elems = Str.split r s in
5498 List.map key_of_string elems
5501 let copykeyhashes c =
5502 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5505 let config_of c attrs =
5506 let apply c k v =
5508 match k with
5509 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5510 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5511 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5512 | "preload" -> { c with preload = bool_of_string v }
5513 | "page-bias" -> { c with pagebias = int_of_string v }
5514 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5515 | "auto-scroll-step" ->
5516 { c with autoscrollstep = max 0 (int_of_string v) }
5517 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5518 | "crop-hack" -> { c with crophack = bool_of_string v }
5519 | "throttle" ->
5520 let mw =
5521 match String.lowercase v with
5522 | "true" -> Some infinity
5523 | "false" -> None
5524 | f -> Some (float_of_string f)
5526 { c with maxwait = mw}
5527 | "highlight-links" -> { c with hlinks = bool_of_string v }
5528 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5529 | "vertical-margin" ->
5530 { c with interpagespace = max 0 (int_of_string v) }
5531 | "zoom" ->
5532 let zoom = float_of_string v /. 100. in
5533 let zoom = max zoom 0.0 in
5534 { c with zoom = zoom }
5535 | "presentation" -> { c with presentation = bool_of_string v }
5536 | "rotation-angle" -> { c with angle = int_of_string v }
5537 | "width" -> { c with winw = max 20 (int_of_string v) }
5538 | "height" -> { c with winh = max 20 (int_of_string v) }
5539 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5540 | "proportional-display" -> { c with proportional = bool_of_string v }
5541 | "pixmap-cache-size" ->
5542 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5543 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5544 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5545 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5546 | "persistent-location" -> { c with jumpback = bool_of_string v }
5547 | "background-color" -> { c with bgcolor = color_of_string v }
5548 | "scrollbar-in-presentation" ->
5549 { c with scrollbarinpm = bool_of_string v }
5550 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5551 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5552 | "mupdf-store-size" ->
5553 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5554 | "checkers" -> { c with checkers = bool_of_string v }
5555 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5556 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5557 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5558 | "uri-launcher" -> { c with urilauncher = unent v }
5559 | "path-launcher" -> { c with pathlauncher = unent v }
5560 | "color-space" -> { c with colorspace = colorspace_of_string v }
5561 | "invert-colors" -> { c with invert = bool_of_string v }
5562 | "brightness" -> { c with colorscale = float_of_string v }
5563 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5564 | "ghyllscroll" ->
5565 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5566 | "columns" ->
5567 let nab = columns_of_string v in
5568 { c with columns = Some (nab, [||]) }
5569 | "birds-eye-columns" ->
5570 { c with beyecolumns = Some (max (int_of_string v) 2) }
5571 | "selection-command" -> { c with selcmd = unent v }
5572 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5573 | _ -> c
5574 with exn ->
5575 prerr_endline ("Error processing attribute (`" ^
5576 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5579 let rec fold c = function
5580 | [] -> c
5581 | (k, v) :: rest ->
5582 let c = apply c k v in
5583 fold c rest
5585 fold { c with keyhashes = copykeyhashes c } attrs;
5588 let fromstring f pos n v d =
5589 try f v
5590 with exn ->
5591 dolog "Error processing attribute (%S=%S) at %d\n%s"
5592 n v pos (Printexc.to_string exn)
5597 let bookmark_of attrs =
5598 let rec fold title page rely = function
5599 | ("title", v) :: rest -> fold v page rely rest
5600 | ("page", v) :: rest -> fold title v rely rest
5601 | ("rely", v) :: rest -> fold title page v rest
5602 | _ :: rest -> fold title page rely rest
5603 | [] -> title, page, rely
5605 fold "invalid" "0" "0" attrs
5608 let doc_of attrs =
5609 let rec fold path page rely pan = function
5610 | ("path", v) :: rest -> fold v page rely pan rest
5611 | ("page", v) :: rest -> fold path v rely pan rest
5612 | ("rely", v) :: rest -> fold path page v pan rest
5613 | ("pan", v) :: rest -> fold path page rely v rest
5614 | _ :: rest -> fold path page rely pan rest
5615 | [] -> path, page, rely, pan
5617 fold "" "0" "0" "0" attrs
5620 let map_of attrs =
5621 let rec fold rs ls = function
5622 | ("out", v) :: rest -> fold v ls rest
5623 | ("in", v) :: rest -> fold rs v rest
5624 | _ :: rest -> fold ls rs rest
5625 | [] -> ls, rs
5627 fold "" "" attrs
5630 let setconf dst src =
5631 dst.scrollbw <- src.scrollbw;
5632 dst.scrollh <- src.scrollh;
5633 dst.icase <- src.icase;
5634 dst.preload <- src.preload;
5635 dst.pagebias <- src.pagebias;
5636 dst.verbose <- src.verbose;
5637 dst.scrollstep <- src.scrollstep;
5638 dst.maxhfit <- src.maxhfit;
5639 dst.crophack <- src.crophack;
5640 dst.autoscrollstep <- src.autoscrollstep;
5641 dst.maxwait <- src.maxwait;
5642 dst.hlinks <- src.hlinks;
5643 dst.underinfo <- src.underinfo;
5644 dst.interpagespace <- src.interpagespace;
5645 dst.zoom <- src.zoom;
5646 dst.presentation <- src.presentation;
5647 dst.angle <- src.angle;
5648 dst.winw <- src.winw;
5649 dst.winh <- src.winh;
5650 dst.savebmarks <- src.savebmarks;
5651 dst.memlimit <- src.memlimit;
5652 dst.proportional <- src.proportional;
5653 dst.texcount <- src.texcount;
5654 dst.sliceheight <- src.sliceheight;
5655 dst.thumbw <- src.thumbw;
5656 dst.jumpback <- src.jumpback;
5657 dst.bgcolor <- src.bgcolor;
5658 dst.scrollbarinpm <- src.scrollbarinpm;
5659 dst.tilew <- src.tilew;
5660 dst.tileh <- src.tileh;
5661 dst.mustoresize <- src.mustoresize;
5662 dst.checkers <- src.checkers;
5663 dst.aalevel <- src.aalevel;
5664 dst.trimmargins <- src.trimmargins;
5665 dst.trimfuzz <- src.trimfuzz;
5666 dst.urilauncher <- src.urilauncher;
5667 dst.colorspace <- src.colorspace;
5668 dst.invert <- src.invert;
5669 dst.colorscale <- src.colorscale;
5670 dst.redirectstderr <- src.redirectstderr;
5671 dst.ghyllscroll <- src.ghyllscroll;
5672 dst.columns <- src.columns;
5673 dst.beyecolumns <- src.beyecolumns;
5674 dst.selcmd <- src.selcmd;
5675 dst.updatecurs <- src.updatecurs;
5676 dst.pathlauncher <- src.pathlauncher;
5677 dst.keyhashes <- copykeyhashes src;
5680 let get s =
5681 let h = Hashtbl.create 10 in
5682 let dc = { defconf with angle = defconf.angle } in
5683 let rec toplevel v t spos _ =
5684 match t with
5685 | Vdata | Vcdata | Vend -> v
5686 | Vopen ("llppconfig", _, closed) ->
5687 if closed
5688 then v
5689 else { v with f = llppconfig }
5690 | Vopen _ ->
5691 error "unexpected subelement at top level" s spos
5692 | Vclose _ -> error "unexpected close at top level" s spos
5694 and llppconfig v t spos _ =
5695 match t with
5696 | Vdata | Vcdata -> v
5697 | Vend -> error "unexpected end of input in llppconfig" s spos
5698 | Vopen ("defaults", attrs, closed) ->
5699 let c = config_of dc attrs in
5700 setconf dc c;
5701 if closed
5702 then v
5703 else { v with f = defaults }
5705 | Vopen ("ui-font", attrs, closed) ->
5706 let rec getsize size = function
5707 | [] -> size
5708 | ("size", v) :: rest ->
5709 let size =
5710 fromstring int_of_string spos "size" v fstate.fontsize in
5711 getsize size rest
5712 | l -> getsize size l
5714 fstate.fontsize <- getsize fstate.fontsize attrs;
5715 if closed
5716 then v
5717 else { v with f = uifont (Buffer.create 10) }
5719 | Vopen ("doc", attrs, closed) ->
5720 let pathent, spage, srely, span = doc_of attrs in
5721 let path = unent pathent
5722 and pageno = fromstring int_of_string spos "page" spage 0
5723 and rely = fromstring float_of_string spos "rely" srely 0.0
5724 and pan = fromstring int_of_string spos "pan" span 0 in
5725 let c = config_of dc attrs in
5726 let anchor = (pageno, rely) in
5727 if closed
5728 then (Hashtbl.add h path (c, [], pan, anchor); v)
5729 else { v with f = doc path pan anchor c [] }
5731 | Vopen _ ->
5732 error "unexpected subelement in llppconfig" s spos
5734 | Vclose "llppconfig" -> { v with f = toplevel }
5735 | Vclose _ -> error "unexpected close in llppconfig" s spos
5737 and defaults v t spos _ =
5738 match t with
5739 | Vdata | Vcdata -> v
5740 | Vend -> error "unexpected end of input in defaults" s spos
5741 | Vopen ("keymap", attrs, closed) ->
5742 let modename =
5743 try List.assoc "mode" attrs
5744 with Not_found -> "global" in
5745 if closed
5746 then v
5747 else
5748 let ret keymap =
5749 let h = findkeyhash dc modename in
5750 KeyMap.iter (Hashtbl.replace h) keymap;
5751 defaults
5753 { v with f = pkeymap ret KeyMap.empty }
5755 | Vopen (_, _, _) ->
5756 error "unexpected subelement in defaults" s spos
5758 | Vclose "defaults" ->
5759 { v with f = llppconfig }
5761 | Vclose _ -> error "unexpected close in defaults" s spos
5763 and uifont b v t spos epos =
5764 match t with
5765 | Vdata | Vcdata ->
5766 Buffer.add_substring b s spos (epos - spos);
5768 | Vopen (_, _, _) ->
5769 error "unexpected subelement in ui-font" s spos
5770 | Vclose "ui-font" ->
5771 if String.length !fontpath = 0
5772 then fontpath := Buffer.contents b;
5773 { v with f = llppconfig }
5774 | Vclose _ -> error "unexpected close in ui-font" s spos
5775 | Vend -> error "unexpected end of input in ui-font" s spos
5777 and doc path pan anchor c bookmarks v t spos _ =
5778 match t with
5779 | Vdata | Vcdata -> v
5780 | Vend -> error "unexpected end of input in doc" s spos
5781 | Vopen ("bookmarks", _, closed) ->
5782 if closed
5783 then v
5784 else { v with f = pbookmarks path pan anchor c bookmarks }
5786 | Vopen ("keymap", attrs, closed) ->
5787 let modename =
5788 try List.assoc "mode" attrs
5789 with Not_found -> "global"
5791 if closed
5792 then v
5793 else
5794 let ret keymap =
5795 let h = findkeyhash c modename in
5796 KeyMap.iter (Hashtbl.replace h) keymap;
5797 doc path pan anchor c bookmarks
5799 { v with f = pkeymap ret KeyMap.empty }
5801 | Vopen (_, _, _) ->
5802 error "unexpected subelement in doc" s spos
5804 | Vclose "doc" ->
5805 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5806 { v with f = llppconfig }
5808 | Vclose _ -> error "unexpected close in doc" s spos
5810 and pkeymap ret keymap v t spos _ =
5811 match t with
5812 | Vdata | Vcdata -> v
5813 | Vend -> error "unexpected end of input in keymap" s spos
5814 | Vopen ("map", attrs, closed) ->
5815 let r, l = map_of attrs in
5816 let kss = fromstring keys_of_string spos "in" r [] in
5817 let lss = fromstring keys_of_string spos "out" l [] in
5818 let keymap =
5819 match kss with
5820 | [] -> keymap
5821 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
5822 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
5824 if closed
5825 then { v with f = pkeymap ret keymap }
5826 else
5827 let f () = v in
5828 { v with f = skip "map" f }
5830 | Vopen _ ->
5831 error "unexpected subelement in keymap" s spos
5833 | Vclose "keymap" ->
5834 { v with f = ret keymap }
5836 | Vclose _ -> error "unexpected close in keymap" s spos
5838 and pbookmarks path pan anchor c bookmarks v t spos _ =
5839 match t with
5840 | Vdata | Vcdata -> v
5841 | Vend -> error "unexpected end of input in bookmarks" s spos
5842 | Vopen ("item", attrs, closed) ->
5843 let titleent, spage, srely = bookmark_of attrs in
5844 let page = fromstring int_of_string spos "page" spage 0
5845 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5846 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5847 if closed
5848 then { v with f = pbookmarks path pan anchor c bookmarks }
5849 else
5850 let f () = v in
5851 { v with f = skip "item" f }
5853 | Vopen _ ->
5854 error "unexpected subelement in bookmarks" s spos
5856 | Vclose "bookmarks" ->
5857 { v with f = doc path pan anchor c bookmarks }
5859 | Vclose _ -> error "unexpected close in bookmarks" s spos
5861 and skip tag f v t spos _ =
5862 match t with
5863 | Vdata | Vcdata -> v
5864 | Vend ->
5865 error ("unexpected end of input in skipped " ^ tag) s spos
5866 | Vopen (tag', _, closed) ->
5867 if closed
5868 then v
5869 else
5870 let f' () = { v with f = skip tag f } in
5871 { v with f = skip tag' f' }
5872 | Vclose ctag ->
5873 if tag = ctag
5874 then f ()
5875 else error ("unexpected close in skipped " ^ tag) s spos
5878 parse { f = toplevel; accu = () } s;
5879 h, dc;
5882 let do_load f ic =
5884 let len = in_channel_length ic in
5885 let s = String.create len in
5886 really_input ic s 0 len;
5887 f s;
5888 with
5889 | Parse_error (msg, s, pos) ->
5890 let subs = subs s pos in
5891 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5892 failwith ("parse error: " ^ s)
5894 | exn ->
5895 failwith ("config load error: " ^ Printexc.to_string exn)
5898 let defconfpath =
5899 let dir =
5901 let dir = Filename.concat home ".config" in
5902 if Sys.is_directory dir then dir else home
5903 with _ -> home
5905 Filename.concat dir "llpp.conf"
5908 let confpath = ref defconfpath;;
5910 let load1 f =
5911 if Sys.file_exists !confpath
5912 then
5913 match
5914 (try Some (open_in_bin !confpath)
5915 with exn ->
5916 prerr_endline
5917 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5918 Printexc.to_string exn);
5919 None
5921 with
5922 | Some ic ->
5923 begin try
5924 f (do_load get ic)
5925 with exn ->
5926 prerr_endline
5927 ("Error loading configuation from `" ^ !confpath ^ "': " ^
5928 Printexc.to_string exn);
5929 end;
5930 close_in ic;
5932 | None -> ()
5933 else
5934 f (Hashtbl.create 0, defconf)
5937 let load () =
5938 let f (h, dc) =
5939 let pc, pb, px, pa =
5941 Hashtbl.find h (Filename.basename state.path)
5942 with Not_found -> dc, [], 0, (0, 0.0)
5944 setconf defconf dc;
5945 setconf conf pc;
5946 state.bookmarks <- pb;
5947 state.x <- px;
5948 state.scrollw <- conf.scrollbw;
5949 if conf.jumpback
5950 then state.anchor <- pa;
5951 cbput state.hists.nav pa;
5953 load1 f
5956 let add_attrs bb always dc c =
5957 let ob s a b =
5958 if always || a != b
5959 then Printf.bprintf bb "\n %s='%b'" s a
5960 and oi s a b =
5961 if always || a != b
5962 then Printf.bprintf bb "\n %s='%d'" s a
5963 and oI s a b =
5964 if always || a != b
5965 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
5966 and oz s a b =
5967 if always || a <> b
5968 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
5969 and oF s a b =
5970 if always || a <> b
5971 then Printf.bprintf bb "\n %s='%f'" s a
5972 and oc s a b =
5973 if always || a <> b
5974 then
5975 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
5976 and oC s a b =
5977 if always || a <> b
5978 then
5979 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
5980 and oR s a b =
5981 if always || a <> b
5982 then
5983 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
5984 and os s a b =
5985 if always || a <> b
5986 then
5987 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
5988 and og s a b =
5989 if always || a <> b
5990 then
5991 match a with
5992 | None -> ()
5993 | Some (_N, _A, _B) ->
5994 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
5995 and oW s a b =
5996 if always || a <> b
5997 then
5998 let v =
5999 match a with
6000 | None -> "false"
6001 | Some f ->
6002 if f = infinity
6003 then "true"
6004 else string_of_float f
6006 Printf.bprintf bb "\n %s='%s'" s v
6007 and oco s a b =
6008 if always || a <> b
6009 then
6010 match a with
6011 | Some ((n, a, b), _) when n > 1 ->
6012 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6013 | _ -> ()
6014 and obeco s a b =
6015 if always || a <> b
6016 then
6017 match a with
6018 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6019 | _ -> ()
6021 let w, h =
6022 if always
6023 then dc.winw, dc.winh
6024 else
6025 match state.fullscreen with
6026 | Some wh -> wh
6027 | None -> c.winw, c.winh
6029 let zoom, presentation, interpagespace, maxwait =
6030 if always
6031 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6032 else
6033 match state.mode with
6034 | Birdseye (bc, _, _, _, _) ->
6035 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6036 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6038 oi "width" w dc.winw;
6039 oi "height" h dc.winh;
6040 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6041 oi "scroll-handle-height" c.scrollh dc.scrollh;
6042 ob "case-insensitive-search" c.icase dc.icase;
6043 ob "preload" c.preload dc.preload;
6044 oi "page-bias" c.pagebias dc.pagebias;
6045 oi "scroll-step" c.scrollstep dc.scrollstep;
6046 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6047 ob "max-height-fit" c.maxhfit dc.maxhfit;
6048 ob "crop-hack" c.crophack dc.crophack;
6049 oW "throttle" maxwait dc.maxwait;
6050 ob "highlight-links" c.hlinks dc.hlinks;
6051 ob "under-cursor-info" c.underinfo dc.underinfo;
6052 oi "vertical-margin" interpagespace dc.interpagespace;
6053 oz "zoom" zoom dc.zoom;
6054 ob "presentation" presentation dc.presentation;
6055 oi "rotation-angle" c.angle dc.angle;
6056 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6057 ob "proportional-display" c.proportional dc.proportional;
6058 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6059 oi "tex-count" c.texcount dc.texcount;
6060 oi "slice-height" c.sliceheight dc.sliceheight;
6061 oi "thumbnail-width" c.thumbw dc.thumbw;
6062 ob "persistent-location" c.jumpback dc.jumpback;
6063 oc "background-color" c.bgcolor dc.bgcolor;
6064 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6065 oi "tile-width" c.tilew dc.tilew;
6066 oi "tile-height" c.tileh dc.tileh;
6067 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6068 ob "checkers" c.checkers dc.checkers;
6069 oi "aalevel" c.aalevel dc.aalevel;
6070 ob "trim-margins" c.trimmargins dc.trimmargins;
6071 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6072 os "uri-launcher" c.urilauncher dc.urilauncher;
6073 os "path-launcher" c.pathlauncher dc.pathlauncher;
6074 oC "color-space" c.colorspace dc.colorspace;
6075 ob "invert-colors" c.invert dc.invert;
6076 oF "brightness" c.colorscale dc.colorscale;
6077 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6078 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6079 oco "columns" c.columns dc.columns;
6080 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6081 os "selection-command" c.selcmd dc.selcmd;
6082 ob "update-cursor" c.updatecurs dc.updatecurs;
6085 let keymapsbuf always dc c =
6086 let bb = Buffer.create 16 in
6087 let rec loop = function
6088 | [] -> ()
6089 | (modename, h) :: rest ->
6090 let dh = findkeyhash dc modename in
6091 if always || h <> dh
6092 then (
6093 if Hashtbl.length h > 0
6094 then (
6095 if Buffer.length bb > 0
6096 then Buffer.add_char bb '\n';
6097 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6098 Hashtbl.iter (fun i o ->
6099 let isdifferent = always ||
6101 let dO = Hashtbl.find dh i in
6102 dO <> o
6103 with Not_found -> true
6105 if isdifferent
6106 then
6107 let addkm (k, m) =
6108 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6109 if Wsi.withalt m then Buffer.add_string bb "alt-";
6110 if Wsi.withshift m then Buffer.add_string bb "shift-";
6111 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6112 Buffer.add_string bb (Wsi.keyname k);
6114 let addkms l =
6115 let rec loop = function
6116 | [] -> ()
6117 | km :: [] -> addkm km
6118 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6120 loop l
6122 Buffer.add_string bb "<map in='";
6123 addkm i;
6124 match o with
6125 | KMinsrt km ->
6126 Buffer.add_string bb "' out='";
6127 addkm km;
6128 Buffer.add_string bb "'/>\n"
6130 | KMinsrl kms ->
6131 Buffer.add_string bb "' out='";
6132 addkms kms;
6133 Buffer.add_string bb "'/>\n"
6135 | KMmulti (ins, kms) ->
6136 Buffer.add_char bb ' ';
6137 addkms ins;
6138 Buffer.add_string bb "' out='";
6139 addkms kms;
6140 Buffer.add_string bb "'/>\n"
6141 ) h;
6142 Buffer.add_string bb "</keymap>";
6145 loop rest
6147 loop c.keyhashes;
6151 let save () =
6152 let uifontsize = fstate.fontsize in
6153 let bb = Buffer.create 32768 in
6154 let f (h, dc) =
6155 let dc = if conf.bedefault then conf else dc in
6156 Buffer.add_string bb "<llppconfig>\n";
6158 if String.length !fontpath > 0
6159 then
6160 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6161 uifontsize
6162 !fontpath
6163 else (
6164 if uifontsize <> 14
6165 then
6166 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6169 Buffer.add_string bb "<defaults ";
6170 add_attrs bb true dc dc;
6171 let kb = keymapsbuf true dc dc in
6172 if Buffer.length kb > 0
6173 then (
6174 Buffer.add_string bb ">\n";
6175 Buffer.add_buffer bb kb;
6176 Buffer.add_string bb "\n</defaults>\n";
6178 else Buffer.add_string bb "/>\n";
6180 let adddoc path pan anchor c bookmarks =
6181 if bookmarks == [] && c = dc && anchor = emptyanchor
6182 then ()
6183 else (
6184 Printf.bprintf bb "<doc path='%s'"
6185 (enent path 0 (String.length path));
6187 if anchor <> emptyanchor
6188 then (
6189 let n, y = anchor in
6190 Printf.bprintf bb " page='%d'" n;
6191 if y > 1e-6
6192 then
6193 Printf.bprintf bb " rely='%f'" y
6197 if pan != 0
6198 then Printf.bprintf bb " pan='%d'" pan;
6200 add_attrs bb false dc c;
6201 let kb = keymapsbuf false dc c in
6203 begin match bookmarks with
6204 | [] ->
6205 if Buffer.length kb > 0
6206 then (
6207 Buffer.add_string bb ">\n";
6208 Buffer.add_buffer bb kb;
6209 Buffer.add_string bb "</doc>\n";
6211 else Buffer.add_string bb "/>\n"
6212 | _ ->
6213 Buffer.add_string bb ">\n<bookmarks>\n";
6214 List.iter (fun (title, _level, (page, rely)) ->
6215 Printf.bprintf bb
6216 "<item title='%s' page='%d'"
6217 (enent title 0 (String.length title))
6218 page
6220 if rely > 1e-6
6221 then
6222 Printf.bprintf bb " rely='%f'" rely
6224 Buffer.add_string bb "/>\n";
6225 ) bookmarks;
6226 Buffer.add_string bb "</bookmarks>";
6227 if Buffer.length kb > 0
6228 then (
6229 Buffer.add_string bb "\n";
6230 Buffer.add_buffer bb kb;
6232 Buffer.add_string bb "\n</doc>\n";
6233 end;
6237 let pan, conf =
6238 match state.mode with
6239 | Birdseye (c, pan, _, _, _) ->
6240 let beyecolumns =
6241 match conf.columns with
6242 | Some ((c, _, _), _) -> Some c
6243 | None -> None
6244 and columns =
6245 match c.columns with
6246 | Some (c, _) -> Some (c, [||])
6247 | None -> None
6249 pan, { c with beyecolumns = beyecolumns; columns = columns }
6250 | _ -> state.x, conf
6252 let basename = Filename.basename state.path in
6253 adddoc basename pan (getanchor ())
6254 { conf with
6255 autoscrollstep =
6256 match state.autoscroll with
6257 | Some step -> step
6258 | None -> conf.autoscrollstep }
6259 (if conf.savebmarks then state.bookmarks else []);
6261 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6262 if basename <> path
6263 then adddoc path x y c bookmarks
6264 ) h;
6265 Buffer.add_string bb "</llppconfig>";
6267 load1 f;
6268 if Buffer.length bb > 0
6269 then
6271 let tmp = !confpath ^ ".tmp" in
6272 let oc = open_out_bin tmp in
6273 Buffer.output_buffer oc bb;
6274 close_out oc;
6275 Unix.rename tmp !confpath;
6276 with exn ->
6277 prerr_endline
6278 ("error while saving configuration: " ^ Printexc.to_string exn)
6280 end;;
6282 let () =
6283 Arg.parse
6284 (Arg.align
6285 [("-p", Arg.String (fun s -> state.password <- s) ,
6286 "<password> Set password");
6288 ("-f", Arg.String (fun s -> Config.fontpath := s),
6289 "<path> Set path to the user interface font");
6291 ("-c", Arg.String (fun s -> Config.confpath := s),
6292 "<path> Set path to the configuration file");
6294 ("-v", Arg.Unit (fun () ->
6295 Printf.printf
6296 "%s\nconfiguration path: %s\n"
6297 (version ())
6298 Config.defconfpath
6300 exit 0), " Print version and exit");
6303 (fun s -> state.path <- s)
6304 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6306 if String.length state.path = 0
6307 then (prerr_endline "file name missing"; exit 1);
6309 Config.load ();
6311 let globalkeyhash = findkeyhash conf "global" in
6312 let wsfd, winw, winh = Wsi.init (object
6313 method expose =
6314 if nogeomcmds state.geomcmds
6315 then display ()
6316 method display = display ()
6317 method reshape w h = reshape w h
6318 method mouse b d x y m = mouse b d x y m
6319 method motion x y = state.mpos <- (x, y); motion x y
6320 method pmotion x y = state.mpos <- (x, y); pmotion x y
6321 method key k m =
6322 let mascm = m land (
6323 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6324 ) in
6325 match state.keystate with
6326 | KSnone ->
6327 let km = k, mascm in
6328 begin
6329 match
6330 try Hashtbl.find globalkeyhash km
6331 with Not_found ->
6332 let modehash = state.uioh#modehash in
6333 try Hashtbl.find modehash km
6334 with Not_found -> KMinsrt (k, m)
6335 with
6336 | KMinsrt (k, m) -> keyboard k m
6337 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6338 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6340 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6341 List.iter (fun (k, m) -> keyboard k m) insrt;
6342 state.keystate <- KSnone
6343 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6344 state.keystate <- KSinto (keys, insrt)
6345 | _ ->
6346 state.keystate <- KSnone
6348 method enter x y = state.mpos <- (x, y); pmotion x y
6349 method leave = state.mpos <- (-1, -1)
6350 method quit = raise Quit
6351 end) conf.winw conf.winh in
6353 state.wsfd <- wsfd;
6355 if not (
6356 List.exists GlMisc.check_extension
6357 [ "GL_ARB_texture_rectangle"
6358 ; "GL_EXT_texture_recangle"
6359 ; "GL_NV_texture_rectangle" ]
6361 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6363 let cr, sw = Unix.pipe ()
6364 and sr, cw = Unix.pipe () in
6366 cloexec cr;
6367 cloexec sw;
6368 cloexec sr;
6369 cloexec cw;
6371 setcheckers conf.checkers;
6372 redirectstderr ();
6374 init (cr, cw) (
6375 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6376 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6377 !Config.fontpath
6379 state.sr <- sr;
6380 state.sw <- sw;
6381 state.text <- "Opening " ^ state.path;
6382 opendoc state.path state.password;
6383 state.uioh <- uioh;
6384 setfontsize fstate.fontsize;
6385 reshape winw winh;
6387 let rec loop deadline =
6388 let r =
6389 match state.errfd with
6390 | None -> [state.sr; state.wsfd]
6391 | Some fd -> [state.sr; state.wsfd; fd]
6393 if state.redisplay
6394 then (
6395 state.redisplay <- false;
6396 display ();
6398 let timeout =
6399 let now = now () in
6400 if deadline > now
6401 then (
6402 if deadline = infinity
6403 then ~-.1.0
6404 else max 0.0 (deadline -. now)
6406 else 0.0
6408 let r, _, _ =
6409 try Unix.select r [] [] timeout
6410 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6412 begin match r with
6413 | [] ->
6414 state.ghyll None;
6415 let newdeadline =
6416 if state.ghyll == noghyll
6417 then
6418 match state.autoscroll with
6419 | Some step when step != 0 ->
6420 let y = state.y + step in
6421 let y =
6422 if y < 0
6423 then state.maxy
6424 else if y >= state.maxy then 0 else y
6426 gotoy y;
6427 if state.mode = View
6428 then state.text <- "";
6429 deadline +. 0.01
6430 | _ -> infinity
6431 else deadline +. 0.01
6433 loop newdeadline
6435 | l ->
6436 let rec checkfds = function
6437 | [] -> ()
6438 | fd :: rest when fd = state.sr ->
6439 let cmd = readcmd state.sr in
6440 act cmd;
6441 checkfds rest
6443 | fd :: rest when fd = state.wsfd ->
6444 Wsi.readresp fd;
6445 checkfds rest
6447 | fd :: rest ->
6448 let s = String.create 80 in
6449 let n = Unix.read fd s 0 80 in
6450 if conf.redirectstderr
6451 then (
6452 Buffer.add_substring state.errmsgs s 0 n;
6453 state.newerrmsgs <- true;
6454 state.redisplay <- true;
6456 else (
6457 prerr_string (String.sub s 0 n);
6458 flush stderr;
6460 checkfds rest
6462 checkfds l;
6463 let newdeadline =
6464 let deadline1 =
6465 if deadline = infinity
6466 then now () +. 0.01
6467 else deadline
6469 match state.autoscroll with
6470 | Some step when step != 0 -> deadline1
6471 | _ -> if state.ghyll == noghyll then infinity else deadline1
6473 loop newdeadline
6474 end;
6477 loop infinity;
6478 with Quit ->
6479 wcmd "quit";
6480 Config.save ();
6481 exit 0;