diff options
author | Sebastien Helleu <flashcode@flashtux.org> | 2007-12-07 15:01:37 +0100 |
---|---|---|
committer | Sebastien Helleu <flashcode@flashtux.org> | 2007-12-07 15:01:37 +0100 |
commit | 72a694ed4c78df15b68ef4698b16f7072919f3ee (patch) | |
tree | 499e976612d6fe1d92675149707350c38b74758b /src/core | |
parent | 495e6bd5df9163148676821d610c9ef863326f70 (diff) | |
download | weechat-72a694ed4c78df15b68ef4698b16f7072919f3ee.zip |
Added completion hook, to let plugins add custom completions for commands
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/wee-command.c | 20 | ||||
-rw-r--r-- | src/core/wee-hook.c | 86 | ||||
-rw-r--r-- | src/core/wee-hook.h | 13 |
3 files changed, 118 insertions, 1 deletions
diff --git a/src/core/wee-command.c b/src/core/wee-command.c index 487527656..72405bc9a 100644 --- a/src/core/wee-command.c +++ b/src/core/wee-command.c @@ -1020,7 +1020,7 @@ command_plugin_list (char *name, int full) { if (!hook_found) gui_chat_printf (NULL, - _(" configuration otions " + _(" configuration options " "hooked:")); hook_found = 1; gui_chat_printf (NULL, @@ -1031,6 +1031,24 @@ command_plugin_list (char *name, int full) HOOK_CONFIG(ptr_hook, option) : "*"); } } + + /* completion hooked */ + hook_found = 0; + for (ptr_hook = weechat_hooks; ptr_hook; + ptr_hook = ptr_hook->next_hook) + { + if ((ptr_hook->plugin == ptr_plugin) + && (ptr_hook->type == HOOK_TYPE_COMPLETION)) + { + if (!hook_found) + gui_chat_printf (NULL, + _(" completion hooked:")); + hook_found = 1; + gui_chat_printf (NULL, + " %s", + HOOK_COMPLETION(ptr_hook, completion)); + } + } } } } diff --git a/src/core/wee-hook.c b/src/core/wee-hook.c index a1db2b586..ebe656c39 100644 --- a/src/core/wee-hook.c +++ b/src/core/wee-hook.c @@ -827,6 +827,80 @@ hook_config_exec (char *type, char *option, char *value) } /* + * hook_completion: hook a completion + */ + +struct t_hook * +hook_completion (void *plugin, char *completion, + t_hook_callback_completion *callback, void *callback_data) +{ + struct t_hook *new_hook; + struct t_hook_completion *new_hook_completion; + + if (!completion || !completion[0] || strchr (completion, ' ')) + return NULL; + + new_hook = (struct t_hook *)malloc (sizeof (struct t_hook)); + if (!new_hook) + return NULL; + new_hook_completion = (struct t_hook_completion *)malloc (sizeof (struct t_hook_completion)); + if (!new_hook_completion) + { + free (new_hook); + return NULL; + } + + hook_init (new_hook, plugin, HOOK_TYPE_COMPLETION, callback_data); + + new_hook->hook_data = new_hook_completion; + new_hook_completion->callback = callback; + new_hook_completion->completion = strdup (completion); + + hook_add_to_list (new_hook); + + return new_hook; +} + +/* + * hook_completion_exec: execute completion hook + */ + +void +hook_completion_exec (void *plugin, char *completion, void *list) +{ + struct t_hook *ptr_hook, *next_hook; + + hook_exec_recursion++; + + ptr_hook = weechat_hooks; + while (ptr_hook) + { + next_hook = ptr_hook->next_hook; + + if ((ptr_hook->type == HOOK_TYPE_COMPLETION) + && (!ptr_hook->running) + && (ptr_hook->plugin == plugin) + && (string_strcasecmp (HOOK_COMPLETION(ptr_hook, completion), + completion) == 0)) + { + ptr_hook->running = 1; + (void) (HOOK_COMPLETION(ptr_hook, callback)) + (ptr_hook->callback_data, completion, list); + if (ptr_hook->type == HOOK_TYPE_COMPLETION) + ptr_hook->running = 0; + } + + ptr_hook = next_hook; + } + + if (hook_exec_recursion > 0) + hook_exec_recursion--; + + if (hook_exec_recursion == 0) + hook_remove_deleted (); +} + +/* * unhook: unhook something */ @@ -894,6 +968,11 @@ unhook (struct t_hook *hook) free (HOOK_CONFIG(hook, option)); free ((struct t_hook_config *)hook->hook_data); break; + case HOOK_TYPE_COMPLETION: + if (HOOK_COMPLETION(hook, completion)) + free (HOOK_COMPLETION(hook, completion)); + free ((struct t_hook_completion *)hook->hook_data); + break; } hook->hook_data = NULL; } @@ -1021,6 +1100,13 @@ hook_print_log () log_printf (" type . . . . . . . . : '%s'", HOOK_CONFIG(ptr_hook, type)); log_printf (" option . . . . . . . : '%s'", HOOK_CONFIG(ptr_hook, option)); break; + case HOOK_TYPE_COMPLETION: + log_printf (" type . . . . . . . . . : %d (completion)", ptr_hook->type); + log_printf (" callback_data. . . . . : 0x%X", ptr_hook->callback_data); + log_printf (" completion data:"); + log_printf (" callback . . . . . . : 0x%X", HOOK_COMPLETION(ptr_hook, callback)); + log_printf (" completion . . . . . : '%s'", HOOK_COMPLETION(ptr_hook, completion)); + break; } log_printf (" running. . . . . . . . : %d", ptr_hook->running); log_printf (" prev_hook. . . . . . . : 0x%X", ptr_hook->prev_hook); diff --git a/src/core/wee-hook.h b/src/core/wee-hook.h index 7bf23300b..ceb49998a 100644 --- a/src/core/wee-hook.h +++ b/src/core/wee-hook.h @@ -31,6 +31,7 @@ enum t_hook_type HOOK_TYPE_PRINT, /* printed message */ HOOK_TYPE_EVENT, /* event */ HOOK_TYPE_CONFIG, /* config option */ + HOOK_TYPE_COMPLETION, /* custom completions */ }; #define HOOK_FD_FLAG_READ 1 @@ -43,6 +44,7 @@ enum t_hook_type #define HOOK_PRINT(hook, var) (((struct t_hook_print *)hook->hook_data)->var) #define HOOK_EVENT(hook, var) (((struct t_hook_event *)hook->hook_data)->var) #define HOOK_CONFIG(hook, var) (((struct t_hook_config *)hook->hook_data)->var) +#define HOOK_COMPLETION(hook, var) (((struct t_hook_completion *)hook->hook_data)->var) struct t_hook { @@ -121,6 +123,14 @@ struct t_hook_config /* (NULL = hook for all options) */ }; +typedef int (t_hook_callback_completion)(void *, char *, void *); + +struct t_hook_completion +{ + t_hook_callback_completion *callback; /* completion callback */ + char *completion; /* name of completion */ +}; + /* hook variables */ extern struct t_hook *weechat_hooks; @@ -149,6 +159,9 @@ extern void hook_event_exec (char *, void *); extern struct t_hook *hook_config (void *, char *, char *, t_hook_callback_config *, void *); extern void hook_config_exec (char *, char *, char *); +extern struct t_hook *hook_completion (void *, char *, + t_hook_callback_completion *, void *); +extern void hook_completion_exec (void *, char *, void *); extern void unhook (struct t_hook *); extern void unhook_all_plugin (void *); |