4 * Copyright (c) Tuomo Valkonen 1999-2001.
5 * See the included file LICENSE for details.
12 #include <libtu/parser.h>
13 #include <libtu/tokenizer.h>
14 #include <libtu/output.h>
23 #include "workspace.h"
25 #include "complete_file.h"
33 static void frame_set_input(WFrame
*frame
, WInput
*input
)
35 frame
->current_input
=input
;
36 if(IS_ACTIVE_FRAME(frame
))
37 set_focus((WThing
*)input
);
41 WEdln
*do_query_edln(WFrame
*frame
, WEdlnHandler
*handler
,
42 const char *prompt
, const char *dflt
,
43 EdlnCompletionHandler
*chnd
)
48 if(frame
->current_input
!=NULL
)
51 frame_client_geom(frame
, &geom
);
53 wedln
=create_wedln((WWindow
*)frame
, geom
, handler
, prompt
, dflt
);
56 wedln
->edln
.completion_handler
=chnd
;
57 frame_set_input(frame
, (WInput
*)wedln
);
64 void fwarn(WFrame
*frame
, char *p
)
71 if(p
==NULL
|| frame
->current_input
!=NULL
)
74 frame_client_geom(frame
, &geom
);
76 p2
=scat("-Error- ", p
);
78 wmsg
=create_wmsg((WWindow
*)frame
, geom
, p2
);
81 wmsg
=create_wmsg((WWindow
*)frame
, geom
, p
);
86 frame_set_input(frame
, (WInput
*)wmsg
);
89 #define FWARN(ARGS) fwarn((WFrame*)thing, errmsg ARGS)
97 static char wdbuf
[PATH_MAX
+10]="/";
98 static int wdstatus
=0;
100 static const char *my_getwd()
104 if(getcwd(wdbuf
, PATH_MAX
)==NULL
){
117 void handler_runfile(WThing
*thing
, char *str
, char *userdata
)
125 do_open_with(SCREEN_OF(thing
), userdata
, str
);
132 strncpy(wdbuf
, str
, PATH_MAX
);
137 void handler_runwith(WThing
*thing
, char *str
, char *userdata
)
139 WScreen
*scr
=SCREEN_OF(thing
);
145 do_open_with(scr
, userdata
, str
);
147 wm_exec(scr
, userdata
);
151 void handler_exec(WThing
*thing
, char *str
, char *userdata
)
153 WScreen
*scr
=SCREEN_OF(thing
);
156 do_open_with(scr
, "ion-runinxterm", str
+1);
162 void query_exec(WFrame
*frame
)
164 do_query_edln(frame
, handler_exec
, "Run:", NULL
, complete_file_with_path
);
168 void query_runfile(WFrame
*frame
, char *prompt
, char *cmd
)
170 WEdln
*wedln
=do_query_edln(frame
, handler_runfile
,
171 prompt
, my_getwd(), complete_file
);
173 wedln
->userdata
=scopy(cmd
);
177 void query_runwith(WFrame
*frame
, char *prompt
, char *cmd
)
179 WEdln
*wedln
=do_query_edln(frame
, handler_runwith
,
182 wedln
->userdata
=scopy(cmd
);
192 static bool attach_test(WFrame
*dst
, WClient
*client
, WFrame
*thing
)
194 if(!same_screen((WThing
*)dst
, (WThing
*)client
)){
195 /* complaint should go in 'thing' -frame */
196 FWARN(("Cannot attach: not on same screen."));
199 return frame_attach_client(dst
, client
, TRUE
);
203 void handler_attachclient(WThing
*thing
, char *str
, char *userdata
)
205 WClient
*client
=lookup_client(str
);
208 FWARN(("No client named '%s'", str
));
212 attach_test((WFrame
*)thing
, client
, (WFrame
*)thing
);
216 void handler_gotoclient(WThing
*thing
, char *str
, char *userdata
)
218 WClient
*client
=lookup_client(str
);
221 FWARN(("No client named '%s'", str
));
229 void query_attachclient(WFrame
*frame
)
231 do_query_edln(frame
, handler_attachclient
,
232 "Attach client:", "", complete_client
);
236 void query_gotoclient(WFrame
*frame
)
238 do_query_edln(frame
, handler_gotoclient
,
239 "Goto client:", "", complete_client
);
243 bool empty_name(const char *p
)
245 return (strspn(p
, " \t")==strlen(p
));
249 void handler_workspace(WThing
*thing
, char *name
, char *userdata
)
251 WScreen
*scr
=SCREEN_OF(thing
);
257 ws
=lookup_workspace(name
);
260 ws
=create_workspace(scr
, name
, TRUE
);
262 FWARN(("Unable to create workspace."));
267 switch_workspace(ws
);
271 void query_workspace(WFrame
*frame
)
273 do_query_edln(frame
, handler_workspace
,
274 "Goto/create workspace:", "", complete_workspace
);
278 void handler_workspace_with(WThing
*thing
, char *name
, char *userdata
)
280 WScreen
*scr
=SCREEN_OF(thing
);
288 ws
=lookup_workspace(name
);
289 client
=lookup_client(userdata
);
292 frame
=(WFrame
*)find_current(ws
);
293 if(frame
==NULL
|| !WTHING_IS(frame
, WFrame
)){
294 FWARN(("Workspace %s has no current frame", name
));
299 FWARN(("Client disappeared"));
303 ws
=create_workspace(scr
, name
, TRUE
);
305 FWARN(("Unable to create workspace."));
309 frame
=FIRST_THING(ws
, WFrame
);
314 if(attach_test((WFrame
*)frame
, client
, (WFrame
*)thing
))
319 void query_workspace_with(WFrame
*frame
)
322 WClient
*client
=frame
->current_client
;
326 query_workspace(frame
);
330 p
=client_full_label(client
);
332 wedln
=do_query_edln(frame
, handler_workspace_with
,
333 "Create workspace/attach:", p
, complete_workspace
);
341 void handler_renameworkspace(WThing
*thing
, char *name
, char *userdata
)
343 WWorkspace
*ws
=SCREEN_OF(thing
)->current_workspace
;
348 rename_workspace(ws
, name
);
352 void query_renameworkspace(WFrame
*frame
)
354 WWorkspace
*ws
= wglobal
.current_screen
->current_workspace
;
356 do_query_edln(frame
, handler_renameworkspace
,
357 "Rename workspace to:", ws
->name
, complete_workspace
);
366 /* ugly hack to know what we operate on in opt_default() */
367 static WThing
*function_thing
=NULL
;
368 static WThing
*function_frame
=NULL
;
369 static char *last_error_message
=NULL
;
372 static void function_warn_handler(const char *message
)
374 if(last_error_message
!=NULL
)
375 free(last_error_message
);
376 last_error_message
=scopy(message
);
380 /* We don't want to refer to destroyed things. */
381 void query_check_function_thing(WThing
*t
)
383 if(function_thing
==t
)
384 function_thing
=t
->t_parent
;
385 if(function_frame
==t
)
390 void query_set_function_thing(WThing
*t
)
397 static bool opt_default(Tokenizer
*tokz
, int n
, Token
*toks
)
399 WThing
*thing
=function_thing
;
401 char *name
=TOK_IDENT_VAL(&(toks
[0]));
406 func
=lookup_func(name
, FUNTAB_MAIN
);
409 warn("Unknown function '%s' or not in FUNTAB_MAIN.", name
);
413 if(!check_args(tokz
, toks
, n
, func
->argtypes
)){
414 warn("Argument check for function '%s' failed. Prototype is '%s'.",
415 name
, func
->argtypes
);
419 func
->callhnd(thing
, func
, n
-1, &(toks
[1]));
421 /*if(wglobal.focus_next!=NULL)
422 function_thing=wglobal.focus_next;*/
428 static ConfOpt command_opts
[]={
429 {"#default", NULL
, opt_default
, NULL
},
430 {NULL
, NULL
, NULL
, NULL
}
434 bool command_sequence(WThing
*thing
, char *fn
)
436 static bool command_sq
=FALSE
;
442 warn("Nested command sequence.");
447 function_thing
=thing
;
449 tokz
=tokz_prepare_buffer(fn
, -1);
450 tokz
->flags
|=TOKZ_DEFAULT_OPTION
;
451 retval
=parse_config_tokz(tokz
, command_opts
);
461 void handler_function(WThing
*thing
, char *fn
, char *userdata
)
463 WarnHandler
*old_warn_handler
;
467 function_frame
=thing
;
469 old_warn_handler
=set_warn_handler(function_warn_handler
);
470 error
=!command_sequence(thing
, fn
);
471 set_warn_handler(old_warn_handler
);
473 if(function_frame
!=NULL
){
474 if(last_error_message
!=NULL
){
475 FWARN(("%s", last_error_message
));
477 FWARN(("An unknown error occurred while trying to "
478 "parse your request"));
484 if(last_error_message
!=NULL
){
485 free(last_error_message
);
486 last_error_message
=NULL
;
491 void handler_yesno(WThing
*thing
, char *yesno
, char *fn
)
493 if(strcasecmp(yesno
, "y") && strcasecmp(yesno
, "yes"))
496 handler_function(thing
, fn
, NULL
);
500 void query_yesno(WFrame
*frame
, char *prompt
, char *fn
)
502 WEdln
*wedln
=do_query_edln(frame
, handler_yesno
,
505 wedln
->userdata
=scopy(fn
);
509 void query_function(WFrame
*frame
)
511 do_query_edln(frame
, handler_function
,
512 "Function name:", NULL
, complete_mainfunc
);