summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2023-01-28 14:57:55 +0100
committerSébastien Helleu <flashcode@flashtux.org>2023-01-28 15:14:31 +0100
commit3318a7f3b8022925645e7d740e8cf58cd85f07a1 (patch)
tree94ff792d187215cfa2ce2f601e22cb93bb44167b /src
parent11ea0aecf40ae758cea504856f0b7f34c918239d (diff)
downloadweechat-3318a7f3b8022925645e7d740e8cf58cd85f07a1.zip
alias: add option `rename` in command `/alias` (issue #1872)
Diffstat (limited to 'src')
-rw-r--r--src/plugins/alias/alias-command.c55
-rw-r--r--src/plugins/alias/alias.c272
-rw-r--r--src/plugins/alias/alias.h1
3 files changed, 219 insertions, 109 deletions
diff --git a/src/plugins/alias/alias-command.c b/src/plugins/alias/alias-command.c
index e8ac4783e..78d6b159c 100644
--- a/src/plugins/alias/alias-command.c
+++ b/src/plugins/alias/alias-command.c
@@ -80,8 +80,8 @@ alias_command_cb (const void *pointer, void *data,
struct t_gui_buffer *buffer, int argc,
char **argv, char **argv_eol)
{
- char *ptr_alias_name;
- struct t_alias *ptr_alias;
+ char *ptr_alias_name, *ptr_alias_name2;
+ struct t_alias *ptr_alias, *ptr_alias2;
struct t_config_option *ptr_option;
int alias_found, i;
@@ -235,6 +235,48 @@ alias_command_cb (const void *pointer, void *data,
return WEECHAT_RC_OK;
}
+ if (weechat_strcmp (argv[1], "rename") == 0)
+ {
+ WEECHAT_COMMAND_MIN_ARGS(4, "rename");
+
+ ptr_alias_name = (weechat_string_is_command_char (argv[2])) ?
+ (char *)weechat_utf8_next_char (argv[2]) : argv[2];
+ ptr_alias_name2 = (weechat_string_is_command_char (argv[3])) ?
+ (char *)weechat_utf8_next_char (argv[3]) : argv[3];
+
+ ptr_alias = alias_search (ptr_alias_name);
+ if (!ptr_alias)
+ {
+ weechat_printf (NULL,
+ _("%sAlias \"%s\" not found"),
+ weechat_prefix ("error"),
+ ptr_alias_name);
+ }
+ else
+ {
+ /* check if target name already exists */
+ ptr_alias2 = alias_search (ptr_alias_name2);
+ if (ptr_alias2)
+ {
+ weechat_printf (NULL, _("%sAlias \"%s\" already exists"),
+ weechat_prefix ("error"), ptr_alias_name2);
+ }
+ else
+ {
+ /* rename alias */
+ if (alias_rename (ptr_alias, ptr_alias_name2))
+ {
+ weechat_printf (
+ NULL,
+ _("Alias \"%s\" has been renamed to \"%s\""),
+ ptr_alias_name,
+ ptr_alias_name2);
+ }
+ }
+ }
+ return WEECHAT_RC_OK;
+ }
+
WEECHAT_COMMAND_ERROR;
}
@@ -251,13 +293,15 @@ alias_command_init ()
N_("list [<alias>]"
" || add <alias> [<command>[;<command>...]]"
" || addcompletion <completion> <alias> [<command>[;<command>...]]"
- " || del <alias> [<alias>...]"),
+ " || del <alias> [<alias>...]"
+ " || rename <alias> <new_alias>"),
/* xgettext:no-c-format */
N_(" list: list aliases (without argument, this list is "
"displayed)\n"
" add: add an alias\n"
"addcompletion: add an alias with a custom completion\n"
" del: delete an alias\n"
+ " rename: rename an alias\n"
" completion: completion for alias: by default completion is "
"done with target command\n"
" note: you can use %%command to use completion of "
@@ -283,12 +327,15 @@ alias_command_init ()
" alias /hello to say \"hello\" on all channels but not on "
"#weechat:\n"
" /alias add hello /allchan -exclude=#weechat hello\n"
+ " rename alias \"hello\" to \"Hello\":\n"
+ " /alias rename hello Hello\n"
" alias /forcejoin to send IRC command \"forcejoin\" with "
"completion of /sajoin:\n"
" /alias addcompletion %%sajoin forcejoin /quote forcejoin"),
"list %(alias)"
" || add %(alias) %(commands:/)|%(alias_value)"
" || addcompletion %- %(alias) %(commands:/)|%(alias_value)"
- " || del %(alias)|%*",
+ " || del %(alias)|%*"
+ " || rename %(alias) %(alias)",
&alias_command_cb, NULL, NULL);
}
diff --git a/src/plugins/alias/alias.c b/src/plugins/alias/alias.c
index 5933fe735..d4678b10b 100644
--- a/src/plugins/alias/alias.c
+++ b/src/plugins/alias/alias.c
@@ -445,77 +445,6 @@ alias_cb (const void *pointer, void *data,
}
/*
- * Frees an alias and remove it from list.
- */
-
-void
-alias_free (struct t_alias *alias)
-{
- struct t_alias *new_alias_list;
-
- if (!alias)
- return;
-
- /* remove alias from list */
- if (last_alias == alias)
- last_alias = alias->prev_alias;
- if (alias->prev_alias)
- {
- (alias->prev_alias)->next_alias = alias->next_alias;
- new_alias_list = alias_list;
- }
- else
- new_alias_list = alias->next_alias;
- if (alias->next_alias)
- (alias->next_alias)->prev_alias = alias->prev_alias;
-
- /* free data */
- if (alias->hook)
- weechat_unhook (alias->hook);
- if (alias->name)
- free (alias->name);
- if (alias->command)
- free (alias->command);
- if (alias->completion)
- free (alias->completion);
- free (alias);
-
- alias_list = new_alias_list;
-}
-
-/*
- * Frees all aliases.
- */
-
-void
-alias_free_all ()
-{
- while (alias_list)
- {
- alias_free (alias_list);
- }
-}
-
-/*
- * Searches for position of alias (to keep aliases sorted by name).
- */
-
-struct t_alias *
-alias_find_pos (const char *name)
-{
- struct t_alias *ptr_alias;
-
- for (ptr_alias = alias_list; ptr_alias; ptr_alias = ptr_alias->next_alias)
- {
- if (weechat_strcmp (name, ptr_alias->name) < 0)
- return ptr_alias;
- }
-
- /* position not found (we will add to the end of list) */
- return NULL;
-}
-
-/*
* Hooks command for an alias.
*/
@@ -525,6 +454,12 @@ alias_hook_command (struct t_alias *alias)
char *str_priority_name, *str_completion;
int length;
+ if (alias->hook)
+ {
+ weechat_unhook (alias->hook);
+ alias->hook = NULL;
+ }
+
/*
* build string with priority and name: the alias priority is 2000, which
* is higher than default one (1000), so the alias is executed before a
@@ -566,6 +501,164 @@ alias_hook_command (struct t_alias *alias)
}
/*
+ * Searches for position of alias (to keep aliases sorted by name).
+ */
+
+struct t_alias *
+alias_find_pos (const char *name)
+{
+ struct t_alias *ptr_alias;
+
+ for (ptr_alias = alias_list; ptr_alias; ptr_alias = ptr_alias->next_alias)
+ {
+ if (weechat_strcmp (name, ptr_alias->name) < 0)
+ return ptr_alias;
+ }
+
+ /* position not found (we will add to the end of list) */
+ return NULL;
+}
+
+/*
+ * Inserts alias in list of aliases.
+ */
+
+void
+alias_insert (struct t_alias *alias)
+{
+ struct t_alias *pos_alias;
+
+ if (alias_list)
+ {
+ pos_alias = alias_find_pos (alias->name);
+ if (pos_alias)
+ {
+ /* insert alias into the list (before alias found) */
+ alias->prev_alias = pos_alias->prev_alias;
+ alias->next_alias = pos_alias;
+ if (pos_alias->prev_alias)
+ (pos_alias->prev_alias)->next_alias = alias;
+ else
+ alias_list = alias;
+ pos_alias->prev_alias = alias;
+ }
+ else
+ {
+ /* add alias to end of list */
+ alias->prev_alias = last_alias;
+ alias->next_alias = NULL;
+ last_alias->next_alias = alias;
+ last_alias = alias;
+ }
+ }
+ else
+ {
+ alias->prev_alias = NULL;
+ alias->next_alias = NULL;
+ alias_list = alias;
+ last_alias = alias;
+ }
+}
+
+/*
+ * Removes alias from list of aliases.
+ */
+
+void
+alias_remove_from_list (struct t_alias *alias)
+{
+ if (last_alias == alias)
+ last_alias = alias->prev_alias;
+ if (alias->prev_alias)
+ (alias->prev_alias)->next_alias = alias->next_alias;
+ else
+ alias_list = alias->next_alias;
+ if (alias->next_alias)
+ (alias->next_alias)->prev_alias = alias->prev_alias;
+}
+
+/*
+ * Renames an alias.
+ *
+ * Returns:
+ * 1: OK
+ * 0: error
+ */
+
+int
+alias_rename (struct t_alias *alias, const char *new_name)
+{
+ struct t_config_option *ptr_option;
+
+ if (!alias || !new_name || !new_name[0] || alias_search (new_name))
+ return 0;
+
+ /* rename options */
+ ptr_option = weechat_config_search_option (
+ alias_config_file,
+ alias_config_section_cmd,
+ alias->name);
+ if (ptr_option)
+ weechat_config_option_rename (ptr_option, new_name);
+ ptr_option = weechat_config_search_option (
+ alias_config_file,
+ alias_config_section_completion,
+ alias->name);
+ if (ptr_option)
+ weechat_config_option_rename (ptr_option, new_name);
+
+ /* rename alias */
+ free (alias->name);
+ alias->name = strdup (new_name);
+
+ /* hook command again */
+ alias_hook_command (alias);
+
+ /* move alias in list (to keep list sorted) */
+ alias_remove_from_list (alias);
+ alias_insert (alias);
+
+ return 1;
+}
+
+/*
+ * Frees an alias and remove it from list.
+ */
+
+void
+alias_free (struct t_alias *alias)
+{
+ if (!alias)
+ return;
+
+ alias_remove_from_list (alias);
+
+ /* free data */
+ if (alias->hook)
+ weechat_unhook (alias->hook);
+ if (alias->name)
+ free (alias->name);
+ if (alias->command)
+ free (alias->command);
+ if (alias->completion)
+ free (alias->completion);
+ free (alias);
+}
+
+/*
+ * Frees all aliases.
+ */
+
+void
+alias_free_all ()
+{
+ while (alias_list)
+ {
+ alias_free (alias_list);
+ }
+}
+
+/*
* Updates completion for an alias.
*/
@@ -577,9 +670,7 @@ alias_update_completion (struct t_alias *alias, const char *completion)
free (alias->completion);
alias->completion = (completion) ? strdup (completion) : NULL;
- /* unhook and hook again command, with new completion */
- weechat_unhook (alias->hook);
- alias->hook = NULL;
+ /* hook command again, with new completion */
alias_hook_command (alias);
}
@@ -619,7 +710,7 @@ alias_name_valid (const char *name)
struct t_alias *
alias_new (const char *name, const char *command, const char *completion)
{
- struct t_alias *new_alias, *ptr_alias, *pos_alias;
+ struct t_alias *new_alias, *ptr_alias;
if (!alias_name_valid (name))
{
@@ -653,36 +744,7 @@ alias_new (const char *name, const char *command, const char *completion)
alias_hook_command (new_alias);
- if (alias_list)
- {
- pos_alias = alias_find_pos (name);
- if (pos_alias)
- {
- /* insert alias into the list (before alias found) */
- new_alias->prev_alias = pos_alias->prev_alias;
- new_alias->next_alias = pos_alias;
- if (pos_alias->prev_alias)
- (pos_alias->prev_alias)->next_alias = new_alias;
- else
- alias_list = new_alias;
- pos_alias->prev_alias = new_alias;
- }
- else
- {
- /* add alias to end of list */
- new_alias->prev_alias = last_alias;
- new_alias->next_alias = NULL;
- last_alias->next_alias = new_alias;
- last_alias = new_alias;
- }
- }
- else
- {
- new_alias->prev_alias = NULL;
- new_alias->next_alias = NULL;
- alias_list = new_alias;
- last_alias = new_alias;
- }
+ alias_insert (new_alias);
}
return new_alias;
diff --git a/src/plugins/alias/alias.h b/src/plugins/alias/alias.h
index 8c80b3586..d81d60200 100644
--- a/src/plugins/alias/alias.h
+++ b/src/plugins/alias/alias.h
@@ -46,6 +46,7 @@ extern void alias_update_completion (struct t_alias *alias,
const char *completion);
extern struct t_alias *alias_new (const char *name, const char *command,
const char *completion);
+extern int alias_rename (struct t_alias *alias, const char *new_name);
extern void alias_free (struct t_alias *alias);
extern void alias_free_all ();
extern int alias_add_to_infolist (struct t_infolist *infolist,