Go to link (F)
[llpp.git] / main.ml
blob234dd61ae401b5e036134613cdd4cfdaa2a7b0b0
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 getlink2 : opaque -> int -> under = "ml_getlink2";;
96 external getlinkrect : opaque -> int -> irect = "ml_getlinkrect";;
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 if nogeomcmds state.geomcmds
1105 then (
1106 let accu =
1108 ~pageno:0
1109 ~pdimno:~-1
1110 ~prev:(0,0,0,0)
1111 ~py:0
1112 ~dy:0
1113 ~pdims:state.pdims
1114 ~accu:[]
1116 List.rev accu
1118 else
1122 let layoutN ((columns, coverA, coverB), b) y sh =
1123 let sh = sh - state.hscrollh in
1124 let rec fold accu n =
1125 if n = Array.length b
1126 then accu
1127 else
1128 let pdimno, dx, vy, (_, w, h, xoff) = b.(n) in
1129 if (vy - y) > sh &&
1130 (n = coverA - 1
1131 || n = state.pagecount - coverB
1132 || (n - coverA) mod columns = columns - 1)
1133 then accu
1134 else
1135 let accu =
1136 if vy + h > y
1137 then
1138 let pagey = max 0 (y - vy) in
1139 let pagedispy = if pagey > 0 then 0 else vy - y in
1140 let pagedispx, pagex, pagevw =
1141 let pdx =
1142 if n = coverA - 1 || n = state.pagecount - coverB
1143 then state.x + (conf.winw - state.scrollw - w) / 2
1144 else dx + xoff + state.x
1146 if pdx < 0
1147 then 0, -pdx, w + pdx
1148 else pdx, 0, min (conf.winw - state.scrollw) w
1150 let pagevh = min (h - pagey) (sh - pagedispy) in
1151 if pagedispx < conf.winw - state.scrollw && pagevw > 0 && pagevh > 0
1152 then
1153 let e =
1154 { pageno = n
1155 ; pagedimno = pdimno
1156 ; pagew = w
1157 ; pageh = h
1158 ; pagex = pagex
1159 ; pagey = pagey
1160 ; pagevw = pagevw
1161 ; pagevh = pagevh
1162 ; pagedispx = pagedispx
1163 ; pagedispy = pagedispy
1166 e :: accu
1167 else
1168 accu
1169 else
1170 accu
1172 fold accu (n+1)
1174 if nogeomcmds state.geomcmds
1175 then List.rev (fold [] 0)
1176 else []
1179 let layout y sh =
1180 match conf.columns with
1181 | None -> layout1 y sh
1182 | Some c -> layoutN c y sh
1185 let clamp incr =
1186 let y = state.y + incr in
1187 let y = max 0 y in
1188 let y = min y (state.maxy - (if conf.maxhfit then conf.winh else 0)) in
1192 let itertiles l f =
1193 let tilex = l.pagex mod conf.tilew in
1194 let tiley = l.pagey mod conf.tileh in
1196 let col = l.pagex / conf.tilew in
1197 let row = l.pagey / conf.tileh in
1199 let vw =
1200 let a = l.pagew - l.pagex in
1201 let b = conf.winw - state.scrollw in
1202 min a b
1203 and vh = l.pagevh in
1205 let rec rowloop row y0 dispy h =
1206 if h = 0
1207 then ()
1208 else (
1209 let dh = conf.tileh - y0 in
1210 let dh = min h dh in
1211 let rec colloop col x0 dispx w =
1212 if w = 0
1213 then ()
1214 else (
1215 let dw = conf.tilew - x0 in
1216 let dw = min w dw in
1218 f col row dispx dispy x0 y0 dw dh;
1219 colloop (col+1) 0 (dispx+dw) (w-dw)
1222 colloop col tilex l.pagedispx vw;
1223 rowloop (row+1) 0 (dispy+dh) (h-dh)
1226 if vw > 0 && vh > 0
1227 then rowloop row tiley l.pagedispy vh;
1230 let gettileopaque l col row =
1231 let key =
1232 l.pageno, state.gen, conf.colorspace, conf.angle, l.pagew, l.pageh, col, row
1234 try Some (Hashtbl.find state.tilemap key)
1235 with Not_found -> None
1238 let puttileopaque l col row gen colorspace angle opaque size elapsed =
1239 let key = l.pageno, gen, colorspace, angle, l.pagew, l.pageh, col, row in
1240 Hashtbl.add state.tilemap key (opaque, size, elapsed)
1243 let drawtiles l color =
1244 GlDraw.color color;
1245 let f col row x y tilex tiley w h =
1246 match gettileopaque l col row with
1247 | Some (opaque, _, t) ->
1248 let params = x, y, w, h, tilex, tiley in
1249 if conf.invert
1250 then (
1251 Gl.enable `blend;
1252 GlFunc.blend_func `zero `one_minus_src_color;
1254 drawtile params opaque;
1255 if conf.invert
1256 then Gl.disable `blend;
1257 if conf.debug
1258 then (
1259 let s = Printf.sprintf
1260 "%d[%d,%d] %f sec"
1261 l.pageno col row t
1263 let w = measurestr fstate.fontsize s in
1264 GlMisc.push_attrib [`current];
1265 GlDraw.color (0.0, 0.0, 0.0);
1266 GlDraw.rect
1267 (float (x-2), float (y-2))
1268 (float (x+2) +. w, float (y + fstate.fontsize + 2));
1269 GlDraw.color (1.0, 1.0, 1.0);
1270 drawstring fstate.fontsize x (y + fstate.fontsize - 1) s;
1271 GlMisc.pop_attrib ();
1274 | _ ->
1275 let w =
1276 let lw = conf.winw - state.scrollw - x in
1277 min lw w
1278 and h =
1279 let lh = conf.winh - y in
1280 min lh h
1282 Gl.enable `texture_2d;
1283 begin match state.texid with
1284 | Some id ->
1285 GlTex.bind_texture `texture_2d id;
1286 let x0 = float x
1287 and y0 = float y
1288 and x1 = float (x+w)
1289 and y1 = float (y+h) in
1291 let tw = float w /. 64.0
1292 and th = float h /. 64.0 in
1293 let tx0 = float tilex /. 64.0
1294 and ty0 = float tiley /. 64.0 in
1295 let tx1 = tx0 +. tw
1296 and ty1 = ty0 +. th in
1297 GlDraw.begins `quads;
1298 GlTex.coord2 (tx0, ty0); GlDraw.vertex2 (x0, y0);
1299 GlTex.coord2 (tx0, ty1); GlDraw.vertex2 (x0, y1);
1300 GlTex.coord2 (tx1, ty1); GlDraw.vertex2 (x1, y1);
1301 GlTex.coord2 (tx1, ty0); GlDraw.vertex2 (x1, y0);
1302 GlDraw.ends ();
1304 Gl.disable `texture_2d;
1305 | None ->
1306 GlDraw.color (1.0, 1.0, 1.0);
1307 GlDraw.rect
1308 (float x, float y)
1309 (float (x+w), float (y+h));
1310 end;
1311 if w > 128 && h > fstate.fontsize + 10
1312 then (
1313 GlDraw.color (0.0, 0.0, 0.0);
1314 let c, r =
1315 if conf.verbose
1316 then (col*conf.tilew, row*conf.tileh)
1317 else col, row
1319 drawstring2 fstate.fontsize x y "Loading %d [%d,%d]" l.pageno c r;
1321 GlDraw.color color;
1323 itertiles l f
1326 let pagevisible layout n = List.exists (fun l -> l.pageno = n) layout;;
1328 let tilevisible1 l x y =
1329 let ax0 = l.pagex
1330 and ax1 = l.pagex + l.pagevw
1331 and ay0 = l.pagey
1332 and ay1 = l.pagey + l.pagevh in
1334 let bx0 = x
1335 and by0 = y in
1336 let bx1 = min (bx0 + conf.tilew) l.pagew
1337 and by1 = min (by0 + conf.tileh) l.pageh in
1339 let rx0 = max ax0 bx0
1340 and ry0 = max ay0 by0
1341 and rx1 = min ax1 bx1
1342 and ry1 = min ay1 by1 in
1344 let nonemptyintersection = rx1 > rx0 && ry1 > ry0 in
1345 nonemptyintersection
1348 let tilevisible layout n x y =
1349 let rec findpageinlayout = function
1350 | l :: _ when l.pageno = n -> tilevisible1 l x y
1351 | _ :: rest -> findpageinlayout rest
1352 | [] -> false
1354 findpageinlayout layout
1357 let tileready l x y =
1358 tilevisible1 l x y &&
1359 gettileopaque l (x/conf.tilew) (y/conf.tileh) != None
1362 let tilepage n p layout =
1363 let rec loop = function
1364 | l :: rest ->
1365 if l.pageno = n
1366 then
1367 let f col row _ _ _ _ _ _ =
1368 if state.currently = Idle
1369 then
1370 match gettileopaque l col row with
1371 | Some _ -> ()
1372 | None ->
1373 let x = col*conf.tilew
1374 and y = row*conf.tileh in
1375 let w =
1376 let w = l.pagew - x in
1377 min w conf.tilew
1379 let h =
1380 let h = l.pageh - y in
1381 min h conf.tileh
1383 wcmd "tile %s %d %d %d %d" p x y w h;
1384 state.currently <-
1385 Tiling (
1386 l, p, conf.colorspace, conf.angle, state.gen, col, row,
1387 conf.tilew, conf.tileh
1390 itertiles l f;
1391 else
1392 loop rest
1394 | [] -> ()
1396 if nogeomcmds state.geomcmds
1397 then loop layout;
1400 let preloadlayout visiblepages =
1401 let presentation = conf.presentation in
1402 let interpagespace = conf.interpagespace in
1403 let maxy = state.maxy in
1404 conf.presentation <- false;
1405 conf.interpagespace <- 0;
1406 state.maxy <- calcheight ();
1407 let y =
1408 match visiblepages with
1409 | [] -> 0
1410 | l :: _ -> getpagey l.pageno + l.pagey
1412 let y = if y < conf.winh then 0 else y - conf.winh in
1413 let h = state.y - y + conf.winh*3 in
1414 let pages = layout y h in
1415 conf.presentation <- presentation;
1416 conf.interpagespace <- interpagespace;
1417 state.maxy <- maxy;
1418 pages;
1421 let load pages =
1422 let rec loop pages =
1423 if state.currently != Idle
1424 then ()
1425 else
1426 match pages with
1427 | l :: rest ->
1428 begin match getopaque l.pageno with
1429 | None ->
1430 wcmd "page %d %d" l.pageno l.pagedimno;
1431 state.currently <- Loading (l, state.gen);
1432 | Some opaque ->
1433 tilepage l.pageno opaque pages;
1434 loop rest
1435 end;
1436 | _ -> ()
1438 if nogeomcmds state.geomcmds
1439 then loop pages
1442 let preload pages =
1443 load pages;
1444 if conf.preload && state.currently = Idle
1445 then load (preloadlayout pages);
1448 let layoutready layout =
1449 let rec fold all ls =
1450 all && match ls with
1451 | l :: rest ->
1452 let seen = ref false in
1453 let allvisible = ref true in
1454 let foo col row _ _ _ _ _ _ =
1455 seen := true;
1456 allvisible := !allvisible &&
1457 begin match gettileopaque l col row with
1458 | Some _ -> true
1459 | None -> false
1462 itertiles l foo;
1463 fold (!seen && !allvisible) rest
1464 | [] -> true
1466 let alltilesvisible = fold true layout in
1467 alltilesvisible;
1470 let gotoy y =
1471 let y = bound y 0 state.maxy in
1472 let y, layout, proceed =
1473 match conf.maxwait with
1474 | Some time when state.ghyll == noghyll ->
1475 begin match state.throttle with
1476 | None ->
1477 let layout = layout y conf.winh in
1478 let ready = layoutready layout in
1479 if not ready
1480 then (
1481 load layout;
1482 state.throttle <- Some (layout, y, now ());
1484 else G.postRedisplay "gotoy showall (None)";
1485 y, layout, ready
1486 | Some (_, _, started) ->
1487 let dt = now () -. started in
1488 if dt > time
1489 then (
1490 state.throttle <- None;
1491 let layout = layout y conf.winh in
1492 load layout;
1493 G.postRedisplay "maxwait";
1494 y, layout, true
1496 else -1, [], false
1499 | _ ->
1500 let layout = layout y conf.winh in
1501 if true || layoutready layout
1502 then G.postRedisplay "gotoy ready";
1503 y, layout, true
1505 if proceed
1506 then (
1507 state.y <- y;
1508 state.layout <- layout;
1509 begin match state.mode with
1510 | LinkNav (Ltexact (pageno, linkno)) ->
1511 let rec loop = function
1512 | [] ->
1513 state.mode <- LinkNav (Ltgendir 0)
1514 | l :: _ when l.pageno = pageno ->
1515 begin match getopaque pageno with
1516 | None ->
1517 state.mode <- LinkNav (Ltgendir 0)
1518 | Some opaque ->
1519 let x0, y0, x1, y1 = getlinkrect opaque linkno in
1520 if not (x0 >= l.pagex && x1 <= l.pagex + l.pagevw
1521 && y0 >= l.pagey && y1 <= l.pagey + l.pagevh)
1522 then state.mode <- LinkNav (Ltgendir 0)
1524 | _ :: rest -> loop rest
1526 loop layout
1527 | _ -> ()
1528 end;
1529 begin match state.mode with
1530 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
1531 if not (pagevisible layout pageno)
1532 then (
1533 match state.layout with
1534 | [] -> ()
1535 | l :: _ ->
1536 state.mode <- Birdseye (
1537 conf, leftx, l.pageno, hooverpageno, anchor
1540 | LinkNav (Ltgendir dir as lt) ->
1541 let linknav =
1542 let rec loop = function
1543 | [] -> lt
1544 | l :: rest ->
1545 match getopaque l.pageno with
1546 | None -> loop rest
1547 | Some opaque ->
1548 let link =
1549 let ld =
1550 if dir = 0
1551 then LDfirstvisible (l.pagex, l.pagey, dir)
1552 else (
1553 if dir > 0 then LDfirst else LDlast
1556 findlink opaque ld
1558 match link with
1559 | Lnotfound -> loop rest
1560 | Lfound n ->
1561 showlinktype (getlink opaque n);
1562 Ltexact (l.pageno, n)
1564 loop state.layout
1566 state.mode <- LinkNav linknav
1567 | _ -> ()
1568 end;
1569 preload layout;
1571 state.ghyll <- noghyll;
1572 if conf.updatecurs
1573 then (
1574 let mx, my = state.mpos in
1575 updateunder mx my;
1579 let conttiling pageno opaque =
1580 tilepage pageno opaque
1581 (if conf.preload then preloadlayout state.layout else state.layout)
1584 let gotoy_and_clear_text y =
1585 if not conf.verbose then state.text <- "";
1586 gotoy y;
1589 let getanchor () =
1590 match state.layout with
1591 | [] -> emptyanchor
1592 | l :: _ -> (l.pageno, float l.pagey /. float l.pageh)
1595 let getanchory (n, top) =
1596 let y, h = getpageyh n in
1597 y + (truncate (top *. float h));
1600 let gotoanchor anchor =
1601 gotoy (getanchory anchor);
1604 let addnav () =
1605 cbput state.hists.nav (getanchor ());
1608 let getnav dir =
1609 let anchor = cbgetc state.hists.nav dir in
1610 getanchory anchor;
1613 let gotoghyll y =
1614 let rec scroll f n a b =
1615 (* http://devmaster.net/forums/topic/9796-ease-in-ease-out-algorithm/ *)
1616 let snake f a b =
1617 let s x = 3.0*.x**2.0 -. 2.0*.x**3.0 in
1618 if f < a
1619 then s (float f /. float a)
1620 else (
1621 if f > b
1622 then 1.0 -. s ((float (f-b) /. float (n-b)))
1623 else 1.0
1626 snake f a b
1627 and summa f n a b =
1628 (* courtesy:
1629 http://integrals.wolfram.com/index.jsp?expr=3x%5E2-2x%5E3&random=false *)
1630 let iv x = -.((-.2.0 +. x)*.x**3.0)/.2.0 in
1631 let iv1 = iv f in
1632 let ins = float a *. iv1
1633 and outs = float (n-b) *. iv1 in
1634 let ones = b - a in
1635 ins +. outs +. float ones
1637 let rec set (_N, _A, _B) y sy =
1638 let sum = summa 1.0 _N _A _B in
1639 let dy = float (y - sy) in
1640 state.ghyll <- (
1641 let rec gf n y1 o =
1642 if n >= _N
1643 then state.ghyll <- noghyll
1644 else
1645 let go n =
1646 let s = scroll n _N _A _B in
1647 let y1 = y1 +. ((s *. dy) /. sum) in
1648 gotoy_and_clear_text (truncate y1);
1649 state.ghyll <- gf (n+1) y1;
1651 match o with
1652 | None -> go n
1653 | Some y' -> set (_N/2, 0, 0) y' state.y
1655 gf 0 (float state.y)
1658 match conf.ghyllscroll with
1659 | None ->
1660 gotoy_and_clear_text y
1661 | Some nab ->
1662 if state.ghyll == noghyll
1663 then set nab y state.y
1664 else state.ghyll (Some y)
1667 let gotopage n top =
1668 let y, h = getpageyh n in
1669 let y = y + (truncate (top *. float h)) in
1670 gotoghyll y
1673 let gotopage1 n top =
1674 let y = getpagey n in
1675 let y = y + top in
1676 gotoghyll y
1679 let invalidate s f =
1680 state.layout <- [];
1681 state.pdims <- [];
1682 state.rects <- [];
1683 state.rects1 <- [];
1684 match state.geomcmds with
1685 | ps, [] when String.length ps = 0 ->
1686 f ();
1687 state.geomcmds <- s, [];
1689 | ps, [] ->
1690 state.geomcmds <- ps, [s, f];
1692 | ps, (s', _) :: rest when s' = s ->
1693 state.geomcmds <- ps, ((s, f) :: rest);
1695 | ps, cmds ->
1696 state.geomcmds <- ps, ((s, f) :: cmds);
1699 let opendoc path password =
1700 state.path <- path;
1701 state.password <- password;
1702 state.gen <- state.gen + 1;
1703 state.docinfo <- [];
1705 setaalevel conf.aalevel;
1706 Wsi.settitle ("llpp " ^ Filename.basename path);
1707 wcmd "open %s\000%s\000" path password;
1708 invalidate "reqlayout"
1709 (fun () ->
1710 wcmd "reqlayout %d %d" conf.angle (btod conf.proportional));
1713 let scalecolor c =
1714 let c = c *. conf.colorscale in
1715 (c, c, c);
1718 let scalecolor2 (r, g, b) =
1719 (r *. conf.colorscale, g *. conf.colorscale, b *. conf.colorscale);
1722 let represent () =
1723 let docolumns = function
1724 | None -> ()
1725 | Some ((columns, coverA, coverB), _) ->
1726 let a = Array.make state.pagecount (-1, -1, -1, (-1, -1, -1, -1)) in
1727 let rec loop pageno pdimno pdim x y rowh pdims =
1728 if pageno = state.pagecount
1729 then ()
1730 else
1731 let pdimno, ((_, w, h, xoff) as pdim), pdims =
1732 match pdims with
1733 | ((pageno', _, _, _) as pdim) :: rest when pageno' = pageno ->
1734 pdimno+1, pdim, rest
1735 | _ ->
1736 pdimno, pdim, pdims
1738 let x, y, rowh' =
1739 if pageno = coverA - 1 || pageno = state.pagecount - coverB
1740 then (
1741 (conf.winw - state.scrollw - w) / 2,
1742 y + rowh + conf.interpagespace, h
1744 else (
1745 if (pageno - coverA) mod columns = 0
1746 then 0, y + rowh + conf.interpagespace, h
1747 else x, y, max rowh h
1750 let rec fixrow m = if m = pageno then () else
1751 let (pdimno, x, y, ((_, _, h, _) as pdim)) = a.(m) in
1752 if h < rowh
1753 then (
1754 let y = y + (rowh - h) / 2 in
1755 a.(m) <- (pdimno, x, y, pdim);
1757 fixrow (m+1)
1759 if pageno > 1 && (pageno - coverA) mod columns = 0
1760 then fixrow (pageno - columns);
1761 a.(pageno) <- (pdimno, x, y, pdim);
1762 let x = x + w + xoff*2 + conf.interpagespace in
1763 loop (pageno+1) pdimno pdim x y rowh' pdims
1765 loop 0 ~-1 (-1,-1,-1,-1) 0 0 0 state.pdims;
1766 conf.columns <- Some ((columns, coverA, coverB), a);
1768 docolumns conf.columns;
1769 state.maxy <- calcheight ();
1770 state.hscrollh <-
1771 if state.w <= conf.winw - state.scrollw
1772 then 0
1773 else state.scrollw
1775 match state.mode with
1776 | Birdseye (_, _, pageno, _, _) ->
1777 let y, h = getpageyh pageno in
1778 let top = (conf.winh - h) / 2 in
1779 gotoy (max 0 (y - top))
1780 | _ -> gotoanchor state.anchor
1783 let reshape w h =
1784 GlDraw.viewport 0 0 w h;
1785 if state.geomcmds != firstgeomcmds && nogeomcmds state.geomcmds
1786 then state.anchor <- getanchor ();
1788 conf.winw <- w;
1789 let w = truncate (float w *. conf.zoom) - state.scrollw in
1790 let w = max w 2 in
1791 conf.winh <- h;
1792 setfontsize fstate.fontsize;
1793 GlMat.mode `modelview;
1794 GlMat.load_identity ();
1796 GlMat.mode `projection;
1797 GlMat.load_identity ();
1798 GlMat.rotate ~x:1.0 ~angle:180.0 ();
1799 GlMat.translate ~x:~-.1.0 ~y:~-.1.0 ();
1800 GlMat.scale3 (2.0 /. float conf.winw, 2.0 /. float conf.winh, 1.0);
1802 let relx =
1803 if conf.zoom <= 1.0
1804 then 0.0
1805 else float state.x /. float state.w
1807 invalidate "geometry"
1808 (fun () ->
1809 state.w <- w;
1810 state.x <- truncate (relx *. float w);
1811 let w =
1812 match conf.columns with
1813 | None -> w
1814 | Some ((c, _, _), _) -> (w - (c-1)*conf.interpagespace) / c
1816 wcmd "geometry %d %d" w h);
1819 let enttext () =
1820 let len = String.length state.text in
1821 let drawstring s =
1822 let hscrollh =
1823 match state.mode with
1824 | Textentry _
1825 | View -> state.hscrollh
1826 | _ -> 0
1828 let rect x w =
1829 GlDraw.rect
1830 (x, float (conf.winh - (fstate.fontsize + 4) - hscrollh))
1831 (x+.w, float (conf.winh - hscrollh))
1834 let w = float (conf.winw - state.scrollw - 1) in
1835 if state.progress >= 0.0 && state.progress < 1.0
1836 then (
1837 GlDraw.color (0.3, 0.3, 0.3);
1838 let w1 = w *. state.progress in
1839 rect 0.0 w1;
1840 GlDraw.color (0.0, 0.0, 0.0);
1841 rect w1 (w-.w1)
1843 else (
1844 GlDraw.color (0.0, 0.0, 0.0);
1845 rect 0.0 w;
1848 GlDraw.color (1.0, 1.0, 1.0);
1849 drawstring fstate.fontsize
1850 (if len > 0 then 8 else 2) (conf.winh - hscrollh - 5) s;
1852 let s =
1853 match state.mode with
1854 | Textentry ((prefix, text, _, _, _), _) ->
1855 let s =
1856 if len > 0
1857 then
1858 Printf.sprintf "%s%s_ [%s]" prefix text state.text
1859 else
1860 Printf.sprintf "%s%s_" prefix text
1864 | _ -> state.text
1866 let s =
1867 if state.newerrmsgs
1868 then (
1869 if not (istextentry state.mode)
1870 then
1871 let s1 = "(press 'e' to review error messasges)" in
1872 if String.length s > 0 then s ^ " " ^ s1 else s1
1873 else s
1875 else s
1877 if String.length s > 0
1878 then drawstring s
1881 let gctiles () =
1882 let len = Queue.length state.tilelru in
1883 let rec loop qpos =
1884 if state.memused <= conf.memlimit
1885 then ()
1886 else (
1887 if qpos < len
1888 then
1889 let (k, p, s) as lruitem = Queue.pop state.tilelru in
1890 let n, gen, colorspace, angle, pagew, pageh, col, row = k in
1891 let (_, pw, ph, _) = getpagedim n in
1893 gen = state.gen
1894 && colorspace = conf.colorspace
1895 && angle = conf.angle
1896 && pagew = pw
1897 && pageh = ph
1898 && (
1899 let layout =
1900 match state.throttle with
1901 | None ->
1902 if conf.preload
1903 then preloadlayout state.layout
1904 else state.layout
1905 | Some (layout, _, _) ->
1906 layout
1908 let x = col*conf.tilew
1909 and y = row*conf.tileh in
1910 tilevisible layout n x y
1912 then Queue.push lruitem state.tilelru
1913 else (
1914 wcmd "freetile %s" p;
1915 state.memused <- state.memused - s;
1916 state.uioh#infochanged Memused;
1917 Hashtbl.remove state.tilemap k;
1919 loop (qpos+1)
1922 loop 0
1925 let flushtiles () =
1926 Queue.iter (fun (k, p, s) ->
1927 wcmd "freetile %s" p;
1928 state.memused <- state.memused - s;
1929 state.uioh#infochanged Memused;
1930 Hashtbl.remove state.tilemap k;
1931 ) state.tilelru;
1932 Queue.clear state.tilelru;
1933 load state.layout;
1936 let logcurrently = function
1937 | Idle -> dolog "Idle"
1938 | Loading (l, gen) ->
1939 dolog "Loading %d gen=%d curgen=%d" l.pageno gen state.gen
1940 | Tiling (l, pageopaque, colorspace, angle, gen, col, row, tilew, tileh) ->
1941 dolog
1942 "Tiling %d[%d,%d] page=%s cs=%s angle"
1943 l.pageno col row pageopaque
1944 (colorspace_to_string colorspace)
1946 dolog "gen=(%d,%d) (%d,%d) tile=(%d,%d) (%d,%d)"
1947 angle gen conf.angle state.gen
1948 tilew tileh
1949 conf.tilew conf.tileh
1951 | Outlining _ ->
1952 dolog "outlining"
1955 let act cmds =
1956 (* dolog "%S" cmds; *)
1957 let op, args =
1958 let spacepos =
1959 try String.index cmds ' '
1960 with Not_found -> -1
1962 if spacepos = -1
1963 then cmds, ""
1964 else
1965 let l = String.length cmds in
1966 let op = String.sub cmds 0 spacepos in
1967 op, begin
1968 if l - spacepos < 2 then ""
1969 else String.sub cmds (spacepos+1) (l-spacepos-1)
1972 match op with
1973 | "clear" ->
1974 state.uioh#infochanged Pdim;
1975 state.pdims <- [];
1977 | "clearrects" ->
1978 state.rects <- state.rects1;
1979 G.postRedisplay "clearrects";
1981 | "continue" ->
1982 let n =
1983 try Scanf.sscanf args "%u" (fun n -> n)
1984 with exn ->
1985 dolog "error processing 'continue' %S: %s"
1986 cmds (Printexc.to_string exn);
1987 exit 1;
1989 state.pagecount <- n;
1990 begin match state.currently with
1991 | Outlining l ->
1992 state.currently <- Idle;
1993 state.outlines <- Array.of_list (List.rev l)
1994 | _ -> ()
1995 end;
1997 let cur, cmds = state.geomcmds in
1998 if String.length cur = 0
1999 then failwith "umpossible";
2001 begin match List.rev cmds with
2002 | [] ->
2003 state.geomcmds <- "", [];
2004 represent ();
2005 | (s, f) :: rest ->
2006 f ();
2007 state.geomcmds <- s, List.rev rest;
2008 end;
2009 if conf.maxwait = None
2010 then G.postRedisplay "continue";
2012 | "title" ->
2013 Wsi.settitle args
2015 | "msg" ->
2016 showtext ' ' args
2018 | "vmsg" ->
2019 if conf.verbose
2020 then showtext ' ' args
2022 | "progress" ->
2023 let progress, text =
2025 Scanf.sscanf args "%f %n"
2026 (fun f pos ->
2027 f, String.sub args pos (String.length args - pos))
2028 with exn ->
2029 dolog "error processing 'progress' %S: %s"
2030 cmds (Printexc.to_string exn);
2031 exit 1;
2033 state.text <- text;
2034 state.progress <- progress;
2035 G.postRedisplay "progress"
2037 | "firstmatch" ->
2038 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2040 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2041 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2042 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2043 with exn ->
2044 dolog "error processing 'firstmatch' %S: %s"
2045 cmds (Printexc.to_string exn);
2046 exit 1;
2048 let y = (getpagey pageno) + truncate y0 in
2049 addnav ();
2050 gotoy y;
2051 state.rects1 <- [pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)]
2053 | "match" ->
2054 let pageno, c, x0, y0, x1, y1, x2, y2, x3, y3 =
2056 Scanf.sscanf args "%u %d %f %f %f %f %f %f %f %f"
2057 (fun p c x0 y0 x1 y1 x2 y2 x3 y3 ->
2058 (p, c, x0, y0, x1, y1, x2, y2, x3, y3))
2059 with exn ->
2060 dolog "error processing 'match' %S: %s"
2061 cmds (Printexc.to_string exn);
2062 exit 1;
2064 state.rects1 <-
2065 (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) :: state.rects1
2067 | "page" ->
2068 let pageopaque, t =
2070 Scanf.sscanf args "%s %f" (fun p t -> p, t)
2071 with exn ->
2072 dolog "error processing 'page' %S: %s"
2073 cmds (Printexc.to_string exn);
2074 exit 1;
2076 begin match state.currently with
2077 | Loading (l, gen) ->
2078 vlog "page %d took %f sec" l.pageno t;
2079 Hashtbl.replace state.pagemap (l.pageno, gen) pageopaque;
2080 begin match state.throttle with
2081 | None ->
2082 let preloadedpages =
2083 if conf.preload
2084 then preloadlayout state.layout
2085 else state.layout
2087 let evict () =
2088 let module IntSet =
2089 Set.Make (struct type t = int let compare = (-) end) in
2090 let set =
2091 List.fold_left (fun s l -> IntSet.add l.pageno s)
2092 IntSet.empty preloadedpages
2094 let evictedpages =
2095 Hashtbl.fold (fun ((pageno, _) as key) opaque accu ->
2096 if not (IntSet.mem pageno set)
2097 then (
2098 wcmd "freepage %s" opaque;
2099 key :: accu
2101 else accu
2102 ) state.pagemap []
2104 List.iter (Hashtbl.remove state.pagemap) evictedpages;
2106 evict ();
2107 state.currently <- Idle;
2108 if gen = state.gen
2109 then (
2110 tilepage l.pageno pageopaque state.layout;
2111 load state.layout;
2112 load preloadedpages;
2113 if pagevisible state.layout l.pageno
2114 && layoutready state.layout
2115 then G.postRedisplay "page";
2118 | Some (layout, _, _) ->
2119 state.currently <- Idle;
2120 tilepage l.pageno pageopaque layout;
2121 load state.layout
2122 end;
2124 | _ ->
2125 dolog "Inconsistent loading state";
2126 logcurrently state.currently;
2127 exit 1
2130 | "tile" ->
2131 let (x, y, opaque, size, t) =
2133 Scanf.sscanf args "%u %u %s %u %f"
2134 (fun x y p size t -> (x, y, p, size, t))
2135 with exn ->
2136 dolog "error processing 'tile' %S: %s"
2137 cmds (Printexc.to_string exn);
2138 exit 1;
2140 begin match state.currently with
2141 | Tiling (l, pageopaque, cs, angle, gen, col, row, tilew, tileh) ->
2142 vlog "tile %d [%d,%d] took %f sec" l.pageno col row t;
2144 if tilew != conf.tilew || tileh != conf.tileh
2145 then (
2146 wcmd "freetile %s" opaque;
2147 state.currently <- Idle;
2148 load state.layout;
2150 else (
2151 puttileopaque l col row gen cs angle opaque size t;
2152 state.memused <- state.memused + size;
2153 state.uioh#infochanged Memused;
2154 gctiles ();
2155 Queue.push ((l.pageno, gen, cs, angle, l.pagew, l.pageh, col, row),
2156 opaque, size) state.tilelru;
2158 let layout =
2159 match state.throttle with
2160 | None -> state.layout
2161 | Some (layout, _, _) -> layout
2164 state.currently <- Idle;
2165 if gen = state.gen
2166 && conf.colorspace = cs
2167 && conf.angle = angle
2168 && tilevisible layout l.pageno x y
2169 then conttiling l.pageno pageopaque;
2171 begin match state.throttle with
2172 | None ->
2173 preload state.layout;
2174 if gen = state.gen
2175 && conf.colorspace = cs
2176 && conf.angle = angle
2177 && tilevisible state.layout l.pageno x y
2178 then G.postRedisplay "tile nothrottle";
2180 | Some (layout, y, _) ->
2181 let ready = layoutready layout in
2182 if ready
2183 then (
2184 state.y <- y;
2185 state.layout <- layout;
2186 state.throttle <- None;
2187 G.postRedisplay "throttle";
2189 else load layout;
2190 end;
2193 | _ ->
2194 dolog "Inconsistent tiling state";
2195 logcurrently state.currently;
2196 exit 1
2199 | "pdim" ->
2200 let pdim =
2202 Scanf.sscanf args "%u %u %u %u" (fun n w h x -> n, w, h, x)
2203 with exn ->
2204 dolog "error processing 'pdim' %S: %s"
2205 cmds (Printexc.to_string exn);
2206 exit 1;
2208 state.uioh#infochanged Pdim;
2209 state.pdims <- pdim :: state.pdims
2211 | "o" ->
2212 let (l, n, t, h, pos) =
2214 Scanf.sscanf args "%u %u %d %u %n"
2215 (fun l n t h pos -> l, n, t, h, pos)
2216 with exn ->
2217 dolog "error processing 'o' %S: %s"
2218 cmds (Printexc.to_string exn);
2219 exit 1;
2221 let s = String.sub args pos (String.length args - pos) in
2222 let outline = (s, l, (n, float t /. float h)) in
2223 begin match state.currently with
2224 | Outlining outlines ->
2225 state.currently <- Outlining (outline :: outlines)
2226 | Idle ->
2227 state.currently <- Outlining [outline]
2228 | currently ->
2229 dolog "invalid outlining state";
2230 logcurrently currently
2233 | "info" ->
2234 state.docinfo <- (1, args) :: state.docinfo
2236 | "infoend" ->
2237 state.uioh#infochanged Docinfo;
2238 state.docinfo <- List.rev state.docinfo
2240 | _ ->
2241 dolog "unknown cmd `%S'" cmds
2244 let onhist cb =
2245 let rc = cb.rc in
2246 let action = function
2247 | HCprev -> cbget cb ~-1
2248 | HCnext -> cbget cb 1
2249 | HCfirst -> cbget cb ~-(cb.rc)
2250 | HClast -> cbget cb (cb.len - 1 - cb.rc)
2251 and cancel () = cb.rc <- rc
2252 in (action, cancel)
2255 let search pattern forward =
2256 if String.length pattern > 0
2257 then
2258 let pn, py =
2259 match state.layout with
2260 | [] -> 0, 0
2261 | l :: _ ->
2262 l.pageno, (l.pagey + if forward then 0 else 0*l.pagevh)
2264 wcmd "search %d %d %d %d,%s\000"
2265 (btod conf.icase) pn py (btod forward) pattern;
2268 let intentry text key =
2269 let c =
2270 if key >= 32 && key < 127
2271 then Char.chr key
2272 else '\000'
2274 match c with
2275 | '0' .. '9' ->
2276 let text = addchar text c in
2277 TEcont text
2279 | _ ->
2280 state.text <- Printf.sprintf "invalid char (%d, `%c')" key c;
2281 TEcont text
2284 let textentry text key =
2285 if key land 0xff00 = 0xff00
2286 then TEcont text
2287 else TEcont (text ^ Wsi.toutf8 key)
2290 let reqlayout angle proportional =
2291 match state.throttle with
2292 | None ->
2293 if nogeomcmds state.geomcmds
2294 then state.anchor <- getanchor ();
2295 conf.angle <- angle mod 360;
2296 if conf.angle != 0
2297 then (
2298 match state.mode with
2299 | LinkNav _ -> state.mode <- View
2300 | _ -> ()
2302 conf.proportional <- proportional;
2303 invalidate "reqlayout"
2304 (fun () -> wcmd "reqlayout %d %d" conf.angle (btod proportional));
2305 | _ -> ()
2308 let settrim trimmargins trimfuzz =
2309 if nogeomcmds state.geomcmds
2310 then state.anchor <- getanchor ();
2311 conf.trimmargins <- trimmargins;
2312 conf.trimfuzz <- trimfuzz;
2313 let x0, y0, x1, y1 = trimfuzz in
2314 invalidate "settrim"
2315 (fun () ->
2316 wcmd "settrim %d %d %d %d %d" (btod conf.trimmargins) x0 y0 x1 y1);
2317 Hashtbl.iter (fun _ opaque ->
2318 wcmd "freepage %s" opaque;
2319 ) state.pagemap;
2320 Hashtbl.clear state.pagemap;
2323 let setzoom zoom =
2324 match state.throttle with
2325 | None ->
2326 let zoom = max 0.01 zoom in
2327 if zoom <> conf.zoom
2328 then (
2329 state.prevzoom <- conf.zoom;
2330 conf.zoom <- zoom;
2331 reshape conf.winw conf.winh;
2332 state.text <- Printf.sprintf "zoom is now %-5.1f" (zoom *. 100.0);
2335 | Some (layout, y, started) ->
2336 let time =
2337 match conf.maxwait with
2338 | None -> 0.0
2339 | Some t -> t
2341 let dt = now () -. started in
2342 if dt > time
2343 then (
2344 state.y <- y;
2345 load layout;
2349 let setcolumns columns coverA coverB =
2350 if columns < 2
2351 then (
2352 conf.columns <- None;
2353 state.x <- 0;
2354 setzoom 1.0;
2356 else (
2357 conf.columns <- Some ((columns, coverA, coverB), [||]);
2358 conf.zoom <- 1.0;
2360 reshape conf.winw conf.winh;
2363 let enterbirdseye () =
2364 let zoom = float conf.thumbw /. float conf.winw in
2365 let birdseyepageno =
2366 let cy = conf.winh / 2 in
2367 let fold = function
2368 | [] -> 0
2369 | l :: rest ->
2370 let rec fold best = function
2371 | [] -> best.pageno
2372 | l :: rest ->
2373 let d = cy - (l.pagedispy + l.pagevh/2)
2374 and dbest = cy - (best.pagedispy + best.pagevh/2) in
2375 if abs d < abs dbest
2376 then fold l rest
2377 else best.pageno
2378 in fold l rest
2380 fold state.layout
2382 state.mode <- Birdseye (
2383 { conf with zoom = conf.zoom }, state.x, birdseyepageno, -1, getanchor ()
2385 conf.zoom <- zoom;
2386 conf.presentation <- false;
2387 conf.interpagespace <- 10;
2388 conf.hlinks <- false;
2389 state.x <- 0;
2390 state.mstate <- Mnone;
2391 conf.maxwait <- None;
2392 conf.columns <- (
2393 match conf.beyecolumns with
2394 | Some c ->
2395 conf.zoom <- 1.0;
2396 Some ((c, 0, 0), [||])
2397 | None -> None
2399 Wsi.setcursor Wsi.CURSOR_INHERIT;
2400 if conf.verbose
2401 then
2402 state.text <- Printf.sprintf "birds eye mode on (zoom %3.1f%%)"
2403 (100.0*.zoom)
2404 else
2405 state.text <- ""
2407 reshape conf.winw conf.winh;
2410 let leavebirdseye (c, leftx, pageno, _, anchor) goback =
2411 state.mode <- View;
2412 conf.zoom <- c.zoom;
2413 conf.presentation <- c.presentation;
2414 conf.interpagespace <- c.interpagespace;
2415 conf.maxwait <- c.maxwait;
2416 conf.hlinks <- c.hlinks;
2417 conf.beyecolumns <- (
2418 match conf.columns with
2419 | Some ((c, _, _), _) -> Some c
2420 | None -> None
2422 conf.columns <- (
2423 match c.columns with
2424 | Some (c, _) -> Some (c, [||])
2425 | None -> None
2427 state.x <- leftx;
2428 if conf.verbose
2429 then
2430 state.text <- Printf.sprintf "birds eye mode off (zoom %3.1f%%)"
2431 (100.0*.conf.zoom)
2433 reshape conf.winw conf.winh;
2434 state.anchor <- if goback then anchor else (pageno, 0.0);
2437 let togglebirdseye () =
2438 match state.mode with
2439 | Birdseye vals -> leavebirdseye vals true
2440 | View -> enterbirdseye ()
2441 | _ -> ()
2444 let upbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2445 let pageno = max 0 (pageno - incr) in
2446 let rec loop = function
2447 | [] -> gotopage1 pageno 0
2448 | l :: _ when l.pageno = pageno ->
2449 if l.pagedispy >= 0 && l.pagey = 0
2450 then G.postRedisplay "upbirdseye"
2451 else gotopage1 pageno 0
2452 | _ :: rest -> loop rest
2454 loop state.layout;
2455 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor)
2458 let downbirdseye incr (conf, leftx, pageno, hooverpageno, anchor) =
2459 let pageno = min (state.pagecount - 1) (pageno + incr) in
2460 state.mode <- Birdseye (conf, leftx, pageno, hooverpageno, anchor);
2461 let rec loop = function
2462 | [] ->
2463 let y, h = getpageyh pageno in
2464 let dy = (y - state.y) - (conf.winh - h - conf.interpagespace) in
2465 gotoy (clamp dy)
2466 | l :: _ when l.pageno = pageno ->
2467 if l.pagevh != l.pageh
2468 then gotoy (clamp (l.pageh - l.pagevh + conf.interpagespace))
2469 else G.postRedisplay "downbirdseye"
2470 | _ :: rest -> loop rest
2472 loop state.layout
2475 let optentry mode _ key =
2476 let btos b = if b then "on" else "off" in
2477 if key >= 32 && key < 127
2478 then
2479 let c = Char.chr key in
2480 match c with
2481 | 's' ->
2482 let ondone s =
2483 try conf.scrollstep <- int_of_string s with exc ->
2484 state.text <- Printf.sprintf "bad integer `%s': %s"
2485 s (Printexc.to_string exc)
2487 TEswitch ("scroll step: ", "", None, intentry, ondone)
2489 | 'A' ->
2490 let ondone s =
2492 conf.autoscrollstep <- int_of_string s;
2493 if state.autoscroll <> None
2494 then state.autoscroll <- Some conf.autoscrollstep
2495 with exc ->
2496 state.text <- Printf.sprintf "bad integer `%s': %s"
2497 s (Printexc.to_string exc)
2499 TEswitch ("auto scroll step: ", "", None, intentry, ondone)
2501 | 'C' ->
2502 let ondone s =
2504 let n, a, b = columns_of_string s in
2505 setcolumns n a b;
2506 with exc ->
2507 state.text <- Printf.sprintf "bad columns `%s': %s"
2508 s (Printexc.to_string exc)
2510 TEswitch ("columns: ", "", None, textentry, ondone)
2512 | 'Z' ->
2513 let ondone s =
2515 let zoom = float (int_of_string s) /. 100.0 in
2516 setzoom zoom
2517 with exc ->
2518 state.text <- Printf.sprintf "bad integer `%s': %s"
2519 s (Printexc.to_string exc)
2521 TEswitch ("zoom: ", "", None, intentry, ondone)
2523 | 't' ->
2524 let ondone s =
2526 conf.thumbw <- bound (int_of_string s) 2 4096;
2527 state.text <-
2528 Printf.sprintf "thumbnail width is set to %d" conf.thumbw;
2529 begin match mode with
2530 | Birdseye beye ->
2531 leavebirdseye beye false;
2532 enterbirdseye ();
2533 | _ -> ();
2535 with exc ->
2536 state.text <- Printf.sprintf "bad integer `%s': %s"
2537 s (Printexc.to_string exc)
2539 TEswitch ("thumbnail width: ", "", None, intentry, ondone)
2541 | 'R' ->
2542 let ondone s =
2543 match try
2544 Some (int_of_string s)
2545 with exc ->
2546 state.text <- Printf.sprintf "bad integer `%s': %s"
2547 s (Printexc.to_string exc);
2548 None
2549 with
2550 | Some angle -> reqlayout angle conf.proportional
2551 | None -> ()
2553 TEswitch ("rotation: ", "", None, intentry, ondone)
2555 | 'i' ->
2556 conf.icase <- not conf.icase;
2557 TEdone ("case insensitive search " ^ (btos conf.icase))
2559 | 'p' ->
2560 conf.preload <- not conf.preload;
2561 gotoy state.y;
2562 TEdone ("preload " ^ (btos conf.preload))
2564 | 'v' ->
2565 conf.verbose <- not conf.verbose;
2566 TEdone ("verbose " ^ (btos conf.verbose))
2568 | 'd' ->
2569 conf.debug <- not conf.debug;
2570 TEdone ("debug " ^ (btos conf.debug))
2572 | 'h' ->
2573 conf.maxhfit <- not conf.maxhfit;
2574 state.maxy <-
2575 state.maxy + (if conf.maxhfit then -conf.winh else conf.winh);
2576 TEdone ("maxhfit " ^ (btos conf.maxhfit))
2578 | 'c' ->
2579 conf.crophack <- not conf.crophack;
2580 TEdone ("crophack " ^ btos conf.crophack)
2582 | 'a' ->
2583 let s =
2584 match conf.maxwait with
2585 | None ->
2586 conf.maxwait <- Some infinity;
2587 "always wait for page to complete"
2588 | Some _ ->
2589 conf.maxwait <- None;
2590 "show placeholder if page is not ready"
2592 TEdone s
2594 | 'f' ->
2595 conf.underinfo <- not conf.underinfo;
2596 TEdone ("underinfo " ^ btos conf.underinfo)
2598 | 'P' ->
2599 conf.savebmarks <- not conf.savebmarks;
2600 TEdone ("persistent bookmarks " ^ btos conf.savebmarks)
2602 | 'S' ->
2603 let ondone s =
2605 let pageno, py =
2606 match state.layout with
2607 | [] -> 0, 0
2608 | l :: _ ->
2609 l.pageno, l.pagey
2611 conf.interpagespace <- int_of_string s;
2612 state.maxy <- calcheight ();
2613 let y = getpagey pageno in
2614 gotoy (y + py)
2615 with exc ->
2616 state.text <- Printf.sprintf "bad integer `%s': %s"
2617 s (Printexc.to_string exc)
2619 TEswitch ("vertical margin: ", "", None, intentry, ondone)
2621 | 'l' ->
2622 reqlayout conf.angle (not conf.proportional);
2623 TEdone ("proportional display " ^ btos conf.proportional)
2625 | 'T' ->
2626 settrim (not conf.trimmargins) conf.trimfuzz;
2627 TEdone ("trim margins " ^ btos conf.trimmargins)
2629 | 'I' ->
2630 conf.invert <- not conf.invert;
2631 TEdone ("invert colors " ^ btos conf.invert)
2633 | 'x' ->
2634 let ondone s =
2635 cbput state.hists.sel s;
2636 conf.selcmd <- s;
2638 TEswitch ("selection command: ", "", Some (onhist state.hists.sel),
2639 textentry, ondone)
2641 | _ ->
2642 state.text <- Printf.sprintf "bad option %d `%c'" key c;
2643 TEstop
2644 else
2645 TEcont state.text
2648 class type lvsource = object
2649 method getitemcount : int
2650 method getitem : int -> (string * int)
2651 method hasaction : int -> bool
2652 method exit :
2653 uioh:uioh ->
2654 cancel:bool ->
2655 active:int ->
2656 first:int ->
2657 pan:int ->
2658 qsearch:string ->
2659 uioh option
2660 method getactive : int
2661 method getfirst : int
2662 method getqsearch : string
2663 method setqsearch : string -> unit
2664 method getpan : int
2665 end;;
2667 class virtual lvsourcebase = object
2668 val mutable m_active = 0
2669 val mutable m_first = 0
2670 val mutable m_qsearch = ""
2671 val mutable m_pan = 0
2672 method getactive = m_active
2673 method getfirst = m_first
2674 method getqsearch = m_qsearch
2675 method getpan = m_pan
2676 method setqsearch s = m_qsearch <- s
2677 end;;
2679 let withoutlastutf8 s =
2680 let len = String.length s in
2681 if len = 0
2682 then s
2683 else
2684 let rec find pos =
2685 if pos = 0
2686 then pos
2687 else
2688 let b = Char.code s.[pos] in
2689 if b land 0b110000 = 0b11000000
2690 then find (pos-1)
2691 else pos-1
2693 let first =
2694 if Char.code s.[len-1] land 0x80 = 0
2695 then len-1
2696 else find (len-1)
2698 String.sub s 0 first;
2701 let textentrykeyboard key _mask ((c, text, opthist, onkey, ondone), onleave) =
2702 let enttext te =
2703 state.mode <- Textentry (te, onleave);
2704 state.text <- "";
2705 enttext ();
2706 G.postRedisplay "textentrykeyboard enttext";
2708 let histaction cmd =
2709 match opthist with
2710 | None -> ()
2711 | Some (action, _) ->
2712 state.mode <- Textentry (
2713 (c, action cmd, opthist, onkey, ondone), onleave
2715 G.postRedisplay "textentry histaction"
2717 match key with
2718 | 0xff08 -> (* backspace *)
2719 let s = withoutlastutf8 text in
2720 let len = String.length s in
2721 if len = 0
2722 then (
2723 onleave Cancel;
2724 G.postRedisplay "textentrykeyboard after cancel";
2726 else (
2727 enttext (c, s, opthist, onkey, ondone)
2730 | 0xff0d ->
2731 ondone text;
2732 onleave Confirm;
2733 G.postRedisplay "textentrykeyboard after confirm"
2735 | 0xff52 -> histaction HCprev
2736 | 0xff54 -> histaction HCnext
2737 | 0xff50 -> histaction HCfirst
2738 | 0xff57 -> histaction HClast
2740 | 0xff1b -> (* escape*)
2741 if String.length text = 0
2742 then (
2743 begin match opthist with
2744 | None -> ()
2745 | Some (_, onhistcancel) -> onhistcancel ()
2746 end;
2747 onleave Cancel;
2748 state.text <- "";
2749 G.postRedisplay "textentrykeyboard after cancel2"
2751 else (
2752 enttext (c, "", opthist, onkey, ondone)
2755 | 0xff9f | 0xffff -> () (* delete *)
2757 | _ when key != 0 && key land 0xff00 != 0xff00 ->
2758 begin match onkey text key with
2759 | TEdone text ->
2760 ondone text;
2761 onleave Confirm;
2762 G.postRedisplay "textentrykeyboard after confirm2";
2764 | TEcont text ->
2765 enttext (c, text, opthist, onkey, ondone);
2767 | TEstop ->
2768 onleave Cancel;
2769 G.postRedisplay "textentrykeyboard after cancel3"
2771 | TEswitch te ->
2772 state.mode <- Textentry (te, onleave);
2773 G.postRedisplay "textentrykeyboard switch";
2774 end;
2776 | _ ->
2777 vlog "unhandled key %s" (Wsi.keyname key)
2780 let firstof first active =
2781 if first > active || abs (first - active) > fstate.maxrows - 1
2782 then max 0 (active - (fstate.maxrows/2))
2783 else first
2786 let calcfirst first active =
2787 if active > first
2788 then
2789 let rows = active - first in
2790 if rows > fstate.maxrows then active - fstate.maxrows else first
2791 else active
2794 let scrollph y maxy =
2795 let sh = (float (maxy + conf.winh) /. float conf.winh) in
2796 let sh = float conf.winh /. sh in
2797 let sh = max sh (float conf.scrollh) in
2799 let percent =
2800 if y = state.maxy
2801 then 1.0
2802 else float y /. float maxy
2804 let position = (float conf.winh -. sh) *. percent in
2806 let position =
2807 if position +. sh > float conf.winh
2808 then float conf.winh -. sh
2809 else position
2811 position, sh;
2814 let coe s = (s :> uioh);;
2816 class listview ~(source:lvsource) ~trusted ~modehash =
2817 object (self)
2818 val m_pan = source#getpan
2819 val m_first = source#getfirst
2820 val m_active = source#getactive
2821 val m_qsearch = source#getqsearch
2822 val m_prev_uioh = state.uioh
2824 method private elemunder y =
2825 let n = y / (fstate.fontsize+1) in
2826 if m_first + n < source#getitemcount
2827 then (
2828 if source#hasaction (m_first + n)
2829 then Some (m_first + n)
2830 else None
2832 else None
2834 method display =
2835 Gl.enable `blend;
2836 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
2837 GlDraw.color (0., 0., 0.) ~alpha:0.85;
2838 GlDraw.rect (0., 0.) (float conf.winw, float conf.winh);
2839 GlDraw.color (1., 1., 1.);
2840 Gl.enable `texture_2d;
2841 let fs = fstate.fontsize in
2842 let nfs = fs + 1 in
2843 let ww = fstate.wwidth in
2844 let tabw = 30.0*.ww in
2845 let itemcount = source#getitemcount in
2846 let rec loop row =
2847 if (row - m_first) * nfs > conf.winh
2848 then ()
2849 else (
2850 if row >= 0 && row < itemcount
2851 then (
2852 let (s, level) = source#getitem row in
2853 let y = (row - m_first) * nfs in
2854 let x = 5.0 +. float (level + m_pan) *. ww in
2855 if row = m_active
2856 then (
2857 Gl.disable `texture_2d;
2858 GlDraw.polygon_mode `both `line;
2859 GlDraw.color (1., 1., 1.) ~alpha:0.9;
2860 GlDraw.rect (1., float (y + 1))
2861 (float (conf.winw - conf.scrollbw - 1), float (y + fs + 3));
2862 GlDraw.polygon_mode `both `fill;
2863 GlDraw.color (1., 1., 1.);
2864 Gl.enable `texture_2d;
2867 let drawtabularstring s =
2868 let drawstr x s = drawstring1 fs (truncate x) (y+nfs) s in
2869 if trusted
2870 then
2871 let tabpos = try String.index s '\t' with Not_found -> -1 in
2872 if tabpos > 0
2873 then
2874 let len = String.length s - tabpos - 1 in
2875 let s1 = String.sub s 0 tabpos
2876 and s2 = String.sub s (tabpos + 1) len in
2877 let nx = drawstr x s1 in
2878 let sw = nx -. x in
2879 let x = x +. (max tabw sw) in
2880 drawstr x s2
2881 else
2882 drawstr x s
2883 else
2884 drawstr x s
2886 let _ = drawtabularstring s in
2887 loop (row+1)
2891 loop m_first;
2892 Gl.disable `blend;
2893 Gl.disable `texture_2d;
2895 method updownlevel incr =
2896 let len = source#getitemcount in
2897 let curlevel =
2898 if m_active >= 0 && m_active < len
2899 then snd (source#getitem m_active)
2900 else -1
2902 let rec flow i =
2903 if i = len then i-1 else if i = -1 then 0 else
2904 let _, l = source#getitem i in
2905 if l != curlevel then i else flow (i+incr)
2907 let active = flow m_active in
2908 let first = calcfirst m_first active in
2909 G.postRedisplay "outline updownlevel";
2910 {< m_active = active; m_first = first >}
2912 method private key1 key mask =
2913 let set1 active first qsearch =
2914 coe {< m_active = active; m_first = first; m_qsearch = qsearch >}
2916 let search active pattern incr =
2917 let dosearch re =
2918 let rec loop n =
2919 if n >= 0 && n < source#getitemcount
2920 then (
2921 let s, _ = source#getitem n in
2923 (try ignore (Str.search_forward re s 0); true
2924 with Not_found -> false)
2925 then Some n
2926 else loop (n + incr)
2928 else None
2930 loop active
2933 let re = Str.regexp_case_fold pattern in
2934 dosearch re
2935 with Failure s ->
2936 state.text <- s;
2937 None
2939 let itemcount = source#getitemcount in
2940 let find start incr =
2941 let rec find i =
2942 if i = -1 || i = itemcount
2943 then -1
2944 else (
2945 if source#hasaction i
2946 then i
2947 else find (i + incr)
2950 find start
2952 let set active first =
2953 let first = bound first 0 (itemcount - fstate.maxrows) in
2954 state.text <- "";
2955 coe {< m_active = active; m_first = first >}
2957 let navigate incr =
2958 let isvisible first n = n >= first && n - first <= fstate.maxrows in
2959 let active, first =
2960 let incr1 = if incr > 0 then 1 else -1 in
2961 if isvisible m_first m_active
2962 then
2963 let next =
2964 let next = m_active + incr in
2965 let next =
2966 if next < 0 || next >= itemcount
2967 then -1
2968 else find next incr1
2970 if next = -1 || abs (m_active - next) > fstate.maxrows
2971 then -1
2972 else next
2974 if next = -1
2975 then
2976 let first = m_first + incr in
2977 let first = bound first 0 (itemcount - 1) in
2978 let next =
2979 let next = m_active + incr in
2980 let next = bound next 0 (itemcount - 1) in
2981 find next ~-incr1
2983 let active = if next = -1 then m_active else next in
2984 active, first
2985 else
2986 let first = min next m_first in
2987 let first =
2988 if abs (next - first) > fstate.maxrows
2989 then first + incr
2990 else first
2992 next, first
2993 else
2994 let first = m_first + incr in
2995 let first = bound first 0 (itemcount - 1) in
2996 let active =
2997 let next = m_active + incr in
2998 let next = bound next 0 (itemcount - 1) in
2999 let next = find next incr1 in
3000 let active =
3001 if next = -1 || abs (m_active - first) > fstate.maxrows
3002 then (
3003 let active = if m_active = -1 then next else m_active in
3004 active
3006 else next
3008 if isvisible first active
3009 then active
3010 else -1
3012 active, first
3014 G.postRedisplay "listview navigate";
3015 set active first;
3017 match key with
3018 | (0x72|0x73) when Wsi.withctrl mask -> (* ctrl-r/ctlr-s *)
3019 let incr = if key = 0x72 then -1 else 1 in
3020 let active, first =
3021 match search (m_active + incr) m_qsearch incr with
3022 | None ->
3023 state.text <- m_qsearch ^ " [not found]";
3024 m_active, m_first
3025 | Some active ->
3026 state.text <- m_qsearch;
3027 active, firstof m_first active
3029 G.postRedisplay "listview ctrl-r/s";
3030 set1 active first m_qsearch;
3032 | 0xff08 -> (* backspace *)
3033 if String.length m_qsearch = 0
3034 then coe self
3035 else (
3036 let qsearch = withoutlastutf8 m_qsearch in
3037 let len = String.length qsearch in
3038 if len = 0
3039 then (
3040 state.text <- "";
3041 G.postRedisplay "listview empty qsearch";
3042 set1 m_active m_first "";
3044 else
3045 let active, first =
3046 match search m_active qsearch ~-1 with
3047 | None ->
3048 state.text <- qsearch ^ " [not found]";
3049 m_active, m_first
3050 | Some active ->
3051 state.text <- qsearch;
3052 active, firstof m_first active
3054 G.postRedisplay "listview backspace qsearch";
3055 set1 active first qsearch
3058 | key when (key != 0 && key land 0xff00 != 0xff00) ->
3059 let pattern = m_qsearch ^ Wsi.toutf8 key in
3060 let active, first =
3061 match search m_active pattern 1 with
3062 | None ->
3063 state.text <- pattern ^ " [not found]";
3064 m_active, m_first
3065 | Some active ->
3066 state.text <- pattern;
3067 active, firstof m_first active
3069 G.postRedisplay "listview qsearch add";
3070 set1 active first pattern;
3072 | 0xff1b -> (* escape *)
3073 state.text <- "";
3074 if String.length m_qsearch = 0
3075 then (
3076 G.postRedisplay "list view escape";
3077 begin
3078 match
3079 source#exit (coe self) true m_active m_first m_pan m_qsearch
3080 with
3081 | None -> m_prev_uioh
3082 | Some uioh -> uioh
3085 else (
3086 G.postRedisplay "list view kill qsearch";
3087 source#setqsearch "";
3088 coe {< m_qsearch = "" >}
3091 | 0xff0d -> (* return *)
3092 state.text <- "";
3093 let self = {< m_qsearch = "" >} in
3094 source#setqsearch "";
3095 let opt =
3096 G.postRedisplay "listview enter";
3097 if m_active >= 0 && m_active < source#getitemcount
3098 then (
3099 source#exit (coe self) false m_active m_first m_pan "";
3101 else (
3102 source#exit (coe self) true m_active m_first m_pan "";
3105 begin match opt with
3106 | None -> m_prev_uioh
3107 | Some uioh -> uioh
3110 | 0xff9f | 0xffff -> (* delete *)
3111 coe self
3113 | 0xff52 -> navigate ~-1 (* up *)
3114 | 0xff54 -> navigate 1 (* down *)
3115 | 0xff55 -> navigate ~-(fstate.maxrows) (* prior *)
3116 | 0xff56 -> navigate fstate.maxrows (* next *)
3118 | 0xff53 -> (* right *)
3119 state.text <- "";
3120 G.postRedisplay "listview right";
3121 coe {< m_pan = m_pan - 1 >}
3123 | 0xff51 -> (* left *)
3124 state.text <- "";
3125 G.postRedisplay "listview left";
3126 coe {< m_pan = m_pan + 1 >}
3128 | 0xff50 -> (* home *)
3129 let active = find 0 1 in
3130 G.postRedisplay "listview home";
3131 set active 0;
3133 | 0xff57 -> (* end *)
3134 let first = max 0 (itemcount - fstate.maxrows) in
3135 let active = find (itemcount - 1) ~-1 in
3136 G.postRedisplay "listview end";
3137 set active first;
3139 | key when (key = 0 || key land 0xff00 = 0xff00) ->
3140 coe self
3142 | _ ->
3143 dolog "listview unknown key %#x" key; coe self
3145 method key key mask =
3146 match state.mode with
3147 | Textentry te -> textentrykeyboard key mask te; coe self
3148 | _ -> self#key1 key mask
3150 method button button down x y _ =
3151 let opt =
3152 match button with
3153 | 1 when x > conf.winw - conf.scrollbw ->
3154 G.postRedisplay "listview scroll";
3155 if down
3156 then
3157 let _, position, sh = self#scrollph in
3158 if y > truncate position && y < truncate (position +. sh)
3159 then (
3160 state.mstate <- Mscrolly;
3161 Some (coe self)
3163 else
3164 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3165 let first = truncate (s *. float source#getitemcount) in
3166 let first = min source#getitemcount first in
3167 Some (coe {< m_first = first; m_active = first >})
3168 else (
3169 state.mstate <- Mnone;
3170 Some (coe self);
3172 | 1 when not down ->
3173 begin match self#elemunder y with
3174 | Some n ->
3175 G.postRedisplay "listview click";
3176 source#exit
3177 (coe {< m_active = n >}) false n m_first m_pan m_qsearch
3178 | _ ->
3179 Some (coe self)
3181 | n when (n == 4 || n == 5) && not down ->
3182 let len = source#getitemcount in
3183 let first =
3184 if n = 5 && m_first + fstate.maxrows >= len
3185 then
3186 m_first
3187 else
3188 let first = m_first + (if n == 4 then -1 else 1) in
3189 bound first 0 (len - 1)
3191 G.postRedisplay "listview wheel";
3192 Some (coe {< m_first = first >})
3193 | _ ->
3194 Some (coe self)
3196 match opt with
3197 | None -> m_prev_uioh
3198 | Some uioh -> uioh
3200 method motion _ y =
3201 match state.mstate with
3202 | Mscrolly ->
3203 let s = float (max 0 (y - conf.scrollh)) /. float conf.winh in
3204 let first = truncate (s *. float source#getitemcount) in
3205 let first = min source#getitemcount first in
3206 G.postRedisplay "listview motion";
3207 coe {< m_first = first; m_active = first >}
3208 | _ -> coe self
3210 method pmotion x y =
3211 if x < conf.winw - conf.scrollbw
3212 then
3213 let n =
3214 match self#elemunder y with
3215 | None -> Wsi.setcursor Wsi.CURSOR_INHERIT; m_active
3216 | Some n -> Wsi.setcursor Wsi.CURSOR_INFO; n
3218 let o =
3219 if n != m_active
3220 then (G.postRedisplay "listview pmotion"; {< m_active = n >})
3221 else self
3223 coe o
3224 else (
3225 Wsi.setcursor Wsi.CURSOR_INHERIT;
3226 coe self
3229 method infochanged _ = ()
3231 method scrollpw = (0, 0.0, 0.0)
3232 method scrollph =
3233 let nfs = fstate.fontsize + 1 in
3234 let y = m_first * nfs in
3235 let itemcount = source#getitemcount in
3236 let maxi = max 0 (itemcount - fstate.maxrows) in
3237 let maxy = maxi * nfs in
3238 let p, h = scrollph y maxy in
3239 conf.scrollbw, p, h
3241 method modehash = modehash
3242 end;;
3244 class outlinelistview ~source =
3245 object (self)
3246 inherit listview
3247 ~source:(source :> lvsource)
3248 ~trusted:false
3249 ~modehash:(findkeyhash conf "outline")
3250 as super
3252 method key key mask =
3253 let calcfirst first active =
3254 if active > first
3255 then
3256 let rows = active - first in
3257 if rows > fstate.maxrows then active - fstate.maxrows else first
3258 else active
3260 let navigate incr =
3261 let active = m_active + incr in
3262 let active = bound active 0 (source#getitemcount - 1) in
3263 let first = calcfirst m_first active in
3264 G.postRedisplay "outline navigate";
3265 coe {< m_active = active; m_first = first >}
3267 let ctrl = Wsi.withctrl mask in
3268 match key with
3269 | 110 when ctrl -> (* ctrl-n *)
3270 source#narrow m_qsearch;
3271 G.postRedisplay "outline ctrl-n";
3272 coe {< m_first = 0; m_active = 0 >}
3274 | 117 when ctrl -> (* ctrl-u *)
3275 source#denarrow;
3276 G.postRedisplay "outline ctrl-u";
3277 state.text <- "";
3278 coe {< m_first = 0; m_active = 0 >}
3280 | 108 when ctrl -> (* ctrl-l *)
3281 let first = m_active - (fstate.maxrows / 2) in
3282 G.postRedisplay "outline ctrl-l";
3283 coe {< m_first = first >}
3285 | 0xff9f | 0xffff -> (* delete *)
3286 source#remove m_active;
3287 G.postRedisplay "outline delete";
3288 let active = max 0 (m_active-1) in
3289 coe {< m_first = firstof m_first active;
3290 m_active = active >}
3292 | 0xff52 -> navigate ~-1 (* up *)
3293 | 0xff54 -> navigate 1 (* down *)
3294 | 0xff55 -> (* prior *)
3295 navigate ~-(fstate.maxrows)
3296 | 0xff56 -> (* next *)
3297 navigate fstate.maxrows
3299 | 0xff53 -> (* [ctrl-]right *)
3300 let o =
3301 if ctrl
3302 then (
3303 G.postRedisplay "outline ctrl right";
3304 {< m_pan = m_pan + 1 >}
3306 else self#updownlevel 1
3308 coe o
3310 | 0xff51 -> (* [ctrl-]left *)
3311 let o =
3312 if ctrl
3313 then (
3314 G.postRedisplay "outline ctrl left";
3315 {< m_pan = m_pan - 1 >}
3317 else self#updownlevel ~-1
3319 coe o
3321 | 0xff50 -> (* home *)
3322 G.postRedisplay "outline home";
3323 coe {< m_first = 0; m_active = 0 >}
3325 | 0xff57 -> (* end *)
3326 let active = source#getitemcount - 1 in
3327 let first = max 0 (active - fstate.maxrows) in
3328 G.postRedisplay "outline end";
3329 coe {< m_active = active; m_first = first >}
3331 | _ -> super#key key mask
3334 let outlinesource usebookmarks =
3335 let empty = [||] in
3336 (object
3337 inherit lvsourcebase
3338 val mutable m_items = empty
3339 val mutable m_orig_items = empty
3340 val mutable m_prev_items = empty
3341 val mutable m_narrow_pattern = ""
3342 val mutable m_hadremovals = false
3344 method getitemcount =
3345 Array.length m_items + (if m_hadremovals then 1 else 0)
3347 method getitem n =
3348 if n == Array.length m_items && m_hadremovals
3349 then
3350 ("[Confirm removal]", 0)
3351 else
3352 let s, n, _ = m_items.(n) in
3353 (s, n)
3355 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3356 ignore (uioh, first, qsearch);
3357 let confrimremoval = m_hadremovals && active = Array.length m_items in
3358 let items =
3359 if String.length m_narrow_pattern = 0
3360 then m_orig_items
3361 else m_items
3363 if not cancel
3364 then (
3365 if not confrimremoval
3366 then(
3367 let _, _, anchor = m_items.(active) in
3368 gotoanchor anchor;
3369 m_items <- items;
3371 else (
3372 state.bookmarks <- Array.to_list m_items;
3373 m_orig_items <- m_items;
3376 else m_items <- items;
3377 m_pan <- pan;
3378 None
3380 method hasaction _ = true
3382 method greetmsg =
3383 if Array.length m_items != Array.length m_orig_items
3384 then "Narrowed to " ^ m_narrow_pattern ^ " (ctrl-u to restore)"
3385 else ""
3387 method narrow pattern =
3388 let reopt = try Some (Str.regexp_case_fold pattern) with _ -> None in
3389 match reopt with
3390 | None -> ()
3391 | Some re ->
3392 let rec loop accu n =
3393 if n = -1
3394 then (
3395 m_narrow_pattern <- pattern;
3396 m_items <- Array.of_list accu
3398 else
3399 let (s, _, _) as o = m_items.(n) in
3400 let accu =
3401 if (try ignore (Str.search_forward re s 0); true
3402 with Not_found -> false)
3403 then o :: accu
3404 else accu
3406 loop accu (n-1)
3408 loop [] (Array.length m_items - 1)
3410 method denarrow =
3411 m_orig_items <- (
3412 if usebookmarks
3413 then Array.of_list state.bookmarks
3414 else state.outlines
3416 m_items <- m_orig_items
3418 method remove m =
3419 if usebookmarks
3420 then
3421 if m >= 0 && m < Array.length m_items
3422 then (
3423 m_hadremovals <- true;
3424 m_items <- Array.init (Array.length m_items - 1) (fun n ->
3425 let n = if n >= m then n+1 else n in
3426 m_items.(n)
3430 method reset anchor items =
3431 m_hadremovals <- false;
3432 if m_orig_items == empty || m_prev_items != items
3433 then (
3434 m_orig_items <- items;
3435 if String.length m_narrow_pattern = 0
3436 then m_items <- items;
3438 m_prev_items <- items;
3439 let rely = getanchory anchor in
3440 let active =
3441 let rec loop n best bestd =
3442 if n = Array.length m_items
3443 then best
3444 else
3445 let (_, _, anchor) = m_items.(n) in
3446 let orely = getanchory anchor in
3447 let d = abs (orely - rely) in
3448 if d < bestd
3449 then loop (n+1) n d
3450 else loop (n+1) best bestd
3452 loop 0 ~-1 max_int
3454 m_active <- active;
3455 m_first <- firstof m_first active
3456 end)
3459 let enterselector usebookmarks =
3460 let source = outlinesource usebookmarks in
3461 fun errmsg ->
3462 let outlines =
3463 if usebookmarks
3464 then Array.of_list state.bookmarks
3465 else state.outlines
3467 if Array.length outlines = 0
3468 then (
3469 showtext ' ' errmsg;
3471 else (
3472 state.text <- source#greetmsg;
3473 Wsi.setcursor Wsi.CURSOR_INHERIT;
3474 let anchor = getanchor () in
3475 source#reset anchor outlines;
3476 state.uioh <- coe (new outlinelistview ~source);
3477 G.postRedisplay "enter selector";
3481 let enteroutlinemode =
3482 let f = enterselector false in
3483 fun ()-> f "Document has no outline";
3486 let enterbookmarkmode =
3487 let f = enterselector true in
3488 fun () -> f "Document has no bookmarks (yet)";
3491 let color_of_string s =
3492 Scanf.sscanf s "%d/%d/%d" (fun r g b ->
3493 (float r /. 256.0, float g /. 256.0, float b /. 256.0)
3497 let color_to_string (r, g, b) =
3498 let r = truncate (r *. 256.0)
3499 and g = truncate (g *. 256.0)
3500 and b = truncate (b *. 256.0) in
3501 Printf.sprintf "%d/%d/%d" r g b
3504 let irect_of_string s =
3505 Scanf.sscanf s "%d/%d/%d/%d" (fun x0 y0 x1 y1 -> (x0,y0,x1,y1))
3508 let irect_to_string (x0,y0,x1,y1) =
3509 Printf.sprintf "%d/%d/%d/%d" x0 y0 x1 y1
3512 let makecheckers () =
3513 (* Appropriated from lablGL-1.04/LablGlut/examples/lablGL/checker.ml which had
3514 following to say:
3515 converted by Issac Trotts. July 25, 2002 *)
3516 let image_height = 64
3517 and image_width = 64 in
3519 let make_image () =
3520 let image =
3521 GlPix.create `ubyte ~format:`rgb ~width:image_width ~height:image_height
3523 for i = 0 to image_width - 1 do
3524 for j = 0 to image_height - 1 do
3525 Raw.sets (GlPix.to_raw image) ~pos:(3*(i*image_height+j))
3526 (if (i land 8 ) lxor (j land 8) = 0
3527 then [|255;255;255|] else [|200;200;200|])
3528 done
3529 done;
3530 image
3532 let image = make_image () in
3533 let id = GlTex.gen_texture () in
3534 GlTex.bind_texture `texture_2d id;
3535 GlPix.store (`unpack_alignment 1);
3536 GlTex.image2d image;
3537 List.iter (GlTex.parameter ~target:`texture_2d)
3538 [ `wrap_s `repeat;
3539 `wrap_t `repeat;
3540 `mag_filter `nearest;
3541 `min_filter `nearest ];
3545 let setcheckers enabled =
3546 match state.texid with
3547 | None ->
3548 if enabled then state.texid <- Some (makecheckers ())
3550 | Some texid ->
3551 if not enabled
3552 then (
3553 GlTex.delete_texture texid;
3554 state.texid <- None;
3558 let int_of_string_with_suffix s =
3559 let l = String.length s in
3560 let s1, shift =
3561 if l > 1
3562 then
3563 let suffix = Char.lowercase s.[l-1] in
3564 match suffix with
3565 | 'k' -> String.sub s 0 (l-1), 10
3566 | 'm' -> String.sub s 0 (l-1), 20
3567 | 'g' -> String.sub s 0 (l-1), 30
3568 | _ -> s, 0
3569 else s, 0
3571 let n = int_of_string s1 in
3572 let m = n lsl shift in
3573 if m < 0 || m < n
3574 then raise (Failure "value too large")
3575 else m
3578 let string_with_suffix_of_int n =
3579 if n = 0
3580 then "0"
3581 else
3582 let n, s =
3583 if n = 0
3584 then 0, ""
3585 else (
3586 if n land ((1 lsl 20) - 1) = 0
3587 then n lsr 20, "M"
3588 else (
3589 if n land ((1 lsl 10) - 1) = 0
3590 then n lsr 10, "K"
3591 else n, ""
3595 let rec loop s n =
3596 let h = n mod 1000 in
3597 let n = n / 1000 in
3598 if n = 0
3599 then string_of_int h ^ s
3600 else (
3601 let s = Printf.sprintf "_%03d%s" h s in
3602 loop s n
3605 loop "" n ^ s;
3608 let defghyllscroll = (40, 8, 32);;
3609 let ghyllscroll_of_string s =
3610 let (n, a, b) as nab =
3611 if s = "default"
3612 then defghyllscroll
3613 else Scanf.sscanf s "%u,%u,%u" (fun n a b -> n, a, b)
3615 if n <= a || n <= b || a >= b
3616 then failwith "invalid ghyll N,A,B (N <= A, A < B, N <= B)";
3617 nab;
3620 let ghyllscroll_to_string ((n, a, b) as nab) =
3621 if nab = defghyllscroll
3622 then "default"
3623 else Printf.sprintf "%d,%d,%d" n a b;
3626 let describe_location () =
3627 let f (fn, _) l =
3628 if fn = -1 then l.pageno, l.pageno else fn, l.pageno
3630 let fn, ln = List.fold_left f (-1, -1) state.layout in
3631 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
3632 let percent =
3633 if maxy <= 0
3634 then 100.
3635 else (100. *. (float state.y /. float maxy))
3637 if fn = ln
3638 then
3639 Printf.sprintf "page %d of %d [%.2f%%]"
3640 (fn+1) state.pagecount percent
3641 else
3642 Printf.sprintf
3643 "pages %d-%d of %d [%.2f%%]"
3644 (fn+1) (ln+1) state.pagecount percent
3647 let enterinfomode =
3648 let btos b = if b then "\xe2\x88\x9a" else "" in
3649 let showextended = ref false in
3650 let leave mode = function
3651 | Confirm -> state.mode <- mode
3652 | Cancel -> state.mode <- mode in
3653 let src =
3654 (object
3655 val mutable m_first_time = true
3656 val mutable m_l = []
3657 val mutable m_a = [||]
3658 val mutable m_prev_uioh = nouioh
3659 val mutable m_prev_mode = View
3661 inherit lvsourcebase
3663 method reset prev_mode prev_uioh =
3664 m_a <- Array.of_list (List.rev m_l);
3665 m_l <- [];
3666 m_prev_mode <- prev_mode;
3667 m_prev_uioh <- prev_uioh;
3668 if m_first_time
3669 then (
3670 let rec loop n =
3671 if n >= Array.length m_a
3672 then ()
3673 else
3674 match m_a.(n) with
3675 | _, _, _, Action _ -> m_active <- n
3676 | _ -> loop (n+1)
3678 loop 0;
3679 m_first_time <- false;
3682 method int name get set =
3683 m_l <-
3684 (name, `int get, 1, Action (
3685 fun u ->
3686 let ondone s =
3687 try set (int_of_string s)
3688 with exn ->
3689 state.text <- Printf.sprintf "bad integer `%s': %s"
3690 s (Printexc.to_string exn)
3692 state.text <- "";
3693 let te = name ^ ": ", "", None, intentry, ondone in
3694 state.mode <- Textentry (te, leave m_prev_mode);
3696 )) :: m_l
3698 method int_with_suffix name get set =
3699 m_l <-
3700 (name, `intws get, 1, Action (
3701 fun u ->
3702 let ondone s =
3703 try set (int_of_string_with_suffix s)
3704 with exn ->
3705 state.text <- Printf.sprintf "bad integer `%s': %s"
3706 s (Printexc.to_string exn)
3708 state.text <- "";
3709 let te =
3710 name ^ ": ", "", None, intentry_with_suffix, ondone
3712 state.mode <- Textentry (te, leave m_prev_mode);
3714 )) :: m_l
3716 method bool ?(offset=1) ?(btos=btos) name get set =
3717 m_l <-
3718 (name, `bool (btos, get), offset, Action (
3719 fun u ->
3720 let v = get () in
3721 set (not v);
3723 )) :: m_l
3725 method color name get set =
3726 m_l <-
3727 (name, `color get, 1, Action (
3728 fun u ->
3729 let invalid = (nan, nan, nan) in
3730 let ondone s =
3731 let c =
3732 try color_of_string s
3733 with exn ->
3734 state.text <- Printf.sprintf "bad color `%s': %s"
3735 s (Printexc.to_string exn);
3736 invalid
3738 if c <> invalid
3739 then set c;
3741 let te = name ^ ": ", "", None, textentry, ondone in
3742 state.text <- color_to_string (get ());
3743 state.mode <- Textentry (te, leave m_prev_mode);
3745 )) :: m_l
3747 method string name get set =
3748 m_l <-
3749 (name, `string get, 1, Action (
3750 fun u ->
3751 let ondone s = set s in
3752 let te = name ^ ": ", "", None, textentry, ondone in
3753 state.mode <- Textentry (te, leave m_prev_mode);
3755 )) :: m_l
3757 method colorspace name get set =
3758 m_l <-
3759 (name, `string get, 1, Action (
3760 fun _ ->
3761 let source =
3762 let vals = [| "rgb"; "bgr"; "gray" |] in
3763 (object
3764 inherit lvsourcebase
3766 initializer
3767 m_active <- int_of_colorspace conf.colorspace;
3768 m_first <- 0;
3770 method getitemcount = Array.length vals
3771 method getitem n = (vals.(n), 0)
3772 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3773 ignore (uioh, first, pan, qsearch);
3774 if not cancel then set active;
3775 None
3776 method hasaction _ = true
3777 end)
3779 state.text <- "";
3780 let modehash = findkeyhash conf "info" in
3781 coe (new listview ~source ~trusted:true ~modehash)
3782 )) :: m_l
3784 method caption s offset =
3785 m_l <- (s, `empty, offset, Noaction) :: m_l
3787 method caption2 s f offset =
3788 m_l <- (s, `string f, offset, Noaction) :: m_l
3790 method getitemcount = Array.length m_a
3792 method getitem n =
3793 let tostr = function
3794 | `int f -> string_of_int (f ())
3795 | `intws f -> string_with_suffix_of_int (f ())
3796 | `string f -> f ()
3797 | `color f -> color_to_string (f ())
3798 | `bool (btos, f) -> btos (f ())
3799 | `empty -> ""
3801 let name, t, offset, _ = m_a.(n) in
3802 ((let s = tostr t in
3803 if String.length s > 0
3804 then Printf.sprintf "%s\t%s" name s
3805 else name),
3806 offset)
3808 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
3809 let uiohopt =
3810 if not cancel
3811 then (
3812 m_qsearch <- qsearch;
3813 let uioh =
3814 match m_a.(active) with
3815 | _, _, _, Action f -> f uioh
3816 | _ -> uioh
3818 Some uioh
3820 else None
3822 m_active <- active;
3823 m_first <- first;
3824 m_pan <- pan;
3825 uiohopt
3827 method hasaction n =
3828 match m_a.(n) with
3829 | _, _, _, Action _ -> true
3830 | _ -> false
3831 end)
3833 let rec fillsrc prevmode prevuioh =
3834 let sep () = src#caption "" 0 in
3835 let colorp name get set =
3836 src#string name
3837 (fun () -> color_to_string (get ()))
3838 (fun v ->
3840 let c = color_of_string v in
3841 set c
3842 with exn ->
3843 state.text <- Printf.sprintf "bad color `%s': %s"
3844 v (Printexc.to_string exn);
3847 let oldmode = state.mode in
3848 let birdseye = isbirdseye state.mode in
3850 src#caption (if birdseye then "Setup (Bird's eye)" else "Setup") 0;
3852 src#bool "presentation mode"
3853 (fun () -> conf.presentation)
3854 (fun v ->
3855 conf.presentation <- v;
3856 state.anchor <- getanchor ();
3857 represent ());
3859 src#bool "ignore case in searches"
3860 (fun () -> conf.icase)
3861 (fun v -> conf.icase <- v);
3863 src#bool "preload"
3864 (fun () -> conf.preload)
3865 (fun v -> conf.preload <- v);
3867 src#bool "highlight links"
3868 (fun () -> conf.hlinks)
3869 (fun v -> conf.hlinks <- v);
3871 src#bool "under info"
3872 (fun () -> conf.underinfo)
3873 (fun v -> conf.underinfo <- v);
3875 src#bool "persistent bookmarks"
3876 (fun () -> conf.savebmarks)
3877 (fun v -> conf.savebmarks <- v);
3879 src#bool "proportional display"
3880 (fun () -> conf.proportional)
3881 (fun v -> reqlayout conf.angle v);
3883 src#bool "trim margins"
3884 (fun () -> conf.trimmargins)
3885 (fun v -> settrim v conf.trimfuzz; fillsrc prevmode prevuioh);
3887 src#bool "persistent location"
3888 (fun () -> conf.jumpback)
3889 (fun v -> conf.jumpback <- v);
3891 sep ();
3892 src#int "inter-page space"
3893 (fun () -> conf.interpagespace)
3894 (fun n ->
3895 conf.interpagespace <- n;
3896 let pageno, py =
3897 match state.layout with
3898 | [] -> 0, 0
3899 | l :: _ ->
3900 l.pageno, l.pagey
3902 state.maxy <- calcheight ();
3903 let y = getpagey pageno in
3904 gotoy (y + py)
3907 src#int "page bias"
3908 (fun () -> conf.pagebias)
3909 (fun v -> conf.pagebias <- v);
3911 src#int "scroll step"
3912 (fun () -> conf.scrollstep)
3913 (fun n -> conf.scrollstep <- n);
3915 src#int "auto scroll step"
3916 (fun () ->
3917 match state.autoscroll with
3918 | Some step -> step
3919 | _ -> conf.autoscrollstep)
3920 (fun n ->
3921 if state.autoscroll <> None
3922 then state.autoscroll <- Some n;
3923 conf.autoscrollstep <- n);
3925 src#int "zoom"
3926 (fun () -> truncate (conf.zoom *. 100.))
3927 (fun v -> setzoom ((float v) /. 100.));
3929 src#int "rotation"
3930 (fun () -> conf.angle)
3931 (fun v -> reqlayout v conf.proportional);
3933 src#int "scroll bar width"
3934 (fun () -> state.scrollw)
3935 (fun v ->
3936 state.scrollw <- v;
3937 conf.scrollbw <- v;
3938 reshape conf.winw conf.winh;
3941 src#int "scroll handle height"
3942 (fun () -> conf.scrollh)
3943 (fun v -> conf.scrollh <- v;);
3945 src#int "thumbnail width"
3946 (fun () -> conf.thumbw)
3947 (fun v ->
3948 conf.thumbw <- min 4096 v;
3949 match oldmode with
3950 | Birdseye beye ->
3951 leavebirdseye beye false;
3952 enterbirdseye ()
3953 | _ -> ()
3956 src#string "columns"
3957 (fun () ->
3958 match conf.columns with
3959 | None -> "1"
3960 | Some (multicol, _) -> columns_to_string multicol)
3961 (fun v ->
3962 let n, a, b = columns_of_string v in
3963 setcolumns n a b);
3965 sep ();
3966 src#caption "Presentation mode" 0;
3967 src#bool "scrollbar visible"
3968 (fun () -> conf.scrollbarinpm)
3969 (fun v ->
3970 if v != conf.scrollbarinpm
3971 then (
3972 conf.scrollbarinpm <- v;
3973 if conf.presentation
3974 then (
3975 state.scrollw <- if v then conf.scrollbw else 0;
3976 reshape conf.winw conf.winh;
3981 sep ();
3982 src#caption "Pixmap cache" 0;
3983 src#int_with_suffix "size (advisory)"
3984 (fun () -> conf.memlimit)
3985 (fun v -> conf.memlimit <- v);
3987 src#caption2 "used"
3988 (fun () -> Printf.sprintf "%s bytes, %d tiles"
3989 (string_with_suffix_of_int state.memused)
3990 (Hashtbl.length state.tilemap)) 1;
3992 sep ();
3993 src#caption "Layout" 0;
3994 src#caption2 "Dimension"
3995 (fun () ->
3996 Printf.sprintf "%dx%d (virtual %dx%d)"
3997 conf.winw conf.winh
3998 state.w state.maxy)
4000 if conf.debug
4001 then
4002 src#caption2 "Position" (fun () ->
4003 Printf.sprintf "%dx%d" state.x state.y
4005 else
4006 src#caption2 "Visible" (fun () -> describe_location ()) 1
4009 sep ();
4010 src#bool ~offset:0 ~btos:(fun v -> if v then "(on)" else "(off)")
4011 "Save these parameters as global defaults at exit"
4012 (fun () -> conf.bedefault)
4013 (fun v -> conf.bedefault <- v)
4016 sep ();
4017 let btos b = if b then "\xc2\xab" else "\xc2\xbb" in
4018 src#bool ~offset:0 ~btos "Extended parameters"
4019 (fun () -> !showextended)
4020 (fun v -> showextended := v; fillsrc prevmode prevuioh);
4021 if !showextended
4022 then (
4023 src#bool "checkers"
4024 (fun () -> conf.checkers)
4025 (fun v -> conf.checkers <- v; setcheckers v);
4026 src#bool "update cursor"
4027 (fun () -> conf.updatecurs)
4028 (fun v -> conf.updatecurs <- v);
4029 src#bool "verbose"
4030 (fun () -> conf.verbose)
4031 (fun v -> conf.verbose <- v);
4032 src#bool "invert colors"
4033 (fun () -> conf.invert)
4034 (fun v -> conf.invert <- v);
4035 src#bool "max fit"
4036 (fun () -> conf.maxhfit)
4037 (fun v -> conf.maxhfit <- v);
4038 src#bool "redirect stderr"
4039 (fun () -> conf.redirectstderr)
4040 (fun v -> conf.redirectstderr <- v; redirectstderr ());
4041 src#string "uri launcher"
4042 (fun () -> conf.urilauncher)
4043 (fun v -> conf.urilauncher <- v);
4044 src#string "path launcher"
4045 (fun () -> conf.pathlauncher)
4046 (fun v -> conf.pathlauncher <- v);
4047 src#string "tile size"
4048 (fun () -> Printf.sprintf "%dx%d" conf.tilew conf.tileh)
4049 (fun v ->
4051 let w, h = Scanf.sscanf v "%dx%d" (fun w h -> w, h) in
4052 conf.tileh <- max 64 w;
4053 conf.tilew <- max 64 h;
4054 flushtiles ();
4055 with exn ->
4056 state.text <- Printf.sprintf "bad tile size `%s': %s"
4057 v (Printexc.to_string exn));
4058 src#int "texture count"
4059 (fun () -> conf.texcount)
4060 (fun v ->
4061 if realloctexts v
4062 then conf.texcount <- v
4063 else showtext '!' " Failed to set texture count please retry later"
4065 src#int "slice height"
4066 (fun () -> conf.sliceheight)
4067 (fun v ->
4068 conf.sliceheight <- v;
4069 wcmd "sliceh %d" conf.sliceheight;
4071 src#int "anti-aliasing level"
4072 (fun () -> conf.aalevel)
4073 (fun v ->
4074 conf.aalevel <- bound v 0 8;
4075 state.anchor <- getanchor ();
4076 opendoc state.path state.password;
4078 src#int "ui font size"
4079 (fun () -> fstate.fontsize)
4080 (fun v -> setfontsize (bound v 5 100));
4081 colorp "background color"
4082 (fun () -> conf.bgcolor)
4083 (fun v -> conf.bgcolor <- v);
4084 src#bool "crop hack"
4085 (fun () -> conf.crophack)
4086 (fun v -> conf.crophack <- v);
4087 src#string "trim fuzz"
4088 (fun () -> irect_to_string conf.trimfuzz)
4089 (fun v ->
4091 conf.trimfuzz <- irect_of_string v;
4092 if conf.trimmargins
4093 then settrim true conf.trimfuzz;
4094 with exn ->
4095 state.text <- Printf.sprintf "bad irect `%s': %s"
4096 v (Printexc.to_string exn)
4098 src#string "throttle"
4099 (fun () ->
4100 match conf.maxwait with
4101 | None -> "show place holder if page is not ready"
4102 | Some time ->
4103 if time = infinity
4104 then "wait for page to fully render"
4105 else
4106 "wait " ^ string_of_float time
4107 ^ " seconds before showing placeholder"
4109 (fun v ->
4111 let f = float_of_string v in
4112 if f <= 0.0
4113 then conf.maxwait <- None
4114 else conf.maxwait <- Some f
4115 with exn ->
4116 state.text <- Printf.sprintf "bad time `%s': %s"
4117 v (Printexc.to_string exn)
4119 src#string "ghyll scroll"
4120 (fun () ->
4121 match conf.ghyllscroll with
4122 | None -> ""
4123 | Some nab -> ghyllscroll_to_string nab
4125 (fun v ->
4127 let gs =
4128 if String.length v = 0
4129 then None
4130 else Some (ghyllscroll_of_string v)
4132 conf.ghyllscroll <- gs
4133 with exn ->
4134 state.text <- Printf.sprintf "bad ghyll `%s': %s"
4135 v (Printexc.to_string exn)
4137 src#string "selection command"
4138 (fun () -> conf.selcmd)
4139 (fun v -> conf.selcmd <- v);
4140 src#colorspace "color space"
4141 (fun () -> colorspace_to_string conf.colorspace)
4142 (fun v ->
4143 conf.colorspace <- colorspace_of_int v;
4144 wcmd "cs %d" v;
4145 load state.layout;
4149 sep ();
4150 src#caption "Document" 0;
4151 List.iter (fun (_, s) -> src#caption s 1) state.docinfo;
4152 src#caption2 "Pages"
4153 (fun () -> string_of_int state.pagecount) 1;
4154 src#caption2 "Dimensions"
4155 (fun () -> string_of_int (List.length state.pdims)) 1;
4156 if conf.trimmargins
4157 then (
4158 sep ();
4159 src#caption "Trimmed margins" 0;
4160 src#caption2 "Dimensions"
4161 (fun () -> string_of_int (List.length state.pdims)) 1;
4164 src#reset prevmode prevuioh;
4166 fun () ->
4167 state.text <- "";
4168 let prevmode = state.mode
4169 and prevuioh = state.uioh in
4170 fillsrc prevmode prevuioh;
4171 let source = (src :> lvsource) in
4172 let modehash = findkeyhash conf "info" in
4173 state.uioh <- coe (object (self)
4174 inherit listview ~source ~trusted:true ~modehash as super
4175 val mutable m_prevmemused = 0
4176 method infochanged = function
4177 | Memused ->
4178 if m_prevmemused != state.memused
4179 then (
4180 m_prevmemused <- state.memused;
4181 G.postRedisplay "memusedchanged";
4183 | Pdim -> G.postRedisplay "pdimchanged"
4184 | Docinfo -> fillsrc prevmode prevuioh
4186 method key key mask =
4187 if not (Wsi.withctrl mask)
4188 then
4189 match key with
4190 | 0xff51 -> coe (self#updownlevel ~-1)
4191 | 0xff53 -> coe (self#updownlevel 1)
4192 | _ -> super#key key mask
4193 else super#key key mask
4194 end);
4195 G.postRedisplay "info";
4198 let enterhelpmode =
4199 let source =
4200 (object
4201 inherit lvsourcebase
4202 method getitemcount = Array.length state.help
4203 method getitem n =
4204 let s, n, _ = state.help.(n) in
4205 (s, n)
4207 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4208 let optuioh =
4209 if not cancel
4210 then (
4211 m_qsearch <- qsearch;
4212 match state.help.(active) with
4213 | _, _, Action f -> Some (f uioh)
4214 | _ -> Some (uioh)
4216 else None
4218 m_active <- active;
4219 m_first <- first;
4220 m_pan <- pan;
4221 optuioh
4223 method hasaction n =
4224 match state.help.(n) with
4225 | _, _, Action _ -> true
4226 | _ -> false
4228 initializer
4229 m_active <- -1
4230 end)
4231 in fun () ->
4232 let modehash = findkeyhash conf "help" in
4233 state.uioh <- coe (new listview ~source ~trusted:true ~modehash);
4234 G.postRedisplay "help";
4237 let entermsgsmode =
4238 let msgsource =
4239 let re = Str.regexp "[\r\n]" in
4240 (object
4241 inherit lvsourcebase
4242 val mutable m_items = [||]
4244 method getitemcount = 1 + Array.length m_items
4246 method getitem n =
4247 if n = 0
4248 then "[Clear]", 0
4249 else m_items.(n-1), 0
4251 method exit ~uioh ~cancel ~active ~first ~pan ~qsearch =
4252 ignore uioh;
4253 if not cancel
4254 then (
4255 if active = 0
4256 then Buffer.clear state.errmsgs;
4257 m_qsearch <- qsearch;
4259 m_active <- active;
4260 m_first <- first;
4261 m_pan <- pan;
4262 None
4264 method hasaction n =
4265 n = 0
4267 method reset =
4268 state.newerrmsgs <- false;
4269 let l = Str.split re (Buffer.contents state.errmsgs) in
4270 m_items <- Array.of_list l
4272 initializer
4273 m_active <- 0
4274 end)
4275 in fun () ->
4276 state.text <- "";
4277 msgsource#reset;
4278 let source = (msgsource :> lvsource) in
4279 let modehash = findkeyhash conf "listview" in
4280 state.uioh <- coe (object
4281 inherit listview ~source ~trusted:false ~modehash as super
4282 method display =
4283 if state.newerrmsgs
4284 then msgsource#reset;
4285 super#display
4286 end);
4287 G.postRedisplay "msgs";
4290 let quickbookmark ?title () =
4291 match state.layout with
4292 | [] -> ()
4293 | l :: _ ->
4294 let title =
4295 match title with
4296 | None ->
4297 let sec = Unix.gettimeofday () in
4298 let tm = Unix.localtime sec in
4299 Printf.sprintf "Quick (page %d) (bookmarked at %d/%d/%d %d:%d)"
4300 (l.pageno+1)
4301 tm.Unix.tm_mday
4302 tm.Unix.tm_mon
4303 (tm.Unix.tm_year + 1900)
4304 tm.Unix.tm_hour
4305 tm.Unix.tm_min
4306 | Some title -> title
4308 state.bookmarks <-
4309 (title, 0, (l.pageno, float l.pagey /. float l.pageh))
4310 :: state.bookmarks
4313 let doreshape w h =
4314 state.fullscreen <- None;
4315 Wsi.reshape w h;
4318 let setautoscrollspeed step goingdown =
4319 let incr = max 1 ((abs step) / 2) in
4320 let incr = if goingdown then incr else -incr in
4321 let astep = step + incr in
4322 state.autoscroll <- Some astep;
4325 let gotounder = function
4326 | Ulinkgoto (pageno, top) ->
4327 if pageno >= 0
4328 then (
4329 addnav ();
4330 gotopage1 pageno top;
4333 | Ulinkuri s ->
4334 gotouri s
4336 | Uremote (filename, pageno) ->
4337 let path =
4338 if Sys.file_exists filename
4339 then filename
4340 else
4341 let dir = Filename.dirname state.path in
4342 let path = Filename.concat dir filename in
4343 if Sys.file_exists path
4344 then path
4345 else ""
4347 if String.length path > 0
4348 then (
4349 let anchor = getanchor () in
4350 let ranchor = state.path, state.password, anchor in
4351 state.anchor <- (pageno, 0.0);
4352 state.ranchors <- ranchor :: state.ranchors;
4353 opendoc path "";
4355 else showtext '!' ("Could not find " ^ filename)
4357 | Uunexpected _ | Ulaunch _ | Unamed _ | Utext _ | Unone -> ()
4360 let viewkeyboard key mask =
4361 let enttext te =
4362 let mode = state.mode in
4363 state.mode <- Textentry (te, fun _ -> state.mode <- mode);
4364 state.text <- "";
4365 enttext ();
4366 G.postRedisplay "view:enttext"
4368 let ctrl = Wsi.withctrl mask in
4369 match key with
4370 | 81 -> (* Q *)
4371 exit 0
4373 | 0xff63 -> (* insert *)
4374 if conf.angle mod 360 = 0
4375 then (
4376 state.mode <- LinkNav (Ltgendir 0);
4377 gotoy state.y;
4379 else showtext '!' "Keyboard link naviagtion does not work under rotation"
4381 | 0xff1b | 113 -> (* escape / q *)
4382 begin match state.mstate with
4383 | Mzoomrect _ ->
4384 state.mstate <- Mnone;
4385 Wsi.setcursor Wsi.CURSOR_INHERIT;
4386 G.postRedisplay "kill zoom rect";
4387 | _ ->
4388 match state.ranchors with
4389 | [] -> raise Quit
4390 | (path, password, anchor) :: rest ->
4391 state.ranchors <- rest;
4392 state.anchor <- anchor;
4393 opendoc path password
4394 end;
4396 | 0xff08 -> (* backspace *)
4397 let y = getnav ~-1 in
4398 gotoy_and_clear_text y
4400 | 111 -> (* o *)
4401 enteroutlinemode ()
4403 | 117 -> (* u *)
4404 state.rects <- [];
4405 state.text <- "";
4406 G.postRedisplay "dehighlight";
4408 | 47 | 63 -> (* / ? *)
4409 let ondone isforw s =
4410 cbput state.hists.pat s;
4411 state.searchpattern <- s;
4412 search s isforw
4414 let s = String.create 1 in
4415 s.[0] <- Char.chr key;
4416 enttext (s, "", Some (onhist state.hists.pat),
4417 textentry, ondone (key = 47))
4419 | 43 | 0xffab when ctrl -> (* ctrl-+ *)
4420 let incr = if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01 in
4421 setzoom (conf.zoom +. incr)
4423 | 43 | 0xffab -> (* + *)
4424 let ondone s =
4425 let n =
4426 try int_of_string s with exc ->
4427 state.text <- Printf.sprintf "bad integer `%s': %s"
4428 s (Printexc.to_string exc);
4429 max_int
4431 if n != max_int
4432 then (
4433 conf.pagebias <- n;
4434 state.text <- "page bias is now " ^ string_of_int n;
4437 enttext ("page bias: ", "", None, intentry, ondone)
4439 | 45 | 0xffad when ctrl -> (* ctrl-- *)
4440 let decr = if conf.zoom -. 0.1 < 0.1 then 0.01 else 0.1 in
4441 setzoom (max 0.01 (conf.zoom -. decr))
4443 | 45 | 0xffad -> (* - *)
4444 let ondone msg = state.text <- msg in
4445 enttext (
4446 "option [acfhilpstvxACPRSZTIS]: ", "", None,
4447 optentry state.mode, ondone
4450 | 48 when ctrl -> (* ctrl-0 *)
4451 setzoom 1.0
4453 | 49 when ctrl -> (* 1 *)
4454 let zoom = zoomforh conf.winw conf.winh state.scrollw in
4455 if zoom < 1.0
4456 then setzoom zoom
4458 | 0xffc6 -> (* f9 *)
4459 togglebirdseye ()
4461 | 57 when ctrl -> (* ctrl-9 *)
4462 togglebirdseye ()
4464 | (48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57)
4465 when not ctrl -> (* 0..9 *)
4466 let ondone s =
4467 let n =
4468 try int_of_string s with exc ->
4469 state.text <- Printf.sprintf "bad integer `%s': %s"
4470 s (Printexc.to_string exc);
4473 if n >= 0
4474 then (
4475 addnav ();
4476 cbput state.hists.pag (string_of_int n);
4477 gotopage1 (n + conf.pagebias - 1) 0;
4480 let pageentry text key =
4481 match Char.unsafe_chr key with
4482 | 'g' -> TEdone text
4483 | _ -> intentry text key
4485 let text = "x" in text.[0] <- Char.chr key;
4486 enttext (":", text, Some (onhist state.hists.pag), pageentry, ondone)
4488 | 98 -> (* b *)
4489 state.scrollw <- if state.scrollw > 0 then 0 else conf.scrollbw;
4490 reshape conf.winw conf.winh;
4492 | 108 -> (* l *)
4493 conf.hlinks <- not conf.hlinks;
4494 state.text <- "highlightlinks " ^ if conf.hlinks then "on" else "off";
4495 G.postRedisplay "toggle highlightlinks";
4497 | 70 -> (* F *)
4498 state.glinks <- true;
4499 let ondone s =
4500 let n =
4501 try int_of_string s with exc ->
4502 state.text <- Printf.sprintf "bad integer `%s': %s"
4503 s (Printexc.to_string exc);
4506 if n >= 0
4507 then (
4508 let rec loop = function
4509 | [] -> ()
4510 | l :: rest ->
4511 match getopaque l.pageno with
4512 | None -> loop rest
4513 | Some opaque ->
4514 match getlink2 opaque n with
4515 | Unone -> loop rest
4516 | under ->
4517 addnav ();
4518 cbput state.hists.pag s;
4519 gotounder under;
4521 loop state.layout;
4524 let onkey text key =
4525 match Char.unsafe_chr key with
4526 | 'g' -> TEdone text
4527 | _ -> intentry text key
4529 let mode = state.mode in
4530 state.mode <- Textentry (
4531 (":", "", Some (onhist state.hists.pag), onkey, ondone),
4532 fun _ ->
4533 state.glinks <- false;
4534 state.mode <- mode
4536 state.text <- "";
4537 G.postRedisplay "view:enttext"
4539 | 97 -> (* a *)
4540 begin match state.autoscroll with
4541 | Some step ->
4542 conf.autoscrollstep <- step;
4543 state.autoscroll <- None
4544 | None ->
4545 if conf.autoscrollstep = 0
4546 then state.autoscroll <- Some 1
4547 else state.autoscroll <- Some conf.autoscrollstep
4550 | 80 -> (* P *)
4551 conf.presentation <- not conf.presentation;
4552 if conf.presentation
4553 then (
4554 if not conf.scrollbarinpm
4555 then state.scrollw <- 0;
4557 else
4558 state.scrollw <- conf.scrollbw;
4560 showtext ' ' ("presentation mode " ^
4561 if conf.presentation then "on" else "off");
4562 state.anchor <- getanchor ();
4563 represent ()
4565 | 102 -> (* f *)
4566 begin match state.fullscreen with
4567 | None ->
4568 state.fullscreen <- Some (conf.winw, conf.winh);
4569 Wsi.fullscreen ()
4570 | Some (w, h) ->
4571 state.fullscreen <- None;
4572 doreshape w h
4575 | 103 -> (* g *)
4576 gotoy_and_clear_text 0
4578 | 71 -> (* G *)
4579 gotopage1 (state.pagecount - 1) 0
4581 | 112 | 78 -> (* p|N *)
4582 search state.searchpattern false
4584 | 110 | 0xffc0 -> (* n|F3 *)
4585 search state.searchpattern true
4587 | 116 -> (* t *)
4588 begin match state.layout with
4589 | [] -> ()
4590 | l :: _ ->
4591 gotoy_and_clear_text (getpagey l.pageno)
4594 | 32 -> (* ' ' *)
4595 begin match List.rev state.layout with
4596 | [] -> ()
4597 | l :: _ ->
4598 let pageno = min (l.pageno+1) (state.pagecount-1) in
4599 gotoy_and_clear_text (getpagey pageno)
4602 | 0xff9f | 0xffff -> (* delete *)
4603 begin match state.layout with
4604 | [] -> ()
4605 | l :: _ ->
4606 let pageno = max 0 (l.pageno-1) in
4607 gotoy_and_clear_text (getpagey pageno)
4610 | 61 -> (* = *)
4611 showtext ' ' (describe_location ());
4613 | 119 -> (* w *)
4614 begin match state.layout with
4615 | [] -> ()
4616 | l :: _ ->
4617 doreshape (l.pagew + state.scrollw) l.pageh;
4618 G.postRedisplay "w"
4621 | 39 -> (* ' *)
4622 enterbookmarkmode ()
4624 | 104 | 0xffbe -> (* h|F1 *)
4625 enterhelpmode ()
4627 | 105 -> (* i *)
4628 enterinfomode ()
4630 | 101 when conf.redirectstderr -> (* e *)
4631 entermsgsmode ()
4633 | 109 -> (* m *)
4634 let ondone s =
4635 match state.layout with
4636 | l :: _ ->
4637 state.bookmarks <-
4638 (s, 0, (l.pageno, float l.pagey /. float l.pageh))
4639 :: state.bookmarks
4640 | _ -> ()
4642 enttext ("bookmark: ", "", None, textentry, ondone)
4644 | 126 -> (* ~ *)
4645 quickbookmark ();
4646 showtext ' ' "Quick bookmark added";
4648 | 122 -> (* z *)
4649 begin match state.layout with
4650 | l :: _ ->
4651 let rect = getpdimrect l.pagedimno in
4652 let w, h =
4653 if conf.crophack
4654 then
4655 (truncate (1.8 *. (rect.(1) -. rect.(0))),
4656 truncate (1.2 *. (rect.(3) -. rect.(0))))
4657 else
4658 (truncate (rect.(1) -. rect.(0)),
4659 truncate (rect.(3) -. rect.(0)))
4661 let w = truncate ((float w)*.conf.zoom)
4662 and h = truncate ((float h)*.conf.zoom) in
4663 if w != 0 && h != 0
4664 then (
4665 state.anchor <- getanchor ();
4666 doreshape (w + state.scrollw) (h + conf.interpagespace)
4668 G.postRedisplay "z";
4670 | [] -> ()
4673 | 50 when ctrl -> (* ctrl-2 *)
4674 let maxw = getmaxw () in
4675 if maxw > 0.0
4676 then setzoom (maxw /. float conf.winw)
4678 | 60 | 62 -> (* < > *)
4679 reqlayout (conf.angle + (if key = 62 then 30 else -30)) conf.proportional
4681 | 91 | 93 -> (* [ ] *)
4682 conf.colorscale <-
4683 bound (conf.colorscale +. (if key = 93 then 0.1 else -0.1)) 0.0 1.0
4685 G.postRedisplay "brightness";
4687 | 0xff54 | 0xff52 when ctrl && Wsi.withshift mask ->
4688 setzoom state.prevzoom
4690 | 107 | 0xff52 -> (* k up *)
4691 begin match state.autoscroll with
4692 | None ->
4693 begin match state.mode with
4694 | Birdseye beye -> upbirdseye 1 beye
4695 | _ ->
4696 if ctrl
4697 then gotoy_and_clear_text (clamp ~-(conf.winh/2))
4698 else gotoy_and_clear_text (clamp (-conf.scrollstep))
4700 | Some n ->
4701 setautoscrollspeed n false
4704 | 106 | 0xff54 -> (* j down *)
4705 begin match state.autoscroll with
4706 | None ->
4707 begin match state.mode with
4708 | Birdseye beye -> downbirdseye 1 beye
4709 | _ ->
4710 if ctrl
4711 then gotoy_and_clear_text (clamp (conf.winh/2))
4712 else gotoy_and_clear_text (clamp conf.scrollstep)
4714 | Some n ->
4715 setautoscrollspeed n true
4718 | 0xff51 | 0xff53 when not (Wsi.withalt mask) -> (* left / right *)
4719 if conf.zoom > 1.0
4720 then
4721 let dx =
4722 if ctrl
4723 then conf.winw / 2
4724 else 10
4726 let dx = if key = 0xff51 then dx else -dx in
4727 state.x <- state.x + dx;
4728 gotoy_and_clear_text state.y
4729 else (
4730 state.text <- "";
4731 G.postRedisplay "lef/right"
4734 | 0xff55 -> (* prior *)
4735 let y =
4736 if ctrl
4737 then
4738 match state.layout with
4739 | [] -> state.y
4740 | l :: _ -> state.y - l.pagey
4741 else
4742 clamp (-conf.winh)
4744 gotoghyll y
4746 | 0xff56 -> (* next *)
4747 let y =
4748 if ctrl
4749 then
4750 match List.rev state.layout with
4751 | [] -> state.y
4752 | l :: _ -> getpagey l.pageno
4753 else
4754 clamp conf.winh
4756 gotoghyll y
4758 | 0xff50 -> gotoghyll 0
4759 | 0xff57 -> gotoghyll (clamp state.maxy)
4760 | 0xff53 when Wsi.withalt mask ->
4761 gotoghyll (getnav ~-1)
4762 | 0xff51 when Wsi.withalt mask ->
4763 gotoghyll (getnav 1)
4765 | 114 -> (* r *)
4766 state.anchor <- getanchor ();
4767 opendoc state.path state.password
4769 | 76 -> (* L *)
4770 launchpath ()
4772 | 118 when conf.debug -> (* v *)
4773 state.rects <- [];
4774 List.iter (fun l ->
4775 match getopaque l.pageno with
4776 | None -> ()
4777 | Some opaque ->
4778 let x0, y0, x1, y1 = pagebbox opaque in
4779 let a,b = float x0, float y0 in
4780 let c,d = float x1, float y0 in
4781 let e,f = float x1, float y1 in
4782 let h,j = float x0, float y1 in
4783 let rect = (a,b,c,d,e,f,h,j) in
4784 debugrect rect;
4785 state.rects <- (l.pageno, l.pageno mod 3, rect) :: state.rects;
4786 ) state.layout;
4787 G.postRedisplay "v";
4789 | _ ->
4790 vlog "huh? %s" (Wsi.keyname key)
4793 let linknavkeyboard key mask linknav =
4794 let getpage pageno =
4795 let rec loop = function
4796 | [] -> None
4797 | l :: _ when l.pageno = pageno -> Some l
4798 | _ :: rest -> loop rest
4799 in loop state.layout
4801 let doexact (pageno, n) =
4802 match getopaque pageno, getpage pageno with
4803 | Some opaque, Some l ->
4804 if key = 0xff0d
4805 then
4806 let under = getlink opaque n in
4807 G.postRedisplay "link gotounder";
4808 gotounder under;
4809 state.mode <- View;
4810 else
4811 let opt, dir =
4812 match key with
4813 | 0xff50 -> (* home *)
4814 Some (findlink opaque LDfirst), -1
4816 | 0xff57 -> (* end *)
4817 Some (findlink opaque LDlast), 1
4819 | 0xff51 -> (* left *)
4820 Some (findlink opaque (LDleft n)), -1
4822 | 0xff53 -> (* right *)
4823 Some (findlink opaque (LDright n)), 1
4825 | 0xff52 -> (* up *)
4826 Some (findlink opaque (LDup n)), -1
4828 | 0xff54 -> (* down *)
4829 Some (findlink opaque (LDdown n)), 1
4831 | _ -> None, 0
4833 let pwl l dir =
4834 begin match findpwl l.pageno dir with
4835 | Pwlnotfound -> ()
4836 | Pwl pageno ->
4837 let notfound dir =
4838 state.mode <- LinkNav (Ltgendir dir);
4839 let y, h = getpageyh pageno in
4840 let y =
4841 if dir < 0
4842 then y + h - conf.winh
4843 else y
4845 gotoy y
4847 begin match getopaque pageno, getpage pageno with
4848 | Some opaque, Some _ ->
4849 let link =
4850 let ld = if dir > 0 then LDfirst else LDlast in
4851 findlink opaque ld
4853 begin match link with
4854 | Lfound m ->
4855 showlinktype (getlink opaque m);
4856 state.mode <- LinkNav (Ltexact (pageno, m));
4857 G.postRedisplay "linknav jpage";
4858 | _ -> notfound dir
4859 end;
4860 | _ -> notfound dir
4861 end;
4862 end;
4864 begin match opt with
4865 | Some Lnotfound -> pwl l dir;
4866 | Some (Lfound m) ->
4867 if m = n
4868 then pwl l dir
4869 else (
4870 let _, y0, _, y1 = getlinkrect opaque m in
4871 if y0 < l.pagey
4872 then gotopage1 l.pageno y0
4873 else (
4874 let d = fstate.fontsize + 1 in
4875 if y1 - l.pagey > l.pagevh - d
4876 then gotopage1 l.pageno (y1 - conf.winh - state.hscrollh + d)
4877 else G.postRedisplay "linknav";
4879 showlinktype (getlink opaque m);
4880 state.mode <- LinkNav (Ltexact (l.pageno, m));
4883 | None -> viewkeyboard key mask
4884 end;
4885 | _ -> viewkeyboard key mask
4887 if key = 0xff63
4888 then (
4889 state.mode <- View;
4890 G.postRedisplay "leave linknav"
4892 else
4893 match linknav with
4894 | Ltgendir _ -> viewkeyboard key mask
4895 | Ltexact exact -> doexact exact
4898 let keyboard key mask =
4899 if (key = 103 && Wsi.withctrl mask) && not (istextentry state.mode)
4900 then wcmd "interrupt"
4901 else state.uioh <- state.uioh#key key mask
4904 let birdseyekeyboard key mask
4905 ((oconf, leftx, pageno, hooverpageno, anchor) as beye) =
4906 let incr =
4907 match conf.columns with
4908 | None -> 1
4909 | Some ((c, _, _), _) -> c
4911 match key with
4912 | 108 when Wsi.withctrl mask -> (* ctrl-l *)
4913 let y, h = getpageyh pageno in
4914 let top = (conf.winh - h) / 2 in
4915 gotoy (max 0 (y - top))
4916 | 0xff0d -> leavebirdseye beye false
4917 | 0xff1b -> leavebirdseye beye true (* escape *)
4918 | 0xff52 -> upbirdseye incr beye (* prior *)
4919 | 0xff54 -> downbirdseye incr beye (* next *)
4920 | 0xff51 -> upbirdseye 1 beye (* up *)
4921 | 0xff53 -> downbirdseye 1 beye (* down *)
4923 | 0xff55 ->
4924 begin match state.layout with
4925 | l :: _ ->
4926 if l.pagey != 0
4927 then (
4928 state.mode <- Birdseye (
4929 oconf, leftx, l.pageno, hooverpageno, anchor
4931 gotopage1 l.pageno 0;
4933 else (
4934 let layout = layout (state.y-conf.winh) conf.winh in
4935 match layout with
4936 | [] -> gotoy (clamp (-conf.winh))
4937 | l :: _ ->
4938 state.mode <- Birdseye (
4939 oconf, leftx, l.pageno, hooverpageno, anchor
4941 gotopage1 l.pageno 0
4944 | [] -> gotoy (clamp (-conf.winh))
4945 end;
4947 | 0xff56 ->
4948 begin match List.rev state.layout with
4949 | l :: _ ->
4950 let layout = layout (state.y + conf.winh) conf.winh in
4951 begin match layout with
4952 | [] ->
4953 let incr = l.pageh - l.pagevh in
4954 if incr = 0
4955 then (
4956 state.mode <-
4957 Birdseye (
4958 oconf, leftx, state.pagecount - 1, hooverpageno, anchor
4960 G.postRedisplay "birdseye pagedown";
4962 else gotoy (clamp (incr + conf.interpagespace*2));
4964 | l :: _ ->
4965 state.mode <-
4966 Birdseye (oconf, leftx, l.pageno, hooverpageno, anchor);
4967 gotopage1 l.pageno 0;
4970 | [] -> gotoy (clamp conf.winh)
4971 end;
4973 | 0xff50 ->
4974 state.mode <- Birdseye (oconf, leftx, 0, hooverpageno, anchor);
4975 gotopage1 0 0
4977 | 0xff57 ->
4978 let pageno = state.pagecount - 1 in
4979 state.mode <- Birdseye (oconf, leftx, pageno, hooverpageno, anchor);
4980 if not (pagevisible state.layout pageno)
4981 then
4982 let h =
4983 match List.rev state.pdims with
4984 | [] -> conf.winh
4985 | (_, _, h, _) :: _ -> h
4987 gotoy (max 0 (getpagey pageno - (conf.winh - h - conf.interpagespace)))
4988 else G.postRedisplay "birdseye end";
4989 | _ -> viewkeyboard key mask
4992 let drawpage l linkindexbase =
4993 let color =
4994 match state.mode with
4995 | Textentry _ -> scalecolor 0.4
4996 | LinkNav _
4997 | View -> scalecolor 1.0
4998 | Birdseye (_, _, pageno, hooverpageno, _) ->
4999 if l.pageno = hooverpageno
5000 then scalecolor 0.9
5001 else (
5002 if l.pageno = pageno
5003 then scalecolor 1.0
5004 else scalecolor 0.8
5007 drawtiles l color;
5008 begin match getopaque l.pageno with
5009 | Some opaque ->
5010 if tileready l l.pagex l.pagey
5011 then
5012 let x = l.pagedispx - l.pagex
5013 and y = l.pagedispy - l.pagey in
5014 let hlmask = (if conf.hlinks then 1 else 0)
5015 + (if state.glinks && not (isbirdseye state.mode) then 2 else 0)
5017 postprocess opaque hlmask x y linkindexbase;
5018 else 0
5020 | _ -> 0
5021 end;
5024 let scrollindicator () =
5025 let sbw, ph, sh = state.uioh#scrollph in
5026 let sbh, pw, sw = state.uioh#scrollpw in
5028 GlDraw.color (0.64, 0.64, 0.64);
5029 GlDraw.rect
5030 (float (conf.winw - sbw), 0.)
5031 (float conf.winw, float conf.winh)
5033 GlDraw.rect
5034 (0., float (conf.winh - sbh))
5035 (float (conf.winw - state.scrollw - 1), float conf.winh)
5037 GlDraw.color (0.0, 0.0, 0.0);
5039 GlDraw.rect
5040 (float (conf.winw - sbw), ph)
5041 (float conf.winw, ph +. sh)
5043 GlDraw.rect
5044 (pw, float (conf.winh - sbh))
5045 (pw +. sw, float conf.winh)
5049 let showsel () =
5050 match state.mstate with
5051 | Mnone | Mscrolly | Mscrollx | Mpan _ | Mzoom _ | Mzoomrect _ ->
5054 | Msel ((x0, y0), (x1, y1)) ->
5055 let rec loop = function
5056 | l :: ls ->
5057 if ((y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5058 || ((y1 >= l.pagedispy && y1 <= (l.pagedispy + l.pagevh))))
5059 && ((x0 >= l.pagedispx && x0 <= (l.pagedispx + l.pagevw))
5060 || ((x1 >= l.pagedispx && x1 <= (l.pagedispx + l.pagevw))))
5061 then
5062 match getopaque l.pageno with
5063 | Some opaque ->
5064 let x0, y0 = pagetranslatepoint l x0 y0 in
5065 let x1, y1 = pagetranslatepoint l x1 y1 in
5066 seltext opaque (x0, y0, x1, y1);
5067 | _ -> ()
5068 else loop ls
5069 | [] -> ()
5071 loop state.layout
5074 let showrects rects =
5075 Gl.enable `blend;
5076 GlDraw.color (0.0, 0.0, 1.0) ~alpha:0.5;
5077 GlDraw.polygon_mode `both `fill;
5078 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5079 List.iter
5080 (fun (pageno, c, (x0, y0, x1, y1, x2, y2, x3, y3)) ->
5081 List.iter (fun l ->
5082 if l.pageno = pageno
5083 then (
5084 let dx = float (l.pagedispx - l.pagex) in
5085 let dy = float (l.pagedispy - l.pagey) in
5086 GlDraw.color (0.0, 0.0, 1.0 /. float c) ~alpha:0.5;
5087 GlDraw.begins `quads;
5089 GlDraw.vertex2 (x0+.dx, y0+.dy);
5090 GlDraw.vertex2 (x1+.dx, y1+.dy);
5091 GlDraw.vertex2 (x2+.dx, y2+.dy);
5092 GlDraw.vertex2 (x3+.dx, y3+.dy);
5094 GlDraw.ends ();
5096 ) state.layout
5097 ) rects
5099 Gl.disable `blend;
5102 let display () =
5103 GlClear.color (scalecolor2 conf.bgcolor);
5104 GlClear.clear [`color];
5105 let rec loop linkindexbase = function
5106 | l :: rest ->
5107 let linkindexbase = linkindexbase + drawpage l linkindexbase in
5108 loop linkindexbase rest
5109 | [] -> ()
5111 loop 0 state.layout;
5112 let rects =
5113 match state.mode with
5114 | LinkNav (Ltexact (pageno, linkno)) ->
5115 begin match getopaque pageno with
5116 | Some opaque ->
5117 let x0, y0, x1, y1 = getlinkrect opaque linkno in
5118 (pageno, 5, (
5119 float x0, float y0,
5120 float x1, float y0,
5121 float x1, float y1,
5122 float x0, float y1)
5123 ) :: state.rects
5124 | None -> state.rects
5126 | _ -> state.rects
5128 showrects rects;
5129 showsel ();
5130 state.uioh#display;
5131 begin match state.mstate with
5132 | Mzoomrect ((x0, y0), (x1, y1)) ->
5133 Gl.enable `blend;
5134 GlDraw.color (0.3, 0.3, 0.3) ~alpha:0.5;
5135 GlFunc.blend_func `src_alpha `one_minus_src_alpha;
5136 GlDraw.rect (float x0, float y0)
5137 (float x1, float y1);
5138 Gl.disable `blend;
5139 | _ -> ()
5140 end;
5141 enttext ();
5142 scrollindicator ();
5143 Wsi.swapb ();
5146 let zoomrect x y x1 y1 =
5147 let x0 = min x x1
5148 and x1 = max x x1
5149 and y0 = min y y1 in
5150 gotoy (state.y + y0);
5151 state.anchor <- getanchor ();
5152 let zoom = (float conf.winw *. conf.zoom) /. float (x1 - x0) in
5153 let margin =
5154 if state.w < conf.winw - state.scrollw
5155 then (conf.winw - state.scrollw - state.w) / 2
5156 else 0
5158 state.x <- (state.x + margin) - x0;
5159 setzoom zoom;
5160 Wsi.setcursor Wsi.CURSOR_INHERIT;
5161 state.mstate <- Mnone;
5164 let scrollx x =
5165 let winw = conf.winw - state.scrollw - 1 in
5166 let s = float x /. float winw in
5167 let destx = truncate (float (state.w + winw) *. s) in
5168 state.x <- winw - destx;
5169 gotoy_and_clear_text state.y;
5170 state.mstate <- Mscrollx;
5173 let scrolly y =
5174 let s = float y /. float conf.winh in
5175 let desty = truncate (float (state.maxy - conf.winh) *. s) in
5176 gotoy_and_clear_text desty;
5177 state.mstate <- Mscrolly;
5180 let viewmouse button down x y mask =
5181 match button with
5182 | n when (n == 4 || n == 5) && not down ->
5183 if Wsi.withctrl mask
5184 then (
5185 match state.mstate with
5186 | Mzoom (oldn, i) ->
5187 if oldn = n
5188 then (
5189 if i = 2
5190 then
5191 let incr =
5192 match n with
5193 | 5 ->
5194 if conf.zoom +. 0.01 > 0.1 then 0.1 else 0.01
5195 | _ ->
5196 if conf.zoom -. 0.1 < 0.1 then -0.01 else -0.1
5198 let zoom = conf.zoom -. incr in
5199 setzoom zoom;
5200 state.mstate <- Mzoom (n, 0);
5201 else
5202 state.mstate <- Mzoom (n, i+1);
5204 else state.mstate <- Mzoom (n, 0)
5206 | _ -> state.mstate <- Mzoom (n, 0)
5208 else (
5209 match state.autoscroll with
5210 | Some step -> setautoscrollspeed step (n=4)
5211 | None ->
5212 let incr =
5213 if n = 4
5214 then -conf.scrollstep
5215 else conf.scrollstep
5217 let incr = incr * 2 in
5218 let y = clamp incr in
5219 gotoy_and_clear_text y
5222 | 1 when Wsi.withctrl mask ->
5223 if down
5224 then (
5225 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5226 state.mstate <- Mpan (x, y)
5228 else
5229 state.mstate <- Mnone
5231 | 3 ->
5232 if down
5233 then (
5234 Wsi.setcursor Wsi.CURSOR_CYCLE;
5235 let p = (x, y) in
5236 state.mstate <- Mzoomrect (p, p)
5238 else (
5239 match state.mstate with
5240 | Mzoomrect ((x0, y0), _) ->
5241 if abs (x-x0) > 10 && abs (y - y0) > 10
5242 then zoomrect x0 y0 x y
5243 else (
5244 state.mstate <- Mnone;
5245 Wsi.setcursor Wsi.CURSOR_INHERIT;
5246 G.postRedisplay "kill accidental zoom rect";
5248 | _ ->
5249 Wsi.setcursor Wsi.CURSOR_INHERIT;
5250 state.mstate <- Mnone
5253 | 1 when x > conf.winw - state.scrollw ->
5254 if down
5255 then
5256 let _, position, sh = state.uioh#scrollph in
5257 if y > truncate position && y < truncate (position +. sh)
5258 then state.mstate <- Mscrolly
5259 else scrolly y
5260 else
5261 state.mstate <- Mnone
5263 | 1 when y > conf.winh - state.hscrollh ->
5264 if down
5265 then
5266 let _, position, sw = state.uioh#scrollpw in
5267 if x > truncate position && x < truncate (position +. sw)
5268 then state.mstate <- Mscrollx
5269 else scrollx x
5270 else
5271 state.mstate <- Mnone
5273 | 1 ->
5274 let dest = if down then getunder x y else Unone in
5275 begin match dest with
5276 | Ulinkgoto (pageno, top) ->
5277 if pageno >= 0
5278 then (
5279 addnav ();
5280 gotopage1 pageno top;
5283 | Ulinkuri s ->
5284 gotouri s
5286 | Uremote (filename, pageno) ->
5287 let path =
5288 if Sys.file_exists filename
5289 then filename
5290 else
5291 let dir = Filename.dirname state.path in
5292 let path = Filename.concat dir filename in
5293 if Sys.file_exists path
5294 then path
5295 else ""
5297 if String.length path > 0
5298 then (
5299 let anchor = getanchor () in
5300 let ranchor = state.path, state.password, anchor in
5301 state.anchor <- (pageno, 0.0);
5302 state.ranchors <- ranchor :: state.ranchors;
5303 opendoc path "";
5305 else showtext '!' ("Could not find " ^ filename)
5307 | Uunexpected _ | Ulaunch _ | Unamed _ -> ()
5309 | Unone when down ->
5310 Wsi.setcursor Wsi.CURSOR_CROSSHAIR;
5311 state.mstate <- Mpan (x, y);
5313 | Unone | Utext _ ->
5314 if down
5315 then (
5316 if conf.angle mod 360 = 0
5317 then (
5318 state.mstate <- Msel ((x, y), (x, y));
5319 G.postRedisplay "mouse select";
5322 else (
5323 match state.mstate with
5324 | Mnone -> ()
5326 | Mzoom _ | Mscrollx | Mscrolly ->
5327 state.mstate <- Mnone
5329 | Mzoomrect ((x0, y0), _) ->
5330 zoomrect x0 y0 x y
5332 | Mpan _ ->
5333 Wsi.setcursor Wsi.CURSOR_INHERIT;
5334 state.mstate <- Mnone
5336 | Msel ((_, y0), (_, y1)) ->
5337 let rec loop = function
5338 | [] -> ()
5339 | l :: rest ->
5340 if (y0 >= l.pagedispy && y0 <= (l.pagedispy + l.pagevh))
5341 || ((y1 >= l.pagedispy
5342 && y1 <= (l.pagedispy + l.pagevh)))
5343 then
5344 match getopaque l.pageno with
5345 | Some opaque ->
5346 copysel conf.selcmd opaque;
5347 G.postRedisplay "copysel"
5348 | _ -> ()
5349 else loop rest
5351 loop state.layout;
5352 Wsi.setcursor Wsi.CURSOR_INHERIT;
5353 state.mstate <- Mnone;
5357 | _ -> ()
5360 let birdseyemouse button down x y mask
5361 (conf, leftx, _, hooverpageno, anchor) =
5362 match button with
5363 | 1 when down ->
5364 let rec loop = function
5365 | [] -> ()
5366 | l :: rest ->
5367 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5368 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5369 then (
5370 leavebirdseye (conf, leftx, l.pageno, hooverpageno, anchor) false;
5372 else loop rest
5374 loop state.layout
5375 | 3 -> ()
5376 | _ -> viewmouse button down x y mask
5379 let mouse button down x y mask =
5380 state.uioh <- state.uioh#button button down x y mask;
5383 let motion ~x ~y =
5384 state.uioh <- state.uioh#motion x y
5387 let pmotion ~x ~y =
5388 state.uioh <- state.uioh#pmotion x y;
5391 let uioh = object
5392 method display = ()
5394 method key key mask =
5395 begin match state.mode with
5396 | Textentry textentry -> textentrykeyboard key mask textentry
5397 | Birdseye birdseye -> birdseyekeyboard key mask birdseye
5398 | View -> viewkeyboard key mask
5399 | LinkNav linknav -> linknavkeyboard key mask linknav
5400 end;
5401 state.uioh
5403 method button button bstate x y mask =
5404 begin match state.mode with
5405 | LinkNav _
5406 | View -> viewmouse button bstate x y mask
5407 | Birdseye beye -> birdseyemouse button bstate x y mask beye
5408 | Textentry _ -> ()
5409 end;
5410 state.uioh
5412 method motion x y =
5413 begin match state.mode with
5414 | Textentry _ -> ()
5415 | View | Birdseye _ | LinkNav _ ->
5416 match state.mstate with
5417 | Mzoom _ | Mnone -> ()
5419 | Mpan (x0, y0) ->
5420 let dx = x - x0
5421 and dy = y0 - y in
5422 state.mstate <- Mpan (x, y);
5423 if conf.zoom > 1.0 then state.x <- state.x + dx;
5424 let y = clamp dy in
5425 gotoy_and_clear_text y
5427 | Msel (a, _) ->
5428 state.mstate <- Msel (a, (x, y));
5429 G.postRedisplay "motion select";
5431 | Mscrolly ->
5432 let y = min conf.winh (max 0 y) in
5433 scrolly y
5435 | Mscrollx ->
5436 let x = min conf.winw (max 0 x) in
5437 scrollx x
5439 | Mzoomrect (p0, _) ->
5440 state.mstate <- Mzoomrect (p0, (x, y));
5441 G.postRedisplay "motion zoomrect";
5442 end;
5443 state.uioh
5445 method pmotion x y =
5446 begin match state.mode with
5447 | Birdseye (conf, leftx, pageno, hooverpageno, anchor) ->
5448 let rec loop = function
5449 | [] ->
5450 if hooverpageno != -1
5451 then (
5452 state.mode <- Birdseye (conf, leftx, pageno, -1, anchor);
5453 G.postRedisplay "pmotion birdseye no hoover";
5455 | l :: rest ->
5456 if y > l.pagedispy && y < l.pagedispy + l.pagevh
5457 && x > l.pagedispx && x < l.pagedispx + l.pagevw
5458 then (
5459 state.mode <- Birdseye (conf, leftx, pageno, l.pageno, anchor);
5460 G.postRedisplay "pmotion birdseye hoover";
5462 else loop rest
5464 loop state.layout
5466 | Textentry _ -> ()
5468 | LinkNav _
5469 | View ->
5470 match state.mstate with
5471 | Mnone -> updateunder x y
5472 | Mpan _ | Msel _ | Mzoom _ | Mscrolly | Mscrollx | Mzoomrect _ ->
5474 end;
5475 state.uioh
5477 method infochanged _ = ()
5479 method scrollph =
5480 let maxy = state.maxy - (if conf.maxhfit then conf.winh else 0) in
5481 let p, h = scrollph state.y maxy in
5482 state.scrollw, p, h
5484 method scrollpw =
5485 let winw = conf.winw - state.scrollw - 1 in
5486 let fwinw = float winw in
5487 let sw =
5488 let sw = fwinw /. float state.w in
5489 let sw = fwinw *. sw in
5490 max sw (float conf.scrollh)
5492 let position, sw =
5493 let f = state.w+winw in
5494 let r = float (winw-state.x) /. float f in
5495 let p = fwinw *. r in
5496 p-.sw/.2., sw
5498 let sw =
5499 if position +. sw > fwinw
5500 then fwinw -. position
5501 else sw
5503 state.hscrollh, position, sw
5505 method modehash =
5506 let modename =
5507 match state.mode with
5508 | LinkNav _ -> "links"
5509 | Textentry _ -> "textentry"
5510 | Birdseye _ -> "birdseye"
5511 | View -> "global"
5513 findkeyhash conf modename
5514 end;;
5516 module Config =
5517 struct
5518 open Parser
5520 let fontpath = ref "";;
5522 module KeyMap =
5523 Map.Make (struct type t = (int * int) let compare = compare end);;
5525 let unent s =
5526 let l = String.length s in
5527 let b = Buffer.create l in
5528 unent b s 0 l;
5529 Buffer.contents b;
5532 let home =
5533 try Sys.getenv "HOME"
5534 with exn ->
5535 prerr_endline
5536 ("Can not determine home directory location: " ^
5537 Printexc.to_string exn);
5541 let modifier_of_string = function
5542 | "alt" -> Wsi.altmask
5543 | "shift" -> Wsi.shiftmask
5544 | "ctrl" | "control" -> Wsi.ctrlmask
5545 | "meta" -> Wsi.metamask
5546 | _ -> 0
5549 let key_of_string =
5550 let r = Str.regexp "-" in
5551 fun s ->
5552 let elems = Str.full_split r s in
5553 let f n k m =
5554 let g s =
5555 let m1 = modifier_of_string s in
5556 if m1 = 0
5557 then (Wsi.namekey s, m)
5558 else (k, m lor m1)
5559 in function
5560 | Str.Delim s when n land 1 = 0 -> g s
5561 | Str.Text s -> g s
5562 | Str.Delim _ -> (k, m)
5564 let rec loop n k m = function
5565 | [] -> (k, m)
5566 | x :: xs ->
5567 let k, m = f n k m x in
5568 loop (n+1) k m xs
5570 loop 0 0 0 elems
5573 let keys_of_string =
5574 let r = Str.regexp "[ \t]" in
5575 fun s ->
5576 let elems = Str.split r s in
5577 List.map key_of_string elems
5580 let copykeyhashes c =
5581 List.map (fun (k, v) -> k, Hashtbl.copy v) c.keyhashes;
5584 let config_of c attrs =
5585 let apply c k v =
5587 match k with
5588 | "scroll-bar-width" -> { c with scrollbw = max 0 (int_of_string v) }
5589 | "scroll-handle-height" -> { c with scrollh = max 0 (int_of_string v) }
5590 | "case-insensitive-search" -> { c with icase = bool_of_string v }
5591 | "preload" -> { c with preload = bool_of_string v }
5592 | "page-bias" -> { c with pagebias = int_of_string v }
5593 | "scroll-step" -> { c with scrollstep = max 1 (int_of_string v) }
5594 | "auto-scroll-step" ->
5595 { c with autoscrollstep = max 0 (int_of_string v) }
5596 | "max-height-fit" -> { c with maxhfit = bool_of_string v }
5597 | "crop-hack" -> { c with crophack = bool_of_string v }
5598 | "throttle" ->
5599 let mw =
5600 match String.lowercase v with
5601 | "true" -> Some infinity
5602 | "false" -> None
5603 | f -> Some (float_of_string f)
5605 { c with maxwait = mw}
5606 | "highlight-links" -> { c with hlinks = bool_of_string v }
5607 | "under-cursor-info" -> { c with underinfo = bool_of_string v }
5608 | "vertical-margin" ->
5609 { c with interpagespace = max 0 (int_of_string v) }
5610 | "zoom" ->
5611 let zoom = float_of_string v /. 100. in
5612 let zoom = max zoom 0.0 in
5613 { c with zoom = zoom }
5614 | "presentation" -> { c with presentation = bool_of_string v }
5615 | "rotation-angle" -> { c with angle = int_of_string v }
5616 | "width" -> { c with winw = max 20 (int_of_string v) }
5617 | "height" -> { c with winh = max 20 (int_of_string v) }
5618 | "persistent-bookmarks" -> { c with savebmarks = bool_of_string v }
5619 | "proportional-display" -> { c with proportional = bool_of_string v }
5620 | "pixmap-cache-size" ->
5621 { c with memlimit = max 2 (int_of_string_with_suffix v) }
5622 | "tex-count" -> { c with texcount = max 1 (int_of_string v) }
5623 | "slice-height" -> { c with sliceheight = max 2 (int_of_string v) }
5624 | "thumbnail-width" -> { c with thumbw = max 2 (int_of_string v) }
5625 | "persistent-location" -> { c with jumpback = bool_of_string v }
5626 | "background-color" -> { c with bgcolor = color_of_string v }
5627 | "scrollbar-in-presentation" ->
5628 { c with scrollbarinpm = bool_of_string v }
5629 | "tile-width" -> { c with tilew = max 2 (int_of_string v) }
5630 | "tile-height" -> { c with tileh = max 2 (int_of_string v) }
5631 | "mupdf-store-size" ->
5632 { c with mustoresize = max 1024 (int_of_string_with_suffix v) }
5633 | "checkers" -> { c with checkers = bool_of_string v }
5634 | "aalevel" -> { c with aalevel = max 0 (int_of_string v) }
5635 | "trim-margins" -> { c with trimmargins = bool_of_string v }
5636 | "trim-fuzz" -> { c with trimfuzz = irect_of_string v }
5637 | "uri-launcher" -> { c with urilauncher = unent v }
5638 | "path-launcher" -> { c with pathlauncher = unent v }
5639 | "color-space" -> { c with colorspace = colorspace_of_string v }
5640 | "invert-colors" -> { c with invert = bool_of_string v }
5641 | "brightness" -> { c with colorscale = float_of_string v }
5642 | "redirectstderr" -> { c with redirectstderr = bool_of_string v }
5643 | "ghyllscroll" ->
5644 { c with ghyllscroll = Some (ghyllscroll_of_string v) }
5645 | "columns" ->
5646 let nab = columns_of_string v in
5647 { c with columns = Some (nab, [||]) }
5648 | "birds-eye-columns" ->
5649 { c with beyecolumns = Some (max (int_of_string v) 2) }
5650 | "selection-command" -> { c with selcmd = unent v }
5651 | "update-cursor" -> { c with updatecurs = bool_of_string v }
5652 | _ -> c
5653 with exn ->
5654 prerr_endline ("Error processing attribute (`" ^
5655 k ^ "'=`" ^ v ^ "'): " ^ Printexc.to_string exn);
5658 let rec fold c = function
5659 | [] -> c
5660 | (k, v) :: rest ->
5661 let c = apply c k v in
5662 fold c rest
5664 fold { c with keyhashes = copykeyhashes c } attrs;
5667 let fromstring f pos n v d =
5668 try f v
5669 with exn ->
5670 dolog "Error processing attribute (%S=%S) at %d\n%s"
5671 n v pos (Printexc.to_string exn)
5676 let bookmark_of attrs =
5677 let rec fold title page rely = function
5678 | ("title", v) :: rest -> fold v page rely rest
5679 | ("page", v) :: rest -> fold title v rely rest
5680 | ("rely", v) :: rest -> fold title page v rest
5681 | _ :: rest -> fold title page rely rest
5682 | [] -> title, page, rely
5684 fold "invalid" "0" "0" attrs
5687 let doc_of attrs =
5688 let rec fold path page rely pan = function
5689 | ("path", v) :: rest -> fold v page rely pan rest
5690 | ("page", v) :: rest -> fold path v rely pan rest
5691 | ("rely", v) :: rest -> fold path page v pan rest
5692 | ("pan", v) :: rest -> fold path page rely v rest
5693 | _ :: rest -> fold path page rely pan rest
5694 | [] -> path, page, rely, pan
5696 fold "" "0" "0" "0" attrs
5699 let map_of attrs =
5700 let rec fold rs ls = function
5701 | ("out", v) :: rest -> fold v ls rest
5702 | ("in", v) :: rest -> fold rs v rest
5703 | _ :: rest -> fold ls rs rest
5704 | [] -> ls, rs
5706 fold "" "" attrs
5709 let setconf dst src =
5710 dst.scrollbw <- src.scrollbw;
5711 dst.scrollh <- src.scrollh;
5712 dst.icase <- src.icase;
5713 dst.preload <- src.preload;
5714 dst.pagebias <- src.pagebias;
5715 dst.verbose <- src.verbose;
5716 dst.scrollstep <- src.scrollstep;
5717 dst.maxhfit <- src.maxhfit;
5718 dst.crophack <- src.crophack;
5719 dst.autoscrollstep <- src.autoscrollstep;
5720 dst.maxwait <- src.maxwait;
5721 dst.hlinks <- src.hlinks;
5722 dst.underinfo <- src.underinfo;
5723 dst.interpagespace <- src.interpagespace;
5724 dst.zoom <- src.zoom;
5725 dst.presentation <- src.presentation;
5726 dst.angle <- src.angle;
5727 dst.winw <- src.winw;
5728 dst.winh <- src.winh;
5729 dst.savebmarks <- src.savebmarks;
5730 dst.memlimit <- src.memlimit;
5731 dst.proportional <- src.proportional;
5732 dst.texcount <- src.texcount;
5733 dst.sliceheight <- src.sliceheight;
5734 dst.thumbw <- src.thumbw;
5735 dst.jumpback <- src.jumpback;
5736 dst.bgcolor <- src.bgcolor;
5737 dst.scrollbarinpm <- src.scrollbarinpm;
5738 dst.tilew <- src.tilew;
5739 dst.tileh <- src.tileh;
5740 dst.mustoresize <- src.mustoresize;
5741 dst.checkers <- src.checkers;
5742 dst.aalevel <- src.aalevel;
5743 dst.trimmargins <- src.trimmargins;
5744 dst.trimfuzz <- src.trimfuzz;
5745 dst.urilauncher <- src.urilauncher;
5746 dst.colorspace <- src.colorspace;
5747 dst.invert <- src.invert;
5748 dst.colorscale <- src.colorscale;
5749 dst.redirectstderr <- src.redirectstderr;
5750 dst.ghyllscroll <- src.ghyllscroll;
5751 dst.columns <- src.columns;
5752 dst.beyecolumns <- src.beyecolumns;
5753 dst.selcmd <- src.selcmd;
5754 dst.updatecurs <- src.updatecurs;
5755 dst.pathlauncher <- src.pathlauncher;
5756 dst.keyhashes <- copykeyhashes src;
5759 let get s =
5760 let h = Hashtbl.create 10 in
5761 let dc = { defconf with angle = defconf.angle } in
5762 let rec toplevel v t spos _ =
5763 match t with
5764 | Vdata | Vcdata | Vend -> v
5765 | Vopen ("llppconfig", _, closed) ->
5766 if closed
5767 then v
5768 else { v with f = llppconfig }
5769 | Vopen _ ->
5770 error "unexpected subelement at top level" s spos
5771 | Vclose _ -> error "unexpected close at top level" s spos
5773 and llppconfig v t spos _ =
5774 match t with
5775 | Vdata | Vcdata -> v
5776 | Vend -> error "unexpected end of input in llppconfig" s spos
5777 | Vopen ("defaults", attrs, closed) ->
5778 let c = config_of dc attrs in
5779 setconf dc c;
5780 if closed
5781 then v
5782 else { v with f = defaults }
5784 | Vopen ("ui-font", attrs, closed) ->
5785 let rec getsize size = function
5786 | [] -> size
5787 | ("size", v) :: rest ->
5788 let size =
5789 fromstring int_of_string spos "size" v fstate.fontsize in
5790 getsize size rest
5791 | l -> getsize size l
5793 fstate.fontsize <- getsize fstate.fontsize attrs;
5794 if closed
5795 then v
5796 else { v with f = uifont (Buffer.create 10) }
5798 | Vopen ("doc", attrs, closed) ->
5799 let pathent, spage, srely, span = doc_of attrs in
5800 let path = unent pathent
5801 and pageno = fromstring int_of_string spos "page" spage 0
5802 and rely = fromstring float_of_string spos "rely" srely 0.0
5803 and pan = fromstring int_of_string spos "pan" span 0 in
5804 let c = config_of dc attrs in
5805 let anchor = (pageno, rely) in
5806 if closed
5807 then (Hashtbl.add h path (c, [], pan, anchor); v)
5808 else { v with f = doc path pan anchor c [] }
5810 | Vopen _ ->
5811 error "unexpected subelement in llppconfig" s spos
5813 | Vclose "llppconfig" -> { v with f = toplevel }
5814 | Vclose _ -> error "unexpected close in llppconfig" s spos
5816 and defaults v t spos _ =
5817 match t with
5818 | Vdata | Vcdata -> v
5819 | Vend -> error "unexpected end of input in defaults" s spos
5820 | Vopen ("keymap", attrs, closed) ->
5821 let modename =
5822 try List.assoc "mode" attrs
5823 with Not_found -> "global" in
5824 if closed
5825 then v
5826 else
5827 let ret keymap =
5828 let h = findkeyhash dc modename in
5829 KeyMap.iter (Hashtbl.replace h) keymap;
5830 defaults
5832 { v with f = pkeymap ret KeyMap.empty }
5834 | Vopen (_, _, _) ->
5835 error "unexpected subelement in defaults" s spos
5837 | Vclose "defaults" ->
5838 { v with f = llppconfig }
5840 | Vclose _ -> error "unexpected close in defaults" s spos
5842 and uifont b v t spos epos =
5843 match t with
5844 | Vdata | Vcdata ->
5845 Buffer.add_substring b s spos (epos - spos);
5847 | Vopen (_, _, _) ->
5848 error "unexpected subelement in ui-font" s spos
5849 | Vclose "ui-font" ->
5850 if String.length !fontpath = 0
5851 then fontpath := Buffer.contents b;
5852 { v with f = llppconfig }
5853 | Vclose _ -> error "unexpected close in ui-font" s spos
5854 | Vend -> error "unexpected end of input in ui-font" s spos
5856 and doc path pan anchor c bookmarks v t spos _ =
5857 match t with
5858 | Vdata | Vcdata -> v
5859 | Vend -> error "unexpected end of input in doc" s spos
5860 | Vopen ("bookmarks", _, closed) ->
5861 if closed
5862 then v
5863 else { v with f = pbookmarks path pan anchor c bookmarks }
5865 | Vopen ("keymap", attrs, closed) ->
5866 let modename =
5867 try List.assoc "mode" attrs
5868 with Not_found -> "global"
5870 if closed
5871 then v
5872 else
5873 let ret keymap =
5874 let h = findkeyhash c modename in
5875 KeyMap.iter (Hashtbl.replace h) keymap;
5876 doc path pan anchor c bookmarks
5878 { v with f = pkeymap ret KeyMap.empty }
5880 | Vopen (_, _, _) ->
5881 error "unexpected subelement in doc" s spos
5883 | Vclose "doc" ->
5884 Hashtbl.add h path (c, List.rev bookmarks, pan, anchor);
5885 { v with f = llppconfig }
5887 | Vclose _ -> error "unexpected close in doc" s spos
5889 and pkeymap ret keymap v t spos _ =
5890 match t with
5891 | Vdata | Vcdata -> v
5892 | Vend -> error "unexpected end of input in keymap" s spos
5893 | Vopen ("map", attrs, closed) ->
5894 let r, l = map_of attrs in
5895 let kss = fromstring keys_of_string spos "in" r [] in
5896 let lss = fromstring keys_of_string spos "out" l [] in
5897 let keymap =
5898 match kss with
5899 | [] -> keymap
5900 | ks :: [] -> KeyMap.add ks (KMinsrl lss) keymap
5901 | ks :: rest -> KeyMap.add ks (KMmulti (rest, lss)) keymap
5903 if closed
5904 then { v with f = pkeymap ret keymap }
5905 else
5906 let f () = v in
5907 { v with f = skip "map" f }
5909 | Vopen _ ->
5910 error "unexpected subelement in keymap" s spos
5912 | Vclose "keymap" ->
5913 { v with f = ret keymap }
5915 | Vclose _ -> error "unexpected close in keymap" s spos
5917 and pbookmarks path pan anchor c bookmarks v t spos _ =
5918 match t with
5919 | Vdata | Vcdata -> v
5920 | Vend -> error "unexpected end of input in bookmarks" s spos
5921 | Vopen ("item", attrs, closed) ->
5922 let titleent, spage, srely = bookmark_of attrs in
5923 let page = fromstring int_of_string spos "page" spage 0
5924 and rely = fromstring float_of_string spos "rely" srely 0.0 in
5925 let bookmarks = (unent titleent, 0, (page, rely)) :: bookmarks in
5926 if closed
5927 then { v with f = pbookmarks path pan anchor c bookmarks }
5928 else
5929 let f () = v in
5930 { v with f = skip "item" f }
5932 | Vopen _ ->
5933 error "unexpected subelement in bookmarks" s spos
5935 | Vclose "bookmarks" ->
5936 { v with f = doc path pan anchor c bookmarks }
5938 | Vclose _ -> error "unexpected close in bookmarks" s spos
5940 and skip tag f v t spos _ =
5941 match t with
5942 | Vdata | Vcdata -> v
5943 | Vend ->
5944 error ("unexpected end of input in skipped " ^ tag) s spos
5945 | Vopen (tag', _, closed) ->
5946 if closed
5947 then v
5948 else
5949 let f' () = { v with f = skip tag f } in
5950 { v with f = skip tag' f' }
5951 | Vclose ctag ->
5952 if tag = ctag
5953 then f ()
5954 else error ("unexpected close in skipped " ^ tag) s spos
5957 parse { f = toplevel; accu = () } s;
5958 h, dc;
5961 let do_load f ic =
5963 let len = in_channel_length ic in
5964 let s = String.create len in
5965 really_input ic s 0 len;
5966 f s;
5967 with
5968 | Parse_error (msg, s, pos) ->
5969 let subs = subs s pos in
5970 let s = Printf.sprintf "%s: at %d [..%s..]" msg pos subs in
5971 failwith ("parse error: " ^ s)
5973 | exn ->
5974 failwith ("config load error: " ^ Printexc.to_string exn)
5977 let defconfpath =
5978 let dir =
5980 let dir = Filename.concat home ".config" in
5981 if Sys.is_directory dir then dir else home
5982 with _ -> home
5984 Filename.concat dir "llpp.conf"
5987 let confpath = ref defconfpath;;
5989 let load1 f =
5990 if Sys.file_exists !confpath
5991 then
5992 match
5993 (try Some (open_in_bin !confpath)
5994 with exn ->
5995 prerr_endline
5996 ("Error opening configuation file `" ^ !confpath ^ "': " ^
5997 Printexc.to_string exn);
5998 None
6000 with
6001 | Some ic ->
6002 begin try
6003 f (do_load get ic)
6004 with exn ->
6005 prerr_endline
6006 ("Error loading configuation from `" ^ !confpath ^ "': " ^
6007 Printexc.to_string exn);
6008 end;
6009 close_in ic;
6011 | None -> ()
6012 else
6013 f (Hashtbl.create 0, defconf)
6016 let load () =
6017 let f (h, dc) =
6018 let pc, pb, px, pa =
6020 Hashtbl.find h (Filename.basename state.path)
6021 with Not_found -> dc, [], 0, (0, 0.0)
6023 setconf defconf dc;
6024 setconf conf pc;
6025 state.bookmarks <- pb;
6026 state.x <- px;
6027 state.scrollw <- conf.scrollbw;
6028 if conf.jumpback
6029 then state.anchor <- pa;
6030 cbput state.hists.nav pa;
6032 load1 f
6035 let add_attrs bb always dc c =
6036 let ob s a b =
6037 if always || a != b
6038 then Printf.bprintf bb "\n %s='%b'" s a
6039 and oi s a b =
6040 if always || a != b
6041 then Printf.bprintf bb "\n %s='%d'" s a
6042 and oI s a b =
6043 if always || a != b
6044 then Printf.bprintf bb "\n %s='%s'" s (string_with_suffix_of_int a)
6045 and oz s a b =
6046 if always || a <> b
6047 then Printf.bprintf bb "\n %s='%d'" s (truncate (a*.100.))
6048 and oF s a b =
6049 if always || a <> b
6050 then Printf.bprintf bb "\n %s='%f'" s a
6051 and oc s a b =
6052 if always || a <> b
6053 then
6054 Printf.bprintf bb "\n %s='%s'" s (color_to_string a)
6055 and oC s a b =
6056 if always || a <> b
6057 then
6058 Printf.bprintf bb "\n %s='%s'" s (colorspace_to_string a)
6059 and oR s a b =
6060 if always || a <> b
6061 then
6062 Printf.bprintf bb "\n %s='%s'" s (irect_to_string a)
6063 and os s a b =
6064 if always || a <> b
6065 then
6066 Printf.bprintf bb "\n %s='%s'" s (enent a 0 (String.length a))
6067 and og s a b =
6068 if always || a <> b
6069 then
6070 match a with
6071 | None -> ()
6072 | Some (_N, _A, _B) ->
6073 Printf.bprintf bb "\n %s='%u,%u,%u'" s _N _A _B
6074 and oW s a b =
6075 if always || a <> b
6076 then
6077 let v =
6078 match a with
6079 | None -> "false"
6080 | Some f ->
6081 if f = infinity
6082 then "true"
6083 else string_of_float f
6085 Printf.bprintf bb "\n %s='%s'" s v
6086 and oco s a b =
6087 if always || a <> b
6088 then
6089 match a with
6090 | Some ((n, a, b), _) when n > 1 ->
6091 Printf.bprintf bb "\n %s='%d,%d,%d'" s n a b
6092 | _ -> ()
6093 and obeco s a b =
6094 if always || a <> b
6095 then
6096 match a with
6097 | Some c when c > 1 -> Printf.bprintf bb "\n %s='%d'" s c
6098 | _ -> ()
6100 let w, h =
6101 if always
6102 then dc.winw, dc.winh
6103 else
6104 match state.fullscreen with
6105 | Some wh -> wh
6106 | None -> c.winw, c.winh
6108 let zoom, presentation, interpagespace, maxwait =
6109 if always
6110 then dc.zoom, dc.presentation, dc.interpagespace, dc.maxwait
6111 else
6112 match state.mode with
6113 | Birdseye (bc, _, _, _, _) ->
6114 bc.zoom, bc.presentation, bc.interpagespace, bc.maxwait
6115 | _ -> c.zoom, c.presentation, c.interpagespace, c.maxwait
6117 oi "width" w dc.winw;
6118 oi "height" h dc.winh;
6119 oi "scroll-bar-width" c.scrollbw dc.scrollbw;
6120 oi "scroll-handle-height" c.scrollh dc.scrollh;
6121 ob "case-insensitive-search" c.icase dc.icase;
6122 ob "preload" c.preload dc.preload;
6123 oi "page-bias" c.pagebias dc.pagebias;
6124 oi "scroll-step" c.scrollstep dc.scrollstep;
6125 oi "auto-scroll-step" c.autoscrollstep dc.autoscrollstep;
6126 ob "max-height-fit" c.maxhfit dc.maxhfit;
6127 ob "crop-hack" c.crophack dc.crophack;
6128 oW "throttle" maxwait dc.maxwait;
6129 ob "highlight-links" c.hlinks dc.hlinks;
6130 ob "under-cursor-info" c.underinfo dc.underinfo;
6131 oi "vertical-margin" interpagespace dc.interpagespace;
6132 oz "zoom" zoom dc.zoom;
6133 ob "presentation" presentation dc.presentation;
6134 oi "rotation-angle" c.angle dc.angle;
6135 ob "persistent-bookmarks" c.savebmarks dc.savebmarks;
6136 ob "proportional-display" c.proportional dc.proportional;
6137 oI "pixmap-cache-size" c.memlimit dc.memlimit;
6138 oi "tex-count" c.texcount dc.texcount;
6139 oi "slice-height" c.sliceheight dc.sliceheight;
6140 oi "thumbnail-width" c.thumbw dc.thumbw;
6141 ob "persistent-location" c.jumpback dc.jumpback;
6142 oc "background-color" c.bgcolor dc.bgcolor;
6143 ob "scrollbar-in-presentation" c.scrollbarinpm dc.scrollbarinpm;
6144 oi "tile-width" c.tilew dc.tilew;
6145 oi "tile-height" c.tileh dc.tileh;
6146 oI "mupdf-store-size" c.mustoresize dc.mustoresize;
6147 ob "checkers" c.checkers dc.checkers;
6148 oi "aalevel" c.aalevel dc.aalevel;
6149 ob "trim-margins" c.trimmargins dc.trimmargins;
6150 oR "trim-fuzz" c.trimfuzz dc.trimfuzz;
6151 os "uri-launcher" c.urilauncher dc.urilauncher;
6152 os "path-launcher" c.pathlauncher dc.pathlauncher;
6153 oC "color-space" c.colorspace dc.colorspace;
6154 ob "invert-colors" c.invert dc.invert;
6155 oF "brightness" c.colorscale dc.colorscale;
6156 ob "redirectstderr" c.redirectstderr dc.redirectstderr;
6157 og "ghyllscroll" c.ghyllscroll dc.ghyllscroll;
6158 oco "columns" c.columns dc.columns;
6159 obeco "birds-eye-columns" c.beyecolumns dc.beyecolumns;
6160 os "selection-command" c.selcmd dc.selcmd;
6161 ob "update-cursor" c.updatecurs dc.updatecurs;
6164 let keymapsbuf always dc c =
6165 let bb = Buffer.create 16 in
6166 let rec loop = function
6167 | [] -> ()
6168 | (modename, h) :: rest ->
6169 let dh = findkeyhash dc modename in
6170 if always || h <> dh
6171 then (
6172 if Hashtbl.length h > 0
6173 then (
6174 if Buffer.length bb > 0
6175 then Buffer.add_char bb '\n';
6176 Printf.bprintf bb "<keymap mode='%s'>\n" modename;
6177 Hashtbl.iter (fun i o ->
6178 let isdifferent = always ||
6180 let dO = Hashtbl.find dh i in
6181 dO <> o
6182 with Not_found -> true
6184 if isdifferent
6185 then
6186 let addkm (k, m) =
6187 if Wsi.withctrl m then Buffer.add_string bb "ctrl-";
6188 if Wsi.withalt m then Buffer.add_string bb "alt-";
6189 if Wsi.withshift m then Buffer.add_string bb "shift-";
6190 if Wsi.withmeta m then Buffer.add_string bb "meta-";
6191 Buffer.add_string bb (Wsi.keyname k);
6193 let addkms l =
6194 let rec loop = function
6195 | [] -> ()
6196 | km :: [] -> addkm km
6197 | km :: rest -> addkm km; Buffer.add_char bb ' '; loop rest
6199 loop l
6201 Buffer.add_string bb "<map in='";
6202 addkm i;
6203 match o with
6204 | KMinsrt km ->
6205 Buffer.add_string bb "' out='";
6206 addkm km;
6207 Buffer.add_string bb "'/>\n"
6209 | KMinsrl kms ->
6210 Buffer.add_string bb "' out='";
6211 addkms kms;
6212 Buffer.add_string bb "'/>\n"
6214 | KMmulti (ins, kms) ->
6215 Buffer.add_char bb ' ';
6216 addkms ins;
6217 Buffer.add_string bb "' out='";
6218 addkms kms;
6219 Buffer.add_string bb "'/>\n"
6220 ) h;
6221 Buffer.add_string bb "</keymap>";
6224 loop rest
6226 loop c.keyhashes;
6230 let save () =
6231 let uifontsize = fstate.fontsize in
6232 let bb = Buffer.create 32768 in
6233 let f (h, dc) =
6234 let dc = if conf.bedefault then conf else dc in
6235 Buffer.add_string bb "<llppconfig>\n";
6237 if String.length !fontpath > 0
6238 then
6239 Printf.bprintf bb "<ui-font size='%d'><![CDATA[%s]]></ui-font>\n"
6240 uifontsize
6241 !fontpath
6242 else (
6243 if uifontsize <> 14
6244 then
6245 Printf.bprintf bb "<ui-font size='%d'/>\n" uifontsize
6248 Buffer.add_string bb "<defaults ";
6249 add_attrs bb true dc dc;
6250 let kb = keymapsbuf true dc dc in
6251 if Buffer.length kb > 0
6252 then (
6253 Buffer.add_string bb ">\n";
6254 Buffer.add_buffer bb kb;
6255 Buffer.add_string bb "\n</defaults>\n";
6257 else Buffer.add_string bb "/>\n";
6259 let adddoc path pan anchor c bookmarks =
6260 if bookmarks == [] && c = dc && anchor = emptyanchor
6261 then ()
6262 else (
6263 Printf.bprintf bb "<doc path='%s'"
6264 (enent path 0 (String.length path));
6266 if anchor <> emptyanchor
6267 then (
6268 let n, y = anchor in
6269 Printf.bprintf bb " page='%d'" n;
6270 if y > 1e-6
6271 then
6272 Printf.bprintf bb " rely='%f'" y
6276 if pan != 0
6277 then Printf.bprintf bb " pan='%d'" pan;
6279 add_attrs bb false dc c;
6280 let kb = keymapsbuf false dc c in
6282 begin match bookmarks with
6283 | [] ->
6284 if Buffer.length kb > 0
6285 then (
6286 Buffer.add_string bb ">\n";
6287 Buffer.add_buffer bb kb;
6288 Buffer.add_string bb "</doc>\n";
6290 else Buffer.add_string bb "/>\n"
6291 | _ ->
6292 Buffer.add_string bb ">\n<bookmarks>\n";
6293 List.iter (fun (title, _level, (page, rely)) ->
6294 Printf.bprintf bb
6295 "<item title='%s' page='%d'"
6296 (enent title 0 (String.length title))
6297 page
6299 if rely > 1e-6
6300 then
6301 Printf.bprintf bb " rely='%f'" rely
6303 Buffer.add_string bb "/>\n";
6304 ) bookmarks;
6305 Buffer.add_string bb "</bookmarks>";
6306 if Buffer.length kb > 0
6307 then (
6308 Buffer.add_string bb "\n";
6309 Buffer.add_buffer bb kb;
6311 Buffer.add_string bb "\n</doc>\n";
6312 end;
6316 let pan, conf =
6317 match state.mode with
6318 | Birdseye (c, pan, _, _, _) ->
6319 let beyecolumns =
6320 match conf.columns with
6321 | Some ((c, _, _), _) -> Some c
6322 | None -> None
6323 and columns =
6324 match c.columns with
6325 | Some (c, _) -> Some (c, [||])
6326 | None -> None
6328 pan, { c with beyecolumns = beyecolumns; columns = columns }
6329 | _ -> state.x, conf
6331 let basename = Filename.basename state.path in
6332 adddoc basename pan (getanchor ())
6333 { conf with
6334 autoscrollstep =
6335 match state.autoscroll with
6336 | Some step -> step
6337 | None -> conf.autoscrollstep }
6338 (if conf.savebmarks then state.bookmarks else []);
6340 Hashtbl.iter (fun path (c, bookmarks, x, y) ->
6341 if basename <> path
6342 then adddoc path x y c bookmarks
6343 ) h;
6344 Buffer.add_string bb "</llppconfig>";
6346 load1 f;
6347 if Buffer.length bb > 0
6348 then
6350 let tmp = !confpath ^ ".tmp" in
6351 let oc = open_out_bin tmp in
6352 Buffer.output_buffer oc bb;
6353 close_out oc;
6354 Unix.rename tmp !confpath;
6355 with exn ->
6356 prerr_endline
6357 ("error while saving configuration: " ^ Printexc.to_string exn)
6359 end;;
6361 let () =
6362 Arg.parse
6363 (Arg.align
6364 [("-p", Arg.String (fun s -> state.password <- s) ,
6365 "<password> Set password");
6367 ("-f", Arg.String (fun s -> Config.fontpath := s),
6368 "<path> Set path to the user interface font");
6370 ("-c", Arg.String (fun s -> Config.confpath := s),
6371 "<path> Set path to the configuration file");
6373 ("-v", Arg.Unit (fun () ->
6374 Printf.printf
6375 "%s\nconfiguration path: %s\n"
6376 (version ())
6377 Config.defconfpath
6379 exit 0), " Print version and exit");
6382 (fun s -> state.path <- s)
6383 ("Usage: " ^ Sys.argv.(0) ^ " [options] some.pdf\nOptions:")
6385 if String.length state.path = 0
6386 then (prerr_endline "file name missing"; exit 1);
6388 Config.load ();
6390 let globalkeyhash = findkeyhash conf "global" in
6391 let wsfd, winw, winh = Wsi.init (object
6392 method expose =
6393 if nogeomcmds state.geomcmds
6394 then display ()
6395 method display = display ()
6396 method reshape w h = reshape w h
6397 method mouse b d x y m = mouse b d x y m
6398 method motion x y = state.mpos <- (x, y); motion x y
6399 method pmotion x y = state.mpos <- (x, y); pmotion x y
6400 method key k m =
6401 let mascm = m land (
6402 Wsi.altmask + Wsi.shiftmask + Wsi.ctrlmask + Wsi.metamask
6403 ) in
6404 match state.keystate with
6405 | KSnone ->
6406 let km = k, mascm in
6407 begin
6408 match
6409 try Hashtbl.find globalkeyhash km
6410 with Not_found ->
6411 let modehash = state.uioh#modehash in
6412 try Hashtbl.find modehash km
6413 with Not_found -> KMinsrt (k, m)
6414 with
6415 | KMinsrt (k, m) -> keyboard k m
6416 | KMinsrl l -> List.iter (fun (k, m) -> keyboard k m) l
6417 | KMmulti (l, r) -> state.keystate <- KSinto (l, r)
6419 | KSinto ((k', m') :: [], insrt) when k'=k && m' land mascm = m' ->
6420 List.iter (fun (k, m) -> keyboard k m) insrt;
6421 state.keystate <- KSnone
6422 | KSinto ((k', m') :: keys, insrt) when k'=k && m' land mascm = m' ->
6423 state.keystate <- KSinto (keys, insrt)
6424 | _ ->
6425 state.keystate <- KSnone
6427 method enter x y = state.mpos <- (x, y); pmotion x y
6428 method leave = state.mpos <- (-1, -1)
6429 method quit = raise Quit
6430 end) conf.winw conf.winh in
6432 state.wsfd <- wsfd;
6434 if not (
6435 List.exists GlMisc.check_extension
6436 [ "GL_ARB_texture_rectangle"
6437 ; "GL_EXT_texture_recangle"
6438 ; "GL_NV_texture_rectangle" ]
6440 then (prerr_endline "OpenGL does not suppport rectangular textures"; exit 1);
6442 let cr, sw = Unix.pipe ()
6443 and sr, cw = Unix.pipe () in
6445 cloexec cr;
6446 cloexec sw;
6447 cloexec sr;
6448 cloexec cw;
6450 setcheckers conf.checkers;
6451 redirectstderr ();
6453 init (cr, cw) (
6454 conf.angle, conf.proportional, (conf.trimmargins, conf.trimfuzz),
6455 conf.texcount, conf.sliceheight, conf.mustoresize, conf.colorspace,
6456 !Config.fontpath
6458 state.sr <- sr;
6459 state.sw <- sw;
6460 state.text <- "Opening " ^ state.path;
6461 opendoc state.path state.password;
6462 state.uioh <- uioh;
6463 setfontsize fstate.fontsize;
6464 reshape winw winh;
6466 let rec loop deadline =
6467 let r =
6468 match state.errfd with
6469 | None -> [state.sr; state.wsfd]
6470 | Some fd -> [state.sr; state.wsfd; fd]
6472 if state.redisplay
6473 then (
6474 state.redisplay <- false;
6475 display ();
6477 let timeout =
6478 let now = now () in
6479 if deadline > now
6480 then (
6481 if deadline = infinity
6482 then ~-.1.0
6483 else max 0.0 (deadline -. now)
6485 else 0.0
6487 let r, _, _ =
6488 try Unix.select r [] [] timeout
6489 with Unix.Unix_error (Unix.EINTR, _, _) -> [], [], []
6491 begin match r with
6492 | [] ->
6493 state.ghyll None;
6494 let newdeadline =
6495 if state.ghyll == noghyll
6496 then
6497 match state.autoscroll with
6498 | Some step when step != 0 ->
6499 let y = state.y + step in
6500 let y =
6501 if y < 0
6502 then state.maxy
6503 else if y >= state.maxy then 0 else y
6505 gotoy y;
6506 if state.mode = View
6507 then state.text <- "";
6508 deadline +. 0.01
6509 | _ -> infinity
6510 else deadline +. 0.01
6512 loop newdeadline
6514 | l ->
6515 let rec checkfds = function
6516 | [] -> ()
6517 | fd :: rest when fd = state.sr ->
6518 let cmd = readcmd state.sr in
6519 act cmd;
6520 checkfds rest
6522 | fd :: rest when fd = state.wsfd ->
6523 Wsi.readresp fd;
6524 checkfds rest
6526 | fd :: rest ->
6527 let s = String.create 80 in
6528 let n = Unix.read fd s 0 80 in
6529 if conf.redirectstderr
6530 then (
6531 Buffer.add_substring state.errmsgs s 0 n;
6532 state.newerrmsgs <- true;
6533 state.redisplay <- true;
6535 else (
6536 prerr_string (String.sub s 0 n);
6537 flush stderr;
6539 checkfds rest
6541 checkfds l;
6542 let newdeadline =
6543 let deadline1 =
6544 if deadline = infinity
6545 then now () +. 0.01
6546 else deadline
6548 match state.autoscroll with
6549 | Some step when step != 0 -> deadline1
6550 | _ -> if state.ghyll == noghyll then infinity else deadline1
6552 loop newdeadline
6553 end;
6556 loop infinity;
6557 with Quit ->
6558 wcmd "quit";
6559 Config.save ();
6560 exit 0;