2
* Copyright (C) 1997-2011 Jiri (George) Lebl
4
* Author: Jiri (George) Lebl
6
* This file is part of Genius.
8
* Genius is free software: you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation, either version 3 of the License, or
11
* (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program. If not, see <http://www.gnu.org/licenses/>.
29
#include <readline/readline.h>
30
#include <readline/history.h>
38
static int toplevelokg = TRUE;
43
char *t = g_strstrip(g_strdup(s));
44
if(strncmp(t,"plugin",strlen(t))==0 ||
45
strncmp(t,"load",strlen(t))==0 ||
46
strncmp(t,"cd",strlen(t))==0 ||
47
strncmp(t,"pwd",strlen(t))==0 ||
48
strncmp(t,"ls",strlen(t))==0) {
58
get_p_expression(void)
61
char *prompt = "genius> ";
63
gel_interrupted = FALSE;
65
gs = g_string_new("");
71
int oldtop = toplevelokg;
73
toplevelokg = ok_for_top(gs->str);
78
g_string_free(gs,TRUE);
86
g_string_append_c(gs,'\n');
87
ret = gel_parseexp(gs->str, NULL, TRUE, FALSE, NULL, NULL);
88
g_string_free(gs,TRUE);
96
g_string_append(gs,s);
98
g_string_append_c(gs,'\n');
100
ret = gel_parseexp(gs->str, NULL, TRUE, TRUE, &finished, NULL);
104
g_string_free(gs,TRUE);
110
static GString *p_expr = NULL;
111
static int old_toplevelokg = FALSE;
112
void (*got_expr_func)(GelETree *) = NULL;
115
write_all_state_to_rl(FILE *fp)
123
for(li=d_getcontext();li;li=li->next) {
124
GelEFunc *f = li->data;
125
if(!f->id || !f->id->token)
129
fprintf(fp,"FUNCTIONS %d\n",count);
130
for(li=d_getcontext();li;li=li->next) {
131
GelEFunc *f = li->data;
132
if(!f->id || !f->id->token)
134
fprintf(fp,"%s\n",f->id->token);
138
for(li = gel_plugin_list;li;li=li->next) {
139
GelPlugin *plg = li->data;
144
fprintf(fp,"PLUGINS %d\n",count);
145
for(li = gel_plugin_list;li;li=li->next) {
146
GelPlugin *plg = li->data;
149
fprintf(fp,"%s\n",plg->base);
152
s = g_get_current_dir ();
153
fprintf (fp, "CWD %s\n", s);
157
fprintf(fp,"TOPLEVEL OK\n");
159
fprintf(fp,"TOPLEVEL NOT OK\n");
165
get_cb_p_expression(char *s, FILE *torlfp)
170
char *prompt = " > ";
171
toplevelokg = old_toplevelokg;
173
if(gel_interrupted) {
174
prompt = "\001\e[1m\002genius>\001\e[0m\002 ";
175
gel_interrupted = FALSE;
176
if(p_expr) g_string_free(p_expr,TRUE);
183
g_string_append_c(p_expr, '\n');
184
ret = gel_parseexp(p_expr->str, NULL, TRUE, FALSE, NULL, NULL);
185
g_string_free(p_expr, TRUE);
187
(*got_expr_func)(ret);
188
gel_interrupted = FALSE;
189
prompt = "\001\e[1m\002genius>\001\e[0m\002 ";
196
g_string_append(p_expr,s);
197
g_string_append_c(p_expr,'\n');
199
ret = gel_parseexp(p_expr->str, NULL, TRUE, TRUE, &finished, NULL);
203
g_string_free(p_expr,TRUE);
205
(*got_expr_func)(ret);
206
gel_interrupted = FALSE;
207
prompt = "\001\e[1m\002genius>\001\e[0m\002 ";
210
if(!p_expr) p_expr = g_string_new("");
211
old_toplevelokg = toplevelokg;
212
toplevelokg = ok_for_top(p_expr->str);
214
write_all_state_to_rl(torlfp);
215
fprintf(torlfp,"READLINE %s\n",prompt);
220
start_cb_p_expression(void (*get_func)(GelETree *), FILE *torlfp)
222
gel_interrupted = FALSE;
224
gel_rewind_file_info();
226
if(p_expr) g_string_free(p_expr,TRUE);
227
p_expr = g_string_new("");
229
got_expr_func = get_func;
231
old_toplevelokg = toplevelokg;
232
toplevelokg = ok_for_top(p_expr->str);
234
write_all_state_to_rl(torlfp);
235
fprintf(torlfp,"READLINE \001\e[1m\002genius>\001\e[0m\002 \n");
240
stop_cb_p_expression(void)
242
if(p_expr) g_string_free(p_expr,TRUE);
245
toplevelokg = old_toplevelokg;
247
got_expr_func = NULL;
252
static int addtoplevels = TRUE;
253
extern const char *genius_toplevels[];
254
extern const char *genius_operators[];
258
command_generator (const char *text, int state)
260
static int oi,ti,len;
270
fli = d_getcontext();
273
while(ti>=0 && genius_toplevels[ti]) {
274
const char *s = genius_toplevels[ti++];
275
if(strncmp(s,text,len)==0)
279
while(genius_operators[oi]) {
280
const char *s = genius_operators[oi++];
281
if(strncmp(s,text,len)==0)
286
GelEFunc *f = fli->data;
287
fli = g_slist_next(fli);
288
if(!f->id || !f->id->token)
290
if(strncmp(f->id->token,text,len)==0)
291
return strdup(f->id->token);
298
plugin_generator (const char *text, int state)
305
li = gel_plugin_list;
309
GelPlugin *plg = li->data;
310
li = g_slist_next(li);
313
if(strncmp(plg->base,text,len)==0)
314
return strdup(plg->base);
320
/* Note: keep in sync with genius-readline-helper.c */
321
/* FIXME: make this common */
323
tab_completion (const char *text, int start, int end)
326
for(p=rl_line_buffer;*p==' ' || *p=='\t';p++)
329
(strncmp(p,"load ",5)==0 ||
330
strncmp(p,"load\t",5)==0)) {
334
(strncmp(p,"ls ",3)==0 ||
335
strncmp(p,"ls\t",3)==0)) {
339
(strncmp(p,"cd ",3)==0 ||
340
strncmp(p,"cd\t",3)==0)) {
345
(strncmp(p,"plugin ",7)==0 ||
346
strncmp(p,"plugin\t",7)==0)) {
347
return rl_completion_matches (text, plugin_generator);
352
strncmp(p,"load",strlen(p))==0 ||
353
strncmp(p,"cd",strlen(p))==0 ||
354
strncmp(p,"ls",strlen(p))==0 ||
355
strncmp(p,"pwd",strlen(p))==0 ||
356
strncmp(p,"help",strlen(p))==0 ||
357
strncmp(p,"plugin",strlen(p))==0))
360
addtoplevels = FALSE;
362
return rl_completion_matches (text, command_generator);
368
rl_readline_name = "Genius";
369
rl_attempted_completion_function =
370
(CPPFunction *)tab_completion;