1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-30 14:56:39 +02:00

trigger: split trigger command on creation, not when executing the callback

This commit is contained in:
Sebastien Helleu
2014-02-06 19:26:39 +01:00
parent 4a9c9a0b07
commit 717d89a1f7
4 changed files with 85 additions and 20 deletions
+14 -19
View File
@@ -139,32 +139,27 @@ trigger_callback_run_command (struct t_trigger *trigger,
struct t_hashtable *pointers,
struct t_hashtable *extra_vars)
{
const char *command;
char *command_eval, **commands, **ptr_command;
char *command_eval;
int i;
command = weechat_config_string (trigger->options[TRIGGER_OPTION_COMMAND]);
if (!command || !command[0])
if (!trigger->commands)
return;
command_eval = weechat_string_eval_expression (command, pointers,
extra_vars, NULL);
if (command_eval)
for (i = 0; trigger->commands[i]; i++)
{
commands = weechat_string_split_command (command_eval, ';');
if (commands)
command_eval = weechat_string_eval_expression (trigger->commands[i],
pointers, extra_vars,
NULL);
if (command_eval)
{
for (ptr_command = commands; *ptr_command; ptr_command++)
/* display debug info on trigger buffer */
if (trigger_buffer)
{
/* display debug info on trigger buffer */
if (trigger_buffer)
{
weechat_printf_tags (trigger_buffer, "no_trigger",
"\t running command \"%s\"",
*ptr_command);
}
weechat_command (buffer, *ptr_command);
weechat_printf_tags (trigger_buffer, "no_trigger",
"\t running command \"%s\"",
trigger->commands[i]);
}
weechat_string_free_split_command (commands);
weechat_command (buffer, trigger->commands[i]);
trigger->hook_count_cmd++;
}
free (command_eval);
+20 -1
View File
@@ -124,6 +124,25 @@ trigger_config_change_regex (void *data, struct t_config_option *option)
trigger_set_regex (ptr_trigger);
}
/*
* Callback called when trigger option "command" is changed.
*/
void
trigger_config_change_command (void *data, struct t_config_option *option)
{
struct t_trigger *ptr_trigger;
/* make C compiler happy */
(void) data;
ptr_trigger = trigger_search_with_option (option);
if (!ptr_trigger)
return;
trigger_set_commands (ptr_trigger);
}
/*
* Creates an option for a trigger.
*
@@ -212,7 +231,7 @@ trigger_config_create_option (const char *trigger_name, int index_option,
option_name, "string",
N_("command run if conditions are OK, after regex replacements"),
NULL, 0, 0, value, NULL, 0,
NULL, NULL, NULL, NULL, NULL, NULL);
NULL, NULL, &trigger_config_change_command, NULL, NULL, NULL);
break;
case TRIGGER_OPTION_RETURN_CODE:
ptr_option = weechat_config_new_option (
+46
View File
@@ -585,6 +585,37 @@ end:
free (delimiter);
}
/*
* Sets the commands in a trigger.
*/
void
trigger_set_commands (struct t_trigger *trigger)
{
const char *option_command;
int i;
if (trigger->commands)
{
weechat_string_free_split (trigger->commands);
trigger->commands = NULL;
}
trigger->commands_count = 0;
option_command = weechat_config_string (trigger->options[TRIGGER_OPTION_COMMAND]);
if (option_command && option_command[0])
{
trigger->commands = weechat_string_split_command (option_command, ';');
if (trigger->commands)
{
for (i = 0; trigger->commands[i]; i++)
{
}
trigger->commands_count = i;
}
}
}
/*
* Checks if a trigger name is valid: it must not start with "-" and not have
* any spaces.
@@ -643,6 +674,8 @@ trigger_alloc (const char *name)
new_trigger->hook_print_buffers = NULL;
new_trigger->regex_count = 0;
new_trigger->regex = NULL;
new_trigger->commands_count = 0;
new_trigger->commands = NULL;
new_trigger->prev_trigger = NULL;
new_trigger->next_trigger = NULL;
@@ -729,6 +762,7 @@ trigger_new_with_options (const char *name, struct t_config_option **options)
triggers_count++;
trigger_set_regex (new_trigger);
trigger_set_commands (new_trigger);
if (weechat_config_boolean (new_trigger->options[TRIGGER_OPTION_ENABLED]))
trigger_hook (new_trigger);
@@ -877,6 +911,8 @@ trigger_free (struct t_trigger *trigger)
if (trigger->options[i])
weechat_config_option_free (trigger->options[i]);
}
if (trigger->commands)
weechat_string_free_split (trigger->commands);
free (trigger);
@@ -954,6 +990,16 @@ trigger_print_log ()
weechat_log_printf (" regex[%03d].replace_eval : '%s'",
i, ptr_trigger->regex[i].replace_eval);
}
weechat_log_printf (" commands_count. . . . . : %d", ptr_trigger->commands_count);
weechat_log_printf (" commands. . . . . . . . : 0x%lx", ptr_trigger->commands);
if (ptr_trigger->commands)
{
for (i = 0; ptr_trigger->commands[i]; i++)
{
weechat_log_printf (" commands[%03d] . . . . : '%s'",
i, ptr_trigger->commands[i]);
}
}
weechat_log_printf (" prev_trigger. . . . . . : 0x%lx", ptr_trigger->prev_trigger);
weechat_log_printf (" next_trigger. . . . . . : 0x%lx", ptr_trigger->next_trigger);
}
+5
View File
@@ -88,6 +88,10 @@ struct t_trigger
int regex_count; /* number of regex */
struct t_trigger_regex *regex; /* array of regex */
/* commands */
int commands_count; /* number of commands */
char **commands; /* commands */
/* links to other triggers */
struct t_trigger *prev_trigger; /* link to previous trigger */
struct t_trigger *next_trigger; /* link to next trigger */
@@ -117,6 +121,7 @@ extern int trigger_search_return_code (const char *return_code);
extern struct t_trigger *trigger_search (const char *name);
extern struct t_trigger *trigger_search_with_option (struct t_config_option *option);
extern void trigger_set_regex (struct t_trigger *trigger);
extern void trigger_set_commands (struct t_trigger *trigger);
extern void trigger_unhook (struct t_trigger *trigger);
extern void trigger_hook (struct t_trigger *trigger);
extern int trigger_name_valid (const char *name);