Allow 'emit' to accept event arguments

This commit is contained in:
Jan Kanis 2012-12-20 01:11:55 +01:00
parent 1f0ae8b06d
commit af3059ab2a
6 changed files with 19 additions and 28 deletions

View file

@ -1010,15 +1010,15 @@ static int builtin_emit(parser_t &parser, wchar_t **argv)
}
for (; woptind < argc; woptind++)
wcstring_list_t args;
wchar_t *eventname = argv[woptind];
for (woptind++; woptind < argc; woptind++)
{
event_fire_generic(argv[woptind]);
args.push_back(argv[woptind]);
}
event_fire_generic(eventname, &args);
return STATUS_BUILTIN_OK;
}

View file

@ -1,11 +1,11 @@
\section emit emit - Emit a generic event
\subsection block-synopsis Synopsis
<tt>emit EVENT_NAME</tt>
<tt>emit EVENT_NAME [ARGUMENTS...]</tt>
\subsection emit-description Description
The emit builtin fires a generic fish event. Such events can be caught by special functions called event handlers.
The emit builtin fires a generic fish event. Such events can be caught by special functions called event handlers. The arguments are passed to the event handlers as function arguments.
\subsection emit-example Example
@ -13,7 +13,8 @@ The following code first defines an event handler for the generic
event named 'test_event', and then emits an event of that type.
<pre>function event_test --on-event test_event
echo event test!!!
echo event test: $argv
end
emit test_event</pre>
emit test_event something
</pre>

View file

@ -689,26 +689,15 @@ void event_free(event_t *e)
delete e;
}
void event_fire_generic_internal(const wchar_t *name, ...)
void event_fire_generic(const wchar_t *name, wcstring_list_t *args)
{
va_list va;
wchar_t *arg;
CHECK(name,);
event_t ev(EVENT_GENERIC);
ev.str_param1 = name;
ev.arguments.reset(new wcstring_list_t);
va_start(va, name);
while ((arg=va_arg(va, wchar_t *))!= 0)
{
ev.arguments->push_back(arg);
}
va_end(va);
if (args)
ev.arguments.reset(new wcstring_list_t(*args));
event_fire(&ev);
ev.arguments.reset(NULL);
}
event_t event_t::signal_event(int sig)

View file

@ -173,8 +173,6 @@ wcstring event_get_desc(const event_t *e);
/**
Fire a generic event with the specified name
*/
#define event_fire_generic( ... ) event_fire_generic_internal( __VA_ARGS__, NULL )
void event_fire_generic_internal(const wchar_t *name,...);
void event_fire_generic(const wchar_t *name, wcstring_list_t *args = NULL);
#endif

View file

@ -2077,6 +2077,7 @@ int parser_t::parse_job(process_t *p,
int tmp;
const wchar_t *cmd = args.at(0).completion.c_str();
wcstring_list_t event_args;
/*
We couldn't find the specified command.
@ -2157,7 +2158,9 @@ int parser_t::parse_job(process_t *p,
current_tokenizer_pos=tmp;
job_set_flag(j, JOB_SKIP, 1);
event_fire_generic(L"fish_command_not_found", (wchar_t *)(args.at(0).completion.c_str()));
event_args.push_back(args.at(0).completion);
event_fire_generic(L"fish_command_not_found", &event_args);
proc_set_last_status(err==ENOENT?STATUS_UNKNOWN_COMMAND:STATUS_NOT_EXECUTABLE);
}
}

View file

@ -1,2 +1,2 @@
before:test1
received event test3 with args:
received event test3 with args: foo bar