1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
(** Evaluating OCaml phrases to control with the editor. *)
open Chamo
let _ = Toploop.set_paths ()
let _ = Toploop.initialize_toplevel_env()
let () = Log.debug (fun m -> m "toploop initialized")
let init_file = Filename.concat Config.rc_dir "chamo_init.ml"
let local_init_file = ".chamo_init.ml"
let prompt_eval_history = Minibuffer.history ()
let prompt_eval args =
match !Gui.active_window with
None -> Lwt.return_unit
| Some w ->
let f code =
Commands.launch_command "eval" [| code |]
in
let mb = w#minibuffer in
Misc.input_string
~history: prompt_eval_history
mb ~title: "eval"
""
f
let _ =
let com = {
Commands.com_name = "prompt_eval" ;
com_args = [| |] ;
com_more_args = None ;
com_f = prompt_eval ;
}
in
Commands.register com
let eval_file args =
if Array.length args < 1 then
match !Gui.active_window with
| None -> Lwt.return_unit
| Some w ->
let f file =
Commands.launch_command "eval_file" [| file |]
in
let mb = w#minibuffer in
Misc.select_file
mb ~title: "eval_file"
""
f
else
(
let buf = Buffer.create 256 in
let fmt = Format.formatter_of_buffer buf in
Log.debug (fun m -> m "Calling Toploop.use_file on %s" args.(0));
ignore(Toploop.use_file fmt args.(0));
Log.debug (fun m -> m "Done");
Commands.launch_command "print_ocaml_output" [| Buffer.contents buf |]
)
let _ =
let com = {
Commands.com_name = "eval_file" ;
com_args = [| "File" |];
com_more_args = None ;
com_f = eval_file ;
}
in
Commands.register com
let eval_ocaml args =
Log.debug (fun m ->
let l = Array.to_list args in
m "eval_ocaml [| %s |]"
(String.concat " ; " (List.map (fun s -> Printf.sprintf "%S" s) l)))
;
let len = Array.length args in
if len < 1 then
Commands.async_command "prompt_eval"
else
(
let code = args.(0) in
let tmp_file = Filename.temp_file Messages.software ".ml" in
Misc.file_of_string ~file: tmp_file code;
eval_file [| tmp_file |];
Misc.safe_remove_file tmp_file;
)
let _ =
let com = {
Commands.com_name = "eval" ;
com_args = [| "OCaml Code" |];
com_more_args = None ;
com_f = (fun args -> eval_ocaml args; Lwt.return_unit) ;
}
in
Commands.register com
let load_file args =
if Array.length args < 1 then
match !Gui.active_window with
None -> Lwt.return_unit
| Some w ->
let f file =
Commands.launch_command "load_file" [| file |]
in
let mb = w#minibuffer in
Misc.select_file
mb ~title: "load_file"
""
f
else
(
let file = args.(0) in
let code = Printf.sprintf "#load \"%s\";;" file in
eval_ocaml [| code |];
Lwt.return_unit
)
let _ =
let com = {
Commands.com_name = "load_file" ;
com_args = [| "File" |];
com_more_args = None ;
com_f = load_file ;
}
in
Commands.register com
(** {2 Adding command line options} *)
let use file =
let com = Printf.sprintf "eval_file %s" (Filename.quote file) in
Args.append_init_command com
let option_use =
("--use", Arg.String use, "<file>\n\t\tocaml-evaluate the given file after initialization")
let _ = Args.add_option option_use;;
(** {2 Init} *)
let packages =
[ "fmt" ; "logs" ; "logs.fmt" ; "lwt.unix" ; "lwt_ppx" ;
"ocf" ; "pcre" ; "re"; "sedlex" ; "str" ; "stk" ; "stk_iconv" ;
"tsdl" ; "tsdl-image" ; "tsdl-ttf" ; "uutf" ; "xmlm" ;
"chamo" ; "lwt_ppx" ; "ocf_ppx" ; "ppx_blob" ; "sedlex.ppx"
]
let _ =
let temp_file = Filename.temp_file "chamo" ".txt" in
let com = Printf.sprintf "%s query -r %s > %s" (Config.ocamlfind())
(String.concat " " packages) (Filename.quote temp_file)
in
match Sys.command com with
| 0 ->
let default_dirs = Misc.split_string (Misc.string_of_file temp_file) ['\n'] in
Misc.safe_remove_file temp_file;
List.iter
(fun d -> eval_ocaml [| Printf.sprintf "#directory \"%s\";;" d |])
default_dirs
| n ->
Misc.safe_remove_file temp_file;
prerr_endline (Printf.sprintf "Command failed: %s" com)
;;
(** If the init file exists, then add a command to eval it at launch time *)
let _ =
let init_files = [ init_file ; local_init_file] in
Log.debug (fun m -> m "Reading init files");
List.iter
(fun file -> if Sys.file_exists file then use file)
init_files
;;