From 1d3a75333eb1b22fef74b8e0e32cdc9a85e97b00 Mon Sep 17 00:00:00 2001 From: Simmo Saan Date: Sat, 22 Aug 2015 12:22:51 +0300 Subject: [PATCH] trigger: add support for one-time triggers (closes #399) --- doc/en/weechat_user.en.asciidoc | 8 ++++ src/plugins/trigger/trigger-callback.h | 12 ++++++ src/plugins/trigger/trigger-command.c | 48 ++++++++++++++++++++---- src/plugins/trigger/trigger-completion.c | 29 ++++++++++++++ src/plugins/trigger/trigger-config.c | 23 +++++++++++- src/plugins/trigger/trigger-config.h | 1 + src/plugins/trigger/trigger.c | 41 +++++++++++++++++--- src/plugins/trigger/trigger.h | 15 +++++++- 8 files changed, 162 insertions(+), 15 deletions(-) diff --git a/doc/en/weechat_user.en.asciidoc b/doc/en/weechat_user.en.asciidoc index 97faa12bf..1461c2e09 100644 --- a/doc/en/weechat_user.en.asciidoc +++ b/doc/en/weechat_user.en.asciidoc @@ -3006,6 +3006,11 @@ A trigger has the following options (names are | return_code | `ok`, `ok_eat`, `error` | The return code of callback (default is `ok`, which should be used in almost all triggers, the other values are rarely used). + +| once_action | `none`, `disable`, `delete` | + Action to take with the trigger after the trigger executes once (default is + `none` which should be used in almost all triggers, the other values are + rarely used). |=== For example, the default 'beep' trigger has following options: @@ -3018,6 +3023,7 @@ trigger.trigger.beep.conditions = "${tg_highlight} || ${tg_msg_pv}" trigger.trigger.beep.regex = "" trigger.trigger.beep.command = "/print -beep" trigger.trigger.beep.return_code = ok +trigger.trigger.beep.once_action = none ---- [[trigger_execution]] @@ -3030,6 +3036,8 @@ order, if triggers are globally enabled and if the trigger itself is enabled: . replace text in trigger using regular expression(s) . execute command(s) . exit with a return code (except for hooks 'modifier' and 'focus'). +. perform once action (when not `none`) + [[trigger_hook_arguments]] ==== Hook arguments diff --git a/src/plugins/trigger/trigger-callback.h b/src/plugins/trigger/trigger-callback.h index e0b8b0663..55449f447 100644 --- a/src/plugins/trigger/trigger-callback.h +++ b/src/plugins/trigger/trigger-callback.h @@ -64,6 +64,18 @@ if (extra_vars) \ weechat_hashtable_free (extra_vars); \ trigger->hook_running = 0; \ + switch (weechat_config_integer ( \ + trigger->options[TRIGGER_OPTION_ONCE_ACTION])) \ + { \ + case TRIGGER_ONCE_DISABLE: \ + weechat_config_option_set ( \ + trigger->options[TRIGGER_OPTION_ENABLED], \ + "off", 1); \ + break; \ + case TRIGGER_ONCE_DELETE: \ + trigger_free (trigger); \ + break; \ + } \ return __rc; extern int trigger_callback_signal_cb (void *data, const char *signal, diff --git a/src/plugins/trigger/trigger-command.c b/src/plugins/trigger/trigger-command.c index 9faa77507..91a14db47 100644 --- a/src/plugins/trigger/trigger-command.c +++ b/src/plugins/trigger/trigger-command.c @@ -59,9 +59,10 @@ trigger_command_display_trigger_internal (const char *name, int commands_count, char **commands, int return_code, + int once_action, int verbose) { - char str_conditions[64], str_regex[64], str_command[64], str_rc[64]; + char str_conditions[64], str_regex[64], str_command[64], str_rc[64], str_once[64]; char spaces[256]; int i, length; @@ -153,6 +154,15 @@ trigger_command_display_trigger_internal (const char *name, weechat_color ("reset"), trigger_return_code_string[return_code]); } + if ((once_action >= 0) && (once_action != TRIGGER_ONCE_NONE)) + { + weechat_printf_tags (NULL, "no_trigger", + "%s %s=1 %s%s", + spaces, + weechat_color (weechat_config_string (trigger_config_color_flag_once_action)), + weechat_color ("reset"), + trigger_once_action_string[once_action]); + } } else { @@ -160,6 +170,7 @@ trigger_command_display_trigger_internal (const char *name, str_regex[0] = '\0'; str_command[0] = '\0'; str_rc[0] = '\0'; + str_once[0] = '\0'; if (conditions && conditions[0]) { snprintf (str_conditions, sizeof (str_conditions), @@ -190,9 +201,16 @@ trigger_command_display_trigger_internal (const char *name, weechat_color (weechat_config_string (trigger_config_color_flag_return_code)), weechat_color ("reset")); } + if ((once_action >= 0) && (once_action != TRIGGER_ONCE_NONE)) + { + snprintf (str_once, sizeof (str_once), + " %s=1%s", + weechat_color (weechat_config_string (trigger_config_color_flag_once_action)), + weechat_color ("reset")); + } weechat_printf_tags ( NULL, "no_trigger", - " %s%s%s: %s%s%s%s%s%s%s%s%s%s%s%s", + " %s%s%s: %s%s%s%s%s%s%s%s%s%s%s%s%s", (enabled) ? weechat_color (weechat_config_string (trigger_config_color_trigger)) : weechat_color (weechat_config_string (trigger_config_color_trigger_disabled)), @@ -209,7 +227,8 @@ trigger_command_display_trigger_internal (const char *name, str_conditions, str_regex, str_command, - str_rc); + str_rc, + str_once); } } @@ -234,6 +253,7 @@ trigger_command_display_trigger (struct t_trigger *trigger, int verbose) trigger->commands_count, trigger->commands, weechat_config_integer (trigger->options[TRIGGER_OPTION_RETURN_CODE]), + weechat_config_integer (trigger->options[TRIGGER_OPTION_ONCE_ACTION]), verbose); } @@ -306,6 +326,7 @@ trigger_command_list_default (int verbose) commands_count, commands, trigger_search_return_code (trigger_config_default_list[i][7]), + trigger_search_once_action (trigger_config_default_list[i][8]), verbose); } @@ -553,6 +574,15 @@ trigger_command_trigger (void *data, struct t_gui_buffer *buffer, int argc, sargv[6]); goto end; } + if ((sargc > 7) && sargv[7][0] + && (trigger_search_once_action (sargv[7]) < 0)) + { + weechat_printf_tags (NULL, "no_trigger", + _("%s%s: invalid once action \"%s\""), + weechat_prefix ("error"), TRIGGER_PLUGIN_NAME, + sargv[7]); + goto end; + } ptr_trigger = trigger_search (sargv[0]); if (ptr_trigger) { @@ -596,7 +626,8 @@ trigger_command_trigger (void *data, struct t_gui_buffer *buffer, int argc, (sargc > 3) ? sargv[3] : "", /* conditions */ (sargc > 4) ? sargv[4] : "", /* regex */ (sargc > 5) ? sargv[5] : "", /* command */ - (sargc > 6) ? sargv[6] : ""); /* return code */ + (sargc > 6) ? sargv[6] : "", /* return code */ + (sargc > 7) ? sargv[7] : ""); /* once action */ if (ptr_trigger) { weechat_printf_tags (NULL, "no_trigger", @@ -983,7 +1014,8 @@ trigger_command_trigger (void *data, struct t_gui_buffer *buffer, int argc, trigger_config_default_list[j][4], /* conditions */ trigger_config_default_list[j][5], /* regex */ trigger_config_default_list[j][6], /* command */ - trigger_config_default_list[j][7]); /* return code */ + trigger_config_default_list[j][7], /* return code */ + trigger_config_default_list[j][8]); /* once action */ weechat_printf_tags (NULL, "no_trigger", _("Trigger \"%s\" restored"), argv[i]); @@ -1067,7 +1099,7 @@ trigger_command_init () N_("list|listfull|listdefault" " || add|addoff|addreplace [\"\" " "[\"\" [\"\" [\"\" " - "[\"\"]]]]]" + "[\"\" [\"\"]]]]]]" " || addinput []" " || input|output|recreate " " || set