1999-12-16 04:25:11 -08:00
|
|
|
(*************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Objective Caml LablTk library *)
|
|
|
|
(* *)
|
|
|
|
(* Jacques Garrigue, Kyoto University RIMS *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 1999 Institut National de Recherche en Informatique et *)
|
|
|
|
(* en Automatique and Kyoto University. All rights reserved. *)
|
|
|
|
(* This file is distributed under the terms of the GNU Library *)
|
2001-12-07 05:41:02 -08:00
|
|
|
(* General Public License, with the special exception on linking *)
|
|
|
|
(* described in file ../../../LICENSE. *)
|
1999-12-16 04:25:11 -08:00
|
|
|
(* *)
|
|
|
|
(*************************************************************************)
|
|
|
|
|
1999-11-30 06:59:39 -08:00
|
|
|
(* $Id$ *)
|
|
|
|
|
|
|
|
(* file selection box *)
|
|
|
|
|
2001-09-06 01:52:32 -07:00
|
|
|
open StdLabels
|
1999-11-30 06:59:39 -08:00
|
|
|
open Useunix
|
|
|
|
open Str
|
|
|
|
open Filename
|
|
|
|
|
|
|
|
open Tk
|
|
|
|
|
|
|
|
(**** Memoized rexgexp *)
|
|
|
|
|
2000-04-11 20:43:25 -07:00
|
|
|
let (~!) = Jg_memo.fast ~f:Str.regexp
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
(************************************************************ Path name *)
|
|
|
|
|
2000-05-08 02:19:41 -07:00
|
|
|
(* Convert Windows-style directory separator '\' to caml-style '/' *)
|
|
|
|
let caml_dir path =
|
|
|
|
if Sys.os_type = "Win32" then
|
2001-09-06 01:52:32 -07:00
|
|
|
global_replace ~!"\\\\" "/" path
|
2000-05-08 02:19:41 -07:00
|
|
|
else path
|
|
|
|
|
|
|
|
let parse_filter s =
|
|
|
|
let s = caml_dir s in
|
1999-11-30 06:59:39 -08:00
|
|
|
(* replace // by / *)
|
2001-09-06 01:52:32 -07:00
|
|
|
let s = global_replace ~!"/+" "/" s in
|
1999-11-30 06:59:39 -08:00
|
|
|
(* replace /./ by / *)
|
2002-01-21 06:36:15 -08:00
|
|
|
let s = global_replace ~!"/\\./" "/" s in
|
1999-11-30 06:59:39 -08:00
|
|
|
(* replace hoge/../ by "" *)
|
2002-01-21 06:36:15 -08:00
|
|
|
let s = global_replace
|
|
|
|
~!"\\([^/]\\|[^\\./][^/]\\|[^/][^\\./]\\|[^/][^/]+\\)/\\.\\./" "" s in
|
1999-11-30 06:59:39 -08:00
|
|
|
(* replace hoge/..$ by *)
|
2002-01-21 06:36:15 -08:00
|
|
|
let s = global_replace
|
|
|
|
~!"\\([^/]\\|[^\\./][^/]\\|[^/][^\\./]\\|[^/][^/]+\\)/\\.\\.$" "" s in
|
2000-05-08 02:19:41 -07:00
|
|
|
(* replace ^/hoge/../ by / *)
|
2002-01-21 06:36:15 -08:00
|
|
|
let s = global_replace ~!"^\\(/\\.\\.\\)+/" "/" s in
|
|
|
|
if string_match ~!"^\\([^\\*?[]*[/:]\\)\\(.*\\)" s 0 then
|
1999-11-30 06:59:39 -08:00
|
|
|
let dirs = matched_group 1 s
|
|
|
|
and ptrn = matched_group 2 s
|
|
|
|
in
|
|
|
|
dirs, ptrn
|
|
|
|
else "", s
|
|
|
|
|
2000-04-11 20:43:25 -07:00
|
|
|
let rec fixpoint ~f v =
|
1999-12-10 01:40:51 -08:00
|
|
|
let v' = f v in
|
2000-04-11 20:43:25 -07:00
|
|
|
if v = v' then v else fixpoint ~f v'
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
let unix_regexp s =
|
2001-09-06 01:52:32 -07:00
|
|
|
let s = Str.global_replace ~!"[$^.+]" "\\\\\\0" s in
|
|
|
|
let s = Str.global_replace ~!"\\*" ".*" s in
|
|
|
|
let s = Str.global_replace ~!"\\?" ".?" s in
|
1999-11-30 06:59:39 -08:00
|
|
|
let s =
|
1999-12-10 01:40:51 -08:00
|
|
|
fixpoint s
|
2001-09-06 01:52:32 -07:00
|
|
|
~f:(Str.replace_first ~!"\\({.*\\),\\(.*}\\)" "\\1\\|\\2") in
|
1999-11-30 06:59:39 -08:00
|
|
|
let s =
|
2001-09-06 01:52:32 -07:00
|
|
|
Str.global_replace ~!"{\\(.*\\)}" "\\(\\1\\)" s in
|
1999-11-30 06:59:39 -08:00
|
|
|
Str.regexp s
|
|
|
|
|
2001-09-06 01:52:32 -07:00
|
|
|
let exact_match ~pat s =
|
|
|
|
Str.string_match pat s 0 && Str.match_end () = String.length s
|
1999-11-30 06:59:39 -08:00
|
|
|
|
2000-04-11 20:43:25 -07:00
|
|
|
let ls ~dir ~pattern =
|
1999-11-30 06:59:39 -08:00
|
|
|
let files = get_files_in_directory dir in
|
|
|
|
let regexp = unix_regexp pattern in
|
2000-04-11 20:43:25 -07:00
|
|
|
List.filter files ~f:(exact_match ~pat:regexp)
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
(********************************************* Creation *)
|
|
|
|
let load_in_path = ref false
|
|
|
|
|
2000-04-11 20:43:25 -07:00
|
|
|
let search_in_path ~name = Misc.find_in_path !Config.load_path name
|
1999-11-30 06:59:39 -08:00
|
|
|
|
2000-04-11 20:43:25 -07:00
|
|
|
let f ~title ~action:proc ?(dir = Unix.getcwd ())
|
|
|
|
?filter:(deffilter ="*") ?file:(deffile ="")
|
|
|
|
?(multi=false) ?(sync=false) ?(usepath=true) () =
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
let current_pattern = ref ""
|
2000-05-08 02:19:41 -07:00
|
|
|
and current_dir = ref (caml_dir dir) in
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
let tl = Jg_toplevel.titled title in
|
|
|
|
Focus.set tl;
|
|
|
|
|
2000-04-11 20:43:25 -07:00
|
|
|
let new_var () = Textvariable.create ~on:tl () in
|
1999-11-30 06:59:39 -08:00
|
|
|
let filter_var = new_var ()
|
|
|
|
and selection_var = new_var ()
|
|
|
|
and sync_var = new_var () in
|
2000-04-02 18:57:52 -07:00
|
|
|
Textvariable.set filter_var deffilter;
|
1999-11-30 06:59:39 -08:00
|
|
|
|
2000-04-11 20:43:25 -07:00
|
|
|
let frm = Frame.create tl ~borderwidth:1 ~relief:`Raised in
|
1999-11-30 06:59:39 -08:00
|
|
|
let df = Frame.create frm in
|
|
|
|
let dfl = Frame.create df in
|
2000-04-11 20:43:25 -07:00
|
|
|
let dfll = Label.create dfl ~text:"Directories" in
|
1999-11-30 06:59:39 -08:00
|
|
|
let dflf, directory_listbox, directory_scrollbar =
|
|
|
|
Jg_box.create_with_scrollbar dfl in
|
|
|
|
let dfr = Frame.create df in
|
2000-04-11 20:43:25 -07:00
|
|
|
let dfrl = Label.create dfr ~text:"Files" in
|
1999-11-30 06:59:39 -08:00
|
|
|
let dfrf, filter_listbox, filter_scrollbar =
|
|
|
|
Jg_box.create_with_scrollbar dfr in
|
2000-04-11 20:43:25 -07:00
|
|
|
let cfrm = Frame.create tl ~borderwidth:1 ~relief:`Raised in
|
1999-11-30 06:59:39 -08:00
|
|
|
|
2000-04-11 20:43:25 -07:00
|
|
|
let configure ~filter =
|
1999-11-30 06:59:39 -08:00
|
|
|
let filter =
|
2000-05-08 03:17:14 -07:00
|
|
|
if Filename.is_relative filter
|
|
|
|
then !current_dir ^ "/" ^ filter
|
|
|
|
else filter
|
1999-11-30 06:59:39 -08:00
|
|
|
in
|
|
|
|
let dir, pattern = parse_filter filter in
|
2000-12-28 05:07:42 -08:00
|
|
|
let dir = if !load_in_path && usepath then "" else
|
1999-11-30 06:59:39 -08:00
|
|
|
(current_dir := Filename.dirname dir; dir)
|
|
|
|
and pattern = if pattern = "" then "*" else pattern in
|
|
|
|
current_pattern := pattern;
|
|
|
|
let filter =
|
2000-12-28 05:07:42 -08:00
|
|
|
if !load_in_path && usepath then pattern else dir ^ pattern in
|
2000-04-11 20:43:25 -07:00
|
|
|
let directories = get_directories_in_files ~path:dir
|
1999-11-30 06:59:39 -08:00
|
|
|
(get_files_in_directory dir) in
|
|
|
|
let matched_files = (* get matched file by subshell call. *)
|
2000-12-28 05:07:42 -08:00
|
|
|
if !load_in_path && usepath then
|
2000-04-11 20:43:25 -07:00
|
|
|
List.fold_left !Config.load_path ~init:[] ~f:
|
2000-04-02 18:57:52 -07:00
|
|
|
begin fun acc dir ->
|
2000-04-11 20:43:25 -07:00
|
|
|
let files = ls ~dir ~pattern in
|
2001-09-06 01:52:32 -07:00
|
|
|
Sort.merge (<) files
|
2000-04-11 20:43:25 -07:00
|
|
|
(List.fold_left files ~init:acc
|
|
|
|
~f:(fun acc name -> List2.exclude name acc))
|
1999-11-30 06:59:39 -08:00
|
|
|
end
|
|
|
|
else
|
2000-04-11 20:43:25 -07:00
|
|
|
List.fold_left directories ~init:(ls ~dir ~pattern)
|
|
|
|
~f:(fun acc dir -> List2.exclude dir acc)
|
1999-11-30 06:59:39 -08:00
|
|
|
in
|
2000-04-02 18:57:52 -07:00
|
|
|
Textvariable.set filter_var filter;
|
|
|
|
Textvariable.set selection_var (dir ^ deffile);
|
2000-04-11 20:43:25 -07:00
|
|
|
Listbox.delete filter_listbox ~first:(`Num 0) ~last:`End;
|
|
|
|
Listbox.insert filter_listbox ~index:`End ~texts:matched_files;
|
|
|
|
Jg_box.recenter filter_listbox ~index:(`Num 0);
|
2000-12-28 05:07:42 -08:00
|
|
|
if !load_in_path && usepath then
|
2000-04-11 20:43:25 -07:00
|
|
|
Listbox.configure directory_listbox ~takefocus:false
|
1999-11-30 06:59:39 -08:00
|
|
|
else
|
|
|
|
begin
|
2000-04-11 20:43:25 -07:00
|
|
|
Listbox.configure directory_listbox ~takefocus:true;
|
|
|
|
Listbox.delete directory_listbox ~first:(`Num 0) ~last:`End;
|
|
|
|
Listbox.insert directory_listbox ~index:`End ~texts:directories;
|
|
|
|
Jg_box.recenter directory_listbox ~index:(`Num 0)
|
1999-11-30 06:59:39 -08:00
|
|
|
end
|
|
|
|
in
|
|
|
|
|
|
|
|
let selected_files = ref [] in (* used for synchronous mode *)
|
|
|
|
let activate l =
|
|
|
|
Grab.release tl;
|
|
|
|
destroy tl;
|
|
|
|
let l =
|
2000-12-28 05:07:42 -08:00
|
|
|
if !load_in_path && usepath then
|
2000-04-11 20:43:25 -07:00
|
|
|
List.fold_right l ~init:[] ~f:
|
2000-04-02 18:57:52 -07:00
|
|
|
begin fun name acc ->
|
2000-05-08 03:17:14 -07:00
|
|
|
if not (Filename.is_implicit name) then
|
|
|
|
if Filename.is_relative name
|
|
|
|
then (!current_dir ^ "/" ^ name) :: acc
|
|
|
|
else name :: acc
|
|
|
|
else try search_in_path ~name :: acc with Not_found -> acc
|
1999-11-30 06:59:39 -08:00
|
|
|
end
|
|
|
|
else
|
2000-04-11 20:43:25 -07:00
|
|
|
List.map l ~f:
|
1999-11-30 06:59:39 -08:00
|
|
|
begin fun x ->
|
2000-05-08 03:17:14 -07:00
|
|
|
if Filename.is_relative x
|
|
|
|
then !current_dir ^ "/" ^ x
|
|
|
|
else x
|
1999-11-30 06:59:39 -08:00
|
|
|
end
|
|
|
|
in
|
|
|
|
if sync then
|
|
|
|
begin
|
|
|
|
selected_files := l;
|
2000-04-02 18:57:52 -07:00
|
|
|
Textvariable.set sync_var "1"
|
1999-11-30 06:59:39 -08:00
|
|
|
end
|
|
|
|
else proc l
|
|
|
|
in
|
|
|
|
|
|
|
|
(* entries *)
|
2000-04-11 20:43:25 -07:00
|
|
|
let fl = Label.create frm ~text:"Filter" in
|
|
|
|
let sl = Label.create frm ~text:"Selection" in
|
|
|
|
let filter_entry = Jg_entry.create frm ~textvariable:filter_var
|
|
|
|
~command:(fun filter -> configure ~filter) in
|
|
|
|
let selection_entry = Jg_entry.create frm ~textvariable:selection_var
|
|
|
|
~command:(fun file -> activate [file]) in
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
(* and buttons *)
|
2000-04-11 20:43:25 -07:00
|
|
|
let set_path = Button.create dfl ~text:"Path editor" ~command:
|
1999-11-30 06:59:39 -08:00
|
|
|
begin fun () ->
|
2000-04-11 20:43:25 -07:00
|
|
|
Setpath.add_update_hook (fun () -> configure ~filter:!current_pattern);
|
|
|
|
let w = Setpath.f ~dir:!current_dir in
|
1999-11-30 06:59:39 -08:00
|
|
|
Grab.set w;
|
2000-04-11 20:43:25 -07:00
|
|
|
bind w ~events:[`Destroy] ~extend:true ~action:(fun _ -> Grab.set tl)
|
1999-11-30 06:59:39 -08:00
|
|
|
end in
|
2000-04-11 20:43:25 -07:00
|
|
|
let toggle_in_path = Checkbutton.create dfl ~text:"Use load path"
|
|
|
|
~command:
|
1999-11-30 06:59:39 -08:00
|
|
|
begin fun () ->
|
|
|
|
load_in_path := not !load_in_path;
|
|
|
|
if !load_in_path then
|
2000-04-11 20:43:25 -07:00
|
|
|
pack [set_path] ~side:`Bottom ~fill:`X ~expand:true
|
1999-11-30 06:59:39 -08:00
|
|
|
else
|
|
|
|
Pack.forget [set_path];
|
2000-04-11 20:43:25 -07:00
|
|
|
configure ~filter:(Textvariable.get filter_var)
|
1999-11-30 06:59:39 -08:00
|
|
|
end
|
2000-04-11 20:43:25 -07:00
|
|
|
and okb = Button.create cfrm ~text:"Ok" ~command:
|
1999-11-30 06:59:39 -08:00
|
|
|
begin fun () ->
|
|
|
|
let files =
|
2000-04-11 20:43:25 -07:00
|
|
|
List.map (Listbox.curselection filter_listbox) ~f:
|
1999-11-30 06:59:39 -08:00
|
|
|
begin fun x ->
|
2000-04-11 20:43:25 -07:00
|
|
|
!current_dir ^ Listbox.get filter_listbox ~index:x
|
1999-11-30 06:59:39 -08:00
|
|
|
end
|
|
|
|
in
|
|
|
|
let files = if files = [] then [Textvariable.get selection_var]
|
|
|
|
else files in
|
|
|
|
activate [Textvariable.get selection_var]
|
|
|
|
end
|
2000-04-11 20:43:25 -07:00
|
|
|
and flb = Button.create cfrm ~text:"Filter"
|
|
|
|
~command:(fun () -> configure ~filter:(Textvariable.get filter_var))
|
|
|
|
and ccb = Button.create cfrm ~text:"Cancel"
|
|
|
|
~command:(fun () -> activate []) in
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
(* binding *)
|
2000-04-11 20:43:25 -07:00
|
|
|
bind tl ~events:[`KeyPressDetail "Escape"] ~action:(fun _ -> activate []);
|
1999-11-30 06:59:39 -08:00
|
|
|
Jg_box.add_completion filter_listbox
|
2000-04-11 20:43:25 -07:00
|
|
|
~action:(fun index -> activate [Listbox.get filter_listbox ~index]);
|
|
|
|
if multi then Listbox.configure filter_listbox ~selectmode:`Multiple else
|
|
|
|
bind filter_listbox ~events:[`ButtonPressDetail 1] ~fields:[`MouseY]
|
|
|
|
~action:(fun ev ->
|
1999-11-30 06:59:39 -08:00
|
|
|
let name = Listbox.get filter_listbox
|
2000-04-11 20:43:25 -07:00
|
|
|
~index:(Listbox.nearest filter_listbox ~y:ev.ev_MouseY) in
|
2000-12-28 05:07:42 -08:00
|
|
|
if !load_in_path && usepath then
|
2000-04-11 20:43:25 -07:00
|
|
|
try Textvariable.set selection_var (search_in_path ~name)
|
1999-11-30 06:59:39 -08:00
|
|
|
with Not_found -> ()
|
2000-04-02 18:57:52 -07:00
|
|
|
else Textvariable.set selection_var (!current_dir ^ "/" ^ name));
|
1999-11-30 06:59:39 -08:00
|
|
|
|
2000-04-11 20:43:25 -07:00
|
|
|
Jg_box.add_completion directory_listbox ~action:
|
1999-11-30 06:59:39 -08:00
|
|
|
begin fun index ->
|
|
|
|
let filter =
|
|
|
|
!current_dir ^ "/" ^
|
2000-04-11 20:43:25 -07:00
|
|
|
(Listbox.get directory_listbox ~index) ^
|
1999-11-30 06:59:39 -08:00
|
|
|
"/" ^ !current_pattern
|
2000-04-11 20:43:25 -07:00
|
|
|
in configure ~filter
|
1999-11-30 06:59:39 -08:00
|
|
|
end;
|
|
|
|
|
2000-04-11 20:43:25 -07:00
|
|
|
pack [frm] ~fill:`Both ~expand:true;
|
1999-11-30 06:59:39 -08:00
|
|
|
(* filter *)
|
2000-04-11 20:43:25 -07:00
|
|
|
pack [fl] ~side:`Top ~anchor:`W;
|
|
|
|
pack [filter_entry] ~side:`Top ~fill:`X;
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
(* directory + files *)
|
2000-04-11 20:43:25 -07:00
|
|
|
pack [df] ~side:`Top ~fill:`Both ~expand:true;
|
1999-11-30 06:59:39 -08:00
|
|
|
(* directory *)
|
2000-04-11 20:43:25 -07:00
|
|
|
pack [dfl] ~side:`Left ~fill:`Both ~expand:true;
|
|
|
|
pack [dfll] ~side:`Top ~anchor:`W;
|
|
|
|
if usepath then pack [toggle_in_path] ~side:`Bottom ~anchor:`W;
|
|
|
|
pack [dflf] ~side:`Top ~fill:`Both ~expand:true;
|
|
|
|
pack [directory_scrollbar] ~side:`Right ~fill:`Y;
|
|
|
|
pack [directory_listbox] ~side:`Left ~fill:`Both ~expand:true;
|
1999-11-30 06:59:39 -08:00
|
|
|
(* files *)
|
2000-04-11 20:43:25 -07:00
|
|
|
pack [dfr] ~side:`Right ~fill:`Both ~expand:true;
|
|
|
|
pack [dfrl] ~side:`Top ~anchor:`W;
|
|
|
|
pack [dfrf] ~side:`Top ~fill:`Both ~expand:true;
|
|
|
|
pack [filter_scrollbar] ~side:`Right ~fill:`Y;
|
|
|
|
pack [filter_listbox] ~side:`Left ~fill:`Both ~expand:true;
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
(* selection *)
|
2000-04-11 20:43:25 -07:00
|
|
|
pack [sl] ~before:df ~side:`Bottom ~anchor:`W;
|
|
|
|
pack [selection_entry] ~before:sl ~side:`Bottom ~fill:`X;
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
(* create OK, Filter and Cancel buttons *)
|
2000-04-11 20:43:25 -07:00
|
|
|
pack [okb; flb; ccb] ~side:`Left ~fill:`X ~expand:true;
|
|
|
|
pack [cfrm] ~before:frm ~side:`Bottom ~fill:`X;
|
1999-11-30 06:59:39 -08:00
|
|
|
|
2000-12-28 05:07:42 -08:00
|
|
|
if !load_in_path && usepath then begin
|
1999-11-30 06:59:39 -08:00
|
|
|
load_in_path := false;
|
|
|
|
Checkbutton.invoke toggle_in_path;
|
|
|
|
Checkbutton.select toggle_in_path
|
|
|
|
end
|
2000-04-11 20:43:25 -07:00
|
|
|
else configure ~filter:deffilter;
|
1999-11-30 06:59:39 -08:00
|
|
|
|
|
|
|
Tkwait.visibility tl;
|
|
|
|
Grab.set tl;
|
|
|
|
|
|
|
|
if sync then
|
|
|
|
begin
|
|
|
|
Tkwait.variable sync_var;
|
|
|
|
proc !selected_files
|
|
|
|
end;
|
|
|
|
()
|