Refactor
[llpp.git] / main.ml
blob7c97e4da9f6c5ebd4f7a01e2637a5d1d30f6165e
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
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 -> int -> int -> int -> int -> int =
87 "ml_postprocess";;
88 external pagebbox : opaque -> (int * int * int * int) = "ml_getpagebox";;
89 external platform : unit -> platform = "ml_platform";;
90 external setaalevel : int -> unit = "ml_setaalevel";;
91 external realloctexts : int -> bool = "ml_realloctexts";;
92 external cloexec : Unix.file_descr -> unit = "ml_cloexec";;
93 external findlink : opaque -> linkdir -> link = "ml_findlink";;
94 external getlink : opaque -> int -> under = "ml_getlink";;
95 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
96 external getlinkcount : opaque -> int = "ml_getlinkcount";;
97 external findpwl: int -> int -> pagewithlinks = "ml_find_page_with_links"
99 let platform_to_string = function
100 | Punknown -> "unknown"
101 | Plinux -> "Linux"
102 | Posx -> "OSX"
103 | Psun -> "Sun"
104 | Pfreebsd -> "FreeBSD"
105 | Pdragonflybsd -> "DragonflyBSD"
106 | Popenbsd -> "OpenBSD"
107 | Pnetbsd -> "NetBSD"
108 | Pcygwin -> "Cygwin"
111 let platform = platform ();;
113 type x = int
114 and y = int
115 and tilex = int
116 and tiley = int
117 and tileparams = (x * y * width * height * tilex * tiley)
120 external drawtile : tileparams -> opaque -> unit = "ml_drawtile";;
122 type mpos = int * int
123 and mstate =
124 | Msel of (mpos * mpos)
125 | Mpan of mpos
126 | Mscrolly | Mscrollx
127 | Mzoom of (int * int)
128 | Mzoomrect of (mpos * mpos)
129 | Mnone
132 type textentry = string * string * onhist option * onkey * ondone
133 and onkey = string -> int -> te
134 and ondone = string -> unit
135 and histcancel = unit -> unit
136 and onhist = ((histcmd -> string) * histcancel)
137 and histcmd = HCnext | HCprev | HCfirst | HClast
138 and te =
139 | TEstop
140 | TEdone of string
141 | TEcont of string
142 | TEswitch of textentry
145 type 'a circbuf =
146 { store : 'a array
147 ; mutable rc : int
148 ; mutable wc : int
149 ; mutable len : int
153 let bound v minv maxv =
154 max minv (min maxv v);
157 let cbnew n v =
158 { store = Array.create n v
159 ; rc = 0
160 ; wc = 0
161 ; len = 0
165 let drawstring size x y s =
166 Gl.enable `blend;
167 Gl.enable `texture_2d;
168 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
169 ignore (drawstr size x y s);
170 Gl.disable `blend;
171 Gl.disable `texture_2d;
174 let drawstring1 size x y s =
175 drawstr size x y s;
178 let drawstring2 size x y fmt =
179 Printf.kprintf (drawstring size (x+1) (y+size+1)) fmt
182 let cbcap b = Array.length b.store;;
184 let cbput b v =
185 let cap = cbcap b in
186 b.store.(b.wc) <- v;
187 b.wc <- (b.wc + 1) mod cap;
188 b.rc <- b.wc;
189 b.len <- min (b.len + 1) cap;
192 let cbempty b = b.len = 0;;
194 let cbgetg b circular dir =
195 if cbempty b
196 then b.store.(0)
197 else
198 let rc = b.rc + dir in
199 let rc =
200 if circular
201 then (
202 if rc = -1
203 then b.len-1
204 else (
205 if rc = b.len
206 then 0
207 else rc
210 else max 0 (min rc (b.len-1))
212 b.rc <- rc;
213 b.store.(rc);
216 let cbget b = cbgetg b false;;
217 let cbgetc b = cbgetg b true;;
219 type page =
220 { pageno : int
221 ; pagedimno : int
222 ; pagew : int
223 ; pageh : int
224 ; pagex : int
225 ; pagey : int
226 ; pagevw : int
227 ; pagevh : int
228 ; pagedispx : int
229 ; pagedispy : int
233 let debugl l =
234 dolog "l %d dim=%d {" l.pageno l.pagedimno;
235 dolog " WxH %dx%d" l.pagew l.pageh;
236 dolog " vWxH %dx%d" l.pagevw l.pagevh;
237 dolog " pagex,y %d,%d" l.pagex l.pagey;
238 dolog " dispx,y %d,%d" l.pagedispx l.pagedispy;
239 dolog "}";
242 let debugrect (x0, y0, x1, y1, x2, y2, x3, y3) =
243 dolog "rect {";
244 dolog " x0,y0=(% f, % f)" x0 y0;
245 dolog " x1,y1=(% f, % f)" x1 y1;
246 dolog " x2,y2=(% f, % f)" x2 y2;
247 dolog " x3,y3=(% f, % f)" x3 y3;
248 dolog "}";
251 type columns =
252 multicol * ((pdimno * x * y * (pageno * width * height * leftx)) array)
253 and multicol = columncount * covercount * covercount
254 and pdimno = int
255 and columncount = int
256 and covercount = int;;
258 type conf =
259 { mutable scrollbw : int
260 ; mutable scrollh : int
261 ; mutable icase : bool
262 ; mutable preload : bool
263 ; mutable pagebias : int
264 ; mutable verbose : bool
265 ; mutable debug : bool
266 ; mutable scrollstep : int
267 ; mutable maxhfit : bool
268 ; mutable crophack : bool
269 ; mutable autoscrollstep : int
270 ; mutable maxwait : float option
271 ; mutable hlinks : bool
272 ; mutable underinfo : bool
273 ; mutable interpagespace : interpagespace
274 ; mutable zoom : float
275 ; mutable presentation : bool
276 ; mutable angle : angle
277 ; mutable winw : int
278 ; mutable winh : int
279 ; mutable savebmarks : bool
280 ; mutable proportional : proportional
281 ; mutable trimmargins : trimmargins
282 ; mutable trimfuzz : irect
283 ; mutable memlimit : memsize
284 ; mutable texcount : texcount
285 ; mutable sliceheight : sliceheight
286 ; mutable thumbw : width
287 ; mutable jumpback : bool
288 ; mutable bgcolor : float * float * float
289 ; mutable bedefault : bool
290 ; mutable scrollbarinpm : bool
291 ; mutable tilew : int
292 ; mutable tileh : int
293 ; mutable mustoresize : memsize
294 ; mutable checkers : bool
295 ; mutable aalevel : int
296 ; mutable urilauncher : string
297 ; mutable pathlauncher : string
298 ; mutable colorspace : colorspace
299 ; mutable invert : bool
300 ; mutable colorscale : float
301 ; mutable redirectstderr : bool
302 ; mutable ghyllscroll : (int * int * int) option
303 ; mutable columns : columns option
304 ; mutable beyecolumns : columncount option
305 ; mutable selcmd : string
306 ; mutable updatecurs : bool
307 ; mutable keyhashes : (string * keyhash) list
311 type anchor = pageno * top;;
313 type outline = string * int * anchor;;
315 type rect = float * float * float * float * float * float * float * float;;
317 type tile = opaque * pixmapsize * elapsed
318 and elapsed = float;;
319 type pagemapkey = pageno * gen;;
320 type tilemapkey = pageno * gen * colorspace * angle * width * height * col * row
321 and row = int
322 and col = int;;
324 let emptyanchor = (0, 0.0);;
326 type infochange = | Memused | Docinfo | Pdim;;
328 class type uioh = object
329 method display : unit
330 method key : int -> int -> uioh
331 method button : int -> bool -> int -> int -> int -> uioh
332 method motion : int -> int -> uioh
333 method pmotion : int -> int -> uioh
334 method infochanged : infochange -> unit
335 method scrollpw : (int * float * float)
336 method scrollph : (int * float * float)
337 method modehash : keyhash
338 end;;
340 type mode =
341 | Birdseye of (conf * leftx * pageno * pageno * anchor)
342 | Textentry of (textentry * onleave)
343 | View
344 | LinkNav of linktarget
345 and onleave = leavetextentrystatus -> unit
346 and leavetextentrystatus = | Cancel | Confirm
347 and helpitem = string * int * action
348 and action =
349 | Noaction
350 | Action of (uioh -> uioh)
351 and linktarget =
352 | Ltexact of (pageno * int)
353 | Ltgendir of int
356 let isbirdseye = function Birdseye _ -> true | _ -> false;;
357 let istextentry = function Textentry _ -> true | _ -> false;;
359 type currently =
360 | Idle
361 | Loading of (page * gen)
362 | Tiling of (
363 page * opaque * colorspace * angle * gen * col * row * width * height
365 | Outlining of outline list
368 let emptykeyhash = Hashtbl.create 0;;
369 let nouioh : uioh = object (self)
370 method display = ()
371 method key _ _ = self
372 method button _ _ _ _ _ = self
373 method motion _ _ = self
374 method pmotion _ _ = self
375 method infochanged _ = ()
376 method scrollpw = (0, nan, nan)
377 method scrollph = (0, nan, nan)
378 method modehash = emptykeyhash
379 end;;
381 type state =
382 { mutable sr : Unix.file_descr
383 ; mutable sw : Unix.file_descr
384 ; mutable wsfd : Unix.file_descr
385 ; mutable errfd : Unix.file_descr option
386 ; mutable stderr : Unix.file_descr
387 ; mutable errmsgs : Buffer.t
388 ; mutable newerrmsgs : bool
389 ; mutable w : int
390 ; mutable x : int
391 ; mutable y : int
392 ; mutable scrollw : int
393 ; mutable hscrollh : int
394 ; mutable anchor : anchor
395 ; mutable ranchors : (string * string * anchor) list
396 ; mutable maxy : int
397 ; mutable layout : page list
398 ; pagemap : (pagemapkey, opaque) Hashtbl.t
399 ; tilemap : (tilemapkey, tile) Hashtbl.t
400 ; tilelru : (tilemapkey * opaque * pixmapsize) Queue.t
401 ; mutable pdims : (pageno * width * height * leftx) list
402 ; mutable pagecount : int
403 ; mutable currently : currently
404 ; mutable mstate : mstate
405 ; mutable searchpattern : string
406 ; mutable rects : (pageno * recttype * rect) list
407 ; mutable rects1 : (pageno * recttype * rect) list
408 ; mutable text : string
409 ; mutable fullscreen : (width * height) option
410 ; mutable mode : mode
411 ; mutable uioh : uioh
412 ; mutable outlines : outline array
413 ; mutable bookmarks : outline list
414 ; mutable path : string
415 ; mutable password : string
416 ; mutable geomcmds : (string * ((string * (unit -> unit)) list))
417 ; mutable memused : memsize
418 ; mutable gen : gen
419 ; mutable throttle : (page list * int * float) option
420 ; mutable autoscroll : int option
421 ; mutable ghyll : (int option -> unit)
422 ; mutable help : helpitem array
423 ; mutable docinfo : (int * string) list
424 ; mutable texid : GlTex.texture_id option
425 ; hists : hists
426 ; mutable prevzoom : float
427 ; mutable progress : float
428 ; mutable redisplay : bool
429 ; mutable mpos : mpos
430 ; mutable keystate : keystate
431 ; mutable glinks : bool
433 and hists =
434 { pat : string circbuf
435 ; pag : string circbuf
436 ; nav : anchor circbuf
437 ; sel : string circbuf
441 let defconf =
442 { scrollbw = 7
443 ; scrollh = 12
444 ; icase = true
445 ; preload = true
446 ; pagebias = 0
447 ; verbose = false
448 ; debug = false
449 ; scrollstep = 24
450 ; maxhfit = true
451 ; crophack = false
452 ; autoscrollstep = 2
453 ; maxwait = None
454 ; hlinks = false
455 ; underinfo = false
456 ; interpagespace = 2
457 ; zoom = 1.0
458 ; presentation = false
459 ; angle = 0
460 ; winw = 900
461 ; winh = 900
462 ; savebmarks = true
463 ; proportional = true
464 ; trimmargins = false
465 ; trimfuzz = (0,0,0,0)
466 ; memlimit = 32 lsl 20
467 ; texcount = 256
468 ; sliceheight = 24
469 ; thumbw = 76
470 ; jumpback = true
471 ; bgcolor = (0.5, 0.5, 0.5)
472 ; bedefault = false
473 ; scrollbarinpm = true
474 ; tilew = 2048
475 ; tileh = 2048
476 ; mustoresize = 128 lsl 20
477 ; checkers = true
478 ; aalevel = 8
479 ; urilauncher =
480 (match platform with
481 | Plinux | Pfreebsd | Pdragonflybsd
482 | Popenbsd | Pnetbsd | Psun -> "xdg-open \"%s\""
483 | Posx -> "open \"%s\""
484 | Pcygwin -> "cygstart %s"
485 | Punknown -> "echo %s")
486 ; pathlauncher = "lp \"%s\""
487 ; selcmd =
488 (match platform with
489 | Plinux | Pfreebsd | Pdragonflybsd
490 | Popenbsd | Pnetbsd | Psun -> "xsel -i"
491 | Posx -> "pbcopy"
492 | Pcygwin -> "wsel"
493 | Punknown -> "cat")
494 ; colorspace = Rgb
495 ; invert = false
496 ; colorscale = 1.0
497 ; redirectstderr = false
498 ; ghyllscroll = None
499 ; columns = None
500 ; beyecolumns = None
501 ; updatecurs = false
502 ; keyhashes =
503 let mk n = (n, Hashtbl.create 1) in
504 [ mk "global"
505 ; mk "info"
506 ; mk "help"
507 ; mk "outline"
508 ; mk "listview"
509 ; mk "birdseye"
510 ; mk "textentry"
511 ; mk "links"
516 let findkeyhash c name =
517 try List.assoc name c.keyhashes
518 with Not_found -> failwith ("invalid mode name `" ^ name ^ "'")
521 let conf = { defconf with angle = defconf.angle };;
523 type fontstate =
524 { mutable fontsize : int
525 ; mutable wwidth : float
526 ; mutable maxrows : int
530 let fstate =
531 { fontsize = 14
532 ; wwidth = nan
533 ; maxrows = -1
537 let setfontsize n =
538 fstate.fontsize <- n;
539 fstate.wwidth <- measurestr fstate.fontsize "w";
540 fstate.maxrows <- (conf.winh - fstate.fontsize - 1) / (fstate.fontsize + 1);
543 let geturl s =
544 let colonpos = try String.index s ':' with Not_found -> -1 in
545 let len = String.length s in
546 if colonpos >= 0 && colonpos + 3 < len
547 then (
548 if s.[colonpos+1] = '/' && s.[colonpos+2] = '/'
549 then
550 let schemestartpos =
551 try String.rindex_from s colonpos ' '
552 with Not_found -> -1
554 let scheme =
555 String.sub s (schemestartpos+1) (colonpos-1-schemestartpos)
557 match scheme with
558 | "http" | "ftp" | "mailto" ->
559 let epos =
560 try String.index_from s colonpos ' '
561 with Not_found -> len
563 String.sub s (schemestartpos+1) (epos-1-schemestartpos)
564 | _ -> ""
565 else ""
567 else ""
570 let popen =
571 let shell, farg = "/bin/sh", "-c" in
572 fun s ->
573 let args = [|shell; farg; s|] in
574 ignore (Unix.create_process shell args Unix.stdin Unix.stdout Unix.stderr)
577 let gotouri uri =
578 if String.length conf.urilauncher = 0
579 then print_endline uri
580 else (
581 let url = geturl uri in
582 if String.length url = 0
583 then print_endline uri
584 else
585 let re = Str.regexp "%s" in
586 let command = Str.global_replace re url conf.urilauncher in
587 try popen command
588 with exn ->
589 Printf.eprintf
590 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
591 flush stderr;
595 let version () =
596 Printf.sprintf "llpp version %s (%s/%dbit, ocaml %s)" Help.version
597 (platform_to_string platform) Sys.word_size Sys.ocaml_version
600 let makehelp () =
601 let strings = version () :: "" :: Help.keys in
602 Array.of_list (
603 List.map (fun s ->
604 let url = geturl s in
605 if String.length url > 0
606 then (s, 0, Action (fun u -> gotouri url; u))
607 else (s, 0, Noaction)
608 ) strings);
611 let noghyll _ = ();;
612 let firstgeomcmds = "", [];;
614 let state =
615 { sr = Unix.stdin
616 ; sw = Unix.stdin
617 ; wsfd = Unix.stdin
618 ; errfd = None
619 ; stderr = Unix.stderr
620 ; errmsgs = Buffer.create 0
621 ; newerrmsgs = false
622 ; x = 0
623 ; y = 0
624 ; w = 0
625 ; scrollw = 0
626 ; hscrollh = 0
627 ; anchor = emptyanchor
628 ; ranchors = []
629 ; layout = []
630 ; maxy = max_int
631 ; tilelru = Queue.create ()
632 ; pagemap = Hashtbl.create 10
633 ; tilemap = Hashtbl.create 10
634 ; pdims = []
635 ; pagecount = 0
636 ; currently = Idle
637 ; mstate = Mnone
638 ; rects = []
639 ; rects1 = []
640 ; text = ""
641 ; mode = View
642 ; fullscreen = None
643 ; searchpattern = ""
644 ; outlines = [||]
645 ; bookmarks = []
646 ; path = ""
647 ; password = ""
648 ; geomcmds = firstgeomcmds
649 ; hists =
650 { nav = cbnew 10 (0, 0.0)
651 ; pat = cbnew 10 ""
652 ; pag = cbnew 10 ""
653 ; sel = cbnew 10 ""
655 ; memused = 0
656 ; gen = 0
657 ; throttle = None
658 ; autoscroll = None
659 ; ghyll = noghyll
660 ; help = makehelp ()
661 ; docinfo = []
662 ; texid = None
663 ; prevzoom = 1.0
664 ; progress = -1.0
665 ; uioh = nouioh
666 ; redisplay = true
667 ; mpos = (-1, -1)
668 ; keystate = KSnone
669 ; glinks = false
673 let vlog fmt =
674 if conf.verbose
675 then
676 Printf.kprintf prerr_endline fmt
677 else
678 Printf.kprintf ignore fmt
681 let launchpath () =
682 if String.length conf.pathlauncher = 0
683 then print_endline state.path
684 else (
685 let re = Str.regexp "%s" in
686 let command = Str.global_replace re state.path conf.pathlauncher in
687 try popen command
688 with exn ->
689 Printf.eprintf
690 "failed to execute `%s': %s\n" command (Printexc.to_string exn);
691 flush stderr;
695 let redirectstderr () =
696 if conf.redirectstderr
697 then
698 let rfd, wfd = Unix.pipe () in
699 state.stderr <- Unix.dup Unix.stderr;
700 state.errfd <- Some rfd;
701 Unix.dup2 wfd Unix.stderr;
702 else (
703 state.newerrmsgs <- false;
704 begin match state.errfd with
705 | Some fd ->
706 Unix.close fd;
707 Unix.dup2 state.stderr Unix.stderr;
708 state.errfd <- None;
709 | None -> ()
710 end;
711 prerr_string (Buffer.contents state.errmsgs);
712 flush stderr;
713 Buffer.clear state.errmsgs;
717 module G =
718 struct
719 let postRedisplay who =
720 if conf.verbose
721 then prerr_endline ("redisplay for " ^ who);
722 state.redisplay <- true;
724 end;;
726 let getopaque pageno =
727 try Some (Hashtbl.find state.pagemap (pageno, state.gen))
728 with Not_found -> None
731 let putopaque pageno opaque =
732 Hashtbl.replace state.pagemap (pageno, state.gen) opaque
735 let pagetranslatepoint l x y =
736 let dy = y - l.pagedispy in
737 let y = dy + l.pagey in
738 let dx = x - l.pagedispx in
739 let x = dx + l.pagex in
740 (x, y);
743 let getunder x y =
744 let rec f = function
745 | l :: rest ->
746 begin match getopaque l.pageno with
747 | Some opaque ->
748 let x0 = l.pagedispx in
749 let x1 = x0 + l.pagevw in
750 let y0 = l.pagedispy in
751 let y1 = y0 + l.pagevh in
752 if y >= y0 && y <= y1 && x >= x0 && x <= x1
753 then
754 let px, py = pagetranslatepoint l x y in
755 match whatsunder opaque px py with
756 | Unone -> f rest
757 | under -> under
758 else f rest
759 | _ ->
760 f rest
762 | [] -> Unone
764 f state.layout
767 let showtext c s =
768 state.text <- Printf.sprintf "%c%s" c s;
769 G.postRedisplay "showtext";
772 let updateunder x y =
773 match getunder x y with
774 | Unone -> Wsi.setcursor Wsi.CURSOR_INHERIT
775 | Ulinkuri uri ->
776 if conf.underinfo then showtext 'u' ("ri: " ^ uri);
777 Wsi.setcursor Wsi.CURSOR_INFO
778 | Ulinkgoto (page, _) ->
779 if conf.underinfo
780 then showtext 'p' ("age: " ^ string_of_int (page+1));
781 Wsi.setcursor Wsi.CURSOR_INFO
782 | Utext s ->
783 if conf.underinfo then showtext 'f' ("ont: " ^ s);
784 Wsi.setcursor Wsi.CURSOR_TEXT
785 | Uunexpected s ->
786 if conf.underinfo then showtext 'u' ("nexpected: " ^ s);
787 Wsi.setcursor Wsi.CURSOR_INHERIT
788 | Ulaunch s ->
789 if conf.underinfo then showtext 'l' ("aunch: " ^ s);
790 Wsi.setcursor Wsi.CURSOR_INHERIT
791 | Unamed s ->
792 if conf.underinfo then showtext 'n' ("amed: " ^ s);
793 Wsi.setcursor Wsi.CURSOR_INHERIT
794 | Uremote (filename, pageno) ->
795 if conf.underinfo then showtext 'r'
796 (Printf.sprintf "emote: %s (%d)" filename pageno);
797 Wsi.setcursor Wsi.CURSOR_INFO
800 let showlinktype under =
801 if conf.underinfo
802 then
803 match under with
804 | Unone -> ()
805 | Ulinkuri uri ->
806 showtext 'u' ("ri: " ^ uri)
807 | Ulinkgoto (page, _) ->
808 showtext 'p' ("age: " ^ string_of_int (page+1));
809 | Utext s ->
810 showtext 'f' ("ont: " ^ s);
811 | Uunexpected s ->
812 showtext 'u' ("nexpected: " ^ s);
813 | Ulaunch s ->
814 showtext 'l' ("aunch: " ^ s);
815 | Unamed s ->
816 showtext 'n' ("amed: " ^ s);
817 | Uremote (filename, pageno) ->
818 showtext 'r' (Printf.sprintf "emote: %s (%d)" filename pageno);
821 let addchar s c =
822 let b = Buffer.create (String.length s + 1) in
823 Buffer.add_string b s;
824 Buffer.add_char b c;
825 Buffer.contents b;
828 let colorspace_of_string s =
829 match String.lowercase s with
830 | "rgb" -> Rgb
831 | "bgr" -> Bgr
832 | "gray" -> Gray
833 | _ -> failwith "invalid colorspace"
836 let int_of_colorspace = function
837 | Rgb -> 0
838 | Bgr -> 1
839 | Gray -> 2
842 let colorspace_of_int = function
843 | 0 -> Rgb
844 | 1 -> Bgr
845 | 2 -> Gray
846 | n -> failwith ("invalid colorspace index " ^ string_of_int n)
849 let colorspace_to_string = function
850 | Rgb -> "rgb"
851 | Bgr -> "bgr"
852 | Gray -> "gray"
855 let intentry_with_suffix text key =
856 let c =
857 if key >= 32 && key < 127
858 then Char.chr key
859 else '\000'
861 match Char.lowercase c with
862 | '0' .. '9' ->
863 let text = addchar text c in
864 TEcont text
866 | 'k' | 'm' | 'g' ->
867 let text = addchar text c in
868 TEcont text
870 | _ ->
871 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
872 TEcont text
875 let columns_to_string (n, a, b) =
876 if a = 0 && b = 0
877 then Printf.sprintf "%d" n
878 else Printf.sprintf "%d,%d,%d" n a b;
881 let columns_of_string s =
883 (int_of_string s, 0, 0)
884 with _ ->
885 Scanf.sscanf s "%u,%u,%u" (fun n a b -> (n, a, b));
888 let readcmd fd =
889 let s = "xxxx" in
890 let n = Unix.read fd s 0 4 in
891 if n != 4 then failwith "incomplete read(len)";
892 let len = 0
893 lor (Char.code s.[0] lsl 24)
894 lor (Char.code s.[1] lsl 16)
895 lor (Char.code s.[2] lsl 8)
896 lor (Char.code s.[3] lsl 0)
898 let s = String.create len in
899 let n = Unix.read fd s 0 len in
900 if n != len then failwith "incomplete read(data)";
904 let btod b = if b then 1 else 0;;
906 let wcmd fmt =
907 let b = Buffer.create 16 in
908 Buffer.add_string b "llll";
909 Printf.kbprintf
910 (fun b ->
911 let s = Buffer.contents b in
912 let n = String.length s in
913 let len = n - 4 in
914 (* dolog "wcmd %S" (String.sub s 4 len); *)
915 s.[0] <- Char.chr ((len lsr 24) land 0xff);
916 s.[1] <- Char.chr ((len lsr 16) land 0xff);
917 s.[2] <- Char.chr ((len lsr 8) land 0xff);
918 s.[3] <- Char.chr (len land 0xff);
919 let n' = Unix.write state.sw s 0 n in
920 if n' != n then failwith "write failed";
921 ) b fmt;
924 let calcips h =
925 if conf.presentation
926 then
927 let d = conf.winh - h in
928 max 0 ((d + 1) / 2)
929 else
930 conf.interpagespace
933 let calcheight () =
934 let rec f pn ph pi fh l =
935 match l with
936 | (n, _, h, _) :: rest ->
937 let ips = calcips h in
938 let fh =
939 if conf.presentation
940 then fh+ips
941 else (
942 if isbirdseye state.mode && pn = 0
943 then fh + ips
944 else fh
947 let fh = fh + ((n - pn) * (ph + pi)) in
948 f n h ips fh rest;
950 | [] ->
951 let inc =
952 if conf.presentation || (isbirdseye state.mode && pn = 0)
953 then 0
954 else -pi
956 let fh = fh + ((state.pagecount - pn) * (ph + pi)) + inc in
957 max 0 fh
959 let fh = f 0 0 0 0 state.pdims in
963 let calcheight () =
964 match conf.columns with
965 | None -> calcheight ()
966 | Some (_, b) ->
967 if Array.length b > 0
968 then
969 let (_, _, y, (_, _, h, _)) = b.(Array.length b - 1) in
970 y + h
971 else 0
974 let getpageyh pageno =
975 let rec f pn ph pi y l =
976 match l with
977 | (n, _, h, _) :: rest ->
978 let ips = calcips h in
979 if n >= pageno
980 then
981 let h = if n = pageno then h else ph in
982 if conf.presentation && n = pageno
983 then
984 y + (pageno - pn) * (ph + pi) + pi, h
985 else
986 y + (pageno - pn) * (ph + pi), h
987 else
988 let y = y + (if conf.presentation then pi else 0) in
989 let y = y + (n - pn) * (ph + pi) in
990 f n h ips y rest
992 | [] ->
993 y + (pageno - pn) * (ph + pi), ph
995 f 0 0 0 0 state.pdims
998 let getpageyh pageno =
999 match conf.columns with
1000 | None -> getpageyh pageno
1001 | Some (_, b) ->
1002 let (_, _, y, (_, _, h, _)) = b.(pageno) in
1003 y, h
1006 let getpagedim pageno =
1007 let rec f ppdim l =
1008 match l with
1009 | (n, _, _, _) as pdim :: rest ->
1010 if n >= pageno
1011 then (if n = pageno then pdim else ppdim)
1012 else f pdim rest
1014 | [] -> ppdim
1016 f (-1, -1, -1, -1) state.pdims
1019 let getpagey pageno = fst (getpageyh pageno);;
1021 let nogeomcmds cmds =
1022 match cmds with
1023 | s, [] -> String.length s = 0
1024 | _ -> false
1027 let layout1 y sh =
1028 let sh = sh - state.hscrollh in
1029 let rec f ~pageno ~pdimno ~prev ~py ~dy ~pdims ~accu =
1030 let ((w, h, ips, xoff) as curr), rest, pdimno, yinc =
1031 match pdims with
1032 | (pageno', w, h, xoff) :: rest when pageno' = pageno ->
1033 let ips = calcips h in
1034 let yinc =
1035 if conf.presentation || (isbirdseye state.mode && pageno = 0)
1036 then ips
1037 else 0
1039 (w, h, ips, xoff), rest, pdimno + 1, yinc
1040 | _ ->
1041 prev, pdims, pdimno, 0
1043 let dy = dy + yinc in
1044 let py = py + yinc in
1045 if pageno = state.pagecount || dy >= sh
1046 then
1047 accu
1048 else
1049 let vy = y + dy in
1050 if py + h <= vy - yinc
1051 then
1052 let py = py + h + ips in
1053 let dy = max 0 (py - y) in
1054 f ~pageno:(pageno+1)
1055 ~pdimno
1056 ~prev:curr
1059 ~pdims:rest
1060 ~accu
1061 else
1062 let pagey = vy - py in
1063 let pagevh = h - pagey in
1064 let pagevh = min (sh - dy) pagevh in
1065 let off = if yinc > 0 then py - vy else 0 in
1066 let py = py + h + ips in
1067 let pagex, dx =
1068 let xoff = xoff +
1069 if state.w < conf.winw - state.scrollw
1070 then (conf.winw - state.scrollw - state.w) / 2
1071 else 0
1073 let dispx = xoff + state.x in
1074 if dispx < 0
1075 then (-dispx, 0)
1076 else (0, dispx)
1078 let pagevw =
1079 let lw = w - pagex in
1080 min lw (conf.winw - state.scrollw)
1082 let e =
1083 { pageno = pageno
1084 ; pagedimno = pdimno
1085 ; pagew = w
1086 ; pageh = h
1087 ; pagex = pagex
1088 ; pagey = pagey + off
1089 ; pagevw = pagevw
1090 ; pagevh = pagevh - off
1091 ; pagedispx = dx
1092 ; pagedispy = dy + off
1095 let accu = e :: accu in
1096 f ~pageno:(pageno+1)
1097 ~pdimno
1098 ~prev:curr
1100 ~dy:(dy+pagevh+ips)
1101 ~pdims:rest
1102 ~accu
1104 let accu =
1106 ~pageno:0
1107 ~pdimno:~-1
1108 ~prev:(0,0,0,0)
1109 ~py:0
1110 ~dy:0
1111 ~pdims:state.pdims
1112 ~accu:[]
1114 List.rev accu
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 List.rev (fold [] 0)
1172 let layout y sh =
1173 if nogeomcmds state.geomcmds
1174 then
1175 match conf.columns with
1176 | None -> layout1 y sh
1177 | Some c -> layoutN c y sh
1178 else []
1181 let clamp incr =
1182 let y = state.y + incr in
1183 let y = max 0 y in
1184 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1188 let itertiles l f =
1189 let tilex = l.pagex mod conf.tilew in
1190 let tiley = l.pagey mod conf.tileh in
1192 let col = l.pagex / conf.tilew in
1193 let row = l.pagey / conf.tileh in
1195 let vw =
1196 let a = l.pagew - l.pagex in
1197 let b = conf.winw - state.scrollw in
1198 min a b
1199 and vh = l.pagevh in
1201 let rec rowloop row y0 dispy h =
1202 if h = 0
1203 then ()
1204 else (
1205 let dh = conf.tileh - y0 in
1206 let dh = min h dh in
1207 let rec colloop col x0 dispx w =
1208 if w = 0
1209 then ()
1210 else (
1211 let dw = conf.tilew - x0 in
1212 let dw = min w dw in
1214 f col row dispx dispy x0 y0 dw dh;
1215 colloop (col+1) 0 (dispx+dw) (w-dw)
1218 colloop col tilex l.pagedispx vw;
1219 rowloop (row+1) 0 (dispy+dh) (h-dh)
1222 if vw > 0 && vh > 0
1223 then rowloop row tiley l.pagedispy vh;
1226 let gettileopaque l col row =
1227 let key =
1228 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1230 try Some (Hashtbl.find state.tilemap key)
1231 with Not_found -> None
1234 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1235 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1236 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1239 let drawtiles l color =
1240 GlDraw.color color;
1241 let f col row x y tilex tiley w h =
1242 match gettileopaque l col row with
1243 | Some (opaque, _, t) ->
1244 let params = x, y, w, h, tilex, tiley in
1245 if conf.invert
1246 then (
1247 Gl.enable `blend;
1248 GlFunc.blend_func `zero `one_minus_src_color;
1250 drawtile params opaque;
1251 if conf.invert
1252 then Gl.disable `blend;
1253 if conf.debug
1254 then (
1255 let s = Printf.sprintf
1256 "%d[%d,%d] %f sec"
1257 l.pageno col row t
1259 let w = measurestr fstate.fontsize s in
1260 GlMisc.push_attrib [`current];
1261 GlDraw.color (0.0, 0.0, 0.0);
1262 GlDraw.rect
1263 (float (x-2), float (y-2))
1264 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1265 GlDraw.color (1.0, 1.0, 1.0);
1266 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1267 GlMisc.pop_attrib ();
1270 | _ ->
1271 let w =
1272 let lw = conf.winw - state.scrollw - x in
1273 min lw w
1274 and h =
1275 let lh = conf.winh - y in
1276 min lh h
1278 Gl.enable `texture_2d;
1279 begin match state.texid with
1280 | Some id ->
1281 GlTex.bind_texture `texture_2d id;
1282 let x0 = float x
1283 and y0 = float y
1284 and x1 = float (x+w)
1285 and y1 = float (y+h) in
1287 let tw = float w /. 64.0
1288 and th = float h /. 64.0 in
1289 let tx0 = float tilex /. 64.0
1290 and ty0 = float tiley /. 64.0 in
1291 let tx1 = tx0 +. tw
1292 and ty1 = ty0 +. th in
1293 GlDraw.begins `quads;
1294 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1295 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1296 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1297 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1298 GlDraw.ends ();
1300 Gl.disable `texture_2d;
1301 | None ->
1302 GlDraw.color (1.0, 1.0, 1.0);
1303 GlDraw.rect
1304 (float x, float y)
1305 (float (x+w), float (y+h));
1306 end;
1307 if w > 128 && h > fstate.fontsize + 10
1308 then (
1309 GlDraw.color (0.0, 0.0, 0.0);
1310 let c, r =
1311 if conf.verbose
1312 then (col*conf.tilew, row*conf.tileh)
1313 else col, row
1315 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1317 GlDraw.color color;
1319 itertiles l f
1322 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1324 let tilevisible1 l x y =
1325 let ax0 = l.pagex
1326 and ax1 = l.pagex + l.pagevw
1327 and ay0 = l.pagey
1328 and ay1 = l.pagey + l.pagevh in
1330 let bx0 = x
1331 and by0 = y in
1332 let bx1 = min (bx0 + conf.tilew) l.pagew
1333 and by1 = min (by0 + conf.tileh) l.pageh in
1335 let rx0 = max ax0 bx0
1336 and ry0 = max ay0 by0
1337 and rx1 = min ax1 bx1
1338 and ry1 = min ay1 by1 in
1340 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1341 nonemptyintersection
1344 let tilevisible layout n x y =
1345 let rec findpageinlayout = function
1346 | l :: _ when l.pageno = n -> tilevisible1 l x y
1347 | _ :: rest -> findpageinlayout rest
1348 | [] -> false
1350 findpageinlayout layout
1353 let tileready l x y =
1354 tilevisible1 l x y &&
1355 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1358 let tilepage n p layout =
1359 let rec loop = function
1360 | l :: rest ->
1361 if l.pageno = n
1362 then
1363 let f col row _ _ _ _ _ _ =
1364 if state.currently = Idle
1365 then
1366 match gettileopaque l col row with
1367 | Some _ -> ()
1368 | None ->
1369 let x = col*conf.tilew
1370 and y = row*conf.tileh in
1371 let w =
1372 let w = l.pagew - x in
1373 min w conf.tilew
1375 let h =
1376 let h = l.pageh - y in
1377 min h conf.tileh
1379 wcmd "tile %s %d %d %d %d" p x y w h;
1380 state.currently <-
1381 Tiling (
1382 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1383 conf.tilew, conf.tileh
1386 itertiles l f;
1387 else
1388 loop rest
1390 | [] -> ()
1392 if nogeomcmds state.geomcmds
1393 then loop layout;
1396 let preloadlayout visiblepages =
1397 let presentation = conf.presentation in
1398 let interpagespace = conf.interpagespace in
1399 let maxy = state.maxy in
1400 conf.presentation <- false;
1401 conf.interpagespace <- 0;
1402 state.maxy <- calcheight ();
1403 let y =
1404 match visiblepages with
1405 | [] -> 0
1406 | l :: _ -> getpagey l.pageno + l.pagey
1408 let y = if y < conf.winh then 0 else y - conf.winh in
1409 let h = state.y - y + conf.winh*3 in
1410 let pages = layout y h in
1411 conf.presentation <- presentation;
1412 conf.interpagespace <- interpagespace;
1413 state.maxy <- maxy;
1414 pages;
1417 let load pages =
1418 let rec loop pages =
1419 if state.currently != Idle
1420 then ()
1421 else
1422 match pages with
1423 | l :: rest ->
1424 begin match getopaque l.pageno with
1425 | None ->
1426 wcmd "page %d %d" l.pageno l.pagedimno;
1427 state.currently <- Loading (l, state.gen);
1428 | Some opaque ->
1429 tilepage l.pageno opaque pages;
1430 loop rest
1431 end;
1432 | _ -> ()
1434 if nogeomcmds state.geomcmds
1435 then loop pages
1438 let preload pages =
1439 load pages;
1440 if conf.preload && state.currently = Idle
1441 then load (preloadlayout pages);
1444 let layoutready layout =
1445 let rec fold all ls =
1446 all && match ls with
1447 | l :: rest ->
1448 let seen = ref false in
1449 let allvisible = ref true in
1450 let foo col row _ _ _ _ _ _ =
1451 seen := true;
1452 allvisible := !allvisible &&
1453 begin match gettileopaque l col row with
1454 | Some _ -> true
1455 | None -> false
1458 itertiles l foo;
1459 fold (!seen && !allvisible) rest
1460 | [] -> true
1462 let alltilesvisible = fold true layout in
1463 alltilesvisible;
1466 let gotoy y =
1467 let y = bound y 0 state.maxy in
1468 let y, layout, proceed =
1469 match conf.maxwait with
1470 | Some time when state.ghyll == noghyll ->
1471 begin match state.throttle with
1472 | None ->
1473 let layout = layout y conf.winh in
1474 let ready = layoutready layout in
1475 if not ready
1476 then (
1477 load layout;
1478 state.throttle <- Some (layout, y, now ());
1480 else G.postRedisplay "gotoy showall (None)";
1481 y, layout, ready
1482 | Some (_, _, started) ->
1483 let dt = now () -. started in
1484 if dt > time
1485 then (
1486 state.throttle <- None;
1487 let layout = layout y conf.winh in
1488 load layout;
1489 G.postRedisplay "maxwait";
1490 y, layout, true
1492 else -1, [], false
1495 | _ ->
1496 let layout = layout y conf.winh in
1497 if true || layoutready layout
1498 then G.postRedisplay "gotoy ready";
1499 y, layout, true
1501 if proceed
1502 then (
1503 state.y <- y;
1504 state.layout <- layout;
1505 begin match state.mode with
1506 | LinkNav (Ltexact (pageno, linkno)) ->
1507 let rec loop = function
1508 | [] ->
1509 state.mode <- LinkNav (Ltgendir 0)
1510 | l :: _ when l.pageno = pageno ->
1511 begin match getopaque pageno with
1512 | None ->
1513 state.mode <- LinkNav (Ltgendir 0)
1514 | Some opaque ->
1515 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1516 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1517 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1518 then state.mode <- LinkNav (Ltgendir 0)
1520 | _ :: rest -> loop rest
1522 loop layout
1523 | _ -> ()
1524 end;
1525 begin match state.mode with
1526 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1527 if not (pagevisible layout pageno)
1528 then (
1529 match state.layout with
1530 | [] -> ()
1531 | l :: _ ->
1532 state.mode <- Birdseye (
1533 conf, leftx, l.pageno, hooverpageno, anchor
1536 | LinkNav (Ltgendir dir as lt) ->
1537 let linknav =
1538 let rec loop = function
1539 | [] -> lt
1540 | l :: rest ->
1541 match getopaque l.pageno with
1542 | None -> loop rest
1543 | Some opaque ->
1544 let link =
1545 let ld =
1546 if dir = 0
1547 then LDfirstvisible (l.pagex, l.pagey, dir)
1548 else (
1549 if dir > 0 then LDfirst else LDlast
1552 findlink opaque ld
1554 match link with
1555 | Lnotfound -> loop rest
1556 | Lfound n ->
1557 showlinktype (getlink opaque n);
1558 Ltexact (l.pageno, n)
1560 loop state.layout
1562 state.mode <- LinkNav linknav
1563 | _ -> ()
1564 end;
1565 preload layout;
1567 state.ghyll <- noghyll;
1568 if conf.updatecurs
1569 then (
1570 let mx, my = state.mpos in
1571 updateunder mx my;
1575 let conttiling pageno opaque =
1576 tilepage pageno opaque
1577 (if conf.preload then preloadlayout state.layout else state.layout)
1580 let gotoy_and_clear_text y =
1581 if not conf.verbose then state.text <- "";
1582 gotoy y;
1585 let getanchor () =
1586 match state.layout with
1587 | [] -> emptyanchor
1588 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1591 let getanchory (n, top) =
1592 let y, h = getpageyh n in
1593 y + (truncate (top *. float h));
1596 let gotoanchor anchor =
1597 gotoy (getanchory anchor);
1600 let addnav () =
1601 cbput state.hists.nav (getanchor ());
1604 let getnav dir =
1605 let anchor = cbgetc state.hists.nav dir in
1606 getanchory anchor;
1609 let gotoghyll y =
1610 let rec scroll f n a b =
1611 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1612 let snake f a b =
1613 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1614 if f < a
1615 then s (float f /. float a)
1616 else (
1617 if f > b
1618 then 1.0 -. s ((float (f-b) /. float (n-b)))
1619 else 1.0
1622 snake f a b
1623 and summa f n a b =
1624 (* courtesy:
1625 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1626 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1627 let iv1 = iv f in
1628 let ins = float a *. iv1
1629 and outs = float (n-b) *. iv1 in
1630 let ones = b - a in
1631 ins +. outs +. float ones
1633 let rec set (_N, _A, _B) y sy =
1634 let sum = summa 1.0 _N _A _B in
1635 let dy = float (y - sy) in
1636 state.ghyll <- (
1637 let rec gf n y1 o =
1638 if n >= _N
1639 then state.ghyll <- noghyll
1640 else
1641 let go n =
1642 let s = scroll n _N _A _B in
1643 let y1 = y1 +. ((s *. dy) /. sum) in
1644 gotoy_and_clear_text (truncate y1);
1645 state.ghyll <- gf (n+1) y1;
1647 match o with
1648 | None -> go n
1649 | Some y' -> set (_N/2, 0, 0) y' state.y
1651 gf 0 (float state.y)
1654 match conf.ghyllscroll with
1655 | None ->
1656 gotoy_and_clear_text y
1657 | Some nab ->
1658 if state.ghyll == noghyll
1659 then set nab y state.y
1660 else state.ghyll (Some y)
1663 let gotopage n top =
1664 let y, h = getpageyh n in
1665 let y = y + (truncate (top *. float h)) in
1666 gotoghyll y
1669 let gotopage1 n top =
1670 let y = getpagey n in
1671 let y = y + top in
1672 gotoghyll y
1675 let invalidate s f =
1676 state.layout <- [];
1677 state.pdims <- [];
1678 state.rects <- [];
1679 state.rects1 <- [];
1680 match state.geomcmds with
1681 | ps, [] when String.length ps = 0 ->
1682 f ();
1683 state.geomcmds <- s, [];
1685 | ps, [] ->
1686 state.geomcmds <- ps, [s, f];
1688 | ps, (s', _) :: rest when s' = s ->
1689 state.geomcmds <- ps, ((s, f) :: rest);
1691 | ps, cmds ->
1692 state.geomcmds <- ps, ((s, f) :: cmds);
1695 let opendoc path password =
1696 state.path <- path;
1697 state.password <- password;
1698 state.gen <- state.gen + 1;
1699 state.docinfo <- [];
1701 setaalevel conf.aalevel;
1702 Wsi.settitle ("llpp " ^ Filename.basename path);
1703 wcmd "open %s\000%s\000" path password;
1704 invalidate "reqlayout"
1705 (fun () ->
1706 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1709 let scalecolor c =
1710 let c = c *. conf.colorscale in
1711 (c, c, c);
1714 let scalecolor2 (r, g, b) =
1715 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1718 let represent () =
1719 let docolumns = function
1720 | None -> ()
1721 | Some ((columns, coverA, coverB), _) ->
1722 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1723 let rec loop pageno pdimno pdim x y rowh pdims =
1724 if pageno = state.pagecount
1725 then ()
1726 else
1727 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1728 match pdims with
1729 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1730 pdimno+1, pdim, rest
1731 | _ ->
1732 pdimno, pdim, pdims
1734 let x, y, rowh' =
1735 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1736 then (
1737 (conf.winw - state.scrollw - w) / 2,
1738 y + rowh + conf.interpagespace, h
1740 else (
1741 if (pageno - coverA) mod columns = 0
1742 then 0, y + rowh + conf.interpagespace, h
1743 else x, y, max rowh h
1746 let rec fixrow m = if m = pageno then () else
1747 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1748 if h < rowh
1749 then (
1750 let y = y + (rowh - h) / 2 in
1751 a.(m) <- (pdimno, x, y, pdim);
1753 fixrow (m+1)
1755 if pageno > 1 && (pageno - coverA) mod columns = 0
1756 then fixrow (pageno - columns);
1757 a.(pageno) <- (pdimno, x, y, pdim);
1758 let x = x + w + xoff*2 + conf.interpagespace in
1759 loop (pageno+1) pdimno pdim x y rowh' pdims
1761 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1762 conf.columns <- Some ((columns, coverA, coverB), a);
1764 docolumns conf.columns;
1765 state.maxy <- calcheight ();
1766 state.hscrollh <-
1767 if state.w <= conf.winw - state.scrollw
1768 then 0
1769 else state.scrollw
1771 match state.mode with
1772 | Birdseye (_, _, pageno, _, _) ->
1773 let y, h = getpageyh pageno in
1774 let top = (conf.winh - h) / 2 in
1775 gotoy (max 0 (y - top))
1776 | _ -> gotoanchor state.anchor
1779 let reshape w h =
1780 GlDraw.viewport 0 0 w h;
1781 if state.geomcmds != firstgeomcmds && nogeomcmds state.geomcmds
1782 then state.anchor <- getanchor ();
1784 conf.winw <- w;
1785 let w = truncate (float w *. conf.zoom) - state.scrollw in
1786 let w = max w 2 in
1787 conf.winh <- h;
1788 setfontsize fstate.fontsize;
1789 GlMat.mode `modelview;
1790 GlMat.load_identity ();
1792 GlMat.mode `projection;
1793 GlMat.load_identity ();
1794 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1795 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1796 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1798 let relx =
1799 if conf.zoom <= 1.0
1800 then 0.0
1801 else float state.x /. float state.w
1803 invalidate "geometry"
1804 (fun () ->
1805 state.w <- w;
1806 state.x <- truncate (relx *. float w);
1807 let w =
1808 match conf.columns with
1809 | None -> w
1810 | Some ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1812 wcmd "geometry %d %d" w h);
1815 let enttext () =
1816 let len = String.length state.text in
1817 let drawstring s =
1818 let hscrollh =
1819 match state.mode with
1820 | Textentry _
1821 | View -> state.hscrollh
1822 | _ -> 0
1824 let rect x w =
1825 GlDraw.rect
1826 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1827 (x+.w, float (conf.winh - hscrollh))
1830 let w = float (conf.winw - state.scrollw - 1) in
1831 if state.progress >= 0.0 && state.progress < 1.0
1832 then (
1833 GlDraw.color (0.3, 0.3, 0.3);
1834 let w1 = w *. state.progress in
1835 rect 0.0 w1;
1836 GlDraw.color (0.0, 0.0, 0.0);
1837 rect w1 (w-.w1)
1839 else (
1840 GlDraw.color (0.0, 0.0, 0.0);
1841 rect 0.0 w;
1844 GlDraw.color (1.0, 1.0, 1.0);
1845 drawstring fstate.fontsize
1846 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1848 let s =
1849 match state.mode with
1850 | Textentry ((prefix, text, _, _, _), _) ->
1851 let s =
1852 if len > 0
1853 then
1854 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1855 else
1856 Printf.sprintf "%s%s_" prefix text
1860 | _ -> state.text
1862 let s =
1863 if state.newerrmsgs
1864 then (
1865 if not (istextentry state.mode)
1866 then
1867 let s1 = "(press 'e' to review error messasges)" in
1868 if String.length s > 0 then s ^ " " ^ s1 else s1
1869 else s
1871 else s
1873 if String.length s > 0
1874 then drawstring s
1877 let gctiles () =
1878 let len = Queue.length state.tilelru in
1879 let rec loop qpos =
1880 if state.memused <= conf.memlimit
1881 then ()
1882 else (
1883 if qpos < len
1884 then
1885 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1886 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1887 let (_, pw, ph, _) = getpagedim n in
1889 gen = state.gen
1890 && colorspace = conf.colorspace
1891 && angle = conf.angle
1892 && pagew = pw
1893 && pageh = ph
1894 && (
1895 let layout =
1896 match state.throttle with
1897 | None ->
1898 if conf.preload
1899 then preloadlayout state.layout
1900 else state.layout
1901 | Some (layout, _, _) ->
1902 layout
1904 let x = col*conf.tilew
1905 and y = row*conf.tileh in
1906 tilevisible layout n x y
1908 then Queue.push lruitem state.tilelru
1909 else (
1910 wcmd "freetile %s" p;
1911 state.memused <- state.memused - s;
1912 state.uioh#infochanged Memused;
1913 Hashtbl.remove state.tilemap k;
1915 loop (qpos+1)
1918 loop 0
1921 let flushtiles () =
1922 Queue.iter (fun (k, p, s) ->
1923 wcmd "freetile %s" p;
1924 state.memused <- state.memused - s;
1925 state.uioh#infochanged Memused;
1926 Hashtbl.remove state.tilemap k;
1927 ) state.tilelru;
1928 Queue.clear state.tilelru;
1929 load state.layout;
1932 let logcurrently = function
1933 | Idle -> dolog "Idle"
1934 | Loading (l, gen) ->
1935 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1936 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1937 dolog
1938 "Tiling %d[%d,%d] page=%s cs=%s angle"
1939 l.pageno col row pageopaque
1940 (colorspace_to_string colorspace)
1942 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1943 angle gen conf.angle state.gen
1944 tilew tileh
1945 conf.tilew conf.tileh
1947 | Outlining _ ->
1948 dolog "outlining"
1951 let act cmds =
1952 (* dolog "%S" cmds; *)
1953 let op, args =
1954 let spacepos =
1955 try String.index cmds ' '
1956 with Not_found -> -1
1958 if spacepos = -1
1959 then cmds, ""
1960 else
1961 let l = String.length cmds in
1962 let op = String.sub cmds 0 spacepos in
1963 op, begin
1964 if l - spacepos < 2 then ""
1965 else String.sub cmds (spacepos+1) (l-spacepos-1)
1968 match op with
1969 | "clear" ->
1970 state.uioh#infochanged Pdim;
1971 state.pdims <- [];
1973 | "clearrects" ->
1974 state.rects <- state.rects1;
1975 G.postRedisplay "clearrects";
1977 | "continue" ->
1978 let n =
1979 try Scanf.sscanf args "%u" (fun n -> n)
1980 with exn ->
1981 dolog "error processing 'continue' %S: %s"
1982 cmds (Printexc.to_string exn);
1983 exit 1;
1985 state.pagecount <- n;
1986 begin match state.currently with
1987 | Outlining l ->
1988 state.currently <- Idle;
1989 state.outlines <- Array.of_list (List.rev l)
1990 | _ -> ()
1991 end;
1993 let cur, cmds = state.geomcmds in
1994 if String.length cur = 0
1995 then failwith "umpossible";
1997 begin match List.rev cmds with
1998 | [] ->
1999 state.geomcmds <- "", [];
2000 represent ();
2001 | (s, f) :: rest ->
2002 f ();
2003 state.geomcmds <- s, List.rev rest;
2004 end;
2005 if conf.maxwait = None
2006 then G.postRedisplay "continue";
2008 | "title" ->
2009 Wsi.settitle args
2011 | "msg" ->
2012 showtext ' ' args
2014 | "vmsg" ->
2015 if conf.verbose
2016 then showtext ' ' args
2018 | "progress" ->
2019 let progress, text =
2021 Scanf.sscanf args "%f %n"
2022 (fun f pos ->
2023 f, String.sub args pos (String.length args - pos))
2024 with exn ->
2025 dolog "error processing 'progress' %S: %s"
2026 cmds (Printexc.to_string exn);
2027 exit 1;
2029 state.text <- text;
2030 state.progress <- progress;
2031 G.postRedisplay "progress"
2033 | "firstmatch" ->
2034 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2036 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2037 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2038 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2039 with exn ->
2040 dolog "error processing 'firstmatch' %S: %s"
2041 cmds (Printexc.to_string exn);
2042 exit 1;
2044 let y = (getpagey pageno) + truncate y0 in
2045 addnav ();
2046 gotoy y;
2047 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2049 | "match" ->
2050 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2052 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2053 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2054 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2055 with exn ->
2056 dolog "error processing 'match' %S: %s"
2057 cmds (Printexc.to_string exn);
2058 exit 1;
2060 state.rects1 <-
2061 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2063 | "page" ->
2064 let pageopaque, t =
2066 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2067 with exn ->
2068 dolog "error processing 'page' %S: %s"
2069 cmds (Printexc.to_string exn);
2070 exit 1;
2072 begin match state.currently with
2073 | Loading (l, gen) ->
2074 vlog "page %d took %f sec" l.pageno t;
2075 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2076 begin match state.throttle with
2077 | None ->
2078 let preloadedpages =
2079 if conf.preload
2080 then preloadlayout state.layout
2081 else state.layout
2083 let evict () =
2084 let module IntSet =
2085 Set.Make (struct type t = int let compare = (-) end) in
2086 let set =
2087 List.fold_left (fun s l -> IntSet.add l.pageno s)
2088 IntSet.empty preloadedpages
2090 let evictedpages =
2091 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2092 if not (IntSet.mem pageno set)
2093 then (
2094 wcmd "freepage %s" opaque;
2095 key :: accu
2097 else accu
2098 ) state.pagemap []
2100 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2102 evict ();
2103 state.currently <- Idle;
2104 if gen = state.gen
2105 then (
2106 tilepage l.pageno pageopaque state.layout;
2107 load state.layout;
2108 load preloadedpages;
2109 if pagevisible state.layout l.pageno
2110 && layoutready state.layout
2111 then G.postRedisplay "page";
2114 | Some (layout, _, _) ->
2115 state.currently <- Idle;
2116 tilepage l.pageno pageopaque layout;
2117 load state.layout
2118 end;
2120 | _ ->
2121 dolog "Inconsistent loading state";
2122 logcurrently state.currently;
2123 exit 1
2126 | "tile" ->
2127 let (x, y, opaque, size, t) =
2129 Scanf.sscanf args "%u %u %s %u %f"
2130 (fun x y p size t -> (x, y, p, size, t))
2131 with exn ->
2132 dolog "error processing 'tile' %S: %s"
2133 cmds (Printexc.to_string exn);
2134 exit 1;
2136 begin match state.currently with
2137 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2138 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2140 if tilew != conf.tilew || tileh != conf.tileh
2141 then (
2142 wcmd "freetile %s" opaque;
2143 state.currently <- Idle;
2144 load state.layout;
2146 else (
2147 puttileopaque l col row gen cs angle opaque size t;
2148 state.memused <- state.memused + size;
2149 state.uioh#infochanged Memused;
2150 gctiles ();
2151 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2152 opaque, size) state.tilelru;
2154 let layout =
2155 match state.throttle with
2156 | None -> state.layout
2157 | Some (layout, _, _) -> layout
2160 state.currently <- Idle;
2161 if gen = state.gen
2162 && conf.colorspace = cs
2163 && conf.angle = angle
2164 && tilevisible layout l.pageno x y
2165 then conttiling l.pageno pageopaque;
2167 begin match state.throttle with
2168 | None ->
2169 preload state.layout;
2170 if gen = state.gen
2171 && conf.colorspace = cs
2172 && conf.angle = angle
2173 && tilevisible state.layout l.pageno x y
2174 then G.postRedisplay "tile nothrottle";
2176 | Some (layout, y, _) ->
2177 let ready = layoutready layout in
2178 if ready
2179 then (
2180 state.y <- y;
2181 state.layout <- layout;
2182 state.throttle <- None;
2183 G.postRedisplay "throttle";
2185 else load layout;
2186 end;
2189 | _ ->
2190 dolog "Inconsistent tiling state";
2191 logcurrently state.currently;
2192 exit 1
2195 | "pdim" ->
2196 let pdim =
2198 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2199 with exn ->
2200 dolog "error processing 'pdim' %S: %s"
2201 cmds (Printexc.to_string exn);
2202 exit 1;
2204 state.uioh#infochanged Pdim;
2205 state.pdims <- pdim :: state.pdims
2207 | "o" ->
2208 let (l, n, t, h, pos) =
2210 Scanf.sscanf args "%u %u %d %u %n"
2211 (fun l n t h pos -> l, n, t, h, pos)
2212 with exn ->
2213 dolog "error processing 'o' %S: %s"
2214 cmds (Printexc.to_string exn);
2215 exit 1;
2217 let s = String.sub args pos (String.length args - pos) in
2218 let outline = (s, l, (n, float t /. float h)) in
2219 begin match state.currently with
2220 | Outlining outlines ->
2221 state.currently <- Outlining (outline :: outlines)
2222 | Idle ->
2223 state.currently <- Outlining [outline]
2224 | currently ->
2225 dolog "invalid outlining state";
2226 logcurrently currently
2229 | "info" ->
2230 state.docinfo <- (1, args) :: state.docinfo
2232 | "infoend" ->
2233 state.uioh#infochanged Docinfo;
2234 state.docinfo <- List.rev state.docinfo
2236 | _ ->
2237 dolog "unknown cmd `%S'" cmds
2240 let onhist cb =
2241 let rc = cb.rc in
2242 let action = function
2243 | HCprev -> cbget cb ~-1
2244 | HCnext -> cbget cb 1
2245 | HCfirst -> cbget cb ~-(cb.rc)
2246 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2247 and cancel () = cb.rc <- rc
2248 in (action, cancel)
2251 let search pattern forward =
2252 if String.length pattern > 0
2253 then
2254 let pn, py =
2255 match state.layout with
2256 | [] -> 0, 0
2257 | l :: _ ->
2258 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2260 wcmd "search %d %d %d %d,%s\000"
2261 (btod conf.icase) pn py (btod forward) pattern;
2264 let intentry text key =
2265 let c =
2266 if key >= 32 && key < 127
2267 then Char.chr key
2268 else '\000'
2270 match c with
2271 | '0' .. '9' ->
2272 let text = addchar text c in
2273 TEcont text
2275 | _ ->
2276 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2277 TEcont text
2280 let textentry text key =
2281 if key land 0xff00 = 0xff00
2282 then TEcont text
2283 else TEcont (text ^ Wsi.toutf8 key)
2286 let reqlayout angle proportional =
2287 match state.throttle with
2288 | None ->
2289 if nogeomcmds state.geomcmds
2290 then state.anchor <- getanchor ();
2291 conf.angle <- angle mod 360;
2292 if conf.angle != 0
2293 then (
2294 match state.mode with
2295 | LinkNav _ -> state.mode <- View
2296 | _ -> ()
2298 conf.proportional <- proportional;
2299 invalidate "reqlayout"
2300 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2301 | _ -> ()
2304 let settrim trimmargins trimfuzz =
2305 if nogeomcmds state.geomcmds
2306 then state.anchor <- getanchor ();
2307 conf.trimmargins <- trimmargins;
2308 conf.trimfuzz <- trimfuzz;
2309 let x0, y0, x1, y1 = trimfuzz in
2310 invalidate "settrim"
2311 (fun () ->
2312 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2313 Hashtbl.iter (fun _ opaque ->
2314 wcmd "freepage %s" opaque;
2315 ) state.pagemap;
2316 Hashtbl.clear state.pagemap;
2319 let setzoom zoom =
2320 match state.throttle with
2321 | None ->
2322 let zoom = max 0.01 zoom in
2323 if zoom <> conf.zoom
2324 then (
2325 state.prevzoom <- conf.zoom;
2326 conf.zoom <- zoom;
2327 reshape conf.winw conf.winh;
2328 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2331 | Some (layout, y, started) ->
2332 let time =
2333 match conf.maxwait with
2334 | None -> 0.0
2335 | Some t -> t
2337 let dt = now () -. started in
2338 if dt > time
2339 then (
2340 state.y <- y;
2341 load layout;
2345 let setcolumns columns coverA coverB =
2346 if columns < 2
2347 then (
2348 conf.columns <- None;
2349 state.x <- 0;
2350 setzoom 1.0;
2352 else (
2353 conf.columns <- Some ((columns, coverA, coverB), [||]);
2354 conf.zoom <- 1.0;
2356 reshape conf.winw conf.winh;
2359 let enterbirdseye () =
2360 let zoom = float conf.thumbw /. float conf.winw in
2361 let birdseyepageno =
2362 let cy = conf.winh / 2 in
2363 let fold = function
2364 | [] -> 0
2365 | l :: rest ->
2366 let rec fold best = function
2367 | [] -> best.pageno
2368 | l :: rest ->
2369 let d = cy - (l.pagedispy + l.pagevh/2)
2370 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2371 if abs d < abs dbest
2372 then fold l rest
2373 else best.pageno
2374 in fold l rest
2376 fold state.layout
2378 state.mode <- Birdseye (
2379 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2381 conf.zoom <- zoom;
2382 conf.presentation <- false;
2383 conf.interpagespace <- 10;
2384 conf.hlinks <- false;
2385 state.x <- 0;
2386 state.mstate <- Mnone;
2387 conf.maxwait <- None;
2388 conf.columns <- (
2389 match conf.beyecolumns with
2390 | Some c ->
2391 conf.zoom <- 1.0;
2392 Some ((c, 0, 0), [||])
2393 | None -> None
2395 Wsi.setcursor Wsi.CURSOR_INHERIT;
2396 if conf.verbose
2397 then
2398 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2399 (100.0*.zoom)
2400 else
2401 state.text <- ""
2403 reshape conf.winw conf.winh;
2406 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2407 state.mode <- View;
2408 conf.zoom <- c.zoom;
2409 conf.presentation <- c.presentation;
2410 conf.interpagespace <- c.interpagespace;
2411 conf.maxwait <- c.maxwait;
2412 conf.hlinks <- c.hlinks;
2413 conf.beyecolumns <- (
2414 match conf.columns with
2415 | Some ((c, _, _), _) -> Some c
2416 | None -> None
2418 conf.columns <- (
2419 match c.columns with
2420 | Some (c, _) -> Some (c, [||])
2421 | None -> None
2423 state.x <- leftx;
2424 if conf.verbose
2425 then
2426 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2427 (100.0*.conf.zoom)
2429 reshape conf.winw conf.winh;
2430 state.anchor <- if goback then anchor else (pageno, 0.0);
2433 let togglebirdseye () =
2434 match state.mode with
2435 | Birdseye vals -> leavebirdseye vals true
2436 | View -> enterbirdseye ()
2437 | _ -> ()
2440 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2441 let pageno = max 0 (pageno - incr) in
2442 let rec loop = function
2443 | [] -> gotopage1 pageno 0
2444 | l :: _ when l.pageno = pageno ->
2445 if l.pagedispy >= 0 && l.pagey = 0
2446 then G.postRedisplay "upbirdseye"
2447 else gotopage1 pageno 0
2448 | _ :: rest -> loop rest
2450 loop state.layout;
2451 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2454 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2455 let pageno = min (state.pagecount - 1) (pageno + incr) in
2456 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2457 let rec loop = function
2458 | [] ->
2459 let y, h = getpageyh pageno in
2460 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2461 gotoy (clamp dy)
2462 | l :: _ when l.pageno = pageno ->
2463 if l.pagevh != l.pageh
2464 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2465 else G.postRedisplay "downbirdseye"
2466 | _ :: rest -> loop rest
2468 loop state.layout
2471 let optentry mode _ key =
2472 let btos b = if b then "on" else "off" in
2473 if key >= 32 && key < 127
2474 then
2475 let c = Char.chr key in
2476 match c with
2477 | 's' ->
2478 let ondone s =
2479 try conf.scrollstep <- int_of_string s with exc ->
2480 state.text <- Printf.sprintf "bad integer `%s': %s"
2481 s (Printexc.to_string exc)
2483 TEswitch ("scroll step: ", "", None, intentry, ondone)
2485 | 'A' ->
2486 let ondone s =
2488 conf.autoscrollstep <- int_of_string s;
2489 if state.autoscroll <> None
2490 then state.autoscroll <- Some conf.autoscrollstep
2491 with exc ->
2492 state.text <- Printf.sprintf "bad integer `%s': %s"
2493 s (Printexc.to_string exc)
2495 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2497 | 'C' ->
2498 let ondone s =
2500 let n, a, b = columns_of_string s in
2501 setcolumns n a b;
2502 with exc ->
2503 state.text <- Printf.sprintf "bad columns `%s': %s"
2504 s (Printexc.to_string exc)
2506 TEswitch ("columns: ", "", None, textentry, ondone)
2508 | 'Z' ->
2509 let ondone s =
2511 let zoom = float (int_of_string s) /. 100.0 in
2512 setzoom zoom
2513 with exc ->
2514 state.text <- Printf.sprintf "bad integer `%s': %s"
2515 s (Printexc.to_string exc)
2517 TEswitch ("zoom: ", "", None, intentry, ondone)
2519 | 't' ->
2520 let ondone s =
2522 conf.thumbw <- bound (int_of_string s) 2 4096;
2523 state.text <-
2524 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2525 begin match mode with
2526 | Birdseye beye ->
2527 leavebirdseye beye false;
2528 enterbirdseye ();
2529 | _ -> ();
2531 with exc ->
2532 state.text <- Printf.sprintf "bad integer `%s': %s"
2533 s (Printexc.to_string exc)
2535 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2537 | 'R' ->
2538 let ondone s =
2539 match try
2540 Some (int_of_string s)
2541 with exc ->
2542 state.text <- Printf.sprintf "bad integer `%s': %s"
2543 s (Printexc.to_string exc);
2544 None
2545 with
2546 | Some angle -> reqlayout angle conf.proportional
2547 | None -> ()
2549 TEswitch ("rotation: ", "", None, intentry, ondone)
2551 | 'i' ->
2552 conf.icase <- not conf.icase;
2553 TEdone ("case insensitive search " ^ (btos conf.icase))
2555 | 'p' ->
2556 conf.preload <- not conf.preload;
2557 gotoy state.y;
2558 TEdone ("preload " ^ (btos conf.preload))
2560 | 'v' ->
2561 conf.verbose <- not conf.verbose;
2562 TEdone ("verbose " ^ (btos conf.verbose))
2564 | 'd' ->
2565 conf.debug <- not conf.debug;
2566 TEdone ("debug " ^ (btos conf.debug))
2568 | 'h' ->
2569 conf.maxhfit <- not conf.maxhfit;
2570 state.maxy <-
2571 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2572 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2574 | 'c' ->
2575 conf.crophack <- not conf.crophack;
2576 TEdone ("crophack " ^ btos conf.crophack)
2578 | 'a' ->
2579 let s =
2580 match conf.maxwait with
2581 | None ->
2582 conf.maxwait <- Some infinity;
2583 "always wait for page to complete"
2584 | Some _ ->
2585 conf.maxwait <- None;
2586 "show placeholder if page is not ready"
2588 TEdone s
2590 | 'f' ->
2591 conf.underinfo <- not conf.underinfo;
2592 TEdone ("underinfo " ^ btos conf.underinfo)
2594 | 'P' ->
2595 conf.savebmarks <- not conf.savebmarks;
2596 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2598 | 'S' ->
2599 let ondone s =
2601 let pageno, py =
2602 match state.layout with
2603 | [] -> 0, 0
2604 | l :: _ ->
2605 l.pageno, l.pagey
2607 conf.interpagespace <- int_of_string s;
2608 state.maxy <- calcheight ();
2609 let y = getpagey pageno in
2610 gotoy (y + py)
2611 with exc ->
2612 state.text <- Printf.sprintf "bad integer `%s': %s"
2613 s (Printexc.to_string exc)
2615 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2617 | 'l' ->
2618 reqlayout conf.angle (not conf.proportional);
2619 TEdone ("proportional display " ^ btos conf.proportional)
2621 | 'T' ->
2622 settrim (not conf.trimmargins) conf.trimfuzz;
2623 TEdone ("trim margins " ^ btos conf.trimmargins)
2625 | 'I' ->
2626 conf.invert <- not conf.invert;
2627 TEdone ("invert colors " ^ btos conf.invert)
2629 | 'x' ->
2630 let ondone s =
2631 cbput state.hists.sel s;
2632 conf.selcmd <- s;
2634 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2635 textentry, ondone)
2637 | _ ->
2638 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2639 TEstop
2640 else
2641 TEcont state.text
2644 class type lvsource = object
2645 method getitemcount : int
2646 method getitem : int -> (string * int)
2647 method hasaction : int -> bool
2648 method exit :
2649 uioh:uioh ->
2650 cancel:bool ->
2651 active:int ->
2652 first:int ->
2653 pan:int ->
2654 qsearch:string ->
2655 uioh option
2656 method getactive : int
2657 method getfirst : int
2658 method getqsearch : string
2659 method setqsearch : string -> unit
2660 method getpan : int
2661 end;;
2663 class virtual lvsourcebase = object
2664 val mutable m_active = 0
2665 val mutable m_first = 0
2666 val mutable m_qsearch = ""
2667 val mutable m_pan = 0
2668 method getactive = m_active
2669 method getfirst = m_first
2670 method getqsearch = m_qsearch
2671 method getpan = m_pan
2672 method setqsearch s = m_qsearch <- s
2673 end;;
2675 let withoutlastutf8 s =
2676 let len = String.length s in
2677 if len = 0
2678 then s
2679 else
2680 let rec find pos =
2681 if pos = 0
2682 then pos
2683 else
2684 let b = Char.code s.[pos] in
2685 if b land 0b110000 = 0b11000000
2686 then find (pos-1)
2687 else pos-1
2689 let first =
2690 if Char.code s.[len-1] land 0x80 = 0
2691 then len-1
2692 else find (len-1)
2694 String.sub s 0 first;
2697 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2698 let enttext te =
2699 state.mode <- Textentry (te, onleave);
2700 state.text <- "";
2701 enttext ();
2702 G.postRedisplay "textentrykeyboard enttext";
2704 let histaction cmd =
2705 match opthist with
2706 | None -> ()
2707 | Some (action, _) ->
2708 state.mode <- Textentry (
2709 (c, action cmd, opthist, onkey, ondone), onleave
2711 G.postRedisplay "textentry histaction"
2713 match key with
2714 | 0xff08 -> (* backspace *)
2715 let s = withoutlastutf8 text in
2716 let len = String.length s in
2717 if len = 0
2718 then (
2719 onleave Cancel;
2720 G.postRedisplay "textentrykeyboard after cancel";
2722 else (
2723 enttext (c, s, opthist, onkey, ondone)
2726 | 0xff0d ->
2727 ondone text;
2728 onleave Confirm;
2729 G.postRedisplay "textentrykeyboard after confirm"
2731 | 0xff52 -> histaction HCprev
2732 | 0xff54 -> histaction HCnext
2733 | 0xff50 -> histaction HCfirst
2734 | 0xff57 -> histaction HClast
2736 | 0xff1b -> (* escape*)
2737 if String.length text = 0
2738 then (
2739 begin match opthist with
2740 | None -> ()
2741 | Some (_, onhistcancel) -> onhistcancel ()
2742 end;
2743 onleave Cancel;
2744 state.text <- "";
2745 G.postRedisplay "textentrykeyboard after cancel2"
2747 else (
2748 enttext (c, "", opthist, onkey, ondone)
2751 | 0xff9f | 0xffff -> () (* delete *)
2753 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2754 begin match onkey text key with
2755 | TEdone text ->
2756 ondone text;
2757 onleave Confirm;
2758 G.postRedisplay "textentrykeyboard after confirm2";
2760 | TEcont text ->
2761 enttext (c, text, opthist, onkey, ondone);
2763 | TEstop ->
2764 onleave Cancel;
2765 G.postRedisplay "textentrykeyboard after cancel3"
2767 | TEswitch te ->
2768 state.mode <- Textentry (te, onleave);
2769 G.postRedisplay "textentrykeyboard switch";
2770 end;
2772 | _ ->
2773 vlog "unhandled key %s" (Wsi.keyname key)
2776 let firstof first active =
2777 if first > active || abs (first - active) > fstate.maxrows - 1
2778 then max 0 (active - (fstate.maxrows/2))
2779 else first
2782 let calcfirst first active =
2783 if active > first
2784 then
2785 let rows = active - first in
2786 if rows > fstate.maxrows then active - fstate.maxrows else first
2787 else active
2790 let scrollph y maxy =
2791 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2792 let sh = float conf.winh /. sh in
2793 let sh = max sh (float conf.scrollh) in
2795 let percent =
2796 if y = state.maxy
2797 then 1.0
2798 else float y /. float maxy
2800 let position = (float conf.winh -. sh) *. percent in
2802 let position =
2803 if position +. sh > float conf.winh
2804 then float conf.winh -. sh
2805 else position
2807 position, sh;
2810 let coe s = (s :> uioh);;
2812 class listview ~(source:lvsource) ~trusted ~modehash =
2813 object (self)
2814 val m_pan = source#getpan
2815 val m_first = source#getfirst
2816 val m_active = source#getactive
2817 val m_qsearch = source#getqsearch
2818 val m_prev_uioh = state.uioh
2820 method private elemunder y =
2821 let n = y / (fstate.fontsize+1) in
2822 if m_first + n < source#getitemcount
2823 then (
2824 if source#hasaction (m_first + n)
2825 then Some (m_first + n)
2826 else None
2828 else None
2830 method display =
2831 Gl.enable `blend;
2832 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2833 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2834 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2835 GlDraw.color (1., 1., 1.);
2836 Gl.enable `texture_2d;
2837 let fs = fstate.fontsize in
2838 let nfs = fs + 1 in
2839 let ww = fstate.wwidth in
2840 let tabw = 30.0*.ww in
2841 let itemcount = source#getitemcount in
2842 let rec loop row =
2843 if (row - m_first) * nfs > conf.winh
2844 then ()
2845 else (
2846 if row >= 0 && row < itemcount
2847 then (
2848 let (s, level) = source#getitem row in
2849 let y = (row - m_first) * nfs in
2850 let x = 5.0 +. float (level + m_pan) *. ww in
2851 if row = m_active
2852 then (
2853 Gl.disable `texture_2d;
2854 GlDraw.polygon_mode `both `line;
2855 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2856 GlDraw.rect (1., float (y + 1))
2857 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2858 GlDraw.polygon_mode `both `fill;
2859 GlDraw.color (1., 1., 1.);
2860 Gl.enable `texture_2d;
2863 let drawtabularstring s =
2864 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2865 if trusted
2866 then
2867 let tabpos = try String.index s '\t' with Not_found -> -1 in
2868 if tabpos > 0
2869 then
2870 let len = String.length s - tabpos - 1 in
2871 let s1 = String.sub s 0 tabpos
2872 and s2 = String.sub s (tabpos + 1) len in
2873 let nx = drawstr x s1 in
2874 let sw = nx -. x in
2875 let x = x +. (max tabw sw) in
2876 drawstr x s2
2877 else
2878 drawstr x s
2879 else
2880 drawstr x s
2882 let _ = drawtabularstring s in
2883 loop (row+1)
2887 loop m_first;
2888 Gl.disable `blend;
2889 Gl.disable `texture_2d;
2891 method updownlevel incr =
2892 let len = source#getitemcount in
2893 let curlevel =
2894 if m_active >= 0 && m_active < len
2895 then snd (source#getitem m_active)
2896 else -1
2898 let rec flow i =
2899 if i = len then i-1 else if i = -1 then 0 else
2900 let _, l = source#getitem i in
2901 if l != curlevel then i else flow (i+incr)
2903 let active = flow m_active in
2904 let first = calcfirst m_first active in
2905 G.postRedisplay "outline updownlevel";
2906 {< m_active = active; m_first = first >}
2908 method private key1 key mask =
2909 let set1 active first qsearch =
2910 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2912 let search active pattern incr =
2913 let dosearch re =
2914 let rec loop n =
2915 if n >= 0 && n < source#getitemcount
2916 then (
2917 let s, _ = source#getitem n in
2919 (try ignore (Str.search_forward re s 0); true
2920 with Not_found -> false)
2921 then Some n
2922 else loop (n + incr)
2924 else None
2926 loop active
2929 let re = Str.regexp_case_fold pattern in
2930 dosearch re
2931 with Failure s ->
2932 state.text <- s;
2933 None
2935 let itemcount = source#getitemcount in
2936 let find start incr =
2937 let rec find i =
2938 if i = -1 || i = itemcount
2939 then -1
2940 else (
2941 if source#hasaction i
2942 then i
2943 else find (i + incr)
2946 find start
2948 let set active first =
2949 let first = bound first 0 (itemcount - fstate.maxrows) in
2950 state.text <- "";
2951 coe {< m_active = active; m_first = first >}
2953 let navigate incr =
2954 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2955 let active, first =
2956 let incr1 = if incr > 0 then 1 else -1 in
2957 if isvisible m_first m_active
2958 then
2959 let next =
2960 let next = m_active + incr in
2961 let next =
2962 if next < 0 || next >= itemcount
2963 then -1
2964 else find next incr1
2966 if next = -1 || abs (m_active - next) > fstate.maxrows
2967 then -1
2968 else next
2970 if next = -1
2971 then
2972 let first = m_first + incr in
2973 let first = bound first 0 (itemcount - 1) in
2974 let next =
2975 let next = m_active + incr in
2976 let next = bound next 0 (itemcount - 1) in
2977 find next ~-incr1
2979 let active = if next = -1 then m_active else next in
2980 active, first
2981 else
2982 let first = min next m_first in
2983 let first =
2984 if abs (next - first) > fstate.maxrows
2985 then first + incr
2986 else first
2988 next, first
2989 else
2990 let first = m_first + incr in
2991 let first = bound first 0 (itemcount - 1) in
2992 let active =
2993 let next = m_active + incr in
2994 let next = bound next 0 (itemcount - 1) in
2995 let next = find next incr1 in
2996 let active =
2997 if next = -1 || abs (m_active - first) > fstate.maxrows
2998 then (
2999 let active = if m_active = -1 then next else m_active in
3000 active
3002 else next
3004 if isvisible first active
3005 then active
3006 else -1
3008 active, first
3010 G.postRedisplay "listview navigate";
3011 set active first;
3013 match key with
3014 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3015 let incr = if key = 0x72 then -1 else 1 in
3016 let active, first =
3017 match search (m_active + incr) m_qsearch incr with
3018 | None ->
3019 state.text <- m_qsearch ^ " [not found]";
3020 m_active, m_first
3021 | Some active ->
3022 state.text <- m_qsearch;
3023 active, firstof m_first active
3025 G.postRedisplay "listview ctrl-r/s";
3026 set1 active first m_qsearch;
3028 | 0xff08 -> (* backspace *)
3029 if String.length m_qsearch = 0
3030 then coe self
3031 else (
3032 let qsearch = withoutlastutf8 m_qsearch in
3033 let len = String.length qsearch in
3034 if len = 0
3035 then (
3036 state.text <- "";
3037 G.postRedisplay "listview empty qsearch";
3038 set1 m_active m_first "";
3040 else
3041 let active, first =
3042 match search m_active qsearch ~-1 with
3043 | None ->
3044 state.text <- qsearch ^ " [not found]";
3045 m_active, m_first
3046 | Some active ->
3047 state.text <- qsearch;
3048 active, firstof m_first active
3050 G.postRedisplay "listview backspace qsearch";
3051 set1 active first qsearch
3054 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3055 let pattern = m_qsearch ^ Wsi.toutf8 key in
3056 let active, first =
3057 match search m_active pattern 1 with
3058 | None ->
3059 state.text <- pattern ^ " [not found]";
3060 m_active, m_first
3061 | Some active ->
3062 state.text <- pattern;
3063 active, firstof m_first active
3065 G.postRedisplay "listview qsearch add";
3066 set1 active first pattern;
3068 | 0xff1b -> (* escape *)
3069 state.text <- "";
3070 if String.length m_qsearch = 0
3071 then (
3072 G.postRedisplay "list view escape";
3073 begin
3074 match
3075 source#exit (coe self) true m_active m_first m_pan m_qsearch
3076 with
3077 | None -> m_prev_uioh
3078 | Some uioh -> uioh
3081 else (
3082 G.postRedisplay "list view kill qsearch";
3083 source#setqsearch "";
3084 coe {< m_qsearch = "" >}
3087 | 0xff0d -> (* return *)
3088 state.text <- "";
3089 let self = {< m_qsearch = "" >} in
3090 source#setqsearch "";
3091 let opt =
3092 G.postRedisplay "listview enter";
3093 if m_active >= 0 && m_active < source#getitemcount
3094 then (
3095 source#exit (coe self) false m_active m_first m_pan "";
3097 else (
3098 source#exit (coe self) true m_active m_first m_pan "";
3101 begin match opt with
3102 | None -> m_prev_uioh
3103 | Some uioh -> uioh
3106 | 0xff9f | 0xffff -> (* delete *)
3107 coe self
3109 | 0xff52 -> navigate ~-1 (* up *)
3110 | 0xff54 -> navigate 1 (* down *)
3111 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3112 | 0xff56 -> navigate fstate.maxrows (* next *)
3114 | 0xff53 -> (* right *)
3115 state.text <- "";
3116 G.postRedisplay "listview right";
3117 coe {< m_pan = m_pan - 1 >}
3119 | 0xff51 -> (* left *)
3120 state.text <- "";
3121 G.postRedisplay "listview left";
3122 coe {< m_pan = m_pan + 1 >}
3124 | 0xff50 -> (* home *)
3125 let active = find 0 1 in
3126 G.postRedisplay "listview home";
3127 set active 0;
3129 | 0xff57 -> (* end *)
3130 let first = max 0 (itemcount - fstate.maxrows) in
3131 let active = find (itemcount - 1) ~-1 in
3132 G.postRedisplay "listview end";
3133 set active first;
3135 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3136 coe self
3138 | _ ->
3139 dolog "listview unknown key %#x" key; coe self
3141 method key key mask =
3142 match state.mode with
3143 | Textentry te -> textentrykeyboard key mask te; coe self
3144 | _ -> self#key1 key mask
3146 method button button down x y _ =
3147 let opt =
3148 match button with
3149 | 1 when x > conf.winw - conf.scrollbw ->
3150 G.postRedisplay "listview scroll";
3151 if down
3152 then
3153 let _, position, sh = self#scrollph in
3154 if y > truncate position && y < truncate (position +. sh)
3155 then (
3156 state.mstate <- Mscrolly;
3157 Some (coe self)
3159 else
3160 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3161 let first = truncate (s *. float source#getitemcount) in
3162 let first = min source#getitemcount first in
3163 Some (coe {< m_first = first; m_active = first >})
3164 else (
3165 state.mstate <- Mnone;
3166 Some (coe self);
3168 | 1 when not down ->
3169 begin match self#elemunder y with
3170 | Some n ->
3171 G.postRedisplay "listview click";
3172 source#exit
3173 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3174 | _ ->
3175 Some (coe self)
3177 | n when (n == 4 || n == 5) && not down ->
3178 let len = source#getitemcount in
3179 let first =
3180 if n = 5 && m_first + fstate.maxrows >= len
3181 then
3182 m_first
3183 else
3184 let first = m_first + (if n == 4 then -1 else 1) in
3185 bound first 0 (len - 1)
3187 G.postRedisplay "listview wheel";
3188 Some (coe {< m_first = first >})
3189 | _ ->
3190 Some (coe self)
3192 match opt with
3193 | None -> m_prev_uioh
3194 | Some uioh -> uioh
3196 method motion _ y =
3197 match state.mstate with
3198 | Mscrolly ->
3199 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3200 let first = truncate (s *. float source#getitemcount) in
3201 let first = min source#getitemcount first in
3202 G.postRedisplay "listview motion";
3203 coe {< m_first = first; m_active = first >}
3204 | _ -> coe self
3206 method pmotion x y =
3207 if x < conf.winw - conf.scrollbw
3208 then
3209 let n =
3210 match self#elemunder y with
3211 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3212 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3214 let o =
3215 if n != m_active
3216 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3217 else self
3219 coe o
3220 else (
3221 Wsi.setcursor Wsi.CURSOR_INHERIT;
3222 coe self
3225 method infochanged _ = ()
3227 method scrollpw = (0, 0.0, 0.0)
3228 method scrollph =
3229 let nfs = fstate.fontsize + 1 in
3230 let y = m_first * nfs in
3231 let itemcount = source#getitemcount in
3232 let maxi = max 0 (itemcount - fstate.maxrows) in
3233 let maxy = maxi * nfs in
3234 let p, h = scrollph y maxy in
3235 conf.scrollbw, p, h
3237 method modehash = modehash
3238 end;;
3240 class outlinelistview ~source =
3241 object (self)
3242 inherit listview
3243 ~source:(source :> lvsource)
3244 ~trusted:false
3245 ~modehash:(findkeyhash conf "outline")
3246 as super
3248 method key key mask =
3249 let calcfirst first active =
3250 if active > first
3251 then
3252 let rows = active - first in
3253 if rows > fstate.maxrows then active - fstate.maxrows else first
3254 else active
3256 let navigate incr =
3257 let active = m_active + incr in
3258 let active = bound active 0 (source#getitemcount - 1) in
3259 let first = calcfirst m_first active in
3260 G.postRedisplay "outline navigate";
3261 coe {< m_active = active; m_first = first >}
3263 let ctrl = Wsi.withctrl mask in
3264 match key with
3265 | 110 when ctrl -> (* ctrl-n *)
3266 source#narrow m_qsearch;
3267 G.postRedisplay "outline ctrl-n";
3268 coe {< m_first = 0; m_active = 0 >}
3270 | 117 when ctrl -> (* ctrl-u *)
3271 source#denarrow;
3272 G.postRedisplay "outline ctrl-u";
3273 state.text <- "";
3274 coe {< m_first = 0; m_active = 0 >}
3276 | 108 when ctrl -> (* ctrl-l *)
3277 let first = m_active - (fstate.maxrows / 2) in
3278 G.postRedisplay "outline ctrl-l";
3279 coe {< m_first = first >}
3281 | 0xff9f | 0xffff -> (* delete *)
3282 source#remove m_active;
3283 G.postRedisplay "outline delete";
3284 let active = max 0 (m_active-1) in
3285 coe {< m_first = firstof m_first active;
3286 m_active = active >}
3288 | 0xff52 -> navigate ~-1 (* up *)
3289 | 0xff54 -> navigate 1 (* down *)
3290 | 0xff55 -> (* prior *)
3291 navigate ~-(fstate.maxrows)
3292 | 0xff56 -> (* next *)
3293 navigate fstate.maxrows
3295 | 0xff53 -> (* [ctrl-]right *)
3296 let o =
3297 if ctrl
3298 then (
3299 G.postRedisplay "outline ctrl right";
3300 {< m_pan = m_pan + 1 >}
3302 else self#updownlevel 1
3304 coe o
3306 | 0xff51 -> (* [ctrl-]left *)
3307 let o =
3308 if ctrl
3309 then (
3310 G.postRedisplay "outline ctrl left";
3311 {< m_pan = m_pan - 1 >}
3313 else self#updownlevel ~-1
3315 coe o
3317 | 0xff50 -> (* home *)
3318 G.postRedisplay "outline home";
3319 coe {< m_first = 0; m_active = 0 >}
3321 | 0xff57 -> (* end *)
3322 let active = source#getitemcount - 1 in
3323 let first = max 0 (active - fstate.maxrows) in
3324 G.postRedisplay "outline end";
3325 coe {< m_active = active; m_first = first >}
3327 | _ -> super#key key mask
3330 let outlinesource usebookmarks =
3331 let empty = [||] in
3332 (object
3333 inherit lvsourcebase
3334 val mutable m_items = empty
3335 val mutable m_orig_items = empty
3336 val mutable m_prev_items = empty
3337 val mutable m_narrow_pattern = ""
3338 val mutable m_hadremovals = false
3340 method getitemcount =
3341 Array.length m_items + (if m_hadremovals then 1 else 0)
3343 method getitem n =
3344 if n == Array.length m_items && m_hadremovals
3345 then
3346 ("[Confirm removal]", 0)
3347 else
3348 let s, n, _ = m_items.(n) in
3349 (s, n)
3351 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3352 ignore (uioh, first, qsearch);
3353 let confrimremoval = m_hadremovals && active = Array.length m_items in
3354 let items =
3355 if String.length m_narrow_pattern = 0
3356 then m_orig_items
3357 else m_items
3359 if not cancel
3360 then (
3361 if not confrimremoval
3362 then(
3363 let _, _, anchor = m_items.(active) in
3364 gotoanchor anchor;
3365 m_items <- items;
3367 else (
3368 state.bookmarks <- Array.to_list m_items;
3369 m_orig_items <- m_items;
3372 else m_items <- items;
3373 m_pan <- pan;
3374 None
3376 method hasaction _ = true
3378 method greetmsg =
3379 if Array.length m_items != Array.length m_orig_items
3380 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3381 else ""
3383 method narrow pattern =
3384 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3385 match reopt with
3386 | None -> ()
3387 | Some re ->
3388 let rec loop accu n =
3389 if n = -1
3390 then (
3391 m_narrow_pattern <- pattern;
3392 m_items <- Array.of_list accu
3394 else
3395 let (s, _, _) as o = m_items.(n) in
3396 let accu =
3397 if (try ignore (Str.search_forward re s 0); true
3398 with Not_found -> false)
3399 then o :: accu
3400 else accu
3402 loop accu (n-1)
3404 loop [] (Array.length m_items - 1)
3406 method denarrow =
3407 m_orig_items <- (
3408 if usebookmarks
3409 then Array.of_list state.bookmarks
3410 else state.outlines
3412 m_items <- m_orig_items
3414 method remove m =
3415 if usebookmarks
3416 then
3417 if m >= 0 && m < Array.length m_items
3418 then (
3419 m_hadremovals <- true;
3420 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3421 let n = if n >= m then n+1 else n in
3422 m_items.(n)
3426 method reset anchor items =
3427 m_hadremovals <- false;
3428 if m_orig_items == empty || m_prev_items != items
3429 then (
3430 m_orig_items <- items;
3431 if String.length m_narrow_pattern = 0
3432 then m_items <- items;
3434 m_prev_items <- items;
3435 let rely = getanchory anchor in
3436 let active =
3437 let rec loop n best bestd =
3438 if n = Array.length m_items
3439 then best
3440 else
3441 let (_, _, anchor) = m_items.(n) in
3442 let orely = getanchory anchor in
3443 let d = abs (orely - rely) in
3444 if d < bestd
3445 then loop (n+1) n d
3446 else loop (n+1) best bestd
3448 loop 0 ~-1 max_int
3450 m_active <- active;
3451 m_first <- firstof m_first active
3452 end)
3455 let enterselector usebookmarks =
3456 let source = outlinesource usebookmarks in
3457 fun errmsg ->
3458 let outlines =
3459 if usebookmarks
3460 then Array.of_list state.bookmarks
3461 else state.outlines
3463 if Array.length outlines = 0
3464 then (
3465 showtext ' ' errmsg;
3467 else (
3468 state.text <- source#greetmsg;
3469 Wsi.setcursor Wsi.CURSOR_INHERIT;
3470 let anchor = getanchor () in
3471 source#reset anchor outlines;
3472 state.uioh <- coe (new outlinelistview ~source);
3473 G.postRedisplay "enter selector";
3477 let enteroutlinemode =
3478 let f = enterselector false in
3479 fun ()-> f "Document has no outline";
3482 let enterbookmarkmode =
3483 let f = enterselector true in
3484 fun () -> f "Document has no bookmarks (yet)";
3487 let color_of_string s =
3488 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3489 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3493 let color_to_string (r, g, b) =
3494 let r = truncate (r *. 256.0)
3495 and g = truncate (g *. 256.0)
3496 and b = truncate (b *. 256.0) in
3497 Printf.sprintf "%d/%d/%d" r g b
3500 let irect_of_string s =
3501 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3504 let irect_to_string (x0,y0,x1,y1) =
3505 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3508 let makecheckers () =
3509 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3510 following to say:
3511 converted by Issac Trotts. July 25, 2002 *)
3512 let image_height = 64
3513 and image_width = 64 in
3515 let make_image () =
3516 let image =
3517 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3519 for i = 0 to image_width - 1 do
3520 for j = 0 to image_height - 1 do
3521 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3522 (if (i land 8 ) lxor (j land 8) = 0
3523 then [|255;255;255|] else [|200;200;200|])
3524 done
3525 done;
3526 image
3528 let image = make_image () in
3529 let id = GlTex.gen_texture () in
3530 GlTex.bind_texture `texture_2d id;
3531 GlPix.store (`unpack_alignment 1);
3532 GlTex.image2d image;
3533 List.iter (GlTex.parameter ~target:`texture_2d)
3534 [ `wrap_s `repeat;
3535 `wrap_t `repeat;
3536 `mag_filter `nearest;
3537 `min_filter `nearest ];
3541 let setcheckers enabled =
3542 match state.texid with
3543 | None ->
3544 if enabled then state.texid <- Some (makecheckers ())
3546 | Some texid ->
3547 if not enabled
3548 then (
3549 GlTex.delete_texture texid;
3550 state.texid <- None;
3554 let int_of_string_with_suffix s =
3555 let l = String.length s in
3556 let s1, shift =
3557 if l > 1
3558 then
3559 let suffix = Char.lowercase s.[l-1] in
3560 match suffix with
3561 | 'k' -> String.sub s 0 (l-1), 10
3562 | 'm' -> String.sub s 0 (l-1), 20
3563 | 'g' -> String.sub s 0 (l-1), 30
3564 | _ -> s, 0
3565 else s, 0
3567 let n = int_of_string s1 in
3568 let m = n lsl shift in
3569 if m < 0 || m < n
3570 then raise (Failure "value too large")
3571 else m
3574 let string_with_suffix_of_int n =
3575 if n = 0
3576 then "0"
3577 else
3578 let n, s =
3579 if n = 0
3580 then 0, ""
3581 else (
3582 if n land ((1 lsl 20) - 1) = 0
3583 then n lsr 20, "M"
3584 else (
3585 if n land ((1 lsl 10) - 1) = 0
3586 then n lsr 10, "K"
3587 else n, ""
3591 let rec loop s n =
3592 let h = n mod 1000 in
3593 let n = n / 1000 in
3594 if n = 0
3595 then string_of_int h ^ s
3596 else (
3597 let s = Printf.sprintf "_%03d%s" h s in
3598 loop s n
3601 loop "" n ^ s;
3604 let defghyllscroll = (40, 8, 32);;
3605 let ghyllscroll_of_string s =
3606 let (n, a, b) as nab =
3607 if s = "default"
3608 then defghyllscroll
3609 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3611 if n <= a || n <= b || a >= b
3612 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3613 nab;
3616 let ghyllscroll_to_string ((n, a, b) as nab) =
3617 if nab = defghyllscroll
3618 then "default"
3619 else Printf.sprintf "%d,%d,%d" n a b;
3622 let describe_location () =
3623 let f (fn, _) l =
3624 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3626 let fn, ln = List.fold_left f (-1, -1) state.layout in
3627 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3628 let percent =
3629 if maxy <= 0
3630 then 100.
3631 else (100. *. (float state.y /. float maxy))
3633 if fn = ln
3634 then
3635 Printf.sprintf "page %d of %d [%.2f%%]"
3636 (fn+1) state.pagecount percent
3637 else
3638 Printf.sprintf
3639 "pages %d-%d of %d [%.2f%%]"
3640 (fn+1) (ln+1) state.pagecount percent
3643 let enterinfomode =
3644 let btos b = if b then "\xe2\x88\x9a" else "" in
3645 let showextended = ref false in
3646 let leave mode = function
3647 | Confirm -> state.mode <- mode
3648 | Cancel -> state.mode <- mode in
3649 let src =
3650 (object
3651 val mutable m_first_time = true
3652 val mutable m_l = []
3653 val mutable m_a = [||]
3654 val mutable m_prev_uioh = nouioh
3655 val mutable m_prev_mode = View
3657 inherit lvsourcebase
3659 method reset prev_mode prev_uioh =
3660 m_a <- Array.of_list (List.rev m_l);
3661 m_l <- [];
3662 m_prev_mode <- prev_mode;
3663 m_prev_uioh <- prev_uioh;
3664 if m_first_time
3665 then (
3666 let rec loop n =
3667 if n >= Array.length m_a
3668 then ()
3669 else
3670 match m_a.(n) with
3671 | _, _, _, Action _ -> m_active <- n
3672 | _ -> loop (n+1)
3674 loop 0;
3675 m_first_time <- false;
3678 method int name get set =
3679 m_l <-
3680 (name, `int get, 1, Action (
3681 fun u ->
3682 let ondone s =
3683 try set (int_of_string s)
3684 with exn ->
3685 state.text <- Printf.sprintf "bad integer `%s': %s"
3686 s (Printexc.to_string exn)
3688 state.text <- "";
3689 let te = name ^ ": ", "", None, intentry, ondone in
3690 state.mode <- Textentry (te, leave m_prev_mode);
3692 )) :: m_l
3694 method int_with_suffix name get set =
3695 m_l <-
3696 (name, `intws get, 1, Action (
3697 fun u ->
3698 let ondone s =
3699 try set (int_of_string_with_suffix s)
3700 with exn ->
3701 state.text <- Printf.sprintf "bad integer `%s': %s"
3702 s (Printexc.to_string exn)
3704 state.text <- "";
3705 let te =
3706 name ^ ": ", "", None, intentry_with_suffix, ondone
3708 state.mode <- Textentry (te, leave m_prev_mode);
3710 )) :: m_l
3712 method bool ?(offset=1) ?(btos=btos) name get set =
3713 m_l <-
3714 (name, `bool (btos, get), offset, Action (
3715 fun u ->
3716 let v = get () in
3717 set (not v);
3719 )) :: m_l
3721 method color name get set =
3722 m_l <-
3723 (name, `color get, 1, Action (
3724 fun u ->
3725 let invalid = (nan, nan, nan) in
3726 let ondone s =
3727 let c =
3728 try color_of_string s
3729 with exn ->
3730 state.text <- Printf.sprintf "bad color `%s': %s"
3731 s (Printexc.to_string exn);
3732 invalid
3734 if c <> invalid
3735 then set c;
3737 let te = name ^ ": ", "", None, textentry, ondone in
3738 state.text <- color_to_string (get ());
3739 state.mode <- Textentry (te, leave m_prev_mode);
3741 )) :: m_l
3743 method string name get set =
3744 m_l <-
3745 (name, `string get, 1, Action (
3746 fun u ->
3747 let ondone s = set s in
3748 let te = name ^ ": ", "", None, textentry, ondone in
3749 state.mode <- Textentry (te, leave m_prev_mode);
3751 )) :: m_l
3753 method colorspace name get set =
3754 m_l <-
3755 (name, `string get, 1, Action (
3756 fun _ ->
3757 let source =
3758 let vals = [| "rgb"; "bgr"; "gray" |] in
3759 (object
3760 inherit lvsourcebase
3762 initializer
3763 m_active <- int_of_colorspace conf.colorspace;
3764 m_first <- 0;
3766 method getitemcount = Array.length vals
3767 method getitem n = (vals.(n), 0)
3768 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3769 ignore (uioh, first, pan, qsearch);
3770 if not cancel then set active;
3771 None
3772 method hasaction _ = true
3773 end)
3775 state.text <- "";
3776 let modehash = findkeyhash conf "info" in
3777 coe (new listview ~source ~trusted:true ~modehash)
3778 )) :: m_l
3780 method caption s offset =
3781 m_l <- (s, `empty, offset, Noaction) :: m_l
3783 method caption2 s f offset =
3784 m_l <- (s, `string f, offset, Noaction) :: m_l
3786 method getitemcount = Array.length m_a
3788 method getitem n =
3789 let tostr = function
3790 | `int f -> string_of_int (f ())
3791 | `intws f -> string_with_suffix_of_int (f ())
3792 | `string f -> f ()
3793 | `color f -> color_to_string (f ())
3794 | `bool (btos, f) -> btos (f ())
3795 | `empty -> ""
3797 let name, t, offset, _ = m_a.(n) in
3798 ((let s = tostr t in
3799 if String.length s > 0
3800 then Printf.sprintf "%s\t%s" name s
3801 else name),
3802 offset)
3804 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3805 let uiohopt =
3806 if not cancel
3807 then (
3808 m_qsearch <- qsearch;
3809 let uioh =
3810 match m_a.(active) with
3811 | _, _, _, Action f -> f uioh
3812 | _ -> uioh
3814 Some uioh
3816 else None
3818 m_active <- active;
3819 m_first <- first;
3820 m_pan <- pan;
3821 uiohopt
3823 method hasaction n =
3824 match m_a.(n) with
3825 | _, _, _, Action _ -> true
3826 | _ -> false
3827 end)
3829 let rec fillsrc prevmode prevuioh =
3830 let sep () = src#caption "" 0 in
3831 let colorp name get set =
3832 src#string name
3833 (fun () -> color_to_string (get ()))
3834 (fun v ->
3836 let c = color_of_string v in
3837 set c
3838 with exn ->
3839 state.text <- Printf.sprintf "bad color `%s': %s"
3840 v (Printexc.to_string exn);
3843 let oldmode = state.mode in
3844 let birdseye = isbirdseye state.mode in
3846 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3848 src#bool "presentation mode"
3849 (fun () -> conf.presentation)
3850 (fun v ->
3851 conf.presentation <- v;
3852 state.anchor <- getanchor ();
3853 represent ());
3855 src#bool "ignore case in searches"
3856 (fun () -> conf.icase)
3857 (fun v -> conf.icase <- v);
3859 src#bool "preload"
3860 (fun () -> conf.preload)
3861 (fun v -> conf.preload <- v);
3863 src#bool "highlight links"
3864 (fun () -> conf.hlinks)
3865 (fun v -> conf.hlinks <- v);
3867 src#bool "under info"
3868 (fun () -> conf.underinfo)
3869 (fun v -> conf.underinfo <- v);
3871 src#bool "persistent bookmarks"
3872 (fun () -> conf.savebmarks)
3873 (fun v -> conf.savebmarks <- v);
3875 src#bool "proportional display"
3876 (fun () -> conf.proportional)
3877 (fun v -> reqlayout conf.angle v);
3879 src#bool "trim margins"
3880 (fun () -> conf.trimmargins)
3881 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3883 src#bool "persistent location"
3884 (fun () -> conf.jumpback)
3885 (fun v -> conf.jumpback <- v);
3887 sep ();
3888 src#int "inter-page space"
3889 (fun () -> conf.interpagespace)
3890 (fun n ->
3891 conf.interpagespace <- n;
3892 let pageno, py =
3893 match state.layout with
3894 | [] -> 0, 0
3895 | l :: _ ->
3896 l.pageno, l.pagey
3898 state.maxy <- calcheight ();
3899 let y = getpagey pageno in
3900 gotoy (y + py)
3903 src#int "page bias"
3904 (fun () -> conf.pagebias)
3905 (fun v -> conf.pagebias <- v);
3907 src#int "scroll step"
3908 (fun () -> conf.scrollstep)
3909 (fun n -> conf.scrollstep <- n);
3911 src#int "auto scroll step"
3912 (fun () ->
3913 match state.autoscroll with
3914 | Some step -> step
3915 | _ -> conf.autoscrollstep)
3916 (fun n ->
3917 if state.autoscroll <> None
3918 then state.autoscroll <- Some n;
3919 conf.autoscrollstep <- n);
3921 src#int "zoom"
3922 (fun () -> truncate (conf.zoom *. 100.))
3923 (fun v -> setzoom ((float v) /. 100.));
3925 src#int "rotation"
3926 (fun () -> conf.angle)
3927 (fun v -> reqlayout v conf.proportional);
3929 src#int "scroll bar width"
3930 (fun () -> state.scrollw)
3931 (fun v ->
3932 state.scrollw <- v;
3933 conf.scrollbw <- v;
3934 reshape conf.winw conf.winh;
3937 src#int "scroll handle height"
3938 (fun () -> conf.scrollh)
3939 (fun v -> conf.scrollh <- v;);
3941 src#int "thumbnail width"
3942 (fun () -> conf.thumbw)
3943 (fun v ->
3944 conf.thumbw <- min 4096 v;
3945 match oldmode with
3946 | Birdseye beye ->
3947 leavebirdseye beye false;
3948 enterbirdseye ()
3949 | _ -> ()
3952 src#string "columns"
3953 (fun () ->
3954 match conf.columns with
3955 | None -> "1"
3956 | Some (multicol, _) -> columns_to_string multicol)
3957 (fun v ->
3958 let n, a, b = columns_of_string v in
3959 setcolumns n a b);
3961 sep ();
3962 src#caption "Presentation mode" 0;
3963 src#bool "scrollbar visible"
3964 (fun () -> conf.scrollbarinpm)
3965 (fun v ->
3966 if v != conf.scrollbarinpm
3967 then (
3968 conf.scrollbarinpm <- v;
3969 if conf.presentation
3970 then (
3971 state.scrollw <- if v then conf.scrollbw else 0;
3972 reshape conf.winw conf.winh;
3977 sep ();
3978 src#caption "Pixmap cache" 0;
3979 src#int_with_suffix "size (advisory)"
3980 (fun () -> conf.memlimit)
3981 (fun v -> conf.memlimit <- v);
3983 src#caption2 "used"
3984 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3985 (string_with_suffix_of_int state.memused)
3986 (Hashtbl.length state.tilemap)) 1;
3988 sep ();
3989 src#caption "Layout" 0;
3990 src#caption2 "Dimension"
3991 (fun () ->
3992 Printf.sprintf "%dx%d (virtual %dx%d)"
3993 conf.winw conf.winh
3994 state.w state.maxy)
3996 if conf.debug
3997 then
3998 src#caption2 "Position" (fun () ->
3999 Printf.sprintf "%dx%d" state.x state.y
4001 else
4002 src#caption2 "Visible" (fun () -> describe_location ()) 1
4005 sep ();
4006 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4007 "Save these parameters as global defaults at exit"
4008 (fun () -> conf.bedefault)
4009 (fun v -> conf.bedefault <- v)
4012 sep ();
4013 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4014 src#bool ~offset:0 ~btos "Extended parameters"
4015 (fun () -> !showextended)
4016 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4017 if !showextended
4018 then (
4019 src#bool "checkers"
4020 (fun () -> conf.checkers)
4021 (fun v -> conf.checkers <- v; setcheckers v);
4022 src#bool "update cursor"
4023 (fun () -> conf.updatecurs)
4024 (fun v -> conf.updatecurs <- v);
4025 src#bool "verbose"
4026 (fun () -> conf.verbose)
4027 (fun v -> conf.verbose <- v);
4028 src#bool "invert colors"
4029 (fun () -> conf.invert)
4030 (fun v -> conf.invert <- v);
4031 src#bool "max fit"
4032 (fun () -> conf.maxhfit)
4033 (fun v -> conf.maxhfit <- v);
4034 src#bool "redirect stderr"
4035 (fun () -> conf.redirectstderr)
4036 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4037 src#string "uri launcher"
4038 (fun () -> conf.urilauncher)
4039 (fun v -> conf.urilauncher <- v);
4040 src#string "path launcher"
4041 (fun () -> conf.pathlauncher)
4042 (fun v -> conf.pathlauncher <- v);
4043 src#string "tile size"
4044 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4045 (fun v ->
4047 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4048 conf.tileh <- max 64 w;
4049 conf.tilew <- max 64 h;
4050 flushtiles ();
4051 with exn ->
4052 state.text <- Printf.sprintf "bad tile size `%s': %s"
4053 v (Printexc.to_string exn));
4054 src#int "texture count"
4055 (fun () -> conf.texcount)
4056 (fun v ->
4057 if realloctexts v
4058 then conf.texcount <- v
4059 else showtext '!' " Failed to set texture count please retry later"
4061 src#int "slice height"
4062 (fun () -> conf.sliceheight)
4063 (fun v ->
4064 conf.sliceheight <- v;
4065 wcmd "sliceh %d" conf.sliceheight;
4067 src#int "anti-aliasing level"
4068 (fun () -> conf.aalevel)
4069 (fun v ->
4070 conf.aalevel <- bound v 0 8;
4071 state.anchor <- getanchor ();
4072 opendoc state.path state.password;
4074 src#int "ui font size"
4075 (fun () -> fstate.fontsize)
4076 (fun v -> setfontsize (bound v 5 100));
4077 colorp "background color"
4078 (fun () -> conf.bgcolor)
4079 (fun v -> conf.bgcolor <- v);
4080 src#bool "crop hack"
4081 (fun () -> conf.crophack)
4082 (fun v -> conf.crophack <- v);
4083 src#string "trim fuzz"
4084 (fun () -> irect_to_string conf.trimfuzz)
4085 (fun v ->
4087 conf.trimfuzz <- irect_of_string v;
4088 if conf.trimmargins
4089 then settrim true conf.trimfuzz;
4090 with exn ->
4091 state.text <- Printf.sprintf "bad irect `%s': %s"
4092 v (Printexc.to_string exn)
4094 src#string "throttle"
4095 (fun () ->
4096 match conf.maxwait with
4097 | None -> "show place holder if page is not ready"
4098 | Some time ->
4099 if time = infinity
4100 then "wait for page to fully render"
4101 else
4102 "wait " ^ string_of_float time
4103 ^ " seconds before showing placeholder"
4105 (fun v ->
4107 let f = float_of_string v in
4108 if f <= 0.0
4109 then conf.maxwait <- None
4110 else conf.maxwait <- Some f
4111 with exn ->
4112 state.text <- Printf.sprintf "bad time `%s': %s"
4113 v (Printexc.to_string exn)
4115 src#string "ghyll scroll"
4116 (fun () ->
4117 match conf.ghyllscroll with
4118 | None -> ""
4119 | Some nab -> ghyllscroll_to_string nab
4121 (fun v ->
4123 let gs =
4124 if String.length v = 0
4125 then None
4126 else Some (ghyllscroll_of_string v)
4128 conf.ghyllscroll <- gs
4129 with exn ->
4130 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4131 v (Printexc.to_string exn)
4133 src#string "selection command"
4134 (fun () -> conf.selcmd)
4135 (fun v -> conf.selcmd <- v);
4136 src#colorspace "color space"
4137 (fun () -> colorspace_to_string conf.colorspace)
4138 (fun v ->
4139 conf.colorspace <- colorspace_of_int v;
4140 wcmd "cs %d" v;
4141 load state.layout;
4145 sep ();
4146 src#caption "Document" 0;
4147 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4148 src#caption2 "Pages"
4149 (fun () -> string_of_int state.pagecount) 1;
4150 src#caption2 "Dimensions"
4151 (fun () -> string_of_int (List.length state.pdims)) 1;
4152 if conf.trimmargins
4153 then (
4154 sep ();
4155 src#caption "Trimmed margins" 0;
4156 src#caption2 "Dimensions"
4157 (fun () -> string_of_int (List.length state.pdims)) 1;
4160 src#reset prevmode prevuioh;
4162 fun () ->
4163 state.text <- "";
4164 let prevmode = state.mode
4165 and prevuioh = state.uioh in
4166 fillsrc prevmode prevuioh;
4167 let source = (src :> lvsource) in
4168 let modehash = findkeyhash conf "info" in
4169 state.uioh <- coe (object (self)
4170 inherit listview ~source ~trusted:true ~modehash as super
4171 val mutable m_prevmemused = 0
4172 method infochanged = function
4173 | Memused ->
4174 if m_prevmemused != state.memused
4175 then (
4176 m_prevmemused <- state.memused;
4177 G.postRedisplay "memusedchanged";
4179 | Pdim -> G.postRedisplay "pdimchanged"
4180 | Docinfo -> fillsrc prevmode prevuioh
4182 method key key mask =
4183 if not (Wsi.withctrl mask)
4184 then
4185 match key with
4186 | 0xff51 -> coe (self#updownlevel ~-1)
4187 | 0xff53 -> coe (self#updownlevel 1)
4188 | _ -> super#key key mask
4189 else super#key key mask
4190 end);
4191 G.postRedisplay "info";
4194 let enterhelpmode =
4195 let source =
4196 (object
4197 inherit lvsourcebase
4198 method getitemcount = Array.length state.help
4199 method getitem n =
4200 let s, n, _ = state.help.(n) in
4201 (s, n)
4203 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4204 let optuioh =
4205 if not cancel
4206 then (
4207 m_qsearch <- qsearch;
4208 match state.help.(active) with
4209 | _, _, Action f -> Some (f uioh)
4210 | _ -> Some (uioh)
4212 else None
4214 m_active <- active;
4215 m_first <- first;
4216 m_pan <- pan;
4217 optuioh
4219 method hasaction n =
4220 match state.help.(n) with
4221 | _, _, Action _ -> true
4222 | _ -> false
4224 initializer
4225 m_active <- -1
4226 end)
4227 in fun () ->
4228 let modehash = findkeyhash conf "help" in
4229 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4230 G.postRedisplay "help";
4233 let entermsgsmode =
4234 let msgsource =
4235 let re = Str.regexp "[\r\n]" in
4236 (object
4237 inherit lvsourcebase
4238 val mutable m_items = [||]
4240 method getitemcount = 1 + Array.length m_items
4242 method getitem n =
4243 if n = 0
4244 then "[Clear]", 0
4245 else m_items.(n-1), 0
4247 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4248 ignore uioh;
4249 if not cancel
4250 then (
4251 if active = 0
4252 then Buffer.clear state.errmsgs;
4253 m_qsearch <- qsearch;
4255 m_active <- active;
4256 m_first <- first;
4257 m_pan <- pan;
4258 None
4260 method hasaction n =
4261 n = 0
4263 method reset =
4264 state.newerrmsgs <- false;
4265 let l = Str.split re (Buffer.contents state.errmsgs) in
4266 m_items <- Array.of_list l
4268 initializer
4269 m_active <- 0
4270 end)
4271 in fun () ->
4272 state.text <- "";
4273 msgsource#reset;
4274 let source = (msgsource :> lvsource) in
4275 let modehash = findkeyhash conf "listview" in
4276 state.uioh <- coe (object
4277 inherit listview ~source ~trusted:false ~modehash as super
4278 method display =
4279 if state.newerrmsgs
4280 then msgsource#reset;
4281 super#display
4282 end);
4283 G.postRedisplay "msgs";
4286 let quickbookmark ?title () =
4287 match state.layout with
4288 | [] -> ()
4289 | l :: _ ->
4290 let title =
4291 match title with
4292 | None ->
4293 let sec = Unix.gettimeofday () in
4294 let tm = Unix.localtime sec in
4295 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4296 (l.pageno+1)
4297 tm.Unix.tm_mday
4298 tm.Unix.tm_mon
4299 (tm.Unix.tm_year + 1900)
4300 tm.Unix.tm_hour
4301 tm.Unix.tm_min
4302 | Some title -> title
4304 state.bookmarks <-
4305 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4306 :: state.bookmarks
4309 let doreshape w h =
4310 state.fullscreen <- None;
4311 Wsi.reshape w h;
4314 let setautoscrollspeed step goingdown =
4315 let incr = max 1 ((abs step) / 2) in
4316 let incr = if goingdown then incr else -incr in
4317 let astep = step + incr in
4318 state.autoscroll <- Some astep;
4321 let gotounder = function
4322 | Ulinkgoto (pageno, top) ->
4323 if pageno >= 0
4324 then (
4325 addnav ();
4326 gotopage1 pageno top;
4329 | Ulinkuri s ->
4330 gotouri s
4332 | Uremote (filename, pageno) ->
4333 let path =
4334 if Sys.file_exists filename
4335 then filename
4336 else
4337 let dir = Filename.dirname state.path in
4338 let path = Filename.concat dir filename in
4339 if Sys.file_exists path
4340 then path
4341 else ""
4343 if String.length path > 0
4344 then (
4345 let anchor = getanchor () in
4346 let ranchor = state.path, state.password, anchor in
4347 state.anchor <- (pageno, 0.0);
4348 state.ranchors <- ranchor :: state.ranchors;
4349 opendoc path "";
4351 else showtext '!' ("Could not find " ^ filename)
4353 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4356 let viewkeyboard key mask =
4357 let enttext te =
4358 let mode = state.mode in
4359 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4360 state.text <- "";
4361 enttext ();
4362 G.postRedisplay "view:enttext"
4364 let ctrl = Wsi.withctrl mask in
4365 match key with
4366 | 81 -> (* Q *)
4367 exit 0
4369 | 0xff63 -> (* insert *)
4370 if conf.angle mod 360 = 0
4371 then (
4372 state.mode <- LinkNav (Ltgendir 0);
4373 gotoy state.y;
4375 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4377 | 0xff1b | 113 -> (* escape / q *)
4378 begin match state.mstate with
4379 | Mzoomrect _ ->
4380 state.mstate <- Mnone;
4381 Wsi.setcursor Wsi.CURSOR_INHERIT;
4382 G.postRedisplay "kill zoom rect";
4383 | _ ->
4384 match state.ranchors with
4385 | [] -> raise Quit
4386 | (path, password, anchor) :: rest ->
4387 state.ranchors <- rest;
4388 state.anchor <- anchor;
4389 opendoc path password
4390 end;
4392 | 0xff08 -> (* backspace *)
4393 let y = getnav ~-1 in
4394 gotoy_and_clear_text y
4396 | 111 -> (* o *)
4397 enteroutlinemode ()
4399 | 117 -> (* u *)
4400 state.rects <- [];
4401 state.text <- "";
4402 G.postRedisplay "dehighlight";
4404 | 47 | 63 -> (* / ? *)
4405 let ondone isforw s =
4406 cbput state.hists.pat s;
4407 state.searchpattern <- s;
4408 search s isforw
4410 let s = String.create 1 in
4411 s.[0] <- Char.chr key;
4412 enttext (s, "", Some (onhist state.hists.pat),
4413 textentry, ondone (key = 47))
4415 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4416 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4417 setzoom (conf.zoom +. incr)
4419 | 43 | 0xffab -> (* + *)
4420 let ondone s =
4421 let n =
4422 try int_of_string s with exc ->
4423 state.text <- Printf.sprintf "bad integer `%s': %s"
4424 s (Printexc.to_string exc);
4425 max_int
4427 if n != max_int
4428 then (
4429 conf.pagebias <- n;
4430 state.text <- "page bias is now " ^ string_of_int n;
4433 enttext ("page bias: ", "", None, intentry, ondone)
4435 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4436 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4437 setzoom (max 0.01 (conf.zoom -. decr))
4439 | 45 | 0xffad -> (* - *)
4440 let ondone msg = state.text <- msg in
4441 enttext (
4442 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4443 optentry state.mode, ondone
4446 | 48 when ctrl -> (* ctrl-0 *)
4447 setzoom 1.0
4449 | 49 when ctrl -> (* 1 *)
4450 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4451 if zoom < 1.0
4452 then setzoom zoom
4454 | 0xffc6 -> (* f9 *)
4455 togglebirdseye ()
4457 | 57 when ctrl -> (* ctrl-9 *)
4458 togglebirdseye ()
4460 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4461 when not ctrl -> (* 0..9 *)
4462 let ondone s =
4463 let n =
4464 try int_of_string s with exc ->
4465 state.text <- Printf.sprintf "bad integer `%s': %s"
4466 s (Printexc.to_string exc);
4469 if n >= 0
4470 then (
4471 addnav ();
4472 cbput state.hists.pag (string_of_int n);
4473 gotopage1 (n + conf.pagebias - 1) 0;
4476 let pageentry text key =
4477 match Char.unsafe_chr key with
4478 | 'g' -> TEdone text
4479 | _ -> intentry text key
4481 let text = "x" in text.[0] <- Char.chr key;
4482 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4484 | 98 -> (* b *)
4485 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4486 reshape conf.winw conf.winh;
4488 | 108 -> (* l *)
4489 conf.hlinks <- not conf.hlinks;
4490 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4491 G.postRedisplay "toggle highlightlinks";
4493 | 70 -> (* F *)
4494 state.glinks <- true;
4495 let ondone s =
4496 let n =
4497 try int_of_string s with exc ->
4498 state.text <- Printf.sprintf "bad integer `%s': %s"
4499 s (Printexc.to_string exc);
4502 if n >= 0
4503 then (
4504 let rec loop n = function
4505 | [] -> ()
4506 | l :: rest ->
4507 match getopaque l.pageno with
4508 | None -> loop n rest
4509 | Some opaque ->
4510 let m = getlinkcount opaque in
4511 if n < m
4512 then (
4513 let under = getlink opaque n in
4514 addnav ();
4515 gotounder under;
4517 else loop (n-m) rest
4519 loop n state.layout;
4522 let onkey text key =
4523 match Char.unsafe_chr key with
4524 | 'g' -> TEdone text
4525 | _ -> intentry text key
4527 let mode = state.mode in
4528 state.mode <- Textentry (
4529 (":", "", Some (onhist state.hists.pag), onkey, ondone),
4530 fun _ ->
4531 state.glinks <- false;
4532 state.mode <- mode
4534 state.text <- "";
4535 G.postRedisplay "view:enttext"
4537 | 97 -> (* a *)
4538 begin match state.autoscroll with
4539 | Some step ->
4540 conf.autoscrollstep <- step;
4541 state.autoscroll <- None
4542 | None ->
4543 if conf.autoscrollstep = 0
4544 then state.autoscroll <- Some 1
4545 else state.autoscroll <- Some conf.autoscrollstep
4548 | 112 when ctrl -> (* ctrl-p *)
4549 launchpath ()
4551 | 80 -> (* P *)
4552 conf.presentation <- not conf.presentation;
4553 if conf.presentation
4554 then (
4555 if not conf.scrollbarinpm
4556 then state.scrollw <- 0;
4558 else
4559 state.scrollw <- conf.scrollbw;
4561 showtext ' ' ("presentation mode " ^
4562 if conf.presentation then "on" else "off");
4563 state.anchor <- getanchor ();
4564 represent ()
4566 | 102 -> (* f *)
4567 begin match state.fullscreen with
4568 | None ->
4569 state.fullscreen <- Some (conf.winw, conf.winh);
4570 Wsi.fullscreen ()
4571 | Some (w, h) ->
4572 state.fullscreen <- None;
4573 doreshape w h
4576 | 103 -> (* g *)
4577 gotoy_and_clear_text 0
4579 | 71 -> (* G *)
4580 gotopage1 (state.pagecount - 1) 0
4582 | 112 | 78 -> (* p|N *)
4583 search state.searchpattern false
4585 | 110 | 0xffc0 -> (* n|F3 *)
4586 search state.searchpattern true
4588 | 116 -> (* t *)
4589 begin match state.layout with
4590 | [] -> ()
4591 | l :: _ ->
4592 gotoy_and_clear_text (getpagey l.pageno)
4595 | 32 -> (* ' ' *)
4596 begin match List.rev state.layout with
4597 | [] -> ()
4598 | l :: _ ->
4599 let pageno = min (l.pageno+1) (state.pagecount-1) in
4600 gotoy_and_clear_text (getpagey pageno)
4603 | 0xff9f | 0xffff -> (* delete *)
4604 begin match state.layout with
4605 | [] -> ()
4606 | l :: _ ->
4607 let pageno = max 0 (l.pageno-1) in
4608 gotoy_and_clear_text (getpagey pageno)
4611 | 61 -> (* = *)
4612 showtext ' ' (describe_location ());
4614 | 119 -> (* w *)
4615 begin match state.layout with
4616 | [] -> ()
4617 | l :: _ ->
4618 doreshape (l.pagew + state.scrollw) l.pageh;
4619 G.postRedisplay "w"
4622 | 39 -> (* ' *)
4623 enterbookmarkmode ()
4625 | 104 | 0xffbe -> (* h|F1 *)
4626 enterhelpmode ()
4628 | 105 -> (* i *)
4629 enterinfomode ()
4631 | 101 when conf.redirectstderr -> (* e *)
4632 entermsgsmode ()
4634 | 109 -> (* m *)
4635 let ondone s =
4636 match state.layout with
4637 | l :: _ ->
4638 state.bookmarks <-
4639 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4640 :: state.bookmarks
4641 | _ -> ()
4643 enttext ("bookmark: ", "", None, textentry, ondone)
4645 | 126 -> (* ~ *)
4646 quickbookmark ();
4647 showtext ' ' "Quick bookmark added";
4649 | 122 -> (* z *)
4650 begin match state.layout with
4651 | l :: _ ->
4652 let rect = getpdimrect l.pagedimno in
4653 let w, h =
4654 if conf.crophack
4655 then
4656 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4657 truncate (1.2 *. (rect.(3) -. rect.(0))))
4658 else
4659 (truncate (rect.(1) -. rect.(0)),
4660 truncate (rect.(3) -. rect.(0)))
4662 let w = truncate ((float w)*.conf.zoom)
4663 and h = truncate ((float h)*.conf.zoom) in
4664 if w != 0 && h != 0
4665 then (
4666 state.anchor <- getanchor ();
4667 doreshape (w + state.scrollw) (h + conf.interpagespace)
4669 G.postRedisplay "z";
4671 | [] -> ()
4674 | 50 when ctrl -> (* ctrl-2 *)
4675 let maxw = getmaxw () in
4676 if maxw > 0.0
4677 then setzoom (maxw /. float conf.winw)
4679 | 60 | 62 -> (* < > *)
4680 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
4682 | 91 | 93 -> (* [ ] *)
4683 conf.colorscale <-
4684 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4686 G.postRedisplay "brightness";
4688 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
4689 setzoom state.prevzoom
4691 | 107 | 0xff52 -> (* k up *)
4692 begin match state.autoscroll with
4693 | None ->
4694 begin match state.mode with
4695 | Birdseye beye -> upbirdseye 1 beye
4696 | _ ->
4697 if ctrl
4698 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
4699 else gotoy_and_clear_text (clamp (-conf.scrollstep))
4701 | Some n ->
4702 setautoscrollspeed n false
4705 | 106 | 0xff54 -> (* j down *)
4706 begin match state.autoscroll with
4707 | None ->
4708 begin match state.mode with
4709 | Birdseye beye -> downbirdseye 1 beye
4710 | _ ->
4711 if ctrl
4712 then gotoy_and_clear_text (clamp (conf.winh/2))
4713 else gotoy_and_clear_text (clamp conf.scrollstep)
4715 | Some n ->
4716 setautoscrollspeed n true
4719 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
4720 if conf.zoom > 1.0
4721 then
4722 let dx =
4723 if ctrl
4724 then conf.winw / 2
4725 else 10
4727 let dx = if key = 0xff51 then dx else -dx in
4728 state.x <- state.x + dx;
4729 gotoy_and_clear_text state.y
4730 else (
4731 state.text <- "";
4732 G.postRedisplay "lef/right"
4735 | 0xff55 -> (* prior *)
4736 let y =
4737 if ctrl
4738 then
4739 match state.layout with
4740 | [] -> state.y
4741 | l :: _ -> state.y - l.pagey
4742 else
4743 clamp (-conf.winh)
4745 gotoghyll y
4747 | 0xff56 -> (* next *)
4748 let y =
4749 if ctrl
4750 then
4751 match List.rev state.layout with
4752 | [] -> state.y
4753 | l :: _ -> getpagey l.pageno
4754 else
4755 clamp conf.winh
4757 gotoghyll y
4759 | 0xff50 -> gotoghyll 0
4760 | 0xff57 -> gotoghyll (clamp state.maxy)
4761 | 0xff53 when Wsi.withalt mask ->
4762 gotoghyll (getnav ~-1)
4763 | 0xff51 when Wsi.withalt mask ->
4764 gotoghyll (getnav 1)
4766 | 114 -> (* r *)
4767 state.anchor <- getanchor ();
4768 opendoc state.path state.password
4770 | 118 when conf.debug -> (* v *)
4771 state.rects <- [];
4772 List.iter (fun l ->
4773 match getopaque l.pageno with
4774 | None -> ()
4775 | Some opaque ->
4776 let x0, y0, x1, y1 = pagebbox opaque in
4777 let a,b = float x0, float y0 in
4778 let c,d = float x1, float y0 in
4779 let e,f = float x1, float y1 in
4780 let h,j = float x0, float y1 in
4781 let rect = (a,b,c,d,e,f,h,j) in
4782 debugrect rect;
4783 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4784 ) state.layout;
4785 G.postRedisplay "v";
4787 | _ ->
4788 vlog "huh? %s" (Wsi.keyname key)
4791 let linknavkeyboard key mask linknav =
4792 let getpage pageno =
4793 let rec loop = function
4794 | [] -> None
4795 | l :: _ when l.pageno = pageno -> Some l
4796 | _ :: rest -> loop rest
4797 in loop state.layout
4799 let doexact (pageno, n) =
4800 match getopaque pageno, getpage pageno with
4801 | Some opaque, Some l ->
4802 if key = 0xff0d
4803 then
4804 let under = getlink opaque n in
4805 G.postRedisplay "link gotounder";
4806 gotounder under;
4807 state.mode <- View;
4808 else
4809 let opt, dir =
4810 match key with
4811 | 0xff50 -> (* home *)
4812 Some (findlink opaque LDfirst), -1
4814 | 0xff57 -> (* end *)
4815 Some (findlink opaque LDlast), 1
4817 | 0xff51 -> (* left *)
4818 Some (findlink opaque (LDleft n)), -1
4820 | 0xff53 -> (* right *)
4821 Some (findlink opaque (LDright n)), 1
4823 | 0xff52 -> (* up *)
4824 Some (findlink opaque (LDup n)), -1
4826 | 0xff54 -> (* down *)
4827 Some (findlink opaque (LDdown n)), 1
4829 | _ -> None, 0
4831 let pwl l dir =
4832 begin match findpwl l.pageno dir with
4833 | Pwlnotfound -> ()
4834 | Pwl pageno ->
4835 let notfound dir =
4836 state.mode <- LinkNav (Ltgendir dir);
4837 let y, h = getpageyh pageno in
4838 let y =
4839 if dir < 0
4840 then y + h - conf.winh
4841 else y
4843 gotoy y
4845 begin match getopaque pageno, getpage pageno with
4846 | Some opaque, Some _ ->
4847 let link =
4848 let ld = if dir > 0 then LDfirst else LDlast in
4849 findlink opaque ld
4851 begin match link with
4852 | Lfound m ->
4853 showlinktype (getlink opaque m);
4854 state.mode <- LinkNav (Ltexact (pageno, m));
4855 G.postRedisplay "linknav jpage";
4856 | _ -> notfound dir
4857 end;
4858 | _ -> notfound dir
4859 end;
4860 end;
4862 begin match opt with
4863 | Some Lnotfound -> pwl l dir;
4864 | Some (Lfound m) ->
4865 if m = n
4866 then pwl l dir
4867 else (
4868 let _, y0, _, y1 = getlinkrect opaque m in
4869 if y0 < l.pagey
4870 then gotopage1 l.pageno y0
4871 else (
4872 let d = fstate.fontsize + 1 in
4873 if y1 - l.pagey > l.pagevh - d
4874 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
4875 else G.postRedisplay "linknav";
4877 showlinktype (getlink opaque m);
4878 state.mode <- LinkNav (Ltexact (l.pageno, m));
4881 | None -> viewkeyboard key mask
4882 end;
4883 | _ -> viewkeyboard key mask
4885 if key = 0xff63
4886 then (
4887 state.mode <- View;
4888 G.postRedisplay "leave linknav"
4890 else
4891 match linknav with
4892 | Ltgendir _ -> viewkeyboard key mask
4893 | Ltexact exact -> doexact exact
4896 let keyboard key mask =
4897 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
4898 then wcmd "interrupt"
4899 else state.uioh <- state.uioh#key key mask
4902 let birdseyekeyboard key mask
4903 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
4904 let incr =
4905 match conf.columns with
4906 | None -> 1
4907 | Some ((c, _, _), _) -> c
4909 match key with
4910 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
4911 let y, h = getpageyh pageno in
4912 let top = (conf.winh - h) / 2 in
4913 gotoy (max 0 (y - top))
4914 | 0xff0d -> leavebirdseye beye false
4915 | 0xff1b -> leavebirdseye beye true (* escape *)
4916 | 0xff52 -> upbirdseye incr beye (* prior *)
4917 | 0xff54 -> downbirdseye incr beye (* next *)
4918 | 0xff51 -> upbirdseye 1 beye (* up *)
4919 | 0xff53 -> downbirdseye 1 beye (* down *)
4921 | 0xff55 ->
4922 begin match state.layout with
4923 | l :: _ ->
4924 if l.pagey != 0
4925 then (
4926 state.mode <- Birdseye (
4927 oconf, leftx, l.pageno, hooverpageno, anchor
4929 gotopage1 l.pageno 0;
4931 else (
4932 let layout = layout (state.y-conf.winh) conf.winh in
4933 match layout with
4934 | [] -> gotoy (clamp (-conf.winh))
4935 | l :: _ ->
4936 state.mode <- Birdseye (
4937 oconf, leftx, l.pageno, hooverpageno, anchor
4939 gotopage1 l.pageno 0
4942 | [] -> gotoy (clamp (-conf.winh))
4943 end;
4945 | 0xff56 ->
4946 begin match List.rev state.layout with
4947 | l :: _ ->
4948 let layout = layout (state.y + conf.winh) conf.winh in
4949 begin match layout with
4950 | [] ->
4951 let incr = l.pageh - l.pagevh in
4952 if incr = 0
4953 then (
4954 state.mode <-
4955 Birdseye (
4956 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4958 G.postRedisplay "birdseye pagedown";
4960 else gotoy (clamp (incr + conf.interpagespace*2));
4962 | l :: _ ->
4963 state.mode <-
4964 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4965 gotopage1 l.pageno 0;
4968 | [] -> gotoy (clamp conf.winh)
4969 end;
4971 | 0xff50 ->
4972 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4973 gotopage1 0 0
4975 | 0xff57 ->
4976 let pageno = state.pagecount - 1 in
4977 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4978 if not (pagevisible state.layout pageno)
4979 then
4980 let h =
4981 match List.rev state.pdims with
4982 | [] -> conf.winh
4983 | (_, _, h, _) :: _ -> h
4985 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4986 else G.postRedisplay "birdseye end";
4987 | _ -> viewkeyboard key mask
4990 let drawpage l linkindexbase =
4991 let color =
4992 match state.mode with
4993 | Textentry _ -> scalecolor 0.4
4994 | LinkNav _
4995 | View -> scalecolor 1.0
4996 | Birdseye (_, _, pageno, hooverpageno, _) ->
4997 if l.pageno = hooverpageno
4998 then scalecolor 0.9
4999 else (
5000 if l.pageno = pageno
5001 then scalecolor 1.0
5002 else scalecolor 0.8
5005 drawtiles l color;
5006 begin match getopaque l.pageno with
5007 | Some opaque ->
5008 if tileready l l.pagex l.pagey
5009 then
5010 let x = l.pagedispx - l.pagex
5011 and y = l.pagedispy - l.pagey in
5012 let hlmask = (if conf.hlinks then 1 else 0)
5013 + (if state.glinks && not (isbirdseye state.mode) then 2 else 0)
5015 postprocess opaque hlmask x y linkindexbase;
5016 else 0
5018 | _ -> 0
5019 end;
5022 let scrollindicator () =
5023 let sbw, ph, sh = state.uioh#scrollph in
5024 let sbh, pw, sw = state.uioh#scrollpw in
5026 GlDraw.color (0.64, 0.64, 0.64);
5027 GlDraw.rect
5028 (float (conf.winw - sbw), 0.)
5029 (float conf.winw, float conf.winh)
5031 GlDraw.rect
5032 (0., float (conf.winh - sbh))
5033 (float (conf.winw - state.scrollw - 1), float conf.winh)
5035 GlDraw.color (0.0, 0.0, 0.0);
5037 GlDraw.rect
5038 (float (conf.winw - sbw), ph)
5039 (float conf.winw, ph +. sh)
5041 GlDraw.rect
5042 (pw, float (conf.winh - sbh))
5043 (pw +. sw, float conf.winh)
5047 let showsel () =
5048 match state.mstate with
5049 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5052 | Msel ((x0, y0), (x1, y1)) ->
5053 let rec loop = function
5054 | l :: ls ->
5055 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5056 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5057 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5058 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5059 then
5060 match getopaque l.pageno with
5061 | Some opaque ->
5062 let x0, y0 = pagetranslatepoint l x0 y0 in
5063 let x1, y1 = pagetranslatepoint l x1 y1 in
5064 seltext opaque (x0, y0, x1, y1);
5065 | _ -> ()
5066 else loop ls
5067 | [] -> ()
5069 loop state.layout
5072 let showrects rects =
5073 Gl.enable `blend;
5074 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5075 GlDraw.polygon_mode `both `fill;
5076 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5077 List.iter
5078 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5079 List.iter (fun l ->
5080 if l.pageno = pageno
5081 then (
5082 let dx = float (l.pagedispx - l.pagex) in
5083 let dy = float (l.pagedispy - l.pagey) in
5084 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5085 GlDraw.begins `quads;
5087 GlDraw.vertex2 (x0+.dx, y0+.dy);
5088 GlDraw.vertex2 (x1+.dx, y1+.dy);
5089 GlDraw.vertex2 (x2+.dx, y2+.dy);
5090 GlDraw.vertex2 (x3+.dx, y3+.dy);
5092 GlDraw.ends ();
5094 ) state.layout
5095 ) rects
5097 Gl.disable `blend;
5100 let display () =
5101 GlClear.color (scalecolor2 conf.bgcolor);
5102 GlClear.clear [`color];
5103 let rec loop linkindexbase = function
5104 | l :: rest ->
5105 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5106 loop linkindexbase rest
5107 | [] -> ()
5109 loop 0 state.layout;
5110 let rects =
5111 match state.mode with
5112 | LinkNav (Ltexact (pageno, linkno)) ->
5113 begin match getopaque pageno with
5114 | Some opaque ->
5115 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5116 (pageno, 5, (
5117 float x0, float y0,
5118 float x1, float y0,
5119 float x1, float y1,
5120 float x0, float y1)
5121 ) :: state.rects
5122 | None -> state.rects
5124 | _ -> state.rects
5126 showrects rects;
5127 showsel ();
5128 state.uioh#display;
5129 begin match state.mstate with
5130 | Mzoomrect ((x0, y0), (x1, y1)) ->
5131 Gl.enable `blend;
5132 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5133 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5134 GlDraw.rect (float x0, float y0)
5135 (float x1, float y1);
5136 Gl.disable `blend;
5137 | _ -> ()
5138 end;
5139 enttext ();
5140 scrollindicator ();
5141 Wsi.swapb ();
5144 let zoomrect x y x1 y1 =
5145 let x0 = min x x1
5146 and x1 = max x x1
5147 and y0 = min y y1 in
5148 gotoy (state.y + y0);
5149 state.anchor <- getanchor ();
5150 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5151 let margin =
5152 if state.w < conf.winw - state.scrollw
5153 then (conf.winw - state.scrollw - state.w) / 2
5154 else 0
5156 state.x <- (state.x + margin) - x0;
5157 setzoom zoom;
5158 Wsi.setcursor Wsi.CURSOR_INHERIT;
5159 state.mstate <- Mnone;
5162 let scrollx x =
5163 let winw = conf.winw - state.scrollw - 1 in
5164 let s = float x /. float winw in
5165 let destx = truncate (float (state.w + winw) *. s) in
5166 state.x <- winw - destx;
5167 gotoy_and_clear_text state.y;
5168 state.mstate <- Mscrollx;
5171 let scrolly y =
5172 let s = float y /. float conf.winh in
5173 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5174 gotoy_and_clear_text desty;
5175 state.mstate <- Mscrolly;
5178 let viewmouse button down x y mask =
5179 match button with
5180 | n when (n == 4 || n == 5) && not down ->
5181 if Wsi.withctrl mask
5182 then (
5183 match state.mstate with
5184 | Mzoom (oldn, i) ->
5185 if oldn = n
5186 then (
5187 if i = 2
5188 then
5189 let incr =
5190 match n with
5191 | 5 ->
5192 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5193 | _ ->
5194 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5196 let zoom = conf.zoom -. incr in
5197 setzoom zoom;
5198 state.mstate <- Mzoom (n, 0);
5199 else
5200 state.mstate <- Mzoom (n, i+1);
5202 else state.mstate <- Mzoom (n, 0)
5204 | _ -> state.mstate <- Mzoom (n, 0)
5206 else (
5207 match state.autoscroll with
5208 | Some step -> setautoscrollspeed step (n=4)
5209 | None ->
5210 let incr =
5211 if n = 4
5212 then -conf.scrollstep
5213 else conf.scrollstep
5215 let incr = incr * 2 in
5216 let y = clamp incr in
5217 gotoy_and_clear_text y
5220 | 1 when Wsi.withctrl mask ->
5221 if down
5222 then (
5223 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5224 state.mstate <- Mpan (x, y)
5226 else
5227 state.mstate <- Mnone
5229 | 3 ->
5230 if down
5231 then (
5232 Wsi.setcursor Wsi.CURSOR_CYCLE;
5233 let p = (x, y) in
5234 state.mstate <- Mzoomrect (p, p)
5236 else (
5237 match state.mstate with
5238 | Mzoomrect ((x0, y0), _) ->
5239 if abs (x-x0) > 10 && abs (y - y0) > 10
5240 then zoomrect x0 y0 x y
5241 else (
5242 state.mstate <- Mnone;
5243 Wsi.setcursor Wsi.CURSOR_INHERIT;
5244 G.postRedisplay "kill accidental zoom rect";
5246 | _ ->
5247 Wsi.setcursor Wsi.CURSOR_INHERIT;
5248 state.mstate <- Mnone
5251 | 1 when x > conf.winw - state.scrollw ->
5252 if down
5253 then
5254 let _, position, sh = state.uioh#scrollph in
5255 if y > truncate position && y < truncate (position +. sh)
5256 then state.mstate <- Mscrolly
5257 else scrolly y
5258 else
5259 state.mstate <- Mnone
5261 | 1 when y > conf.winh - state.hscrollh ->
5262 if down
5263 then
5264 let _, position, sw = state.uioh#scrollpw in
5265 if x > truncate position && x < truncate (position +. sw)
5266 then state.mstate <- Mscrollx
5267 else scrollx x
5268 else
5269 state.mstate <- Mnone
5271 | 1 ->
5272 let dest = if down then getunder x y else Unone in
5273 begin match dest with
5274 | Ulinkgoto _
5275 | Ulinkuri _
5276 | Uremote _
5277 | Uunexpected _ | Ulaunch _ | Unamed _ ->
5278 gotounder dest
5280 | Unone when down ->
5281 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5282 state.mstate <- Mpan (x, y);
5284 | Unone | Utext _ ->
5285 if down
5286 then (
5287 if conf.angle mod 360 = 0
5288 then (
5289 state.mstate <- Msel ((x, y), (x, y));
5290 G.postRedisplay "mouse select";
5293 else (
5294 match state.mstate with
5295 | Mnone -> ()
5297 | Mzoom _ | Mscrollx | Mscrolly ->
5298 state.mstate <- Mnone
5300 | Mzoomrect ((x0, y0), _) ->
5301 zoomrect x0 y0 x y
5303 | Mpan _ ->
5304 Wsi.setcursor Wsi.CURSOR_INHERIT;
5305 state.mstate <- Mnone
5307 | Msel ((_, y0), (_, y1)) ->
5308 let rec loop = function
5309 | [] -> ()
5310 | l :: rest ->
5311 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5312 || ((y1 >= l.pagedispy
5313 && y1 <= (l.pagedispy + l.pagevh)))
5314 then
5315 match getopaque l.pageno with
5316 | Some opaque ->
5317 copysel conf.selcmd opaque;
5318 G.postRedisplay "copysel"
5319 | _ -> ()
5320 else loop rest
5322 loop state.layout;
5323 Wsi.setcursor Wsi.CURSOR_INHERIT;
5324 state.mstate <- Mnone;
5328 | _ -> ()
5331 let birdseyemouse button down x y mask
5332 (conf, leftx, _, hooverpageno, anchor) =
5333 match button with
5334 | 1 when down ->
5335 let rec loop = function
5336 | [] -> ()
5337 | l :: rest ->
5338 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5339 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5340 then (
5341 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5343 else loop rest
5345 loop state.layout
5346 | 3 -> ()
5347 | _ -> viewmouse button down x y mask
5350 let mouse button down x y mask =
5351 state.uioh <- state.uioh#button button down x y mask;
5354 let motion ~x ~y =
5355 state.uioh <- state.uioh#motion x y
5358 let pmotion ~x ~y =
5359 state.uioh <- state.uioh#pmotion x y;
5362 let uioh = object
5363 method display = ()
5365 method key key mask =
5366 begin match state.mode with
5367 | Textentry textentry -> textentrykeyboard key mask textentry
5368 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5369 | View -> viewkeyboard key mask
5370 | LinkNav linknav -> linknavkeyboard key mask linknav
5371 end;
5372 state.uioh
5374 method button button bstate x y mask =
5375 begin match state.mode with
5376 | LinkNav _
5377 | View -> viewmouse button bstate x y mask
5378 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5379 | Textentry _ -> ()
5380 end;
5381 state.uioh
5383 method motion x y =
5384 begin match state.mode with
5385 | Textentry _ -> ()
5386 | View | Birdseye _ | LinkNav _ ->
5387 match state.mstate with
5388 | Mzoom _ | Mnone -> ()
5390 | Mpan (x0, y0) ->
5391 let dx = x - x0
5392 and dy = y0 - y in
5393 state.mstate <- Mpan (x, y);
5394 if conf.zoom > 1.0 then state.x <- state.x + dx;
5395 let y = clamp dy in
5396 gotoy_and_clear_text y
5398 | Msel (a, _) ->
5399 state.mstate <- Msel (a, (x, y));
5400 G.postRedisplay "motion select";
5402 | Mscrolly ->
5403 let y = min conf.winh (max 0 y) in
5404 scrolly y
5406 | Mscrollx ->
5407 let x = min conf.winw (max 0 x) in
5408 scrollx x
5410 | Mzoomrect (p0, _) ->
5411 state.mstate <- Mzoomrect (p0, (x, y));
5412 G.postRedisplay "motion zoomrect";
5413 end;
5414 state.uioh
5416 method pmotion x y =
5417 begin match state.mode with
5418 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5419 let rec loop = function
5420 | [] ->
5421 if hooverpageno != -1
5422 then (
5423 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5424 G.postRedisplay "pmotion birdseye no hoover";
5426 | l :: rest ->
5427 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5428 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5429 then (
5430 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5431 G.postRedisplay "pmotion birdseye hoover";
5433 else loop rest
5435 loop state.layout
5437 | Textentry _ -> ()
5439 | LinkNav _
5440 | View ->
5441 match state.mstate with
5442 | Mnone -> updateunder x y
5443 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5445 end;
5446 state.uioh
5448 method infochanged _ = ()
5450 method scrollph =
5451 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5452 let p, h = scrollph state.y maxy in
5453 state.scrollw, p, h
5455 method scrollpw =
5456 let winw = conf.winw - state.scrollw - 1 in
5457 let fwinw = float winw in
5458 let sw =
5459 let sw = fwinw /. float state.w in
5460 let sw = fwinw *. sw in
5461 max sw (float conf.scrollh)
5463 let position, sw =
5464 let f = state.w+winw in
5465 let r = float (winw-state.x) /. float f in
5466 let p = fwinw *. r in
5467 p-.sw/.2., sw
5469 let sw =
5470 if position +. sw > fwinw
5471 then fwinw -. position
5472 else sw
5474 state.hscrollh, position, sw
5476 method modehash =
5477 let modename =
5478 match state.mode with
5479 | LinkNav _ -> "links"
5480 | Textentry _ -> "textentry"
5481 | Birdseye _ -> "birdseye"
5482 | View -> "global"
5484 findkeyhash conf modename
5485 end;;
5487 module Config =
5488 struct
5489 open Parser
5491 let fontpath = ref "";;
5493 module KeyMap =
5494 Map.Make (struct type t = (int * int) let compare = compare end);;
5496 let unent s =
5497 let l = String.length s in
5498 let b = Buffer.create l in
5499 unent b s 0 l;
5500 Buffer.contents b;
5503 let home =
5504 try Sys.getenv "HOME"
5505 with exn ->
5506 prerr_endline
5507 ("Can not determine home directory location: " ^
5508 Printexc.to_string exn);
5512 let modifier_of_string = function
5513 | "alt" -> Wsi.altmask
5514 | "shift" -> Wsi.shiftmask
5515 | "ctrl" | "control" -> Wsi.ctrlmask
5516 | "meta" -> Wsi.metamask
5517 | _ -> 0
5520 let key_of_string =
5521 let r = Str.regexp "-" in
5522 fun s ->
5523 let elems = Str.full_split r s in
5524 let f n k m =
5525 let g s =
5526 let m1 = modifier_of_string s in
5527 if m1 = 0
5528 then (Wsi.namekey s, m)
5529 else (k, m lor m1)
5530 in function
5531 | Str.Delim s when n land 1 = 0 -> g s
5532 | Str.Text s -> g s
5533 | Str.Delim _ -> (k, m)
5535 let rec loop n k m = function
5536 | [] -> (k, m)
5537 | x :: xs ->
5538 let k, m = f n k m x in
5539 loop (n+1) k m xs
5541 loop 0 0 0 elems
5544 let keys_of_string =
5545 let r = Str.regexp "[ \t]" in
5546 fun s ->
5547 let elems = Str.split r s in
5548 List.map key_of_string elems
5551 let copykeyhashes c =
5552 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5555 let config_of c attrs =
5556 let apply c k v =
5558 match k with
5559 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5560 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5561 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5562 | "preload" -> { c with preload = bool_of_string v }
5563 | "page-bias" -> { c with pagebias = int_of_string v }
5564 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5565 | "auto-scroll-step" ->
5566 { c with autoscrollstep = max 0 (int_of_string v) }
5567 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5568 | "crop-hack" -> { c with crophack = bool_of_string v }
5569 | "throttle" ->
5570 let mw =
5571 match String.lowercase v with
5572 | "true" -> Some infinity
5573 | "false" -> None
5574 | f -> Some (float_of_string f)
5576 { c with maxwait = mw}
5577 | "highlight-links" -> { c with hlinks = bool_of_string v }
5578 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5579 | "vertical-margin" ->
5580 { c with interpagespace = max 0 (int_of_string v) }
5581 | "zoom" ->
5582 let zoom = float_of_string v /. 100. in
5583 let zoom = max zoom 0.0 in
5584 { c with zoom = zoom }
5585 | "presentation" -> { c with presentation = bool_of_string v }
5586 | "rotation-angle" -> { c with angle = int_of_string v }
5587 | "width" -> { c with winw = max 20 (int_of_string v) }
5588 | "height" -> { c with winh = max 20 (int_of_string v) }
5589 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5590 | "proportional-display" -> { c with proportional = bool_of_string v }
5591 | "pixmap-cache-size" ->
5592 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5593 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5594 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5595 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5596 | "persistent-location" -> { c with jumpback = bool_of_string v }
5597 | "background-color" -> { c with bgcolor = color_of_string v }
5598 | "scrollbar-in-presentation" ->
5599 { c with scrollbarinpm = bool_of_string v }
5600 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5601 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5602 | "mupdf-store-size" ->
5603 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5604 | "checkers" -> { c with checkers = bool_of_string v }
5605 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5606 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5607 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5608 | "uri-launcher" -> { c with urilauncher = unent v }
5609 | "path-launcher" -> { c with pathlauncher = unent v }
5610 | "color-space" -> { c with colorspace = colorspace_of_string v }
5611 | "invert-colors" -> { c with invert = bool_of_string v }
5612 | "brightness" -> { c with colorscale = float_of_string v }
5613 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5614 | "ghyllscroll" ->
5615 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5616 | "columns" ->
5617 let nab = columns_of_string v in
5618 { c with columns = Some (nab, [||]) }
5619 | "birds-eye-columns" ->
5620 { c with beyecolumns = Some (max (int_of_string v) 2) }
5621 | "selection-command" -> { c with selcmd = unent v }
5622 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5623 | _ -> c
5624 with exn ->
5625 prerr_endline ("Error processing attribute (`" ^
5626 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5629 let rec fold c = function
5630 | [] -> c
5631 | (k, v) :: rest ->
5632 let c = apply c k v in
5633 fold c rest
5635 fold { c with keyhashes = copykeyhashes c } attrs;
5638 let fromstring f pos n v d =
5639 try f v
5640 with exn ->
5641 dolog "Error processing attribute (%S=%S) at %d\n%s"
5642 n v pos (Printexc.to_string exn)
5647 let bookmark_of attrs =
5648 let rec fold title page rely = function
5649 | ("title", v) :: rest -> fold v page rely rest
5650 | ("page", v) :: rest -> fold title v rely rest
5651 | ("rely", v) :: rest -> fold title page v rest
5652 | _ :: rest -> fold title page rely rest
5653 | [] -> title, page, rely
5655 fold "invalid" "0" "0" attrs
5658 let doc_of attrs =
5659 let rec fold path page rely pan = function
5660 | ("path", v) :: rest -> fold v page rely pan rest
5661 | ("page", v) :: rest -> fold path v rely pan rest
5662 | ("rely", v) :: rest -> fold path page v pan rest
5663 | ("pan", v) :: rest -> fold path page rely v rest
5664 | _ :: rest -> fold path page rely pan rest
5665 | [] -> path, page, rely, pan
5667 fold "" "0" "0" "0" attrs
5670 let map_of attrs =
5671 let rec fold rs ls = function
5672 | ("out", v) :: rest -> fold v ls rest
5673 | ("in", v) :: rest -> fold rs v rest
5674 | _ :: rest -> fold ls rs rest
5675 | [] -> ls, rs
5677 fold "" "" attrs
5680 let setconf dst src =
5681 dst.scrollbw <- src.scrollbw;
5682 dst.scrollh <- src.scrollh;
5683 dst.icase <- src.icase;
5684 dst.preload <- src.preload;
5685 dst.pagebias <- src.pagebias;
5686 dst.verbose <- src.verbose;
5687 dst.scrollstep <- src.scrollstep;
5688 dst.maxhfit <- src.maxhfit;
5689 dst.crophack <- src.crophack;
5690 dst.autoscrollstep <- src.autoscrollstep;
5691 dst.maxwait <- src.maxwait;
5692 dst.hlinks <- src.hlinks;
5693 dst.underinfo <- src.underinfo;
5694 dst.interpagespace <- src.interpagespace;
5695 dst.zoom <- src.zoom;
5696 dst.presentation <- src.presentation;
5697 dst.angle <- src.angle;
5698 dst.winw <- src.winw;
5699 dst.winh <- src.winh;
5700 dst.savebmarks <- src.savebmarks;
5701 dst.memlimit <- src.memlimit;
5702 dst.proportional <- src.proportional;
5703 dst.texcount <- src.texcount;
5704 dst.sliceheight <- src.sliceheight;
5705 dst.thumbw <- src.thumbw;
5706 dst.jumpback <- src.jumpback;
5707 dst.bgcolor <- src.bgcolor;
5708 dst.scrollbarinpm <- src.scrollbarinpm;
5709 dst.tilew <- src.tilew;
5710 dst.tileh <- src.tileh;
5711 dst.mustoresize <- src.mustoresize;
5712 dst.checkers <- src.checkers;
5713 dst.aalevel <- src.aalevel;
5714 dst.trimmargins <- src.trimmargins;
5715 dst.trimfuzz <- src.trimfuzz;
5716 dst.urilauncher <- src.urilauncher;
5717 dst.colorspace <- src.colorspace;
5718 dst.invert <- src.invert;
5719 dst.colorscale <- src.colorscale;
5720 dst.redirectstderr <- src.redirectstderr;
5721 dst.ghyllscroll <- src.ghyllscroll;
5722 dst.columns <- src.columns;
5723 dst.beyecolumns <- src.beyecolumns;
5724 dst.selcmd <- src.selcmd;
5725 dst.updatecurs <- src.updatecurs;
5726 dst.pathlauncher <- src.pathlauncher;
5727 dst.keyhashes <- copykeyhashes src;
5730 let get s =
5731 let h = Hashtbl.create 10 in
5732 let dc = { defconf with angle = defconf.angle } in
5733 let rec toplevel v t spos _ =
5734 match t with
5735 | Vdata | Vcdata | Vend -> v
5736 | Vopen ("llppconfig", _, closed) ->
5737 if closed
5738 then v
5739 else { v with f = llppconfig }
5740 | Vopen _ ->
5741 error "unexpected subelement at top level" s spos
5742 | Vclose _ -> error "unexpected close at top level" s spos
5744 and llppconfig v t spos _ =
5745 match t with
5746 | Vdata | Vcdata -> v
5747 | Vend -> error "unexpected end of input in llppconfig" s spos
5748 | Vopen ("defaults", attrs, closed) ->
5749 let c = config_of dc attrs in
5750 setconf dc c;
5751 if closed
5752 then v
5753 else { v with f = defaults }
5755 | Vopen ("ui-font", attrs, closed) ->
5756 let rec getsize size = function
5757 | [] -> size
5758 | ("size", v) :: rest ->
5759 let size =
5760 fromstring int_of_string spos "size" v fstate.fontsize in
5761 getsize size rest
5762 | l -> getsize size l
5764 fstate.fontsize <- getsize fstate.fontsize attrs;
5765 if closed
5766 then v
5767 else { v with f = uifont (Buffer.create 10) }
5769 | Vopen ("doc", attrs, closed) ->
5770 let pathent, spage, srely, span = doc_of attrs in
5771 let path = unent pathent
5772 and pageno = fromstring int_of_string spos "page" spage 0
5773 and rely = fromstring float_of_string spos "rely" srely 0.0
5774 and pan = fromstring int_of_string spos "pan" span 0 in
5775 let c = config_of dc attrs in
5776 let anchor = (pageno, rely) in
5777 if closed
5778 then (Hashtbl.add h path (c, [], pan, anchor); v)
5779 else { v with f = doc path pan anchor c [] }
5781 | Vopen _ ->
5782 error "unexpected subelement in llppconfig" s spos
5784 | Vclose "llppconfig" -> { v with f = toplevel }
5785 | Vclose _ -> error "unexpected close in llppconfig" s spos
5787 and defaults v t spos _ =
5788 match t with
5789 | Vdata | Vcdata -> v
5790 | Vend -> error "unexpected end of input in defaults" s spos
5791 | Vopen ("keymap", attrs, closed) ->
5792 let modename =
5793 try List.assoc "mode" attrs
5794 with Not_found -> "global" in
5795 if closed
5796 then v
5797 else
5798 let ret keymap =
5799 let h = findkeyhash dc modename in
5800 KeyMap.iter (Hashtbl.replace h) keymap;
5801 defaults
5803 { v with f = pkeymap ret KeyMap.empty }
5805 | Vopen (_, _, _) ->
5806 error "unexpected subelement in defaults" s spos
5808 | Vclose "defaults" ->
5809 { v with f = llppconfig }
5811 | Vclose _ -> error "unexpected close in defaults" s spos
5813 and uifont b v t spos epos =
5814 match t with
5815 | Vdata | Vcdata ->
5816 Buffer.add_substring b s spos (epos - spos);
5818 | Vopen (_, _, _) ->
5819 error "unexpected subelement in ui-font" s spos
5820 | Vclose "ui-font" ->
5821 if String.length !fontpath = 0
5822 then fontpath := Buffer.contents b;
5823 { v with f = llppconfig }
5824 | Vclose _ -> error "unexpected close in ui-font" s spos
5825 | Vend -> error "unexpected end of input in ui-font" s spos
5827 and doc path pan anchor c bookmarks v t spos _ =
5828 match t with
5829 | Vdata | Vcdata -> v
5830 | Vend -> error "unexpected end of input in doc" s spos
5831 | Vopen ("bookmarks", _, closed) ->
5832 if closed
5833 then v
5834 else { v with f = pbookmarks path pan anchor c bookmarks }
5836 | Vopen ("keymap", attrs, closed) ->
5837 let modename =
5838 try List.assoc "mode" attrs
5839 with Not_found -> "global"
5841 if closed
5842 then v
5843 else
5844 let ret keymap =
5845 let h = findkeyhash c modename in
5846 KeyMap.iter (Hashtbl.replace h) keymap;
5847 doc path pan anchor c bookmarks
5849 { v with f = pkeymap ret KeyMap.empty }
5851 | Vopen (_, _, _) ->
5852 error "unexpected subelement in doc" s spos
5854 | Vclose "doc" ->
5855 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5856 { v with f = llppconfig }
5858 | Vclose _ -> error "unexpected close in doc" s spos
5860 and pkeymap ret keymap v t spos _ =
5861 match t with
5862 | Vdata | Vcdata -> v
5863 | Vend -> error "unexpected end of input in keymap" s spos
5864 | Vopen ("map", attrs, closed) ->
5865 let r, l = map_of attrs in
5866 let kss = fromstring keys_of_string spos "in" r [] in
5867 let lss = fromstring keys_of_string spos "out" l [] in
5868 let keymap =
5869 match kss with
5870 | [] -> keymap
5871 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
5872 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
5874 if closed
5875 then { v with f = pkeymap ret keymap }
5876 else
5877 let f () = v in
5878 { v with f = skip "map" f }
5880 | Vopen _ ->
5881 error "unexpected subelement in keymap" s spos
5883 | Vclose "keymap" ->
5884 { v with f = ret keymap }
5886 | Vclose _ -> error "unexpected close in keymap" s spos
5888 and pbookmarks path pan anchor c bookmarks v t spos _ =
5889 match t with
5890 | Vdata | Vcdata -> v
5891 | Vend -> error "unexpected end of input in bookmarks" s spos
5892 | Vopen ("item", attrs, closed) ->
5893 let titleent, spage, srely = bookmark_of attrs in
5894 let page = fromstring int_of_string spos "page" spage 0
5895 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5896 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5897 if closed
5898 then { v with f = pbookmarks path pan anchor c bookmarks }
5899 else
5900 let f () = v in
5901 { v with f = skip "item" f }
5903 | Vopen _ ->
5904 error "unexpected subelement in bookmarks" s spos
5906 | Vclose "bookmarks" ->
5907 { v with f = doc path pan anchor c bookmarks }
5909 | Vclose _ -> error "unexpected close in bookmarks" s spos
5911 and skip tag f v t spos _ =
5912 match t with
5913 | Vdata | Vcdata -> v
5914 | Vend ->
5915 error ("unexpected end of input in skipped " ^ tag) s spos
5916 | Vopen (tag', _, closed) ->
5917 if closed
5918 then v
5919 else
5920 let f' () = { v with f = skip tag f } in
5921 { v with f = skip tag' f' }
5922 | Vclose ctag ->
5923 if tag = ctag
5924 then f ()
5925 else error ("unexpected close in skipped " ^ tag) s spos
5928 parse { f = toplevel; accu = () } s;
5929 h, dc;
5932 let do_load f ic =
5934 let len = in_channel_length ic in
5935 let s = String.create len in
5936 really_input ic s 0 len;
5937 f s;
5938 with
5939 | Parse_error (msg, s, pos) ->
5940 let subs = subs s pos in
5941 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5942 failwith ("parse error: " ^ s)
5944 | exn ->
5945 failwith ("config load error: " ^ Printexc.to_string exn)
5948 let defconfpath =
5949 let dir =
5951 let dir = Filename.concat home ".config" in
5952 if Sys.is_directory dir then dir else home
5953 with _ -> home
5955 Filename.concat dir "llpp.conf"
5958 let confpath = ref defconfpath;;
5960 let load1 f =
5961 if Sys.file_exists !confpath
5962 then
5963 match
5964 (try Some (open_in_bin !confpath)
5965 with exn ->
5966 prerr_endline
5967 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5968 Printexc.to_string exn);
5969 None
5971 with
5972 | Some ic ->
5973 begin try
5974 f (do_load get ic)
5975 with exn ->
5976 prerr_endline
5977 ("Error loading configuation from `" ^ !confpath ^ "': " ^
5978 Printexc.to_string exn);
5979 end;
5980 close_in ic;
5982 | None -> ()
5983 else
5984 f (Hashtbl.create 0, defconf)
5987 let load () =
5988 let f (h, dc) =
5989 let pc, pb, px, pa =
5991 Hashtbl.find h (Filename.basename state.path)
5992 with Not_found -> dc, [], 0, (0, 0.0)
5994 setconf defconf dc;
5995 setconf conf pc;
5996 state.bookmarks <- pb;
5997 state.x <- px;
5998 state.scrollw <- conf.scrollbw;
5999 if conf.jumpback
6000 then state.anchor <- pa;
6001 cbput state.hists.nav pa;
6003 load1 f
6006 let add_attrs bb always dc c =
6007 let ob s a b =
6008 if always || a != b
6009 then Printf.bprintf bb "\n %s='%b'" s a
6010 and oi s a b =
6011 if always || a != b
6012 then Printf.bprintf bb "\n %s='%d'" s a
6013 and oI s a b =
6014 if always || a != b
6015 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6016 and oz s a b =
6017 if always || a <> b
6018 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
6019 and oF s a b =
6020 if always || a <> b
6021 then Printf.bprintf bb "\n %s='%f'" s a
6022 and oc s a b =
6023 if always || a <> b
6024 then
6025 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6026 and oC s a b =
6027 if always || a <> b
6028 then
6029 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6030 and oR s a b =
6031 if always || a <> b
6032 then
6033 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6034 and os s a b =
6035 if always || a <> b
6036 then
6037 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6038 and og s a b =
6039 if always || a <> b
6040 then
6041 match a with
6042 | None -> ()
6043 | Some (_N, _A, _B) ->
6044 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6045 and oW s a b =
6046 if always || a <> b
6047 then
6048 let v =
6049 match a with
6050 | None -> "false"
6051 | Some f ->
6052 if f = infinity
6053 then "true"
6054 else string_of_float f
6056 Printf.bprintf bb "\n %s='%s'" s v
6057 and oco s a b =
6058 if always || a <> b
6059 then
6060 match a with
6061 | Some ((n, a, b), _) when n > 1 ->
6062 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6063 | _ -> ()
6064 and obeco s a b =
6065 if always || a <> b
6066 then
6067 match a with
6068 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6069 | _ -> ()
6071 let w, h =
6072 if always
6073 then dc.winw, dc.winh
6074 else
6075 match state.fullscreen with
6076 | Some wh -> wh
6077 | None -> c.winw, c.winh
6079 let zoom, presentation, interpagespace, maxwait =
6080 if always
6081 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6082 else
6083 match state.mode with
6084 | Birdseye (bc, _, _, _, _) ->
6085 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6086 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6088 oi "width" w dc.winw;
6089 oi "height" h dc.winh;
6090 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6091 oi "scroll-handle-height" c.scrollh dc.scrollh;
6092 ob "case-insensitive-search" c.icase dc.icase;
6093 ob "preload" c.preload dc.preload;
6094 oi "page-bias" c.pagebias dc.pagebias;
6095 oi "scroll-step" c.scrollstep dc.scrollstep;
6096 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6097 ob "max-height-fit" c.maxhfit dc.maxhfit;
6098 ob "crop-hack" c.crophack dc.crophack;
6099 oW "throttle" maxwait dc.maxwait;
6100 ob "highlight-links" c.hlinks dc.hlinks;
6101 ob "under-cursor-info" c.underinfo dc.underinfo;
6102 oi "vertical-margin" interpagespace dc.interpagespace;
6103 oz "zoom" zoom dc.zoom;
6104 ob "presentation" presentation dc.presentation;
6105 oi "rotation-angle" c.angle dc.angle;
6106 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6107 ob "proportional-display" c.proportional dc.proportional;
6108 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6109 oi "tex-count" c.texcount dc.texcount;
6110 oi "slice-height" c.sliceheight dc.sliceheight;
6111 oi "thumbnail-width" c.thumbw dc.thumbw;
6112 ob "persistent-location" c.jumpback dc.jumpback;
6113 oc "background-color" c.bgcolor dc.bgcolor;
6114 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6115 oi "tile-width" c.tilew dc.tilew;
6116 oi "tile-height" c.tileh dc.tileh;
6117 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6118 ob "checkers" c.checkers dc.checkers;
6119 oi "aalevel" c.aalevel dc.aalevel;
6120 ob "trim-margins" c.trimmargins dc.trimmargins;
6121 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6122 os "uri-launcher" c.urilauncher dc.urilauncher;
6123 os "path-launcher" c.pathlauncher dc.pathlauncher;
6124 oC "color-space" c.colorspace dc.colorspace;
6125 ob "invert-colors" c.invert dc.invert;
6126 oF "brightness" c.colorscale dc.colorscale;
6127 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6128 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6129 oco "columns" c.columns dc.columns;
6130 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6131 os "selection-command" c.selcmd dc.selcmd;
6132 ob "update-cursor" c.updatecurs dc.updatecurs;
6135 let keymapsbuf always dc c =
6136 let bb = Buffer.create 16 in
6137 let rec loop = function
6138 | [] -> ()
6139 | (modename, h) :: rest ->
6140 let dh = findkeyhash dc modename in
6141 if always || h <> dh
6142 then (
6143 if Hashtbl.length h > 0
6144 then (
6145 if Buffer.length bb > 0
6146 then Buffer.add_char bb '\n';
6147 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6148 Hashtbl.iter (fun i o ->
6149 let isdifferent = always ||
6151 let dO = Hashtbl.find dh i in
6152 dO <> o
6153 with Not_found -> true
6155 if isdifferent
6156 then
6157 let addkm (k, m) =
6158 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6159 if Wsi.withalt m then Buffer.add_string bb "alt-";
6160 if Wsi.withshift m then Buffer.add_string bb "shift-";
6161 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6162 Buffer.add_string bb (Wsi.keyname k);
6164 let addkms l =
6165 let rec loop = function
6166 | [] -> ()
6167 | km :: [] -> addkm km
6168 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6170 loop l
6172 Buffer.add_string bb "<map in='";
6173 addkm i;
6174 match o with
6175 | KMinsrt km ->
6176 Buffer.add_string bb "' out='";
6177 addkm km;
6178 Buffer.add_string bb "'/>\n"
6180 | KMinsrl kms ->
6181 Buffer.add_string bb "' out='";
6182 addkms kms;
6183 Buffer.add_string bb "'/>\n"
6185 | KMmulti (ins, kms) ->
6186 Buffer.add_char bb ' ';
6187 addkms ins;
6188 Buffer.add_string bb "' out='";
6189 addkms kms;
6190 Buffer.add_string bb "'/>\n"
6191 ) h;
6192 Buffer.add_string bb "</keymap>";
6195 loop rest
6197 loop c.keyhashes;
6201 let save () =
6202 let uifontsize = fstate.fontsize in
6203 let bb = Buffer.create 32768 in
6204 let f (h, dc) =
6205 let dc = if conf.bedefault then conf else dc in
6206 Buffer.add_string bb "<llppconfig>\n";
6208 if String.length !fontpath > 0
6209 then
6210 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6211 uifontsize
6212 !fontpath
6213 else (
6214 if uifontsize <> 14
6215 then
6216 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6219 Buffer.add_string bb "<defaults ";
6220 add_attrs bb true dc dc;
6221 let kb = keymapsbuf true dc dc in
6222 if Buffer.length kb > 0
6223 then (
6224 Buffer.add_string bb ">\n";
6225 Buffer.add_buffer bb kb;
6226 Buffer.add_string bb "\n</defaults>\n";
6228 else Buffer.add_string bb "/>\n";
6230 let adddoc path pan anchor c bookmarks =
6231 if bookmarks == [] && c = dc && anchor = emptyanchor
6232 then ()
6233 else (
6234 Printf.bprintf bb "<doc path='%s'"
6235 (enent path 0 (String.length path));
6237 if anchor <> emptyanchor
6238 then (
6239 let n, y = anchor in
6240 Printf.bprintf bb " page='%d'" n;
6241 if y > 1e-6
6242 then
6243 Printf.bprintf bb " rely='%f'" y
6247 if pan != 0
6248 then Printf.bprintf bb " pan='%d'" pan;
6250 add_attrs bb false dc c;
6251 let kb = keymapsbuf false dc c in
6253 begin match bookmarks with
6254 | [] ->
6255 if Buffer.length kb > 0
6256 then (
6257 Buffer.add_string bb ">\n";
6258 Buffer.add_buffer bb kb;
6259 Buffer.add_string bb "</doc>\n";
6261 else Buffer.add_string bb "/>\n"
6262 | _ ->
6263 Buffer.add_string bb ">\n<bookmarks>\n";
6264 List.iter (fun (title, _level, (page, rely)) ->
6265 Printf.bprintf bb
6266 "<item title='%s' page='%d'"
6267 (enent title 0 (String.length title))
6268 page
6270 if rely > 1e-6
6271 then
6272 Printf.bprintf bb " rely='%f'" rely
6274 Buffer.add_string bb "/>\n";
6275 ) bookmarks;
6276 Buffer.add_string bb "</bookmarks>";
6277 if Buffer.length kb > 0
6278 then (
6279 Buffer.add_string bb "\n";
6280 Buffer.add_buffer bb kb;
6282 Buffer.add_string bb "\n</doc>\n";
6283 end;
6287 let pan, conf =
6288 match state.mode with
6289 | Birdseye (c, pan, _, _, _) ->
6290 let beyecolumns =
6291 match conf.columns with
6292 | Some ((c, _, _), _) -> Some c
6293 | None -> None
6294 and columns =
6295 match c.columns with
6296 | Some (c, _) -> Some (c, [||])
6297 | None -> None
6299 pan, { c with beyecolumns = beyecolumns; columns = columns }
6300 | _ -> state.x, conf
6302 let basename = Filename.basename state.path in
6303 adddoc basename pan (getanchor ())
6304 { conf with
6305 autoscrollstep =
6306 match state.autoscroll with
6307 | Some step -> step
6308 | None -> conf.autoscrollstep }
6309 (if conf.savebmarks then state.bookmarks else []);
6311 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6312 if basename <> path
6313 then adddoc path x y c bookmarks
6314 ) h;
6315 Buffer.add_string bb "</llppconfig>";
6317 load1 f;
6318 if Buffer.length bb > 0
6319 then
6321 let tmp = !confpath ^ ".tmp" in
6322 let oc = open_out_bin tmp in
6323 Buffer.output_buffer oc bb;
6324 close_out oc;
6325 Unix.rename tmp !confpath;
6326 with exn ->
6327 prerr_endline
6328 ("error while saving configuration: " ^ Printexc.to_string exn)
6330 end;;
6332 let () =
6333 Arg.parse
6334 (Arg.align
6335 [("-p", Arg.String (fun s -> state.password <- s) ,
6336 "<password> Set password");
6338 ("-f", Arg.String (fun s -> Config.fontpath := s),
6339 "<path> Set path to the user interface font");
6341 ("-c", Arg.String (fun s -> Config.confpath := s),
6342 "<path> Set path to the configuration file");
6344 ("-v", Arg.Unit (fun () ->
6345 Printf.printf
6346 "%s\nconfiguration path: %s\n"
6347 (version ())
6348 Config.defconfpath
6350 exit 0), " Print version and exit");
6353 (fun s -> state.path <- s)
6354 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6356 if String.length state.path = 0
6357 then (prerr_endline "file name missing"; exit 1);
6359 Config.load ();
6361 let globalkeyhash = findkeyhash conf "global" in
6362 let wsfd, winw, winh = Wsi.init (object
6363 method expose =
6364 if nogeomcmds state.geomcmds
6365 then display ()
6366 method display = display ()
6367 method reshape w h = reshape w h
6368 method mouse b d x y m = mouse b d x y m
6369 method motion x y = state.mpos <- (x, y); motion x y
6370 method pmotion x y = state.mpos <- (x, y); pmotion x y
6371 method key k m =
6372 let mascm = m land (
6373 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6374 ) in
6375 match state.keystate with
6376 | KSnone ->
6377 let km = k, mascm in
6378 begin
6379 match
6380 try Hashtbl.find globalkeyhash km
6381 with Not_found ->
6382 let modehash = state.uioh#modehash in
6383 try Hashtbl.find modehash km
6384 with Not_found -> KMinsrt (k, m)
6385 with
6386 | KMinsrt (k, m) -> keyboard k m
6387 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6388 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6390 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6391 List.iter (fun (k, m) -> keyboard k m) insrt;
6392 state.keystate <- KSnone
6393 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6394 state.keystate <- KSinto (keys, insrt)
6395 | _ ->
6396 state.keystate <- KSnone
6398 method enter x y = state.mpos <- (x, y); pmotion x y
6399 method leave = state.mpos <- (-1, -1)
6400 method quit = raise Quit
6401 end) conf.winw conf.winh (platform = Posx) in
6403 state.wsfd <- wsfd;
6405 if not (
6406 List.exists GlMisc.check_extension
6407 [ "GL_ARB_texture_rectangle"
6408 ; "GL_EXT_texture_recangle"
6409 ; "GL_NV_texture_rectangle" ]
6411 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6413 let cr, sw = Unix.pipe ()
6414 and sr, cw = Unix.pipe () in
6416 cloexec cr;
6417 cloexec sw;
6418 cloexec sr;
6419 cloexec cw;
6421 setcheckers conf.checkers;
6422 redirectstderr ();
6424 init (cr, cw) (
6425 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6426 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6427 !Config.fontpath
6429 state.sr <- sr;
6430 state.sw <- sw;
6431 state.text <- "Opening " ^ state.path;
6432 reshape winw winh;
6433 opendoc state.path state.password;
6434 state.uioh <- uioh;
6436 let rec loop deadline =
6437 let r =
6438 match state.errfd with
6439 | None -> [state.sr; state.wsfd]
6440 | Some fd -> [state.sr; state.wsfd; fd]
6442 if state.redisplay
6443 then (
6444 state.redisplay <- false;
6445 display ();
6447 let timeout =
6448 let now = now () in
6449 if deadline > now
6450 then (
6451 if deadline = infinity
6452 then ~-.1.0
6453 else max 0.0 (deadline -. now)
6455 else 0.0
6457 let r, _, _ =
6458 try Unix.select r [] [] timeout
6459 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6461 begin match r with
6462 | [] ->
6463 state.ghyll None;
6464 let newdeadline =
6465 if state.ghyll == noghyll
6466 then
6467 match state.autoscroll with
6468 | Some step when step != 0 ->
6469 let y = state.y + step in
6470 let y =
6471 if y < 0
6472 then state.maxy
6473 else if y >= state.maxy then 0 else y
6475 gotoy y;
6476 if state.mode = View
6477 then state.text <- "";
6478 deadline +. 0.01
6479 | _ -> infinity
6480 else deadline +. 0.01
6482 loop newdeadline
6484 | l ->
6485 let rec checkfds = function
6486 | [] -> ()
6487 | fd :: rest when fd = state.sr ->
6488 let cmd = readcmd state.sr in
6489 act cmd;
6490 checkfds rest
6492 | fd :: rest when fd = state.wsfd ->
6493 Wsi.readresp fd;
6494 checkfds rest
6496 | fd :: rest ->
6497 let s = String.create 80 in
6498 let n = Unix.read fd s 0 80 in
6499 if conf.redirectstderr
6500 then (
6501 Buffer.add_substring state.errmsgs s 0 n;
6502 state.newerrmsgs <- true;
6503 state.redisplay <- true;
6505 else (
6506 prerr_string (String.sub s 0 n);
6507 flush stderr;
6509 checkfds rest
6511 checkfds l;
6512 let newdeadline =
6513 let deadline1 =
6514 if deadline = infinity
6515 then now () +. 0.01
6516 else deadline
6518 match state.autoscroll with
6519 | Some step when step != 0 -> deadline1
6520 | _ -> if state.ghyll == noghyll then infinity else deadline1
6522 loop newdeadline
6523 end;
6526 loop infinity;
6527 with Quit ->
6528 Config.save ();
6529 exit 0;