1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-07 10:13:12 +02:00

Added completion hook, to let plugins add custom completions for commands

This commit is contained in:
Sebastien Helleu
2007-12-07 15:01:37 +01:00
parent 495e6bd5df
commit 72a694ed4c
9 changed files with 230 additions and 23 deletions
+19 -1
View File
@@ -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));
}
}
}
}
}
+86
View File
@@ -826,6 +826,80 @@ hook_config_exec (char *type, char *option, char *value)
hook_remove_deleted ();
}
/*
* 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);
+13
View File
@@ -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 *);