Continue fixing grave slinks related bug(s)
[llpp.git] / main.ml
blob19806195c7c483cd41d692a8acb1f7cfd026a189
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 <- [];
1951 begin match state.mode with
1952 | LinkNav (Ltexact _) -> state.mode <- LinkNav (Ltgendir 0)
1953 | _ -> ()
1954 end;
1956 | "clearrects" ->
1957 state.rects <- state.rects1;
1958 G.postRedisplay "clearrects";
1960 | "continue" ->
1961 let n =
1962 try Scanf.sscanf args "%u" (fun n -> n)
1963 with exn ->
1964 dolog "error processing 'continue' %S: %s"
1965 cmds (Printexc.to_string exn);
1966 exit 1;
1968 state.pagecount <- n;
1969 begin match state.currently with
1970 | Outlining l ->
1971 state.currently <- Idle;
1972 state.outlines <- Array.of_list (List.rev l)
1973 | _ -> ()
1974 end;
1976 let cur, cmds = state.geomcmds in
1977 if String.length cur = 0
1978 then failwith "umpossible";
1980 begin match List.rev cmds with
1981 | [] ->
1982 state.geomcmds <- "", [];
1983 represent ();
1984 | (s, f) :: rest ->
1985 f ();
1986 state.geomcmds <- s, List.rev rest;
1987 end;
1988 if conf.maxwait = None
1989 then G.postRedisplay "continue";
1991 | "title" ->
1992 Wsi.settitle args
1994 | "msg" ->
1995 showtext ' ' args
1997 | "vmsg" ->
1998 if conf.verbose
1999 then showtext ' ' args
2001 | "progress" ->
2002 let progress, text =
2004 Scanf.sscanf args "%f %n"
2005 (fun f pos ->
2006 f, String.sub args pos (String.length args - pos))
2007 with exn ->
2008 dolog "error processing 'progress' %S: %s"
2009 cmds (Printexc.to_string exn);
2010 exit 1;
2012 state.text <- text;
2013 state.progress <- progress;
2014 G.postRedisplay "progress"
2016 | "firstmatch" ->
2017 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2019 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2020 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2021 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2022 with exn ->
2023 dolog "error processing 'firstmatch' %S: %s"
2024 cmds (Printexc.to_string exn);
2025 exit 1;
2027 let y = (getpagey pageno) + truncate y0 in
2028 addnav ();
2029 gotoy y;
2030 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2032 | "match" ->
2033 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2035 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2036 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2037 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2038 with exn ->
2039 dolog "error processing 'match' %S: %s"
2040 cmds (Printexc.to_string exn);
2041 exit 1;
2043 state.rects1 <-
2044 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2046 | "page" ->
2047 let pageopaque, t =
2049 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2050 with exn ->
2051 dolog "error processing 'page' %S: %s"
2052 cmds (Printexc.to_string exn);
2053 exit 1;
2055 begin match state.currently with
2056 | Loading (l, gen) ->
2057 vlog "page %d took %f sec" l.pageno t;
2058 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2059 begin match state.throttle with
2060 | None ->
2061 let preloadedpages =
2062 if conf.preload
2063 then preloadlayout state.layout
2064 else state.layout
2066 let evict () =
2067 let module IntSet =
2068 Set.Make (struct type t = int let compare = (-) end) in
2069 let set =
2070 List.fold_left (fun s l -> IntSet.add l.pageno s)
2071 IntSet.empty preloadedpages
2073 let evictedpages =
2074 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2075 if not (IntSet.mem pageno set)
2076 then (
2077 wcmd "freepage %s" opaque;
2078 key :: accu
2080 else accu
2081 ) state.pagemap []
2083 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2085 evict ();
2086 state.currently <- Idle;
2087 if gen = state.gen
2088 then (
2089 tilepage l.pageno pageopaque state.layout;
2090 load state.layout;
2091 load preloadedpages;
2092 if pagevisible state.layout l.pageno
2093 && layoutready state.layout
2094 then G.postRedisplay "page";
2097 | Some (layout, _, _) ->
2098 state.currently <- Idle;
2099 tilepage l.pageno pageopaque layout;
2100 load state.layout
2101 end;
2103 | _ ->
2104 dolog "Inconsistent loading state";
2105 logcurrently state.currently;
2106 exit 1
2109 | "tile" ->
2110 let (x, y, opaque, size, t) =
2112 Scanf.sscanf args "%u %u %s %u %f"
2113 (fun x y p size t -> (x, y, p, size, t))
2114 with exn ->
2115 dolog "error processing 'tile' %S: %s"
2116 cmds (Printexc.to_string exn);
2117 exit 1;
2119 begin match state.currently with
2120 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2121 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2123 if tilew != conf.tilew || tileh != conf.tileh
2124 then (
2125 wcmd "freetile %s" opaque;
2126 state.currently <- Idle;
2127 load state.layout;
2129 else (
2130 puttileopaque l col row gen cs angle opaque size t;
2131 state.memused <- state.memused + size;
2132 state.uioh#infochanged Memused;
2133 gctiles ();
2134 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2135 opaque, size) state.tilelru;
2137 let layout =
2138 match state.throttle with
2139 | None -> state.layout
2140 | Some (layout, _, _) -> layout
2143 state.currently <- Idle;
2144 if gen = state.gen
2145 && conf.colorspace = cs
2146 && conf.angle = angle
2147 && tilevisible layout l.pageno x y
2148 then conttiling l.pageno pageopaque;
2150 begin match state.throttle with
2151 | None ->
2152 preload state.layout;
2153 if gen = state.gen
2154 && conf.colorspace = cs
2155 && conf.angle = angle
2156 && tilevisible state.layout l.pageno x y
2157 then G.postRedisplay "tile nothrottle";
2159 | Some (layout, y, _) ->
2160 let ready = layoutready layout in
2161 if ready
2162 then (
2163 state.y <- y;
2164 state.layout <- layout;
2165 state.throttle <- None;
2166 G.postRedisplay "throttle";
2168 else load layout;
2169 end;
2172 | _ ->
2173 dolog "Inconsistent tiling state";
2174 logcurrently state.currently;
2175 exit 1
2178 | "pdim" ->
2179 let pdim =
2181 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2182 with exn ->
2183 dolog "error processing 'pdim' %S: %s"
2184 cmds (Printexc.to_string exn);
2185 exit 1;
2187 state.uioh#infochanged Pdim;
2188 state.pdims <- pdim :: state.pdims
2190 | "o" ->
2191 let (l, n, t, h, pos) =
2193 Scanf.sscanf args "%u %u %d %u %n"
2194 (fun l n t h pos -> l, n, t, h, pos)
2195 with exn ->
2196 dolog "error processing 'o' %S: %s"
2197 cmds (Printexc.to_string exn);
2198 exit 1;
2200 let s = String.sub args pos (String.length args - pos) in
2201 let outline = (s, l, (n, float t /. float h)) in
2202 begin match state.currently with
2203 | Outlining outlines ->
2204 state.currently <- Outlining (outline :: outlines)
2205 | Idle ->
2206 state.currently <- Outlining [outline]
2207 | currently ->
2208 dolog "invalid outlining state";
2209 logcurrently currently
2212 | "info" ->
2213 state.docinfo <- (1, args) :: state.docinfo
2215 | "infoend" ->
2216 state.uioh#infochanged Docinfo;
2217 state.docinfo <- List.rev state.docinfo
2219 | _ ->
2220 dolog "unknown cmd `%S'" cmds
2223 let onhist cb =
2224 let rc = cb.rc in
2225 let action = function
2226 | HCprev -> cbget cb ~-1
2227 | HCnext -> cbget cb 1
2228 | HCfirst -> cbget cb ~-(cb.rc)
2229 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2230 and cancel () = cb.rc <- rc
2231 in (action, cancel)
2234 let search pattern forward =
2235 if String.length pattern > 0
2236 then
2237 let pn, py =
2238 match state.layout with
2239 | [] -> 0, 0
2240 | l :: _ ->
2241 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2243 wcmd "search %d %d %d %d,%s\000"
2244 (btod conf.icase) pn py (btod forward) pattern;
2247 let intentry text key =
2248 let c =
2249 if key >= 32 && key < 127
2250 then Char.chr key
2251 else '\000'
2253 match c with
2254 | '0' .. '9' ->
2255 let text = addchar text c in
2256 TEcont text
2258 | _ ->
2259 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2260 TEcont text
2263 let textentry text key =
2264 if key land 0xff00 = 0xff00
2265 then TEcont text
2266 else TEcont (text ^ Wsi.toutf8 key)
2269 let reqlayout angle proportional =
2270 match state.throttle with
2271 | None ->
2272 if nogeomcmds state.geomcmds
2273 then state.anchor <- getanchor ();
2274 conf.angle <- angle mod 360;
2275 if conf.angle != 0
2276 then (
2277 match state.mode with
2278 | LinkNav _ -> state.mode <- View
2279 | _ -> ()
2281 conf.proportional <- proportional;
2282 invalidate "reqlayout"
2283 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2284 | _ -> ()
2287 let settrim trimmargins trimfuzz =
2288 if nogeomcmds state.geomcmds
2289 then state.anchor <- getanchor ();
2290 conf.trimmargins <- trimmargins;
2291 conf.trimfuzz <- trimfuzz;
2292 let x0, y0, x1, y1 = trimfuzz in
2293 invalidate "settrim"
2294 (fun () ->
2295 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2296 Hashtbl.iter (fun _ opaque ->
2297 wcmd "freepage %s" opaque;
2298 ) state.pagemap;
2299 Hashtbl.clear state.pagemap;
2302 let setzoom zoom =
2303 match state.throttle with
2304 | None ->
2305 let zoom = max 0.01 zoom in
2306 if zoom <> conf.zoom
2307 then (
2308 state.prevzoom <- conf.zoom;
2309 conf.zoom <- zoom;
2310 reshape conf.winw conf.winh;
2311 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2314 | Some (layout, y, started) ->
2315 let time =
2316 match conf.maxwait with
2317 | None -> 0.0
2318 | Some t -> t
2320 let dt = now () -. started in
2321 if dt > time
2322 then (
2323 state.y <- y;
2324 load layout;
2328 let setcolumns columns coverA coverB =
2329 if columns < 2
2330 then (
2331 conf.columns <- None;
2332 state.x <- 0;
2333 setzoom 1.0;
2335 else (
2336 conf.columns <- Some ((columns, coverA, coverB), [||]);
2337 conf.zoom <- 1.0;
2339 reshape conf.winw conf.winh;
2342 let enterbirdseye () =
2343 let zoom = float conf.thumbw /. float conf.winw in
2344 let birdseyepageno =
2345 let cy = conf.winh / 2 in
2346 let fold = function
2347 | [] -> 0
2348 | l :: rest ->
2349 let rec fold best = function
2350 | [] -> best.pageno
2351 | l :: rest ->
2352 let d = cy - (l.pagedispy + l.pagevh/2)
2353 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2354 if abs d < abs dbest
2355 then fold l rest
2356 else best.pageno
2357 in fold l rest
2359 fold state.layout
2361 state.mode <- Birdseye (
2362 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2364 conf.zoom <- zoom;
2365 conf.presentation <- false;
2366 conf.interpagespace <- 10;
2367 conf.hlinks <- false;
2368 state.x <- 0;
2369 state.mstate <- Mnone;
2370 conf.maxwait <- None;
2371 conf.columns <- (
2372 match conf.beyecolumns with
2373 | Some c ->
2374 conf.zoom <- 1.0;
2375 Some ((c, 0, 0), [||])
2376 | None -> None
2378 Wsi.setcursor Wsi.CURSOR_INHERIT;
2379 if conf.verbose
2380 then
2381 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2382 (100.0*.zoom)
2383 else
2384 state.text <- ""
2386 reshape conf.winw conf.winh;
2389 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2390 state.mode <- View;
2391 conf.zoom <- c.zoom;
2392 conf.presentation <- c.presentation;
2393 conf.interpagespace <- c.interpagespace;
2394 conf.maxwait <- c.maxwait;
2395 conf.hlinks <- c.hlinks;
2396 conf.beyecolumns <- (
2397 match conf.columns with
2398 | Some ((c, _, _), _) -> Some c
2399 | None -> None
2401 conf.columns <- (
2402 match c.columns with
2403 | Some (c, _) -> Some (c, [||])
2404 | None -> None
2406 state.x <- leftx;
2407 if conf.verbose
2408 then
2409 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2410 (100.0*.conf.zoom)
2412 reshape conf.winw conf.winh;
2413 state.anchor <- if goback then anchor else (pageno, 0.0);
2416 let togglebirdseye () =
2417 match state.mode with
2418 | Birdseye vals -> leavebirdseye vals true
2419 | View -> enterbirdseye ()
2420 | _ -> ()
2423 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2424 let pageno = max 0 (pageno - incr) in
2425 let rec loop = function
2426 | [] -> gotopage1 pageno 0
2427 | l :: _ when l.pageno = pageno ->
2428 if l.pagedispy >= 0 && l.pagey = 0
2429 then G.postRedisplay "upbirdseye"
2430 else gotopage1 pageno 0
2431 | _ :: rest -> loop rest
2433 loop state.layout;
2434 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2437 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2438 let pageno = min (state.pagecount - 1) (pageno + incr) in
2439 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2440 let rec loop = function
2441 | [] ->
2442 let y, h = getpageyh pageno in
2443 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2444 gotoy (clamp dy)
2445 | l :: _ when l.pageno = pageno ->
2446 if l.pagevh != l.pageh
2447 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2448 else G.postRedisplay "downbirdseye"
2449 | _ :: rest -> loop rest
2451 loop state.layout
2454 let optentry mode _ key =
2455 let btos b = if b then "on" else "off" in
2456 if key >= 32 && key < 127
2457 then
2458 let c = Char.chr key in
2459 match c with
2460 | 's' ->
2461 let ondone s =
2462 try conf.scrollstep <- int_of_string s with exc ->
2463 state.text <- Printf.sprintf "bad integer `%s': %s"
2464 s (Printexc.to_string exc)
2466 TEswitch ("scroll step: ", "", None, intentry, ondone)
2468 | 'A' ->
2469 let ondone s =
2471 conf.autoscrollstep <- int_of_string s;
2472 if state.autoscroll <> None
2473 then state.autoscroll <- Some conf.autoscrollstep
2474 with exc ->
2475 state.text <- Printf.sprintf "bad integer `%s': %s"
2476 s (Printexc.to_string exc)
2478 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2480 | 'C' ->
2481 let ondone s =
2483 let n, a, b = columns_of_string s in
2484 setcolumns n a b;
2485 with exc ->
2486 state.text <- Printf.sprintf "bad columns `%s': %s"
2487 s (Printexc.to_string exc)
2489 TEswitch ("columns: ", "", None, textentry, ondone)
2491 | 'Z' ->
2492 let ondone s =
2494 let zoom = float (int_of_string s) /. 100.0 in
2495 setzoom zoom
2496 with exc ->
2497 state.text <- Printf.sprintf "bad integer `%s': %s"
2498 s (Printexc.to_string exc)
2500 TEswitch ("zoom: ", "", None, intentry, ondone)
2502 | 't' ->
2503 let ondone s =
2505 conf.thumbw <- bound (int_of_string s) 2 4096;
2506 state.text <-
2507 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2508 begin match mode with
2509 | Birdseye beye ->
2510 leavebirdseye beye false;
2511 enterbirdseye ();
2512 | _ -> ();
2514 with exc ->
2515 state.text <- Printf.sprintf "bad integer `%s': %s"
2516 s (Printexc.to_string exc)
2518 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2520 | 'R' ->
2521 let ondone s =
2522 match try
2523 Some (int_of_string s)
2524 with exc ->
2525 state.text <- Printf.sprintf "bad integer `%s': %s"
2526 s (Printexc.to_string exc);
2527 None
2528 with
2529 | Some angle -> reqlayout angle conf.proportional
2530 | None -> ()
2532 TEswitch ("rotation: ", "", None, intentry, ondone)
2534 | 'i' ->
2535 conf.icase <- not conf.icase;
2536 TEdone ("case insensitive search " ^ (btos conf.icase))
2538 | 'p' ->
2539 conf.preload <- not conf.preload;
2540 gotoy state.y;
2541 TEdone ("preload " ^ (btos conf.preload))
2543 | 'v' ->
2544 conf.verbose <- not conf.verbose;
2545 TEdone ("verbose " ^ (btos conf.verbose))
2547 | 'd' ->
2548 conf.debug <- not conf.debug;
2549 TEdone ("debug " ^ (btos conf.debug))
2551 | 'h' ->
2552 conf.maxhfit <- not conf.maxhfit;
2553 state.maxy <-
2554 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2555 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2557 | 'c' ->
2558 conf.crophack <- not conf.crophack;
2559 TEdone ("crophack " ^ btos conf.crophack)
2561 | 'a' ->
2562 let s =
2563 match conf.maxwait with
2564 | None ->
2565 conf.maxwait <- Some infinity;
2566 "always wait for page to complete"
2567 | Some _ ->
2568 conf.maxwait <- None;
2569 "show placeholder if page is not ready"
2571 TEdone s
2573 | 'f' ->
2574 conf.underinfo <- not conf.underinfo;
2575 TEdone ("underinfo " ^ btos conf.underinfo)
2577 | 'P' ->
2578 conf.savebmarks <- not conf.savebmarks;
2579 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2581 | 'S' ->
2582 let ondone s =
2584 let pageno, py =
2585 match state.layout with
2586 | [] -> 0, 0
2587 | l :: _ ->
2588 l.pageno, l.pagey
2590 conf.interpagespace <- int_of_string s;
2591 state.maxy <- calcheight ();
2592 let y = getpagey pageno in
2593 gotoy (y + py)
2594 with exc ->
2595 state.text <- Printf.sprintf "bad integer `%s': %s"
2596 s (Printexc.to_string exc)
2598 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2600 | 'l' ->
2601 reqlayout conf.angle (not conf.proportional);
2602 TEdone ("proportional display " ^ btos conf.proportional)
2604 | 'T' ->
2605 settrim (not conf.trimmargins) conf.trimfuzz;
2606 TEdone ("trim margins " ^ btos conf.trimmargins)
2608 | 'I' ->
2609 conf.invert <- not conf.invert;
2610 TEdone ("invert colors " ^ btos conf.invert)
2612 | 'x' ->
2613 let ondone s =
2614 cbput state.hists.sel s;
2615 conf.selcmd <- s;
2617 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2618 textentry, ondone)
2620 | _ ->
2621 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2622 TEstop
2623 else
2624 TEcont state.text
2627 class type lvsource = object
2628 method getitemcount : int
2629 method getitem : int -> (string * int)
2630 method hasaction : int -> bool
2631 method exit :
2632 uioh:uioh ->
2633 cancel:bool ->
2634 active:int ->
2635 first:int ->
2636 pan:int ->
2637 qsearch:string ->
2638 uioh option
2639 method getactive : int
2640 method getfirst : int
2641 method getqsearch : string
2642 method setqsearch : string -> unit
2643 method getpan : int
2644 end;;
2646 class virtual lvsourcebase = object
2647 val mutable m_active = 0
2648 val mutable m_first = 0
2649 val mutable m_qsearch = ""
2650 val mutable m_pan = 0
2651 method getactive = m_active
2652 method getfirst = m_first
2653 method getqsearch = m_qsearch
2654 method getpan = m_pan
2655 method setqsearch s = m_qsearch <- s
2656 end;;
2658 let withoutlastutf8 s =
2659 let len = String.length s in
2660 if len = 0
2661 then s
2662 else
2663 let rec find pos =
2664 if pos = 0
2665 then pos
2666 else
2667 let b = Char.code s.[pos] in
2668 if b land 0b110000 = 0b11000000
2669 then find (pos-1)
2670 else pos-1
2672 let first =
2673 if Char.code s.[len-1] land 0x80 = 0
2674 then len-1
2675 else find (len-1)
2677 String.sub s 0 first;
2680 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2681 let enttext te =
2682 state.mode <- Textentry (te, onleave);
2683 state.text <- "";
2684 enttext ();
2685 G.postRedisplay "textentrykeyboard enttext";
2687 let histaction cmd =
2688 match opthist with
2689 | None -> ()
2690 | Some (action, _) ->
2691 state.mode <- Textentry (
2692 (c, action cmd, opthist, onkey, ondone), onleave
2694 G.postRedisplay "textentry histaction"
2696 match key with
2697 | 0xff08 -> (* backspace *)
2698 let s = withoutlastutf8 text in
2699 let len = String.length s in
2700 if len = 0
2701 then (
2702 onleave Cancel;
2703 G.postRedisplay "textentrykeyboard after cancel";
2705 else (
2706 enttext (c, s, opthist, onkey, ondone)
2709 | 0xff0d ->
2710 ondone text;
2711 onleave Confirm;
2712 G.postRedisplay "textentrykeyboard after confirm"
2714 | 0xff52 -> histaction HCprev
2715 | 0xff54 -> histaction HCnext
2716 | 0xff50 -> histaction HCfirst
2717 | 0xff57 -> histaction HClast
2719 | 0xff1b -> (* escape*)
2720 if String.length text = 0
2721 then (
2722 begin match opthist with
2723 | None -> ()
2724 | Some (_, onhistcancel) -> onhistcancel ()
2725 end;
2726 onleave Cancel;
2727 state.text <- "";
2728 G.postRedisplay "textentrykeyboard after cancel2"
2730 else (
2731 enttext (c, "", opthist, onkey, ondone)
2734 | 0xff9f | 0xffff -> () (* delete *)
2736 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2737 begin match onkey text key with
2738 | TEdone text ->
2739 ondone text;
2740 onleave Confirm;
2741 G.postRedisplay "textentrykeyboard after confirm2";
2743 | TEcont text ->
2744 enttext (c, text, opthist, onkey, ondone);
2746 | TEstop ->
2747 onleave Cancel;
2748 G.postRedisplay "textentrykeyboard after cancel3"
2750 | TEswitch te ->
2751 state.mode <- Textentry (te, onleave);
2752 G.postRedisplay "textentrykeyboard switch";
2753 end;
2755 | _ ->
2756 vlog "unhandled key %s" (Wsi.keyname key)
2759 let firstof first active =
2760 if first > active || abs (first - active) > fstate.maxrows - 1
2761 then max 0 (active - (fstate.maxrows/2))
2762 else first
2765 let calcfirst first active =
2766 if active > first
2767 then
2768 let rows = active - first in
2769 if rows > fstate.maxrows then active - fstate.maxrows else first
2770 else active
2773 let scrollph y maxy =
2774 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2775 let sh = float conf.winh /. sh in
2776 let sh = max sh (float conf.scrollh) in
2778 let percent =
2779 if y = state.maxy
2780 then 1.0
2781 else float y /. float maxy
2783 let position = (float conf.winh -. sh) *. percent in
2785 let position =
2786 if position +. sh > float conf.winh
2787 then float conf.winh -. sh
2788 else position
2790 position, sh;
2793 let coe s = (s :> uioh);;
2795 class listview ~(source:lvsource) ~trusted ~modehash =
2796 object (self)
2797 val m_pan = source#getpan
2798 val m_first = source#getfirst
2799 val m_active = source#getactive
2800 val m_qsearch = source#getqsearch
2801 val m_prev_uioh = state.uioh
2803 method private elemunder y =
2804 let n = y / (fstate.fontsize+1) in
2805 if m_first + n < source#getitemcount
2806 then (
2807 if source#hasaction (m_first + n)
2808 then Some (m_first + n)
2809 else None
2811 else None
2813 method display =
2814 Gl.enable `blend;
2815 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2816 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2817 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2818 GlDraw.color (1., 1., 1.);
2819 Gl.enable `texture_2d;
2820 let fs = fstate.fontsize in
2821 let nfs = fs + 1 in
2822 let ww = fstate.wwidth in
2823 let tabw = 30.0*.ww in
2824 let itemcount = source#getitemcount in
2825 let rec loop row =
2826 if (row - m_first) * nfs > conf.winh
2827 then ()
2828 else (
2829 if row >= 0 && row < itemcount
2830 then (
2831 let (s, level) = source#getitem row in
2832 let y = (row - m_first) * nfs in
2833 let x = 5.0 +. float (level + m_pan) *. ww in
2834 if row = m_active
2835 then (
2836 Gl.disable `texture_2d;
2837 GlDraw.polygon_mode `both `line;
2838 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2839 GlDraw.rect (1., float (y + 1))
2840 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2841 GlDraw.polygon_mode `both `fill;
2842 GlDraw.color (1., 1., 1.);
2843 Gl.enable `texture_2d;
2846 let drawtabularstring s =
2847 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2848 if trusted
2849 then
2850 let tabpos = try String.index s '\t' with Not_found -> -1 in
2851 if tabpos > 0
2852 then
2853 let len = String.length s - tabpos - 1 in
2854 let s1 = String.sub s 0 tabpos
2855 and s2 = String.sub s (tabpos + 1) len in
2856 let nx = drawstr x s1 in
2857 let sw = nx -. x in
2858 let x = x +. (max tabw sw) in
2859 drawstr x s2
2860 else
2861 drawstr x s
2862 else
2863 drawstr x s
2865 let _ = drawtabularstring s in
2866 loop (row+1)
2870 loop m_first;
2871 Gl.disable `blend;
2872 Gl.disable `texture_2d;
2874 method updownlevel incr =
2875 let len = source#getitemcount in
2876 let curlevel =
2877 if m_active >= 0 && m_active < len
2878 then snd (source#getitem m_active)
2879 else -1
2881 let rec flow i =
2882 if i = len then i-1 else if i = -1 then 0 else
2883 let _, l = source#getitem i in
2884 if l != curlevel then i else flow (i+incr)
2886 let active = flow m_active in
2887 let first = calcfirst m_first active in
2888 G.postRedisplay "outline updownlevel";
2889 {< m_active = active; m_first = first >}
2891 method private key1 key mask =
2892 let set1 active first qsearch =
2893 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2895 let search active pattern incr =
2896 let dosearch re =
2897 let rec loop n =
2898 if n >= 0 && n < source#getitemcount
2899 then (
2900 let s, _ = source#getitem n in
2902 (try ignore (Str.search_forward re s 0); true
2903 with Not_found -> false)
2904 then Some n
2905 else loop (n + incr)
2907 else None
2909 loop active
2912 let re = Str.regexp_case_fold pattern in
2913 dosearch re
2914 with Failure s ->
2915 state.text <- s;
2916 None
2918 let itemcount = source#getitemcount in
2919 let find start incr =
2920 let rec find i =
2921 if i = -1 || i = itemcount
2922 then -1
2923 else (
2924 if source#hasaction i
2925 then i
2926 else find (i + incr)
2929 find start
2931 let set active first =
2932 let first = bound first 0 (itemcount - fstate.maxrows) in
2933 state.text <- "";
2934 coe {< m_active = active; m_first = first >}
2936 let navigate incr =
2937 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2938 let active, first =
2939 let incr1 = if incr > 0 then 1 else -1 in
2940 if isvisible m_first m_active
2941 then
2942 let next =
2943 let next = m_active + incr in
2944 let next =
2945 if next < 0 || next >= itemcount
2946 then -1
2947 else find next incr1
2949 if next = -1 || abs (m_active - next) > fstate.maxrows
2950 then -1
2951 else next
2953 if next = -1
2954 then
2955 let first = m_first + incr in
2956 let first = bound first 0 (itemcount - 1) in
2957 let next =
2958 let next = m_active + incr in
2959 let next = bound next 0 (itemcount - 1) in
2960 find next ~-incr1
2962 let active = if next = -1 then m_active else next in
2963 active, first
2964 else
2965 let first = min next m_first in
2966 let first =
2967 if abs (next - first) > fstate.maxrows
2968 then first + incr
2969 else first
2971 next, first
2972 else
2973 let first = m_first + incr in
2974 let first = bound first 0 (itemcount - 1) in
2975 let active =
2976 let next = m_active + incr in
2977 let next = bound next 0 (itemcount - 1) in
2978 let next = find next incr1 in
2979 let active =
2980 if next = -1 || abs (m_active - first) > fstate.maxrows
2981 then (
2982 let active = if m_active = -1 then next else m_active in
2983 active
2985 else next
2987 if isvisible first active
2988 then active
2989 else -1
2991 active, first
2993 G.postRedisplay "listview navigate";
2994 set active first;
2996 match key with
2997 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
2998 let incr = if key = 0x72 then -1 else 1 in
2999 let active, first =
3000 match search (m_active + incr) m_qsearch incr with
3001 | None ->
3002 state.text <- m_qsearch ^ " [not found]";
3003 m_active, m_first
3004 | Some active ->
3005 state.text <- m_qsearch;
3006 active, firstof m_first active
3008 G.postRedisplay "listview ctrl-r/s";
3009 set1 active first m_qsearch;
3011 | 0xff08 -> (* backspace *)
3012 if String.length m_qsearch = 0
3013 then coe self
3014 else (
3015 let qsearch = withoutlastutf8 m_qsearch in
3016 let len = String.length qsearch in
3017 if len = 0
3018 then (
3019 state.text <- "";
3020 G.postRedisplay "listview empty qsearch";
3021 set1 m_active m_first "";
3023 else
3024 let active, first =
3025 match search m_active qsearch ~-1 with
3026 | None ->
3027 state.text <- qsearch ^ " [not found]";
3028 m_active, m_first
3029 | Some active ->
3030 state.text <- qsearch;
3031 active, firstof m_first active
3033 G.postRedisplay "listview backspace qsearch";
3034 set1 active first qsearch
3037 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3038 let pattern = m_qsearch ^ Wsi.toutf8 key in
3039 let active, first =
3040 match search m_active pattern 1 with
3041 | None ->
3042 state.text <- pattern ^ " [not found]";
3043 m_active, m_first
3044 | Some active ->
3045 state.text <- pattern;
3046 active, firstof m_first active
3048 G.postRedisplay "listview qsearch add";
3049 set1 active first pattern;
3051 | 0xff1b -> (* escape *)
3052 state.text <- "";
3053 if String.length m_qsearch = 0
3054 then (
3055 G.postRedisplay "list view escape";
3056 begin
3057 match
3058 source#exit (coe self) true m_active m_first m_pan m_qsearch
3059 with
3060 | None -> m_prev_uioh
3061 | Some uioh -> uioh
3064 else (
3065 G.postRedisplay "list view kill qsearch";
3066 source#setqsearch "";
3067 coe {< m_qsearch = "" >}
3070 | 0xff0d -> (* return *)
3071 state.text <- "";
3072 let self = {< m_qsearch = "" >} in
3073 source#setqsearch "";
3074 let opt =
3075 G.postRedisplay "listview enter";
3076 if m_active >= 0 && m_active < source#getitemcount
3077 then (
3078 source#exit (coe self) false m_active m_first m_pan "";
3080 else (
3081 source#exit (coe self) true m_active m_first m_pan "";
3084 begin match opt with
3085 | None -> m_prev_uioh
3086 | Some uioh -> uioh
3089 | 0xff9f | 0xffff -> (* delete *)
3090 coe self
3092 | 0xff52 -> navigate ~-1 (* up *)
3093 | 0xff54 -> navigate 1 (* down *)
3094 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3095 | 0xff56 -> navigate fstate.maxrows (* next *)
3097 | 0xff53 -> (* right *)
3098 state.text <- "";
3099 G.postRedisplay "listview right";
3100 coe {< m_pan = m_pan - 1 >}
3102 | 0xff51 -> (* left *)
3103 state.text <- "";
3104 G.postRedisplay "listview left";
3105 coe {< m_pan = m_pan + 1 >}
3107 | 0xff50 -> (* home *)
3108 let active = find 0 1 in
3109 G.postRedisplay "listview home";
3110 set active 0;
3112 | 0xff57 -> (* end *)
3113 let first = max 0 (itemcount - fstate.maxrows) in
3114 let active = find (itemcount - 1) ~-1 in
3115 G.postRedisplay "listview end";
3116 set active first;
3118 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3119 coe self
3121 | _ ->
3122 dolog "listview unknown key %#x" key; coe self
3124 method key key mask =
3125 match state.mode with
3126 | Textentry te -> textentrykeyboard key mask te; coe self
3127 | _ -> self#key1 key mask
3129 method button button down x y _ =
3130 let opt =
3131 match button with
3132 | 1 when x > conf.winw - conf.scrollbw ->
3133 G.postRedisplay "listview scroll";
3134 if down
3135 then
3136 let _, position, sh = self#scrollph in
3137 if y > truncate position && y < truncate (position +. sh)
3138 then (
3139 state.mstate <- Mscrolly;
3140 Some (coe self)
3142 else
3143 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3144 let first = truncate (s *. float source#getitemcount) in
3145 let first = min source#getitemcount first in
3146 Some (coe {< m_first = first; m_active = first >})
3147 else (
3148 state.mstate <- Mnone;
3149 Some (coe self);
3151 | 1 when not down ->
3152 begin match self#elemunder y with
3153 | Some n ->
3154 G.postRedisplay "listview click";
3155 source#exit
3156 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3157 | _ ->
3158 Some (coe self)
3160 | n when (n == 4 || n == 5) && not down ->
3161 let len = source#getitemcount in
3162 let first =
3163 if n = 5 && m_first + fstate.maxrows >= len
3164 then
3165 m_first
3166 else
3167 let first = m_first + (if n == 4 then -1 else 1) in
3168 bound first 0 (len - 1)
3170 G.postRedisplay "listview wheel";
3171 Some (coe {< m_first = first >})
3172 | _ ->
3173 Some (coe self)
3175 match opt with
3176 | None -> m_prev_uioh
3177 | Some uioh -> uioh
3179 method motion _ y =
3180 match state.mstate with
3181 | Mscrolly ->
3182 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3183 let first = truncate (s *. float source#getitemcount) in
3184 let first = min source#getitemcount first in
3185 G.postRedisplay "listview motion";
3186 coe {< m_first = first; m_active = first >}
3187 | _ -> coe self
3189 method pmotion x y =
3190 if x < conf.winw - conf.scrollbw
3191 then
3192 let n =
3193 match self#elemunder y with
3194 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3195 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3197 let o =
3198 if n != m_active
3199 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3200 else self
3202 coe o
3203 else (
3204 Wsi.setcursor Wsi.CURSOR_INHERIT;
3205 coe self
3208 method infochanged _ = ()
3210 method scrollpw = (0, 0.0, 0.0)
3211 method scrollph =
3212 let nfs = fstate.fontsize + 1 in
3213 let y = m_first * nfs in
3214 let itemcount = source#getitemcount in
3215 let maxi = max 0 (itemcount - fstate.maxrows) in
3216 let maxy = maxi * nfs in
3217 let p, h = scrollph y maxy in
3218 conf.scrollbw, p, h
3220 method modehash = modehash
3221 end;;
3223 class outlinelistview ~source =
3224 object (self)
3225 inherit listview
3226 ~source:(source :> lvsource)
3227 ~trusted:false
3228 ~modehash:(findkeyhash conf "outline")
3229 as super
3231 method key key mask =
3232 let calcfirst first active =
3233 if active > first
3234 then
3235 let rows = active - first in
3236 if rows > fstate.maxrows then active - fstate.maxrows else first
3237 else active
3239 let navigate incr =
3240 let active = m_active + incr in
3241 let active = bound active 0 (source#getitemcount - 1) in
3242 let first = calcfirst m_first active in
3243 G.postRedisplay "outline navigate";
3244 coe {< m_active = active; m_first = first >}
3246 let ctrl = Wsi.withctrl mask in
3247 match key with
3248 | 110 when ctrl -> (* ctrl-n *)
3249 source#narrow m_qsearch;
3250 G.postRedisplay "outline ctrl-n";
3251 coe {< m_first = 0; m_active = 0 >}
3253 | 117 when ctrl -> (* ctrl-u *)
3254 source#denarrow;
3255 G.postRedisplay "outline ctrl-u";
3256 state.text <- "";
3257 coe {< m_first = 0; m_active = 0 >}
3259 | 108 when ctrl -> (* ctrl-l *)
3260 let first = m_active - (fstate.maxrows / 2) in
3261 G.postRedisplay "outline ctrl-l";
3262 coe {< m_first = first >}
3264 | 0xff9f | 0xffff -> (* delete *)
3265 source#remove m_active;
3266 G.postRedisplay "outline delete";
3267 let active = max 0 (m_active-1) in
3268 coe {< m_first = firstof m_first active;
3269 m_active = active >}
3271 | 0xff52 -> navigate ~-1 (* up *)
3272 | 0xff54 -> navigate 1 (* down *)
3273 | 0xff55 -> (* prior *)
3274 navigate ~-(fstate.maxrows)
3275 | 0xff56 -> (* next *)
3276 navigate fstate.maxrows
3278 | 0xff53 -> (* [ctrl-]right *)
3279 let o =
3280 if ctrl
3281 then (
3282 G.postRedisplay "outline ctrl right";
3283 {< m_pan = m_pan + 1 >}
3285 else self#updownlevel 1
3287 coe o
3289 | 0xff51 -> (* [ctrl-]left *)
3290 let o =
3291 if ctrl
3292 then (
3293 G.postRedisplay "outline ctrl left";
3294 {< m_pan = m_pan - 1 >}
3296 else self#updownlevel ~-1
3298 coe o
3300 | 0xff50 -> (* home *)
3301 G.postRedisplay "outline home";
3302 coe {< m_first = 0; m_active = 0 >}
3304 | 0xff57 -> (* end *)
3305 let active = source#getitemcount - 1 in
3306 let first = max 0 (active - fstate.maxrows) in
3307 G.postRedisplay "outline end";
3308 coe {< m_active = active; m_first = first >}
3310 | _ -> super#key key mask
3313 let outlinesource usebookmarks =
3314 let empty = [||] in
3315 (object
3316 inherit lvsourcebase
3317 val mutable m_items = empty
3318 val mutable m_orig_items = empty
3319 val mutable m_prev_items = empty
3320 val mutable m_narrow_pattern = ""
3321 val mutable m_hadremovals = false
3323 method getitemcount =
3324 Array.length m_items + (if m_hadremovals then 1 else 0)
3326 method getitem n =
3327 if n == Array.length m_items && m_hadremovals
3328 then
3329 ("[Confirm removal]", 0)
3330 else
3331 let s, n, _ = m_items.(n) in
3332 (s, n)
3334 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3335 ignore (uioh, first, qsearch);
3336 let confrimremoval = m_hadremovals && active = Array.length m_items in
3337 let items =
3338 if String.length m_narrow_pattern = 0
3339 then m_orig_items
3340 else m_items
3342 if not cancel
3343 then (
3344 if not confrimremoval
3345 then(
3346 let _, _, anchor = m_items.(active) in
3347 gotoanchor anchor;
3348 m_items <- items;
3350 else (
3351 state.bookmarks <- Array.to_list m_items;
3352 m_orig_items <- m_items;
3355 else m_items <- items;
3356 m_pan <- pan;
3357 None
3359 method hasaction _ = true
3361 method greetmsg =
3362 if Array.length m_items != Array.length m_orig_items
3363 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3364 else ""
3366 method narrow pattern =
3367 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3368 match reopt with
3369 | None -> ()
3370 | Some re ->
3371 let rec loop accu n =
3372 if n = -1
3373 then (
3374 m_narrow_pattern <- pattern;
3375 m_items <- Array.of_list accu
3377 else
3378 let (s, _, _) as o = m_items.(n) in
3379 let accu =
3380 if (try ignore (Str.search_forward re s 0); true
3381 with Not_found -> false)
3382 then o :: accu
3383 else accu
3385 loop accu (n-1)
3387 loop [] (Array.length m_items - 1)
3389 method denarrow =
3390 m_orig_items <- (
3391 if usebookmarks
3392 then Array.of_list state.bookmarks
3393 else state.outlines
3395 m_items <- m_orig_items
3397 method remove m =
3398 if usebookmarks
3399 then
3400 if m >= 0 && m < Array.length m_items
3401 then (
3402 m_hadremovals <- true;
3403 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3404 let n = if n >= m then n+1 else n in
3405 m_items.(n)
3409 method reset anchor items =
3410 m_hadremovals <- false;
3411 if m_orig_items == empty || m_prev_items != items
3412 then (
3413 m_orig_items <- items;
3414 if String.length m_narrow_pattern = 0
3415 then m_items <- items;
3417 m_prev_items <- items;
3418 let rely = getanchory anchor in
3419 let active =
3420 let rec loop n best bestd =
3421 if n = Array.length m_items
3422 then best
3423 else
3424 let (_, _, anchor) = m_items.(n) in
3425 let orely = getanchory anchor in
3426 let d = abs (orely - rely) in
3427 if d < bestd
3428 then loop (n+1) n d
3429 else loop (n+1) best bestd
3431 loop 0 ~-1 max_int
3433 m_active <- active;
3434 m_first <- firstof m_first active
3435 end)
3438 let enterselector usebookmarks =
3439 let source = outlinesource usebookmarks in
3440 fun errmsg ->
3441 let outlines =
3442 if usebookmarks
3443 then Array.of_list state.bookmarks
3444 else state.outlines
3446 if Array.length outlines = 0
3447 then (
3448 showtext ' ' errmsg;
3450 else (
3451 state.text <- source#greetmsg;
3452 Wsi.setcursor Wsi.CURSOR_INHERIT;
3453 let anchor = getanchor () in
3454 source#reset anchor outlines;
3455 state.uioh <- coe (new outlinelistview ~source);
3456 G.postRedisplay "enter selector";
3460 let enteroutlinemode =
3461 let f = enterselector false in
3462 fun ()-> f "Document has no outline";
3465 let enterbookmarkmode =
3466 let f = enterselector true in
3467 fun () -> f "Document has no bookmarks (yet)";
3470 let color_of_string s =
3471 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3472 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3476 let color_to_string (r, g, b) =
3477 let r = truncate (r *. 256.0)
3478 and g = truncate (g *. 256.0)
3479 and b = truncate (b *. 256.0) in
3480 Printf.sprintf "%d/%d/%d" r g b
3483 let irect_of_string s =
3484 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3487 let irect_to_string (x0,y0,x1,y1) =
3488 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3491 let makecheckers () =
3492 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3493 following to say:
3494 converted by Issac Trotts. July 25, 2002 *)
3495 let image_height = 64
3496 and image_width = 64 in
3498 let make_image () =
3499 let image =
3500 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3502 for i = 0 to image_width - 1 do
3503 for j = 0 to image_height - 1 do
3504 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3505 (if (i land 8 ) lxor (j land 8) = 0
3506 then [|255;255;255|] else [|200;200;200|])
3507 done
3508 done;
3509 image
3511 let image = make_image () in
3512 let id = GlTex.gen_texture () in
3513 GlTex.bind_texture `texture_2d id;
3514 GlPix.store (`unpack_alignment 1);
3515 GlTex.image2d image;
3516 List.iter (GlTex.parameter ~target:`texture_2d)
3517 [ `wrap_s `repeat;
3518 `wrap_t `repeat;
3519 `mag_filter `nearest;
3520 `min_filter `nearest ];
3524 let setcheckers enabled =
3525 match state.texid with
3526 | None ->
3527 if enabled then state.texid <- Some (makecheckers ())
3529 | Some texid ->
3530 if not enabled
3531 then (
3532 GlTex.delete_texture texid;
3533 state.texid <- None;
3537 let int_of_string_with_suffix s =
3538 let l = String.length s in
3539 let s1, shift =
3540 if l > 1
3541 then
3542 let suffix = Char.lowercase s.[l-1] in
3543 match suffix with
3544 | 'k' -> String.sub s 0 (l-1), 10
3545 | 'm' -> String.sub s 0 (l-1), 20
3546 | 'g' -> String.sub s 0 (l-1), 30
3547 | _ -> s, 0
3548 else s, 0
3550 let n = int_of_string s1 in
3551 let m = n lsl shift in
3552 if m < 0 || m < n
3553 then raise (Failure "value too large")
3554 else m
3557 let string_with_suffix_of_int n =
3558 if n = 0
3559 then "0"
3560 else
3561 let n, s =
3562 if n = 0
3563 then 0, ""
3564 else (
3565 if n land ((1 lsl 20) - 1) = 0
3566 then n lsr 20, "M"
3567 else (
3568 if n land ((1 lsl 10) - 1) = 0
3569 then n lsr 10, "K"
3570 else n, ""
3574 let rec loop s n =
3575 let h = n mod 1000 in
3576 let n = n / 1000 in
3577 if n = 0
3578 then string_of_int h ^ s
3579 else (
3580 let s = Printf.sprintf "_%03d%s" h s in
3581 loop s n
3584 loop "" n ^ s;
3587 let defghyllscroll = (40, 8, 32);;
3588 let ghyllscroll_of_string s =
3589 let (n, a, b) as nab =
3590 if s = "default"
3591 then defghyllscroll
3592 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3594 if n <= a || n <= b || a >= b
3595 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3596 nab;
3599 let ghyllscroll_to_string ((n, a, b) as nab) =
3600 if nab = defghyllscroll
3601 then "default"
3602 else Printf.sprintf "%d,%d,%d" n a b;
3605 let describe_location () =
3606 let f (fn, _) l =
3607 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3609 let fn, ln = List.fold_left f (-1, -1) state.layout in
3610 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3611 let percent =
3612 if maxy <= 0
3613 then 100.
3614 else (100. *. (float state.y /. float maxy))
3616 if fn = ln
3617 then
3618 Printf.sprintf "page %d of %d [%.2f%%]"
3619 (fn+1) state.pagecount percent
3620 else
3621 Printf.sprintf
3622 "pages %d-%d of %d [%.2f%%]"
3623 (fn+1) (ln+1) state.pagecount percent
3626 let enterinfomode =
3627 let btos b = if b then "\xe2\x88\x9a" else "" in
3628 let showextended = ref false in
3629 let leave mode = function
3630 | Confirm -> state.mode <- mode
3631 | Cancel -> state.mode <- mode in
3632 let src =
3633 (object
3634 val mutable m_first_time = true
3635 val mutable m_l = []
3636 val mutable m_a = [||]
3637 val mutable m_prev_uioh = nouioh
3638 val mutable m_prev_mode = View
3640 inherit lvsourcebase
3642 method reset prev_mode prev_uioh =
3643 m_a <- Array.of_list (List.rev m_l);
3644 m_l <- [];
3645 m_prev_mode <- prev_mode;
3646 m_prev_uioh <- prev_uioh;
3647 if m_first_time
3648 then (
3649 let rec loop n =
3650 if n >= Array.length m_a
3651 then ()
3652 else
3653 match m_a.(n) with
3654 | _, _, _, Action _ -> m_active <- n
3655 | _ -> loop (n+1)
3657 loop 0;
3658 m_first_time <- false;
3661 method int name get set =
3662 m_l <-
3663 (name, `int get, 1, Action (
3664 fun u ->
3665 let ondone s =
3666 try set (int_of_string s)
3667 with exn ->
3668 state.text <- Printf.sprintf "bad integer `%s': %s"
3669 s (Printexc.to_string exn)
3671 state.text <- "";
3672 let te = name ^ ": ", "", None, intentry, ondone in
3673 state.mode <- Textentry (te, leave m_prev_mode);
3675 )) :: m_l
3677 method int_with_suffix name get set =
3678 m_l <-
3679 (name, `intws get, 1, Action (
3680 fun u ->
3681 let ondone s =
3682 try set (int_of_string_with_suffix s)
3683 with exn ->
3684 state.text <- Printf.sprintf "bad integer `%s': %s"
3685 s (Printexc.to_string exn)
3687 state.text <- "";
3688 let te =
3689 name ^ ": ", "", None, intentry_with_suffix, ondone
3691 state.mode <- Textentry (te, leave m_prev_mode);
3693 )) :: m_l
3695 method bool ?(offset=1) ?(btos=btos) name get set =
3696 m_l <-
3697 (name, `bool (btos, get), offset, Action (
3698 fun u ->
3699 let v = get () in
3700 set (not v);
3702 )) :: m_l
3704 method color name get set =
3705 m_l <-
3706 (name, `color get, 1, Action (
3707 fun u ->
3708 let invalid = (nan, nan, nan) in
3709 let ondone s =
3710 let c =
3711 try color_of_string s
3712 with exn ->
3713 state.text <- Printf.sprintf "bad color `%s': %s"
3714 s (Printexc.to_string exn);
3715 invalid
3717 if c <> invalid
3718 then set c;
3720 let te = name ^ ": ", "", None, textentry, ondone in
3721 state.text <- color_to_string (get ());
3722 state.mode <- Textentry (te, leave m_prev_mode);
3724 )) :: m_l
3726 method string name get set =
3727 m_l <-
3728 (name, `string get, 1, Action (
3729 fun u ->
3730 let ondone s = set s in
3731 let te = name ^ ": ", "", None, textentry, ondone in
3732 state.mode <- Textentry (te, leave m_prev_mode);
3734 )) :: m_l
3736 method colorspace name get set =
3737 m_l <-
3738 (name, `string get, 1, Action (
3739 fun _ ->
3740 let source =
3741 let vals = [| "rgb"; "bgr"; "gray" |] in
3742 (object
3743 inherit lvsourcebase
3745 initializer
3746 m_active <- int_of_colorspace conf.colorspace;
3747 m_first <- 0;
3749 method getitemcount = Array.length vals
3750 method getitem n = (vals.(n), 0)
3751 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3752 ignore (uioh, first, pan, qsearch);
3753 if not cancel then set active;
3754 None
3755 method hasaction _ = true
3756 end)
3758 state.text <- "";
3759 let modehash = findkeyhash conf "info" in
3760 coe (new listview ~source ~trusted:true ~modehash)
3761 )) :: m_l
3763 method caption s offset =
3764 m_l <- (s, `empty, offset, Noaction) :: m_l
3766 method caption2 s f offset =
3767 m_l <- (s, `string f, offset, Noaction) :: m_l
3769 method getitemcount = Array.length m_a
3771 method getitem n =
3772 let tostr = function
3773 | `int f -> string_of_int (f ())
3774 | `intws f -> string_with_suffix_of_int (f ())
3775 | `string f -> f ()
3776 | `color f -> color_to_string (f ())
3777 | `bool (btos, f) -> btos (f ())
3778 | `empty -> ""
3780 let name, t, offset, _ = m_a.(n) in
3781 ((let s = tostr t in
3782 if String.length s > 0
3783 then Printf.sprintf "%s\t%s" name s
3784 else name),
3785 offset)
3787 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3788 let uiohopt =
3789 if not cancel
3790 then (
3791 m_qsearch <- qsearch;
3792 let uioh =
3793 match m_a.(active) with
3794 | _, _, _, Action f -> f uioh
3795 | _ -> uioh
3797 Some uioh
3799 else None
3801 m_active <- active;
3802 m_first <- first;
3803 m_pan <- pan;
3804 uiohopt
3806 method hasaction n =
3807 match m_a.(n) with
3808 | _, _, _, Action _ -> true
3809 | _ -> false
3810 end)
3812 let rec fillsrc prevmode prevuioh =
3813 let sep () = src#caption "" 0 in
3814 let colorp name get set =
3815 src#string name
3816 (fun () -> color_to_string (get ()))
3817 (fun v ->
3819 let c = color_of_string v in
3820 set c
3821 with exn ->
3822 state.text <- Printf.sprintf "bad color `%s': %s"
3823 v (Printexc.to_string exn);
3826 let oldmode = state.mode in
3827 let birdseye = isbirdseye state.mode in
3829 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3831 src#bool "presentation mode"
3832 (fun () -> conf.presentation)
3833 (fun v ->
3834 conf.presentation <- v;
3835 state.anchor <- getanchor ();
3836 represent ());
3838 src#bool "ignore case in searches"
3839 (fun () -> conf.icase)
3840 (fun v -> conf.icase <- v);
3842 src#bool "preload"
3843 (fun () -> conf.preload)
3844 (fun v -> conf.preload <- v);
3846 src#bool "highlight links"
3847 (fun () -> conf.hlinks)
3848 (fun v -> conf.hlinks <- v);
3850 src#bool "under info"
3851 (fun () -> conf.underinfo)
3852 (fun v -> conf.underinfo <- v);
3854 src#bool "persistent bookmarks"
3855 (fun () -> conf.savebmarks)
3856 (fun v -> conf.savebmarks <- v);
3858 src#bool "proportional display"
3859 (fun () -> conf.proportional)
3860 (fun v -> reqlayout conf.angle v);
3862 src#bool "trim margins"
3863 (fun () -> conf.trimmargins)
3864 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3866 src#bool "persistent location"
3867 (fun () -> conf.jumpback)
3868 (fun v -> conf.jumpback <- v);
3870 sep ();
3871 src#int "inter-page space"
3872 (fun () -> conf.interpagespace)
3873 (fun n ->
3874 conf.interpagespace <- n;
3875 let pageno, py =
3876 match state.layout with
3877 | [] -> 0, 0
3878 | l :: _ ->
3879 l.pageno, l.pagey
3881 state.maxy <- calcheight ();
3882 let y = getpagey pageno in
3883 gotoy (y + py)
3886 src#int "page bias"
3887 (fun () -> conf.pagebias)
3888 (fun v -> conf.pagebias <- v);
3890 src#int "scroll step"
3891 (fun () -> conf.scrollstep)
3892 (fun n -> conf.scrollstep <- n);
3894 src#int "auto scroll step"
3895 (fun () ->
3896 match state.autoscroll with
3897 | Some step -> step
3898 | _ -> conf.autoscrollstep)
3899 (fun n ->
3900 if state.autoscroll <> None
3901 then state.autoscroll <- Some n;
3902 conf.autoscrollstep <- n);
3904 src#int "zoom"
3905 (fun () -> truncate (conf.zoom *. 100.))
3906 (fun v -> setzoom ((float v) /. 100.));
3908 src#int "rotation"
3909 (fun () -> conf.angle)
3910 (fun v -> reqlayout v conf.proportional);
3912 src#int "scroll bar width"
3913 (fun () -> state.scrollw)
3914 (fun v ->
3915 state.scrollw <- v;
3916 conf.scrollbw <- v;
3917 reshape conf.winw conf.winh;
3920 src#int "scroll handle height"
3921 (fun () -> conf.scrollh)
3922 (fun v -> conf.scrollh <- v;);
3924 src#int "thumbnail width"
3925 (fun () -> conf.thumbw)
3926 (fun v ->
3927 conf.thumbw <- min 4096 v;
3928 match oldmode with
3929 | Birdseye beye ->
3930 leavebirdseye beye false;
3931 enterbirdseye ()
3932 | _ -> ()
3935 src#string "columns"
3936 (fun () ->
3937 match conf.columns with
3938 | None -> "1"
3939 | Some (multicol, _) -> columns_to_string multicol)
3940 (fun v ->
3941 let n, a, b = columns_of_string v in
3942 setcolumns n a b);
3944 sep ();
3945 src#caption "Presentation mode" 0;
3946 src#bool "scrollbar visible"
3947 (fun () -> conf.scrollbarinpm)
3948 (fun v ->
3949 if v != conf.scrollbarinpm
3950 then (
3951 conf.scrollbarinpm <- v;
3952 if conf.presentation
3953 then (
3954 state.scrollw <- if v then conf.scrollbw else 0;
3955 reshape conf.winw conf.winh;
3960 sep ();
3961 src#caption "Pixmap cache" 0;
3962 src#int_with_suffix "size (advisory)"
3963 (fun () -> conf.memlimit)
3964 (fun v -> conf.memlimit <- v);
3966 src#caption2 "used"
3967 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3968 (string_with_suffix_of_int state.memused)
3969 (Hashtbl.length state.tilemap)) 1;
3971 sep ();
3972 src#caption "Layout" 0;
3973 src#caption2 "Dimension"
3974 (fun () ->
3975 Printf.sprintf "%dx%d (virtual %dx%d)"
3976 conf.winw conf.winh
3977 state.w state.maxy)
3979 if conf.debug
3980 then
3981 src#caption2 "Position" (fun () ->
3982 Printf.sprintf "%dx%d" state.x state.y
3984 else
3985 src#caption2 "Visible" (fun () -> describe_location ()) 1
3988 sep ();
3989 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
3990 "Save these parameters as global defaults at exit"
3991 (fun () -> conf.bedefault)
3992 (fun v -> conf.bedefault <- v)
3995 sep ();
3996 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
3997 src#bool ~offset:0 ~btos "Extended parameters"
3998 (fun () -> !showextended)
3999 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4000 if !showextended
4001 then (
4002 src#bool "checkers"
4003 (fun () -> conf.checkers)
4004 (fun v -> conf.checkers <- v; setcheckers v);
4005 src#bool "update cursor"
4006 (fun () -> conf.updatecurs)
4007 (fun v -> conf.updatecurs <- v);
4008 src#bool "verbose"
4009 (fun () -> conf.verbose)
4010 (fun v -> conf.verbose <- v);
4011 src#bool "invert colors"
4012 (fun () -> conf.invert)
4013 (fun v -> conf.invert <- v);
4014 src#bool "max fit"
4015 (fun () -> conf.maxhfit)
4016 (fun v -> conf.maxhfit <- v);
4017 src#bool "redirect stderr"
4018 (fun () -> conf.redirectstderr)
4019 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4020 src#string "uri launcher"
4021 (fun () -> conf.urilauncher)
4022 (fun v -> conf.urilauncher <- v);
4023 src#string "path launcher"
4024 (fun () -> conf.pathlauncher)
4025 (fun v -> conf.pathlauncher <- v);
4026 src#string "tile size"
4027 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4028 (fun v ->
4030 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4031 conf.tileh <- max 64 w;
4032 conf.tilew <- max 64 h;
4033 flushtiles ();
4034 with exn ->
4035 state.text <- Printf.sprintf "bad tile size `%s': %s"
4036 v (Printexc.to_string exn));
4037 src#int "texture count"
4038 (fun () -> conf.texcount)
4039 (fun v ->
4040 if realloctexts v
4041 then conf.texcount <- v
4042 else showtext '!' " Failed to set texture count please retry later"
4044 src#int "slice height"
4045 (fun () -> conf.sliceheight)
4046 (fun v ->
4047 conf.sliceheight <- v;
4048 wcmd "sliceh %d" conf.sliceheight;
4050 src#int "anti-aliasing level"
4051 (fun () -> conf.aalevel)
4052 (fun v ->
4053 conf.aalevel <- bound v 0 8;
4054 state.anchor <- getanchor ();
4055 opendoc state.path state.password;
4057 src#int "ui font size"
4058 (fun () -> fstate.fontsize)
4059 (fun v -> setfontsize (bound v 5 100));
4060 colorp "background color"
4061 (fun () -> conf.bgcolor)
4062 (fun v -> conf.bgcolor <- v);
4063 src#bool "crop hack"
4064 (fun () -> conf.crophack)
4065 (fun v -> conf.crophack <- v);
4066 src#string "trim fuzz"
4067 (fun () -> irect_to_string conf.trimfuzz)
4068 (fun v ->
4070 conf.trimfuzz <- irect_of_string v;
4071 if conf.trimmargins
4072 then settrim true conf.trimfuzz;
4073 with exn ->
4074 state.text <- Printf.sprintf "bad irect `%s': %s"
4075 v (Printexc.to_string exn)
4077 src#string "throttle"
4078 (fun () ->
4079 match conf.maxwait with
4080 | None -> "show place holder if page is not ready"
4081 | Some time ->
4082 if time = infinity
4083 then "wait for page to fully render"
4084 else
4085 "wait " ^ string_of_float time
4086 ^ " seconds before showing placeholder"
4088 (fun v ->
4090 let f = float_of_string v in
4091 if f <= 0.0
4092 then conf.maxwait <- None
4093 else conf.maxwait <- Some f
4094 with exn ->
4095 state.text <- Printf.sprintf "bad time `%s': %s"
4096 v (Printexc.to_string exn)
4098 src#string "ghyll scroll"
4099 (fun () ->
4100 match conf.ghyllscroll with
4101 | None -> ""
4102 | Some nab -> ghyllscroll_to_string nab
4104 (fun v ->
4106 let gs =
4107 if String.length v = 0
4108 then None
4109 else Some (ghyllscroll_of_string v)
4111 conf.ghyllscroll <- gs
4112 with exn ->
4113 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4114 v (Printexc.to_string exn)
4116 src#string "selection command"
4117 (fun () -> conf.selcmd)
4118 (fun v -> conf.selcmd <- v);
4119 src#colorspace "color space"
4120 (fun () -> colorspace_to_string conf.colorspace)
4121 (fun v ->
4122 conf.colorspace <- colorspace_of_int v;
4123 wcmd "cs %d" v;
4124 load state.layout;
4128 sep ();
4129 src#caption "Document" 0;
4130 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4131 src#caption2 "Pages"
4132 (fun () -> string_of_int state.pagecount) 1;
4133 src#caption2 "Dimensions"
4134 (fun () -> string_of_int (List.length state.pdims)) 1;
4135 if conf.trimmargins
4136 then (
4137 sep ();
4138 src#caption "Trimmed margins" 0;
4139 src#caption2 "Dimensions"
4140 (fun () -> string_of_int (List.length state.pdims)) 1;
4143 src#reset prevmode prevuioh;
4145 fun () ->
4146 state.text <- "";
4147 let prevmode = state.mode
4148 and prevuioh = state.uioh in
4149 fillsrc prevmode prevuioh;
4150 let source = (src :> lvsource) in
4151 let modehash = findkeyhash conf "info" in
4152 state.uioh <- coe (object (self)
4153 inherit listview ~source ~trusted:true ~modehash as super
4154 val mutable m_prevmemused = 0
4155 method infochanged = function
4156 | Memused ->
4157 if m_prevmemused != state.memused
4158 then (
4159 m_prevmemused <- state.memused;
4160 G.postRedisplay "memusedchanged";
4162 | Pdim -> G.postRedisplay "pdimchanged"
4163 | Docinfo -> fillsrc prevmode prevuioh
4165 method key key mask =
4166 if not (Wsi.withctrl mask)
4167 then
4168 match key with
4169 | 0xff51 -> coe (self#updownlevel ~-1)
4170 | 0xff53 -> coe (self#updownlevel 1)
4171 | _ -> super#key key mask
4172 else super#key key mask
4173 end);
4174 G.postRedisplay "info";
4177 let enterhelpmode =
4178 let source =
4179 (object
4180 inherit lvsourcebase
4181 method getitemcount = Array.length state.help
4182 method getitem n =
4183 let s, n, _ = state.help.(n) in
4184 (s, n)
4186 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4187 let optuioh =
4188 if not cancel
4189 then (
4190 m_qsearch <- qsearch;
4191 match state.help.(active) with
4192 | _, _, Action f -> Some (f uioh)
4193 | _ -> Some (uioh)
4195 else None
4197 m_active <- active;
4198 m_first <- first;
4199 m_pan <- pan;
4200 optuioh
4202 method hasaction n =
4203 match state.help.(n) with
4204 | _, _, Action _ -> true
4205 | _ -> false
4207 initializer
4208 m_active <- -1
4209 end)
4210 in fun () ->
4211 let modehash = findkeyhash conf "help" in
4212 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4213 G.postRedisplay "help";
4216 let entermsgsmode =
4217 let msgsource =
4218 let re = Str.regexp "[\r\n]" in
4219 (object
4220 inherit lvsourcebase
4221 val mutable m_items = [||]
4223 method getitemcount = 1 + Array.length m_items
4225 method getitem n =
4226 if n = 0
4227 then "[Clear]", 0
4228 else m_items.(n-1), 0
4230 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4231 ignore uioh;
4232 if not cancel
4233 then (
4234 if active = 0
4235 then Buffer.clear state.errmsgs;
4236 m_qsearch <- qsearch;
4238 m_active <- active;
4239 m_first <- first;
4240 m_pan <- pan;
4241 None
4243 method hasaction n =
4244 n = 0
4246 method reset =
4247 state.newerrmsgs <- false;
4248 let l = Str.split re (Buffer.contents state.errmsgs) in
4249 m_items <- Array.of_list l
4251 initializer
4252 m_active <- 0
4253 end)
4254 in fun () ->
4255 state.text <- "";
4256 msgsource#reset;
4257 let source = (msgsource :> lvsource) in
4258 let modehash = findkeyhash conf "listview" in
4259 state.uioh <- coe (object
4260 inherit listview ~source ~trusted:false ~modehash as super
4261 method display =
4262 if state.newerrmsgs
4263 then msgsource#reset;
4264 super#display
4265 end);
4266 G.postRedisplay "msgs";
4269 let quickbookmark ?title () =
4270 match state.layout with
4271 | [] -> ()
4272 | l :: _ ->
4273 let title =
4274 match title with
4275 | None ->
4276 let sec = Unix.gettimeofday () in
4277 let tm = Unix.localtime sec in
4278 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4279 (l.pageno+1)
4280 tm.Unix.tm_mday
4281 tm.Unix.tm_mon
4282 (tm.Unix.tm_year + 1900)
4283 tm.Unix.tm_hour
4284 tm.Unix.tm_min
4285 | Some title -> title
4287 state.bookmarks <-
4288 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4289 :: state.bookmarks
4292 let doreshape w h =
4293 state.fullscreen <- None;
4294 Wsi.reshape w h;
4297 let setautoscrollspeed step goingdown =
4298 let incr = max 1 ((abs step) / 2) in
4299 let incr = if goingdown then incr else -incr in
4300 let astep = step + incr in
4301 state.autoscroll <- Some astep;
4304 let viewkeyboard key mask =
4305 let enttext te =
4306 let mode = state.mode in
4307 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4308 state.text <- "";
4309 enttext ();
4310 G.postRedisplay "view:enttext"
4312 let ctrl = Wsi.withctrl mask in
4313 match key with
4314 | 81 -> (* Q *)
4315 exit 0
4317 | 0xff63 -> (* insert *)
4318 if conf.angle mod 360 = 0
4319 then (
4320 state.mode <- LinkNav (Ltgendir 0);
4321 gotoy state.y;
4323 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4325 | 0xff1b | 113 -> (* escape / q *)
4326 begin match state.mstate with
4327 | Mzoomrect _ ->
4328 state.mstate <- Mnone;
4329 Wsi.setcursor Wsi.CURSOR_INHERIT;
4330 G.postRedisplay "kill zoom rect";
4331 | _ ->
4332 match state.ranchors with
4333 | [] -> raise Quit
4334 | (path, password, anchor) :: rest ->
4335 state.ranchors <- rest;
4336 state.anchor <- anchor;
4337 opendoc path password
4338 end;
4340 | 0xff08 -> (* backspace *)
4341 let y = getnav ~-1 in
4342 gotoy_and_clear_text y
4344 | 111 -> (* o *)
4345 enteroutlinemode ()
4347 | 117 -> (* u *)
4348 state.rects <- [];
4349 state.text <- "";
4350 G.postRedisplay "dehighlight";
4352 | 47 | 63 -> (* / ? *)
4353 let ondone isforw s =
4354 cbput state.hists.pat s;
4355 state.searchpattern <- s;
4356 search s isforw
4358 let s = String.create 1 in
4359 s.[0] <- Char.chr key;
4360 enttext (s, "", Some (onhist state.hists.pat),
4361 textentry, ondone (key = 47))
4363 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4364 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4365 setzoom (conf.zoom +. incr)
4367 | 43 | 0xffab -> (* + *)
4368 let ondone s =
4369 let n =
4370 try int_of_string s with exc ->
4371 state.text <- Printf.sprintf "bad integer `%s': %s"
4372 s (Printexc.to_string exc);
4373 max_int
4375 if n != max_int
4376 then (
4377 conf.pagebias <- n;
4378 state.text <- "page bias is now " ^ string_of_int n;
4381 enttext ("page bias: ", "", None, intentry, ondone)
4383 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4384 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4385 setzoom (max 0.01 (conf.zoom -. decr))
4387 | 45 | 0xffad -> (* - *)
4388 let ondone msg = state.text <- msg in
4389 enttext (
4390 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4391 optentry state.mode, ondone
4394 | 48 when ctrl -> (* ctrl-0 *)
4395 setzoom 1.0
4397 | 49 when ctrl -> (* 1 *)
4398 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4399 if zoom < 1.0
4400 then setzoom zoom
4402 | 0xffc6 -> (* f9 *)
4403 togglebirdseye ()
4405 | 57 when ctrl -> (* ctrl-9 *)
4406 togglebirdseye ()
4408 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4409 when not ctrl -> (* 0..9 *)
4410 let ondone s =
4411 let n =
4412 try int_of_string s with exc ->
4413 state.text <- Printf.sprintf "bad integer `%s': %s"
4414 s (Printexc.to_string exc);
4417 if n >= 0
4418 then (
4419 addnav ();
4420 cbput state.hists.pag (string_of_int n);
4421 gotopage1 (n + conf.pagebias - 1) 0;
4424 let pageentry text key =
4425 match Char.unsafe_chr key with
4426 | 'g' -> TEdone text
4427 | _ -> intentry text key
4429 let text = "x" in text.[0] <- Char.chr key;
4430 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4432 | 98 -> (* b *)
4433 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4434 reshape conf.winw conf.winh;
4436 | 108 -> (* l *)
4437 conf.hlinks <- not conf.hlinks;
4438 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4439 G.postRedisplay "toggle highlightlinks";
4441 | 97 -> (* a *)
4442 begin match state.autoscroll with
4443 | Some step ->
4444 conf.autoscrollstep <- step;
4445 state.autoscroll <- None
4446 | None ->
4447 if conf.autoscrollstep = 0
4448 then state.autoscroll <- Some 1
4449 else state.autoscroll <- Some conf.autoscrollstep
4452 | 80 -> (* P *)
4453 conf.presentation <- not conf.presentation;
4454 if conf.presentation
4455 then (
4456 if not conf.scrollbarinpm
4457 then state.scrollw <- 0;
4459 else
4460 state.scrollw <- conf.scrollbw;
4462 showtext ' ' ("presentation mode " ^
4463 if conf.presentation then "on" else "off");
4464 state.anchor <- getanchor ();
4465 represent ()
4467 | 102 -> (* f *)
4468 begin match state.fullscreen with
4469 | None ->
4470 state.fullscreen <- Some (conf.winw, conf.winh);
4471 Wsi.fullscreen ()
4472 | Some (w, h) ->
4473 state.fullscreen <- None;
4474 doreshape w h
4477 | 103 -> (* g *)
4478 gotoy_and_clear_text 0
4480 | 71 -> (* G *)
4481 gotopage1 (state.pagecount - 1) 0
4483 | 112 | 78 -> (* p|N *)
4484 search state.searchpattern false
4486 | 110 | 0xffc0 -> (* n|F3 *)
4487 search state.searchpattern true
4489 | 116 -> (* t *)
4490 begin match state.layout with
4491 | [] -> ()
4492 | l :: _ ->
4493 gotoy_and_clear_text (getpagey l.pageno)
4496 | 32 -> (* ' ' *)
4497 begin match List.rev state.layout with
4498 | [] -> ()
4499 | l :: _ ->
4500 let pageno = min (l.pageno+1) (state.pagecount-1) in
4501 gotoy_and_clear_text (getpagey pageno)
4504 | 0xff9f | 0xffff -> (* delete *)
4505 begin match state.layout with
4506 | [] -> ()
4507 | l :: _ ->
4508 let pageno = max 0 (l.pageno-1) in
4509 gotoy_and_clear_text (getpagey pageno)
4512 | 61 -> (* = *)
4513 showtext ' ' (describe_location ());
4515 | 119 -> (* w *)
4516 begin match state.layout with
4517 | [] -> ()
4518 | l :: _ ->
4519 doreshape (l.pagew + state.scrollw) l.pageh;
4520 G.postRedisplay "w"
4523 | 39 -> (* ' *)
4524 enterbookmarkmode ()
4526 | 104 | 0xffbe -> (* h|F1 *)
4527 enterhelpmode ()
4529 | 105 -> (* i *)
4530 enterinfomode ()
4532 | 101 when conf.redirectstderr -> (* e *)
4533 entermsgsmode ()
4535 | 109 -> (* m *)
4536 let ondone s =
4537 match state.layout with
4538 | l :: _ ->
4539 state.bookmarks <-
4540 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4541 :: state.bookmarks
4542 | _ -> ()
4544 enttext ("bookmark: ", "", None, textentry, ondone)
4546 | 126 -> (* ~ *)
4547 quickbookmark ();
4548 showtext ' ' "Quick bookmark added";
4550 | 122 -> (* z *)
4551 begin match state.layout with
4552 | l :: _ ->
4553 let rect = getpdimrect l.pagedimno in
4554 let w, h =
4555 if conf.crophack
4556 then
4557 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4558 truncate (1.2 *. (rect.(3) -. rect.(0))))
4559 else
4560 (truncate (rect.(1) -. rect.(0)),
4561 truncate (rect.(3) -. rect.(0)))
4563 let w = truncate ((float w)*.conf.zoom)
4564 and h = truncate ((float h)*.conf.zoom) in
4565 if w != 0 && h != 0
4566 then (
4567 state.anchor <- getanchor ();
4568 doreshape (w + state.scrollw) (h + conf.interpagespace)
4570 G.postRedisplay "z";
4572 | [] -> ()
4575 | 50 when ctrl -> (* ctrl-2 *)
4576 let maxw = getmaxw () in
4577 if maxw > 0.0
4578 then setzoom (maxw /. float conf.winw)
4580 | 60 | 62 -> (* < > *)
4581 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
4583 | 91 | 93 -> (* [ ] *)
4584 conf.colorscale <-
4585 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4587 G.postRedisplay "brightness";
4589 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
4590 setzoom state.prevzoom
4592 | 107 | 0xff52 -> (* k up *)
4593 begin match state.autoscroll with
4594 | None ->
4595 begin match state.mode with
4596 | Birdseye beye -> upbirdseye 1 beye
4597 | _ ->
4598 if ctrl
4599 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
4600 else gotoy_and_clear_text (clamp (-conf.scrollstep))
4602 | Some n ->
4603 setautoscrollspeed n false
4606 | 106 | 0xff54 -> (* j down *)
4607 begin match state.autoscroll with
4608 | None ->
4609 begin match state.mode with
4610 | Birdseye beye -> downbirdseye 1 beye
4611 | _ ->
4612 if ctrl
4613 then gotoy_and_clear_text (clamp (conf.winh/2))
4614 else gotoy_and_clear_text (clamp conf.scrollstep)
4616 | Some n ->
4617 setautoscrollspeed n true
4620 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
4621 if conf.zoom > 1.0
4622 then
4623 let dx =
4624 if ctrl
4625 then conf.winw / 2
4626 else 10
4628 let dx = if key = 0xff51 then dx else -dx in
4629 state.x <- state.x + dx;
4630 gotoy_and_clear_text state.y
4631 else (
4632 state.text <- "";
4633 G.postRedisplay "lef/right"
4636 | 0xff55 -> (* prior *)
4637 let y =
4638 if ctrl
4639 then
4640 match state.layout with
4641 | [] -> state.y
4642 | l :: _ -> state.y - l.pagey
4643 else
4644 clamp (-conf.winh)
4646 gotoghyll y
4648 | 0xff56 -> (* next *)
4649 let y =
4650 if ctrl
4651 then
4652 match List.rev state.layout with
4653 | [] -> state.y
4654 | l :: _ -> getpagey l.pageno
4655 else
4656 clamp conf.winh
4658 gotoghyll y
4660 | 0xff50 -> gotoghyll 0
4661 | 0xff57 -> gotoghyll (clamp state.maxy)
4662 | 0xff53 when Wsi.withalt mask ->
4663 gotoghyll (getnav ~-1)
4664 | 0xff51 when Wsi.withalt mask ->
4665 gotoghyll (getnav 1)
4667 | 114 -> (* r *)
4668 state.anchor <- getanchor ();
4669 opendoc state.path state.password
4671 | 76 -> (* L *)
4672 launchpath ()
4674 | 118 when conf.debug -> (* v *)
4675 state.rects <- [];
4676 List.iter (fun l ->
4677 match getopaque l.pageno with
4678 | None -> ()
4679 | Some opaque ->
4680 let x0, y0, x1, y1 = pagebbox opaque in
4681 let a,b = float x0, float y0 in
4682 let c,d = float x1, float y0 in
4683 let e,f = float x1, float y1 in
4684 let h,j = float x0, float y1 in
4685 let rect = (a,b,c,d,e,f,h,j) in
4686 debugrect rect;
4687 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4688 ) state.layout;
4689 G.postRedisplay "v";
4691 | _ ->
4692 vlog "huh? %s" (Wsi.keyname key)
4695 let gotounder = function
4696 | Ulinkgoto (pageno, top) ->
4697 if pageno >= 0
4698 then (
4699 addnav ();
4700 gotopage1 pageno top;
4703 | Ulinkuri s ->
4704 gotouri s
4706 | Uremote (filename, pageno) ->
4707 let path =
4708 if Sys.file_exists filename
4709 then filename
4710 else
4711 let dir = Filename.dirname state.path in
4712 let path = Filename.concat dir filename in
4713 if Sys.file_exists path
4714 then path
4715 else ""
4717 if String.length path > 0
4718 then (
4719 let anchor = getanchor () in
4720 let ranchor = state.path, state.password, anchor in
4721 state.anchor <- (pageno, 0.0);
4722 state.ranchors <- ranchor :: state.ranchors;
4723 opendoc path "";
4725 else showtext '!' ("Could not find " ^ filename)
4727 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4730 let linknavkeyboard key mask linknav =
4731 let getpage pageno =
4732 let rec loop = function
4733 | [] -> None
4734 | l :: _ when l.pageno = pageno -> Some l
4735 | _ :: rest -> loop rest
4736 in loop state.layout
4738 let doexact (pageno, n) =
4739 match getopaque pageno, getpage pageno with
4740 | Some opaque, Some l ->
4741 if key = 0xff0d
4742 then
4743 let under = getlink opaque n in
4744 G.postRedisplay "link gotounder";
4745 gotounder under;
4746 state.mode <- View;
4747 else
4748 let opt, dir =
4749 match key with
4750 | 0xff50 -> (* home *)
4751 Some (findlink opaque LDfirst), -1
4753 | 0xff57 -> (* end *)
4754 Some (findlink opaque LDlast), 1
4756 | 0xff51 -> (* left *)
4757 Some (findlink opaque (LDleft n)), -1
4759 | 0xff53 -> (* right *)
4760 Some (findlink opaque (LDright n)), 1
4762 | 0xff52 -> (* up *)
4763 Some (findlink opaque (LDup n)), -1
4765 | 0xff54 -> (* down *)
4766 Some (findlink opaque (LDdown n)), 1
4768 | _ -> None, 0
4770 let pwl l dir =
4771 begin match findpwl l.pageno dir with
4772 | Pwlnotfound -> ()
4773 | Pwl pageno ->
4774 let notfound dir =
4775 state.mode <- LinkNav (Ltgendir dir);
4776 let y, h = getpageyh pageno in
4777 let y =
4778 if dir < 0
4779 then y + h - conf.winh
4780 else y
4782 gotoy y
4784 begin match getopaque pageno, getpage pageno with
4785 | Some opaque, Some _ ->
4786 let link =
4787 let ld = if dir > 0 then LDfirst else LDlast in
4788 findlink opaque ld
4790 begin match link with
4791 | Lfound (m, x0, y0, x1, y1) ->
4792 let r = x0, y0, x1, y1 in
4793 showlinktype (getlink opaque m);
4794 state.mode <- LinkNav (Ltexact ((pageno, m), r));
4795 G.postRedisplay "linknav jpage";
4796 | _ -> notfound dir
4797 end;
4798 | _ -> notfound dir
4799 end;
4800 end;
4802 begin match opt with
4803 | Some Lnotfound -> pwl l dir;
4804 | Some (Lfound (m, x0, y0, x1, y1)) ->
4805 if m = n
4806 then pwl l dir
4807 else (
4808 if y0 < l.pagey
4809 then gotopage1 l.pageno y0
4810 else (
4811 let d = fstate.fontsize + 1 in
4812 if y1 - l.pagey > l.pagevh - d
4813 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
4814 else G.postRedisplay "linknav";
4816 let r = x0, y0, x1, y1 in
4817 showlinktype (getlink opaque m);
4818 state.mode <- LinkNav (Ltexact ((l.pageno, m), r));
4821 | None ->
4822 state.mode <- LinkNav (Ltgendir 0);
4823 viewkeyboard key mask
4824 end;
4825 | _ -> viewkeyboard key mask
4827 if key = 0xff63
4828 then (
4829 state.mode <- View;
4830 G.postRedisplay "leave linknav"
4832 else
4833 match linknav with
4834 | Ltgendir _ -> viewkeyboard key mask
4835 | Ltexact (exact, _) -> doexact exact
4838 let keyboard key mask =
4839 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
4840 then wcmd "interrupt"
4841 else state.uioh <- state.uioh#key key mask
4844 let birdseyekeyboard key mask
4845 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
4846 let incr =
4847 match conf.columns with
4848 | None -> 1
4849 | Some ((c, _, _), _) -> c
4851 match key with
4852 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
4853 let y, h = getpageyh pageno in
4854 let top = (conf.winh - h) / 2 in
4855 gotoy (max 0 (y - top))
4856 | 0xff0d -> leavebirdseye beye false
4857 | 0xff1b -> leavebirdseye beye true (* escape *)
4858 | 0xff52 -> upbirdseye incr beye (* prior *)
4859 | 0xff54 -> downbirdseye incr beye (* next *)
4860 | 0xff51 -> upbirdseye 1 beye (* up *)
4861 | 0xff53 -> downbirdseye 1 beye (* down *)
4863 | 0xff55 ->
4864 begin match state.layout with
4865 | l :: _ ->
4866 if l.pagey != 0
4867 then (
4868 state.mode <- Birdseye (
4869 oconf, leftx, l.pageno, hooverpageno, anchor
4871 gotopage1 l.pageno 0;
4873 else (
4874 let layout = layout (state.y-conf.winh) conf.winh in
4875 match layout with
4876 | [] -> gotoy (clamp (-conf.winh))
4877 | l :: _ ->
4878 state.mode <- Birdseye (
4879 oconf, leftx, l.pageno, hooverpageno, anchor
4881 gotopage1 l.pageno 0
4884 | [] -> gotoy (clamp (-conf.winh))
4885 end;
4887 | 0xff56 ->
4888 begin match List.rev state.layout with
4889 | l :: _ ->
4890 let layout = layout (state.y + conf.winh) conf.winh in
4891 begin match layout with
4892 | [] ->
4893 let incr = l.pageh - l.pagevh in
4894 if incr = 0
4895 then (
4896 state.mode <-
4897 Birdseye (
4898 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4900 G.postRedisplay "birdseye pagedown";
4902 else gotoy (clamp (incr + conf.interpagespace*2));
4904 | l :: _ ->
4905 state.mode <-
4906 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4907 gotopage1 l.pageno 0;
4910 | [] -> gotoy (clamp conf.winh)
4911 end;
4913 | 0xff50 ->
4914 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4915 gotopage1 0 0
4917 | 0xff57 ->
4918 let pageno = state.pagecount - 1 in
4919 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4920 if not (pagevisible state.layout pageno)
4921 then
4922 let h =
4923 match List.rev state.pdims with
4924 | [] -> conf.winh
4925 | (_, _, h, _) :: _ -> h
4927 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4928 else G.postRedisplay "birdseye end";
4929 | _ -> viewkeyboard key mask
4932 let drawpage l =
4933 let color =
4934 match state.mode with
4935 | Textentry _ -> scalecolor 0.4
4936 | LinkNav _
4937 | View -> scalecolor 1.0
4938 | Birdseye (_, _, pageno, hooverpageno, _) ->
4939 if l.pageno = hooverpageno
4940 then scalecolor 0.9
4941 else (
4942 if l.pageno = pageno
4943 then scalecolor 1.0
4944 else scalecolor 0.8
4947 drawtiles l color;
4948 begin match getopaque l.pageno with
4949 | Some opaque ->
4950 if tileready l l.pagex l.pagey
4951 then
4952 let x = l.pagedispx - l.pagex
4953 and y = l.pagedispy - l.pagey in
4954 postprocess opaque conf.hlinks x y;
4956 | _ -> ()
4957 end;
4960 let scrollindicator () =
4961 let sbw, ph, sh = state.uioh#scrollph in
4962 let sbh, pw, sw = state.uioh#scrollpw in
4964 GlDraw.color (0.64, 0.64, 0.64);
4965 GlDraw.rect
4966 (float (conf.winw - sbw), 0.)
4967 (float conf.winw, float conf.winh)
4969 GlDraw.rect
4970 (0., float (conf.winh - sbh))
4971 (float (conf.winw - state.scrollw - 1), float conf.winh)
4973 GlDraw.color (0.0, 0.0, 0.0);
4975 GlDraw.rect
4976 (float (conf.winw - sbw), ph)
4977 (float conf.winw, ph +. sh)
4979 GlDraw.rect
4980 (pw, float (conf.winh - sbh))
4981 (pw +. sw, float conf.winh)
4985 let showsel () =
4986 match state.mstate with
4987 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
4990 | Msel ((x0, y0), (x1, y1)) ->
4991 let rec loop = function
4992 | l :: ls ->
4993 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
4994 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
4995 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
4996 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
4997 then
4998 match getopaque l.pageno with
4999 | Some opaque ->
5000 let x0, y0 = pagetranslatepoint l x0 y0 in
5001 let x1, y1 = pagetranslatepoint l x1 y1 in
5002 seltext opaque (x0, y0, x1, y1);
5003 | _ -> ()
5004 else loop ls
5005 | [] -> ()
5007 loop state.layout
5010 let showrects rects =
5011 Gl.enable `blend;
5012 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5013 GlDraw.polygon_mode `both `fill;
5014 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5015 List.iter
5016 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5017 List.iter (fun l ->
5018 if l.pageno = pageno
5019 then (
5020 let dx = float (l.pagedispx - l.pagex) in
5021 let dy = float (l.pagedispy - l.pagey) in
5022 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5023 GlDraw.begins `quads;
5025 GlDraw.vertex2 (x0+.dx, y0+.dy);
5026 GlDraw.vertex2 (x1+.dx, y1+.dy);
5027 GlDraw.vertex2 (x2+.dx, y2+.dy);
5028 GlDraw.vertex2 (x3+.dx, y3+.dy);
5030 GlDraw.ends ();
5032 ) state.layout
5033 ) rects
5035 Gl.disable `blend;
5038 let display () =
5039 GlClear.color (scalecolor2 conf.bgcolor);
5040 GlClear.clear [`color];
5041 List.iter drawpage state.layout;
5042 let rects =
5043 match state.mode with
5044 | LinkNav (Ltexact ((pageno, _), (x0, y0, x1, y1))) ->
5045 (pageno, 5, (
5046 float x0, float y0,
5047 float x1, float y0,
5048 float x1, float y1,
5049 float x0, float y1)
5050 ) :: state.rects
5051 | _ -> state.rects
5053 showrects rects;
5054 showsel ();
5055 state.uioh#display;
5056 begin match state.mstate with
5057 | Mzoomrect ((x0, y0), (x1, y1)) ->
5058 Gl.enable `blend;
5059 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5060 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5061 GlDraw.rect (float x0, float y0)
5062 (float x1, float y1);
5063 Gl.disable `blend;
5064 | _ -> ()
5065 end;
5066 enttext ();
5067 scrollindicator ();
5068 Wsi.swapb ();
5071 let zoomrect x y x1 y1 =
5072 let x0 = min x x1
5073 and x1 = max x x1
5074 and y0 = min y y1 in
5075 gotoy (state.y + y0);
5076 state.anchor <- getanchor ();
5077 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5078 let margin =
5079 if state.w < conf.winw - state.scrollw
5080 then (conf.winw - state.scrollw - state.w) / 2
5081 else 0
5083 state.x <- (state.x + margin) - x0;
5084 setzoom zoom;
5085 Wsi.setcursor Wsi.CURSOR_INHERIT;
5086 state.mstate <- Mnone;
5089 let scrollx x =
5090 let winw = conf.winw - state.scrollw - 1 in
5091 let s = float x /. float winw in
5092 let destx = truncate (float (state.w + winw) *. s) in
5093 state.x <- winw - destx;
5094 gotoy_and_clear_text state.y;
5095 state.mstate <- Mscrollx;
5098 let scrolly y =
5099 let s = float y /. float conf.winh in
5100 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5101 gotoy_and_clear_text desty;
5102 state.mstate <- Mscrolly;
5105 let viewmouse button down x y mask =
5106 match button with
5107 | n when (n == 4 || n == 5) && not down ->
5108 if Wsi.withctrl mask
5109 then (
5110 match state.mstate with
5111 | Mzoom (oldn, i) ->
5112 if oldn = n
5113 then (
5114 if i = 2
5115 then
5116 let incr =
5117 match n with
5118 | 5 ->
5119 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5120 | _ ->
5121 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5123 let zoom = conf.zoom -. incr in
5124 setzoom zoom;
5125 state.mstate <- Mzoom (n, 0);
5126 else
5127 state.mstate <- Mzoom (n, i+1);
5129 else state.mstate <- Mzoom (n, 0)
5131 | _ -> state.mstate <- Mzoom (n, 0)
5133 else (
5134 match state.autoscroll with
5135 | Some step -> setautoscrollspeed step (n=4)
5136 | None ->
5137 let incr =
5138 if n = 4
5139 then -conf.scrollstep
5140 else conf.scrollstep
5142 let incr = incr * 2 in
5143 let y = clamp incr in
5144 gotoy_and_clear_text y
5147 | 1 when Wsi.withctrl mask ->
5148 if down
5149 then (
5150 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5151 state.mstate <- Mpan (x, y)
5153 else
5154 state.mstate <- Mnone
5156 | 3 ->
5157 if down
5158 then (
5159 Wsi.setcursor Wsi.CURSOR_CYCLE;
5160 let p = (x, y) in
5161 state.mstate <- Mzoomrect (p, p)
5163 else (
5164 match state.mstate with
5165 | Mzoomrect ((x0, y0), _) ->
5166 if abs (x-x0) > 10 && abs (y - y0) > 10
5167 then zoomrect x0 y0 x y
5168 else (
5169 state.mstate <- Mnone;
5170 Wsi.setcursor Wsi.CURSOR_INHERIT;
5171 G.postRedisplay "kill accidental zoom rect";
5173 | _ ->
5174 Wsi.setcursor Wsi.CURSOR_INHERIT;
5175 state.mstate <- Mnone
5178 | 1 when x > conf.winw - state.scrollw ->
5179 if down
5180 then
5181 let _, position, sh = state.uioh#scrollph in
5182 if y > truncate position && y < truncate (position +. sh)
5183 then state.mstate <- Mscrolly
5184 else scrolly y
5185 else
5186 state.mstate <- Mnone
5188 | 1 when y > conf.winh - state.hscrollh ->
5189 if down
5190 then
5191 let _, position, sw = state.uioh#scrollpw in
5192 if x > truncate position && x < truncate (position +. sw)
5193 then state.mstate <- Mscrollx
5194 else scrollx x
5195 else
5196 state.mstate <- Mnone
5198 | 1 ->
5199 let dest = if down then getunder x y else Unone in
5200 begin match dest with
5201 | Ulinkgoto (pageno, top) ->
5202 if pageno >= 0
5203 then (
5204 addnav ();
5205 gotopage1 pageno top;
5208 | Ulinkuri s ->
5209 gotouri s
5211 | Uremote (filename, pageno) ->
5212 let path =
5213 if Sys.file_exists filename
5214 then filename
5215 else
5216 let dir = Filename.dirname state.path in
5217 let path = Filename.concat dir filename in
5218 if Sys.file_exists path
5219 then path
5220 else ""
5222 if String.length path > 0
5223 then (
5224 let anchor = getanchor () in
5225 let ranchor = state.path, state.password, anchor in
5226 state.anchor <- (pageno, 0.0);
5227 state.ranchors <- ranchor :: state.ranchors;
5228 opendoc path "";
5230 else showtext '!' ("Could not find " ^ filename)
5232 | Uunexpected _ | Ulaunch _ | Unamed _ -> ()
5234 | Unone when down ->
5235 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5236 state.mstate <- Mpan (x, y);
5238 | Unone | Utext _ ->
5239 if down
5240 then (
5241 if conf.angle mod 360 = 0
5242 then (
5243 state.mstate <- Msel ((x, y), (x, y));
5244 G.postRedisplay "mouse select";
5247 else (
5248 match state.mstate with
5249 | Mnone -> ()
5251 | Mzoom _ | Mscrollx | Mscrolly ->
5252 state.mstate <- Mnone
5254 | Mzoomrect ((x0, y0), _) ->
5255 zoomrect x0 y0 x y
5257 | Mpan _ ->
5258 Wsi.setcursor Wsi.CURSOR_INHERIT;
5259 state.mstate <- Mnone
5261 | Msel ((_, y0), (_, y1)) ->
5262 let rec loop = function
5263 | [] -> ()
5264 | l :: rest ->
5265 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5266 || ((y1 >= l.pagedispy
5267 && y1 <= (l.pagedispy + l.pagevh)))
5268 then
5269 match getopaque l.pageno with
5270 | Some opaque ->
5271 copysel conf.selcmd opaque;
5272 G.postRedisplay "copysel"
5273 | _ -> ()
5274 else loop rest
5276 loop state.layout;
5277 Wsi.setcursor Wsi.CURSOR_INHERIT;
5278 state.mstate <- Mnone;
5282 | _ -> ()
5285 let birdseyemouse button down x y mask
5286 (conf, leftx, _, hooverpageno, anchor) =
5287 match button with
5288 | 1 when down ->
5289 let rec loop = function
5290 | [] -> ()
5291 | l :: rest ->
5292 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5293 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5294 then (
5295 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5297 else loop rest
5299 loop state.layout
5300 | 3 -> ()
5301 | _ -> viewmouse button down x y mask
5304 let mouse button down x y mask =
5305 state.uioh <- state.uioh#button button down x y mask;
5308 let motion ~x ~y =
5309 state.uioh <- state.uioh#motion x y
5312 let pmotion ~x ~y =
5313 state.uioh <- state.uioh#pmotion x y;
5316 let uioh = object
5317 method display = ()
5319 method key key mask =
5320 begin match state.mode with
5321 | Textentry textentry -> textentrykeyboard key mask textentry
5322 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5323 | View -> viewkeyboard key mask
5324 | LinkNav linknav -> linknavkeyboard key mask linknav
5325 end;
5326 state.uioh
5328 method button button bstate x y mask =
5329 begin match state.mode with
5330 | LinkNav _
5331 | View -> viewmouse button bstate x y mask
5332 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5333 | Textentry _ -> ()
5334 end;
5335 state.uioh
5337 method motion x y =
5338 begin match state.mode with
5339 | Textentry _ -> ()
5340 | View | Birdseye _ | LinkNav _ ->
5341 match state.mstate with
5342 | Mzoom _ | Mnone -> ()
5344 | Mpan (x0, y0) ->
5345 let dx = x - x0
5346 and dy = y0 - y in
5347 state.mstate <- Mpan (x, y);
5348 if conf.zoom > 1.0 then state.x <- state.x + dx;
5349 let y = clamp dy in
5350 gotoy_and_clear_text y
5352 | Msel (a, _) ->
5353 state.mstate <- Msel (a, (x, y));
5354 G.postRedisplay "motion select";
5356 | Mscrolly ->
5357 let y = min conf.winh (max 0 y) in
5358 scrolly y
5360 | Mscrollx ->
5361 let x = min conf.winw (max 0 x) in
5362 scrollx x
5364 | Mzoomrect (p0, _) ->
5365 state.mstate <- Mzoomrect (p0, (x, y));
5366 G.postRedisplay "motion zoomrect";
5367 end;
5368 state.uioh
5370 method pmotion x y =
5371 begin match state.mode with
5372 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5373 let rec loop = function
5374 | [] ->
5375 if hooverpageno != -1
5376 then (
5377 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5378 G.postRedisplay "pmotion birdseye no hoover";
5380 | l :: rest ->
5381 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5382 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5383 then (
5384 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5385 G.postRedisplay "pmotion birdseye hoover";
5387 else loop rest
5389 loop state.layout
5391 | Textentry _ -> ()
5393 | LinkNav _
5394 | View ->
5395 match state.mstate with
5396 | Mnone -> updateunder x y
5397 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5399 end;
5400 state.uioh
5402 method infochanged _ = ()
5404 method scrollph =
5405 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5406 let p, h = scrollph state.y maxy in
5407 state.scrollw, p, h
5409 method scrollpw =
5410 let winw = conf.winw - state.scrollw - 1 in
5411 let fwinw = float winw in
5412 let sw =
5413 let sw = fwinw /. float state.w in
5414 let sw = fwinw *. sw in
5415 max sw (float conf.scrollh)
5417 let position, sw =
5418 let f = state.w+winw in
5419 let r = float (winw-state.x) /. float f in
5420 let p = fwinw *. r in
5421 p-.sw/.2., sw
5423 let sw =
5424 if position +. sw > fwinw
5425 then fwinw -. position
5426 else sw
5428 state.hscrollh, position, sw
5430 method modehash =
5431 let modename =
5432 match state.mode with
5433 | LinkNav _ -> "links"
5434 | Textentry _ -> "textentry"
5435 | Birdseye _ -> "birdseye"
5436 | View -> "global"
5438 findkeyhash conf modename
5439 end;;
5441 module Config =
5442 struct
5443 open Parser
5445 let fontpath = ref "";;
5447 module KeyMap =
5448 Map.Make (struct type t = (int * int) let compare = compare end);;
5450 let unent s =
5451 let l = String.length s in
5452 let b = Buffer.create l in
5453 unent b s 0 l;
5454 Buffer.contents b;
5457 let home =
5458 try Sys.getenv "HOME"
5459 with exn ->
5460 prerr_endline
5461 ("Can not determine home directory location: " ^
5462 Printexc.to_string exn);
5466 let modifier_of_string = function
5467 | "alt" -> Wsi.altmask
5468 | "shift" -> Wsi.shiftmask
5469 | "ctrl" | "control" -> Wsi.ctrlmask
5470 | "meta" -> Wsi.metamask
5471 | _ -> 0
5474 let key_of_string =
5475 let r = Str.regexp "-" in
5476 fun s ->
5477 let elems = Str.full_split r s in
5478 let f n k m =
5479 let g s =
5480 let m1 = modifier_of_string s in
5481 if m1 = 0
5482 then (Wsi.namekey s, m)
5483 else (k, m lor m1)
5484 in function
5485 | Str.Delim s when n land 1 = 0 -> g s
5486 | Str.Text s -> g s
5487 | Str.Delim _ -> (k, m)
5489 let rec loop n k m = function
5490 | [] -> (k, m)
5491 | x :: xs ->
5492 let k, m = f n k m x in
5493 loop (n+1) k m xs
5495 loop 0 0 0 elems
5498 let keys_of_string =
5499 let r = Str.regexp "[ \t]" in
5500 fun s ->
5501 let elems = Str.split r s in
5502 List.map key_of_string elems
5505 let copykeyhashes c =
5506 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5509 let config_of c attrs =
5510 let apply c k v =
5512 match k with
5513 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5514 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5515 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5516 | "preload" -> { c with preload = bool_of_string v }
5517 | "page-bias" -> { c with pagebias = int_of_string v }
5518 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5519 | "auto-scroll-step" ->
5520 { c with autoscrollstep = max 0 (int_of_string v) }
5521 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5522 | "crop-hack" -> { c with crophack = bool_of_string v }
5523 | "throttle" ->
5524 let mw =
5525 match String.lowercase v with
5526 | "true" -> Some infinity
5527 | "false" -> None
5528 | f -> Some (float_of_string f)
5530 { c with maxwait = mw}
5531 | "highlight-links" -> { c with hlinks = bool_of_string v }
5532 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5533 | "vertical-margin" ->
5534 { c with interpagespace = max 0 (int_of_string v) }
5535 | "zoom" ->
5536 let zoom = float_of_string v /. 100. in
5537 let zoom = max zoom 0.0 in
5538 { c with zoom = zoom }
5539 | "presentation" -> { c with presentation = bool_of_string v }
5540 | "rotation-angle" -> { c with angle = int_of_string v }
5541 | "width" -> { c with winw = max 20 (int_of_string v) }
5542 | "height" -> { c with winh = max 20 (int_of_string v) }
5543 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5544 | "proportional-display" -> { c with proportional = bool_of_string v }
5545 | "pixmap-cache-size" ->
5546 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5547 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5548 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5549 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5550 | "persistent-location" -> { c with jumpback = bool_of_string v }
5551 | "background-color" -> { c with bgcolor = color_of_string v }
5552 | "scrollbar-in-presentation" ->
5553 { c with scrollbarinpm = bool_of_string v }
5554 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5555 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5556 | "mupdf-store-size" ->
5557 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5558 | "checkers" -> { c with checkers = bool_of_string v }
5559 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5560 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5561 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5562 | "uri-launcher" -> { c with urilauncher = unent v }
5563 | "path-launcher" -> { c with pathlauncher = unent v }
5564 | "color-space" -> { c with colorspace = colorspace_of_string v }
5565 | "invert-colors" -> { c with invert = bool_of_string v }
5566 | "brightness" -> { c with colorscale = float_of_string v }
5567 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5568 | "ghyllscroll" ->
5569 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5570 | "columns" ->
5571 let nab = columns_of_string v in
5572 { c with columns = Some (nab, [||]) }
5573 | "birds-eye-columns" ->
5574 { c with beyecolumns = Some (max (int_of_string v) 2) }
5575 | "selection-command" -> { c with selcmd = unent v }
5576 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5577 | _ -> c
5578 with exn ->
5579 prerr_endline ("Error processing attribute (`" ^
5580 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5583 let rec fold c = function
5584 | [] -> c
5585 | (k, v) :: rest ->
5586 let c = apply c k v in
5587 fold c rest
5589 fold { c with keyhashes = copykeyhashes c } attrs;
5592 let fromstring f pos n v d =
5593 try f v
5594 with exn ->
5595 dolog "Error processing attribute (%S=%S) at %d\n%s"
5596 n v pos (Printexc.to_string exn)
5601 let bookmark_of attrs =
5602 let rec fold title page rely = function
5603 | ("title", v) :: rest -> fold v page rely rest
5604 | ("page", v) :: rest -> fold title v rely rest
5605 | ("rely", v) :: rest -> fold title page v rest
5606 | _ :: rest -> fold title page rely rest
5607 | [] -> title, page, rely
5609 fold "invalid" "0" "0" attrs
5612 let doc_of attrs =
5613 let rec fold path page rely pan = function
5614 | ("path", v) :: rest -> fold v page rely pan rest
5615 | ("page", v) :: rest -> fold path v rely pan rest
5616 | ("rely", v) :: rest -> fold path page v pan rest
5617 | ("pan", v) :: rest -> fold path page rely v rest
5618 | _ :: rest -> fold path page rely pan rest
5619 | [] -> path, page, rely, pan
5621 fold "" "0" "0" "0" attrs
5624 let map_of attrs =
5625 let rec fold rs ls = function
5626 | ("out", v) :: rest -> fold v ls rest
5627 | ("in", v) :: rest -> fold rs v rest
5628 | _ :: rest -> fold ls rs rest
5629 | [] -> ls, rs
5631 fold "" "" attrs
5634 let setconf dst src =
5635 dst.scrollbw <- src.scrollbw;
5636 dst.scrollh <- src.scrollh;
5637 dst.icase <- src.icase;
5638 dst.preload <- src.preload;
5639 dst.pagebias <- src.pagebias;
5640 dst.verbose <- src.verbose;
5641 dst.scrollstep <- src.scrollstep;
5642 dst.maxhfit <- src.maxhfit;
5643 dst.crophack <- src.crophack;
5644 dst.autoscrollstep <- src.autoscrollstep;
5645 dst.maxwait <- src.maxwait;
5646 dst.hlinks <- src.hlinks;
5647 dst.underinfo <- src.underinfo;
5648 dst.interpagespace <- src.interpagespace;
5649 dst.zoom <- src.zoom;
5650 dst.presentation <- src.presentation;
5651 dst.angle <- src.angle;
5652 dst.winw <- src.winw;
5653 dst.winh <- src.winh;
5654 dst.savebmarks <- src.savebmarks;
5655 dst.memlimit <- src.memlimit;
5656 dst.proportional <- src.proportional;
5657 dst.texcount <- src.texcount;
5658 dst.sliceheight <- src.sliceheight;
5659 dst.thumbw <- src.thumbw;
5660 dst.jumpback <- src.jumpback;
5661 dst.bgcolor <- src.bgcolor;
5662 dst.scrollbarinpm <- src.scrollbarinpm;
5663 dst.tilew <- src.tilew;
5664 dst.tileh <- src.tileh;
5665 dst.mustoresize <- src.mustoresize;
5666 dst.checkers <- src.checkers;
5667 dst.aalevel <- src.aalevel;
5668 dst.trimmargins <- src.trimmargins;
5669 dst.trimfuzz <- src.trimfuzz;
5670 dst.urilauncher <- src.urilauncher;
5671 dst.colorspace <- src.colorspace;
5672 dst.invert <- src.invert;
5673 dst.colorscale <- src.colorscale;
5674 dst.redirectstderr <- src.redirectstderr;
5675 dst.ghyllscroll <- src.ghyllscroll;
5676 dst.columns <- src.columns;
5677 dst.beyecolumns <- src.beyecolumns;
5678 dst.selcmd <- src.selcmd;
5679 dst.updatecurs <- src.updatecurs;
5680 dst.pathlauncher <- src.pathlauncher;
5681 dst.keyhashes <- copykeyhashes src;
5684 let get s =
5685 let h = Hashtbl.create 10 in
5686 let dc = { defconf with angle = defconf.angle } in
5687 let rec toplevel v t spos _ =
5688 match t with
5689 | Vdata | Vcdata | Vend -> v
5690 | Vopen ("llppconfig", _, closed) ->
5691 if closed
5692 then v
5693 else { v with f = llppconfig }
5694 | Vopen _ ->
5695 error "unexpected subelement at top level" s spos
5696 | Vclose _ -> error "unexpected close at top level" s spos
5698 and llppconfig v t spos _ =
5699 match t with
5700 | Vdata | Vcdata -> v
5701 | Vend -> error "unexpected end of input in llppconfig" s spos
5702 | Vopen ("defaults", attrs, closed) ->
5703 let c = config_of dc attrs in
5704 setconf dc c;
5705 if closed
5706 then v
5707 else { v with f = defaults }
5709 | Vopen ("ui-font", attrs, closed) ->
5710 let rec getsize size = function
5711 | [] -> size
5712 | ("size", v) :: rest ->
5713 let size =
5714 fromstring int_of_string spos "size" v fstate.fontsize in
5715 getsize size rest
5716 | l -> getsize size l
5718 fstate.fontsize <- getsize fstate.fontsize attrs;
5719 if closed
5720 then v
5721 else { v with f = uifont (Buffer.create 10) }
5723 | Vopen ("doc", attrs, closed) ->
5724 let pathent, spage, srely, span = doc_of attrs in
5725 let path = unent pathent
5726 and pageno = fromstring int_of_string spos "page" spage 0
5727 and rely = fromstring float_of_string spos "rely" srely 0.0
5728 and pan = fromstring int_of_string spos "pan" span 0 in
5729 let c = config_of dc attrs in
5730 let anchor = (pageno, rely) in
5731 if closed
5732 then (Hashtbl.add h path (c, [], pan, anchor); v)
5733 else { v with f = doc path pan anchor c [] }
5735 | Vopen _ ->
5736 error "unexpected subelement in llppconfig" s spos
5738 | Vclose "llppconfig" -> { v with f = toplevel }
5739 | Vclose _ -> error "unexpected close in llppconfig" s spos
5741 and defaults v t spos _ =
5742 match t with
5743 | Vdata | Vcdata -> v
5744 | Vend -> error "unexpected end of input in defaults" s spos
5745 | Vopen ("keymap", attrs, closed) ->
5746 let modename =
5747 try List.assoc "mode" attrs
5748 with Not_found -> "global" in
5749 if closed
5750 then v
5751 else
5752 let ret keymap =
5753 let h = findkeyhash dc modename in
5754 KeyMap.iter (Hashtbl.replace h) keymap;
5755 defaults
5757 { v with f = pkeymap ret KeyMap.empty }
5759 | Vopen (_, _, _) ->
5760 error "unexpected subelement in defaults" s spos
5762 | Vclose "defaults" ->
5763 { v with f = llppconfig }
5765 | Vclose _ -> error "unexpected close in defaults" s spos
5767 and uifont b v t spos epos =
5768 match t with
5769 | Vdata | Vcdata ->
5770 Buffer.add_substring b s spos (epos - spos);
5772 | Vopen (_, _, _) ->
5773 error "unexpected subelement in ui-font" s spos
5774 | Vclose "ui-font" ->
5775 if String.length !fontpath = 0
5776 then fontpath := Buffer.contents b;
5777 { v with f = llppconfig }
5778 | Vclose _ -> error "unexpected close in ui-font" s spos
5779 | Vend -> error "unexpected end of input in ui-font" s spos
5781 and doc path pan anchor c bookmarks v t spos _ =
5782 match t with
5783 | Vdata | Vcdata -> v
5784 | Vend -> error "unexpected end of input in doc" s spos
5785 | Vopen ("bookmarks", _, closed) ->
5786 if closed
5787 then v
5788 else { v with f = pbookmarks path pan anchor c bookmarks }
5790 | Vopen ("keymap", attrs, closed) ->
5791 let modename =
5792 try List.assoc "mode" attrs
5793 with Not_found -> "global"
5795 if closed
5796 then v
5797 else
5798 let ret keymap =
5799 let h = findkeyhash c modename in
5800 KeyMap.iter (Hashtbl.replace h) keymap;
5801 doc path pan anchor c bookmarks
5803 { v with f = pkeymap ret KeyMap.empty }
5805 | Vopen (_, _, _) ->
5806 error "unexpected subelement in doc" s spos
5808 | Vclose "doc" ->
5809 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5810 { v with f = llppconfig }
5812 | Vclose _ -> error "unexpected close in doc" s spos
5814 and pkeymap ret keymap v t spos _ =
5815 match t with
5816 | Vdata | Vcdata -> v
5817 | Vend -> error "unexpected end of input in keymap" s spos
5818 | Vopen ("map", attrs, closed) ->
5819 let r, l = map_of attrs in
5820 let kss = fromstring keys_of_string spos "in" r [] in
5821 let lss = fromstring keys_of_string spos "out" l [] in
5822 let keymap =
5823 match kss with
5824 | [] -> keymap
5825 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
5826 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
5828 if closed
5829 then { v with f = pkeymap ret keymap }
5830 else
5831 let f () = v in
5832 { v with f = skip "map" f }
5834 | Vopen _ ->
5835 error "unexpected subelement in keymap" s spos
5837 | Vclose "keymap" ->
5838 { v with f = ret keymap }
5840 | Vclose _ -> error "unexpected close in keymap" s spos
5842 and pbookmarks path pan anchor c bookmarks v t spos _ =
5843 match t with
5844 | Vdata | Vcdata -> v
5845 | Vend -> error "unexpected end of input in bookmarks" s spos
5846 | Vopen ("item", attrs, closed) ->
5847 let titleent, spage, srely = bookmark_of attrs in
5848 let page = fromstring int_of_string spos "page" spage 0
5849 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5850 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5851 if closed
5852 then { v with f = pbookmarks path pan anchor c bookmarks }
5853 else
5854 let f () = v in
5855 { v with f = skip "item" f }
5857 | Vopen _ ->
5858 error "unexpected subelement in bookmarks" s spos
5860 | Vclose "bookmarks" ->
5861 { v with f = doc path pan anchor c bookmarks }
5863 | Vclose _ -> error "unexpected close in bookmarks" s spos
5865 and skip tag f v t spos _ =
5866 match t with
5867 | Vdata | Vcdata -> v
5868 | Vend ->
5869 error ("unexpected end of input in skipped " ^ tag) s spos
5870 | Vopen (tag', _, closed) ->
5871 if closed
5872 then v
5873 else
5874 let f' () = { v with f = skip tag f } in
5875 { v with f = skip tag' f' }
5876 | Vclose ctag ->
5877 if tag = ctag
5878 then f ()
5879 else error ("unexpected close in skipped " ^ tag) s spos
5882 parse { f = toplevel; accu = () } s;
5883 h, dc;
5886 let do_load f ic =
5888 let len = in_channel_length ic in
5889 let s = String.create len in
5890 really_input ic s 0 len;
5891 f s;
5892 with
5893 | Parse_error (msg, s, pos) ->
5894 let subs = subs s pos in
5895 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5896 failwith ("parse error: " ^ s)
5898 | exn ->
5899 failwith ("config load error: " ^ Printexc.to_string exn)
5902 let defconfpath =
5903 let dir =
5905 let dir = Filename.concat home ".config" in
5906 if Sys.is_directory dir then dir else home
5907 with _ -> home
5909 Filename.concat dir "llpp.conf"
5912 let confpath = ref defconfpath;;
5914 let load1 f =
5915 if Sys.file_exists !confpath
5916 then
5917 match
5918 (try Some (open_in_bin !confpath)
5919 with exn ->
5920 prerr_endline
5921 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5922 Printexc.to_string exn);
5923 None
5925 with
5926 | Some ic ->
5927 begin try
5928 f (do_load get ic)
5929 with exn ->
5930 prerr_endline
5931 ("Error loading configuation from `" ^ !confpath ^ "': " ^
5932 Printexc.to_string exn);
5933 end;
5934 close_in ic;
5936 | None -> ()
5937 else
5938 f (Hashtbl.create 0, defconf)
5941 let load () =
5942 let f (h, dc) =
5943 let pc, pb, px, pa =
5945 Hashtbl.find h (Filename.basename state.path)
5946 with Not_found -> dc, [], 0, (0, 0.0)
5948 setconf defconf dc;
5949 setconf conf pc;
5950 state.bookmarks <- pb;
5951 state.x <- px;
5952 state.scrollw <- conf.scrollbw;
5953 if conf.jumpback
5954 then state.anchor <- pa;
5955 cbput state.hists.nav pa;
5957 load1 f
5960 let add_attrs bb always dc c =
5961 let ob s a b =
5962 if always || a != b
5963 then Printf.bprintf bb "\n %s='%b'" s a
5964 and oi s a b =
5965 if always || a != b
5966 then Printf.bprintf bb "\n %s='%d'" s a
5967 and oI s a b =
5968 if always || a != b
5969 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
5970 and oz s a b =
5971 if always || a <> b
5972 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
5973 and oF s a b =
5974 if always || a <> b
5975 then Printf.bprintf bb "\n %s='%f'" s a
5976 and oc s a b =
5977 if always || a <> b
5978 then
5979 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
5980 and oC s a b =
5981 if always || a <> b
5982 then
5983 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
5984 and oR s a b =
5985 if always || a <> b
5986 then
5987 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
5988 and os s a b =
5989 if always || a <> b
5990 then
5991 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
5992 and og s a b =
5993 if always || a <> b
5994 then
5995 match a with
5996 | None -> ()
5997 | Some (_N, _A, _B) ->
5998 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
5999 and oW s a b =
6000 if always || a <> b
6001 then
6002 let v =
6003 match a with
6004 | None -> "false"
6005 | Some f ->
6006 if f = infinity
6007 then "true"
6008 else string_of_float f
6010 Printf.bprintf bb "\n %s='%s'" s v
6011 and oco s a b =
6012 if always || a <> b
6013 then
6014 match a with
6015 | Some ((n, a, b), _) when n > 1 ->
6016 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6017 | _ -> ()
6018 and obeco s a b =
6019 if always || a <> b
6020 then
6021 match a with
6022 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6023 | _ -> ()
6025 let w, h =
6026 if always
6027 then dc.winw, dc.winh
6028 else
6029 match state.fullscreen with
6030 | Some wh -> wh
6031 | None -> c.winw, c.winh
6033 let zoom, presentation, interpagespace, maxwait =
6034 if always
6035 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6036 else
6037 match state.mode with
6038 | Birdseye (bc, _, _, _, _) ->
6039 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6040 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6042 oi "width" w dc.winw;
6043 oi "height" h dc.winh;
6044 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6045 oi "scroll-handle-height" c.scrollh dc.scrollh;
6046 ob "case-insensitive-search" c.icase dc.icase;
6047 ob "preload" c.preload dc.preload;
6048 oi "page-bias" c.pagebias dc.pagebias;
6049 oi "scroll-step" c.scrollstep dc.scrollstep;
6050 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6051 ob "max-height-fit" c.maxhfit dc.maxhfit;
6052 ob "crop-hack" c.crophack dc.crophack;
6053 oW "throttle" maxwait dc.maxwait;
6054 ob "highlight-links" c.hlinks dc.hlinks;
6055 ob "under-cursor-info" c.underinfo dc.underinfo;
6056 oi "vertical-margin" interpagespace dc.interpagespace;
6057 oz "zoom" zoom dc.zoom;
6058 ob "presentation" presentation dc.presentation;
6059 oi "rotation-angle" c.angle dc.angle;
6060 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6061 ob "proportional-display" c.proportional dc.proportional;
6062 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6063 oi "tex-count" c.texcount dc.texcount;
6064 oi "slice-height" c.sliceheight dc.sliceheight;
6065 oi "thumbnail-width" c.thumbw dc.thumbw;
6066 ob "persistent-location" c.jumpback dc.jumpback;
6067 oc "background-color" c.bgcolor dc.bgcolor;
6068 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6069 oi "tile-width" c.tilew dc.tilew;
6070 oi "tile-height" c.tileh dc.tileh;
6071 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6072 ob "checkers" c.checkers dc.checkers;
6073 oi "aalevel" c.aalevel dc.aalevel;
6074 ob "trim-margins" c.trimmargins dc.trimmargins;
6075 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6076 os "uri-launcher" c.urilauncher dc.urilauncher;
6077 os "path-launcher" c.pathlauncher dc.pathlauncher;
6078 oC "color-space" c.colorspace dc.colorspace;
6079 ob "invert-colors" c.invert dc.invert;
6080 oF "brightness" c.colorscale dc.colorscale;
6081 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6082 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6083 oco "columns" c.columns dc.columns;
6084 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6085 os "selection-command" c.selcmd dc.selcmd;
6086 ob "update-cursor" c.updatecurs dc.updatecurs;
6089 let keymapsbuf always dc c =
6090 let bb = Buffer.create 16 in
6091 let rec loop = function
6092 | [] -> ()
6093 | (modename, h) :: rest ->
6094 let dh = findkeyhash dc modename in
6095 if always || h <> dh
6096 then (
6097 if Hashtbl.length h > 0
6098 then (
6099 if Buffer.length bb > 0
6100 then Buffer.add_char bb '\n';
6101 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6102 Hashtbl.iter (fun i o ->
6103 let isdifferent = always ||
6105 let dO = Hashtbl.find dh i in
6106 dO <> o
6107 with Not_found -> true
6109 if isdifferent
6110 then
6111 let addkm (k, m) =
6112 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6113 if Wsi.withalt m then Buffer.add_string bb "alt-";
6114 if Wsi.withshift m then Buffer.add_string bb "shift-";
6115 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6116 Buffer.add_string bb (Wsi.keyname k);
6118 let addkms l =
6119 let rec loop = function
6120 | [] -> ()
6121 | km :: [] -> addkm km
6122 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6124 loop l
6126 Buffer.add_string bb "<map in='";
6127 addkm i;
6128 match o with
6129 | KMinsrt km ->
6130 Buffer.add_string bb "' out='";
6131 addkm km;
6132 Buffer.add_string bb "'/>\n"
6134 | KMinsrl kms ->
6135 Buffer.add_string bb "' out='";
6136 addkms kms;
6137 Buffer.add_string bb "'/>\n"
6139 | KMmulti (ins, kms) ->
6140 Buffer.add_char bb ' ';
6141 addkms ins;
6142 Buffer.add_string bb "' out='";
6143 addkms kms;
6144 Buffer.add_string bb "'/>\n"
6145 ) h;
6146 Buffer.add_string bb "</keymap>";
6149 loop rest
6151 loop c.keyhashes;
6155 let save () =
6156 let uifontsize = fstate.fontsize in
6157 let bb = Buffer.create 32768 in
6158 let f (h, dc) =
6159 let dc = if conf.bedefault then conf else dc in
6160 Buffer.add_string bb "<llppconfig>\n";
6162 if String.length !fontpath > 0
6163 then
6164 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6165 uifontsize
6166 !fontpath
6167 else (
6168 if uifontsize <> 14
6169 then
6170 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6173 Buffer.add_string bb "<defaults ";
6174 add_attrs bb true dc dc;
6175 let kb = keymapsbuf true dc dc in
6176 if Buffer.length kb > 0
6177 then (
6178 Buffer.add_string bb ">\n";
6179 Buffer.add_buffer bb kb;
6180 Buffer.add_string bb "\n</defaults>\n";
6182 else Buffer.add_string bb "/>\n";
6184 let adddoc path pan anchor c bookmarks =
6185 if bookmarks == [] && c = dc && anchor = emptyanchor
6186 then ()
6187 else (
6188 Printf.bprintf bb "<doc path='%s'"
6189 (enent path 0 (String.length path));
6191 if anchor <> emptyanchor
6192 then (
6193 let n, y = anchor in
6194 Printf.bprintf bb " page='%d'" n;
6195 if y > 1e-6
6196 then
6197 Printf.bprintf bb " rely='%f'" y
6201 if pan != 0
6202 then Printf.bprintf bb " pan='%d'" pan;
6204 add_attrs bb false dc c;
6205 let kb = keymapsbuf false dc c in
6207 begin match bookmarks with
6208 | [] ->
6209 if Buffer.length kb > 0
6210 then (
6211 Buffer.add_string bb ">\n";
6212 Buffer.add_buffer bb kb;
6213 Buffer.add_string bb "</doc>\n";
6215 else Buffer.add_string bb "/>\n"
6216 | _ ->
6217 Buffer.add_string bb ">\n<bookmarks>\n";
6218 List.iter (fun (title, _level, (page, rely)) ->
6219 Printf.bprintf bb
6220 "<item title='%s' page='%d'"
6221 (enent title 0 (String.length title))
6222 page
6224 if rely > 1e-6
6225 then
6226 Printf.bprintf bb " rely='%f'" rely
6228 Buffer.add_string bb "/>\n";
6229 ) bookmarks;
6230 Buffer.add_string bb "</bookmarks>";
6231 if Buffer.length kb > 0
6232 then (
6233 Buffer.add_string bb "\n";
6234 Buffer.add_buffer bb kb;
6236 Buffer.add_string bb "\n</doc>\n";
6237 end;
6241 let pan, conf =
6242 match state.mode with
6243 | Birdseye (c, pan, _, _, _) ->
6244 let beyecolumns =
6245 match conf.columns with
6246 | Some ((c, _, _), _) -> Some c
6247 | None -> None
6248 and columns =
6249 match c.columns with
6250 | Some (c, _) -> Some (c, [||])
6251 | None -> None
6253 pan, { c with beyecolumns = beyecolumns; columns = columns }
6254 | _ -> state.x, conf
6256 let basename = Filename.basename state.path in
6257 adddoc basename pan (getanchor ())
6258 { conf with
6259 autoscrollstep =
6260 match state.autoscroll with
6261 | Some step -> step
6262 | None -> conf.autoscrollstep }
6263 (if conf.savebmarks then state.bookmarks else []);
6265 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6266 if basename <> path
6267 then adddoc path x y c bookmarks
6268 ) h;
6269 Buffer.add_string bb "</llppconfig>";
6271 load1 f;
6272 if Buffer.length bb > 0
6273 then
6275 let tmp = !confpath ^ ".tmp" in
6276 let oc = open_out_bin tmp in
6277 Buffer.output_buffer oc bb;
6278 close_out oc;
6279 Unix.rename tmp !confpath;
6280 with exn ->
6281 prerr_endline
6282 ("error while saving configuration: " ^ Printexc.to_string exn)
6284 end;;
6286 let () =
6287 Arg.parse
6288 (Arg.align
6289 [("-p", Arg.String (fun s -> state.password <- s) ,
6290 "<password> Set password");
6292 ("-f", Arg.String (fun s -> Config.fontpath := s),
6293 "<path> Set path to the user interface font");
6295 ("-c", Arg.String (fun s -> Config.confpath := s),
6296 "<path> Set path to the configuration file");
6298 ("-v", Arg.Unit (fun () ->
6299 Printf.printf
6300 "%s\nconfiguration path: %s\n"
6301 (version ())
6302 Config.defconfpath
6304 exit 0), " Print version and exit");
6307 (fun s -> state.path <- s)
6308 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6310 if String.length state.path = 0
6311 then (prerr_endline "file name missing"; exit 1);
6313 Config.load ();
6315 let globalkeyhash = findkeyhash conf "global" in
6316 let wsfd, winw, winh = Wsi.init (object
6317 method expose =
6318 if nogeomcmds state.geomcmds
6319 then display ()
6320 method display = display ()
6321 method reshape w h = reshape w h
6322 method mouse b d x y m = mouse b d x y m
6323 method motion x y = state.mpos <- (x, y); motion x y
6324 method pmotion x y = state.mpos <- (x, y); pmotion x y
6325 method key k m =
6326 let mascm = m land (
6327 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6328 ) in
6329 match state.keystate with
6330 | KSnone ->
6331 let km = k, mascm in
6332 begin
6333 match
6334 try Hashtbl.find globalkeyhash km
6335 with Not_found ->
6336 let modehash = state.uioh#modehash in
6337 try Hashtbl.find modehash km
6338 with Not_found -> KMinsrt (k, m)
6339 with
6340 | KMinsrt (k, m) -> keyboard k m
6341 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6342 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6344 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6345 List.iter (fun (k, m) -> keyboard k m) insrt;
6346 state.keystate <- KSnone
6347 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6348 state.keystate <- KSinto (keys, insrt)
6349 | _ ->
6350 state.keystate <- KSnone
6352 method enter x y = state.mpos <- (x, y); pmotion x y
6353 method leave = state.mpos <- (-1, -1)
6354 method quit = raise Quit
6355 end) conf.winw conf.winh in
6357 state.wsfd <- wsfd;
6359 if not (
6360 List.exists GlMisc.check_extension
6361 [ "GL_ARB_texture_rectangle"
6362 ; "GL_EXT_texture_recangle"
6363 ; "GL_NV_texture_rectangle" ]
6365 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6367 let cr, sw = Unix.pipe ()
6368 and sr, cw = Unix.pipe () in
6370 cloexec cr;
6371 cloexec sw;
6372 cloexec sr;
6373 cloexec cw;
6375 setcheckers conf.checkers;
6376 redirectstderr ();
6378 init (cr, cw) (
6379 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6380 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6381 !Config.fontpath
6383 state.sr <- sr;
6384 state.sw <- sw;
6385 state.text <- "Opening " ^ state.path;
6386 opendoc state.path state.password;
6387 state.uioh <- uioh;
6388 setfontsize fstate.fontsize;
6389 reshape winw winh;
6391 let rec loop deadline =
6392 let r =
6393 match state.errfd with
6394 | None -> [state.sr; state.wsfd]
6395 | Some fd -> [state.sr; state.wsfd; fd]
6397 if state.redisplay
6398 then (
6399 state.redisplay <- false;
6400 display ();
6402 let timeout =
6403 let now = now () in
6404 if deadline > now
6405 then (
6406 if deadline = infinity
6407 then ~-.1.0
6408 else max 0.0 (deadline -. now)
6410 else 0.0
6412 let r, _, _ =
6413 try Unix.select r [] [] timeout
6414 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6416 begin match r with
6417 | [] ->
6418 state.ghyll None;
6419 let newdeadline =
6420 if state.ghyll == noghyll
6421 then
6422 match state.autoscroll with
6423 | Some step when step != 0 ->
6424 let y = state.y + step in
6425 let y =
6426 if y < 0
6427 then state.maxy
6428 else if y >= state.maxy then 0 else y
6430 gotoy y;
6431 if state.mode = View
6432 then state.text <- "";
6433 deadline +. 0.01
6434 | _ -> infinity
6435 else deadline +. 0.01
6437 loop newdeadline
6439 | l ->
6440 let rec checkfds = function
6441 | [] -> ()
6442 | fd :: rest when fd = state.sr ->
6443 let cmd = readcmd state.sr in
6444 act cmd;
6445 checkfds rest
6447 | fd :: rest when fd = state.wsfd ->
6448 Wsi.readresp fd;
6449 checkfds rest
6451 | fd :: rest ->
6452 let s = String.create 80 in
6453 let n = Unix.read fd s 0 80 in
6454 if conf.redirectstderr
6455 then (
6456 Buffer.add_substring state.errmsgs s 0 n;
6457 state.newerrmsgs <- true;
6458 state.redisplay <- true;
6460 else (
6461 prerr_string (String.sub s 0 n);
6462 flush stderr;
6464 checkfds rest
6466 checkfds l;
6467 let newdeadline =
6468 let deadline1 =
6469 if deadline = infinity
6470 then now () +. 0.01
6471 else deadline
6473 match state.autoscroll with
6474 | Some step when step != 0 -> deadline1
6475 | _ -> if state.ghyll == noghyll then infinity else deadline1
6477 loop newdeadline
6478 end;
6481 loop infinity;
6482 with Quit ->
6483 wcmd "quit";
6484 Config.save ();
6485 exit 0;