mirror of
https://github.com/weechat/weechat.git
synced 2026-07-05 01:03:14 +02:00
core: add /theme rename to rename a user theme file
This commit is contained in:
@@ -7349,6 +7349,13 @@ COMMAND_CALLBACK(theme)
|
||||
? 1 : 0);
|
||||
}
|
||||
|
||||
/* "/theme rename <old> <new>": rename a user theme file */
|
||||
if (string_strcmp (argv[1], "rename") == 0)
|
||||
{
|
||||
COMMAND_MIN_ARGS(4, "rename");
|
||||
return theme_rename (argv[2], argv[3]);
|
||||
}
|
||||
|
||||
/* "/theme delete <name>": remove a user theme file */
|
||||
if (string_strcmp (argv[1], "delete") == 0)
|
||||
{
|
||||
@@ -10081,6 +10088,7 @@ command_init (void)
|
||||
" || apply <name>"
|
||||
" || reset"
|
||||
" || save <name> [-full]"
|
||||
" || rename <old> <new>"
|
||||
" || delete <name>"
|
||||
" || info <name>"),
|
||||
CMD_ARGS_DESC(
|
||||
@@ -10101,6 +10109,11 @@ command_init (void)
|
||||
"written, use \"-full\" to write every themable option; "
|
||||
"the name must not match a built-in theme or start with "
|
||||
"\"backup-\""),
|
||||
N_("raw[rename]: rename a user theme file (typically to "
|
||||
"give an automatic backup a meaningful name); refuses to "
|
||||
"rename built-in themes, refuses target names matching a "
|
||||
"built-in or starting with \"backup-\", and refuses if "
|
||||
"the target file already exists"),
|
||||
N_("raw[delete]: delete a user theme file (refuses to delete "
|
||||
"built-in themes, which have no file)"),
|
||||
N_("raw[info]: display details on a theme (name, description, "
|
||||
@@ -10123,6 +10136,7 @@ command_init (void)
|
||||
" || apply %(theme_themes_all)"
|
||||
" || reset"
|
||||
" || save %(theme_themes_user) -full"
|
||||
" || rename %(theme_themes_files)"
|
||||
" || delete %(theme_themes_user)"
|
||||
" || info %(theme_themes_all)",
|
||||
&command_theme, NULL, NULL);
|
||||
|
||||
@@ -2086,6 +2086,40 @@ completion_list_add_theme_themes_user_cb (const void *pointer, void *data,
|
||||
return WEECHAT_RC_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* Add every on-disk theme file (user files + backups, no built-ins)
|
||||
* to the completion list; suitable for /theme rename which can take a
|
||||
* backup as its source.
|
||||
*/
|
||||
|
||||
int
|
||||
completion_list_add_theme_themes_files_cb (const void *pointer, void *data,
|
||||
const char *completion_item,
|
||||
struct t_gui_buffer *buffer,
|
||||
struct t_gui_completion *completion)
|
||||
{
|
||||
struct t_completion_theme_dir ctx;
|
||||
char *dir;
|
||||
|
||||
/* make C compiler happy */
|
||||
(void) pointer;
|
||||
(void) data;
|
||||
(void) completion_item;
|
||||
(void) buffer;
|
||||
|
||||
dir = NULL;
|
||||
string_asprintf (&dir, "%s/themes", weechat_config_dir);
|
||||
if (dir)
|
||||
{
|
||||
ctx.completion = completion;
|
||||
ctx.show_backups = 1;
|
||||
dir_exec_on_files (dir, 0, 0, &completion_theme_add_file_cb, &ctx);
|
||||
free (dir);
|
||||
}
|
||||
|
||||
return WEECHAT_RC_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* Add a secured data to completion list.
|
||||
*/
|
||||
@@ -2483,6 +2517,10 @@ completion_init (void)
|
||||
hook_completion (NULL, "theme_themes_user",
|
||||
N_("names of user theme files (excludes built-ins and backups)"),
|
||||
&completion_list_add_theme_themes_user_cb, NULL, NULL);
|
||||
hook_completion (NULL, "theme_themes_files",
|
||||
N_("names of theme files on disk (user files + backups, "
|
||||
"no built-ins)"),
|
||||
&completion_list_add_theme_themes_files_cb, NULL, NULL);
|
||||
hook_completion (NULL, "secured_data",
|
||||
N_("names of secured data (file sec.conf, section data)"),
|
||||
&completion_list_add_secured_data_cb, NULL, NULL);
|
||||
|
||||
@@ -1175,6 +1175,160 @@ theme_save (const char *name, int full)
|
||||
return WEECHAT_RC_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* Rename a user theme file.
|
||||
*
|
||||
* Refuse to rename a built-in (no file) or to a name reserved for
|
||||
* built-ins or automatic backups. The target name must not already
|
||||
* exist on disk. The file content is copied with the [info] name
|
||||
* field rewritten so the parsed theme name stays consistent with the
|
||||
* new filename. If "weechat.look.theme" was pointing at the old name,
|
||||
* it is updated to the new name.
|
||||
*
|
||||
* Return WEECHAT_RC_OK on success, WEECHAT_RC_ERROR on validation or
|
||||
* I/O failure (in which case no file is created or removed).
|
||||
*/
|
||||
|
||||
int
|
||||
theme_rename (const char *old_name, const char *new_name)
|
||||
{
|
||||
char *old_path, *new_path, line[2048];
|
||||
FILE *fin, *fout;
|
||||
const char *trimmed;
|
||||
int in_info, name_done;
|
||||
|
||||
if (!old_name || !old_name[0] || !new_name || !new_name[0])
|
||||
return WEECHAT_RC_ERROR;
|
||||
|
||||
if (theme_search (old_name))
|
||||
{
|
||||
gui_chat_printf (NULL,
|
||||
_("%sCannot rename built-in theme \"%s\""),
|
||||
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
|
||||
old_name);
|
||||
return WEECHAT_RC_ERROR;
|
||||
}
|
||||
|
||||
if (strcmp (old_name, new_name) == 0)
|
||||
{
|
||||
gui_chat_printf (NULL,
|
||||
_("%sNew name is the same as old name"),
|
||||
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR]);
|
||||
return WEECHAT_RC_ERROR;
|
||||
}
|
||||
|
||||
if (strncmp (new_name, "backup-", 7) == 0)
|
||||
{
|
||||
gui_chat_printf (NULL,
|
||||
_("%sName \"%s\" is reserved for automatic backups"),
|
||||
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
|
||||
new_name);
|
||||
return WEECHAT_RC_ERROR;
|
||||
}
|
||||
|
||||
if (theme_search (new_name))
|
||||
{
|
||||
gui_chat_printf (NULL,
|
||||
_("%sName \"%s\" is reserved for a built-in theme"),
|
||||
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
|
||||
new_name);
|
||||
return WEECHAT_RC_ERROR;
|
||||
}
|
||||
|
||||
old_path = theme_user_file_path (old_name);
|
||||
if (!old_path)
|
||||
return WEECHAT_RC_ERROR;
|
||||
if (access (old_path, R_OK) != 0)
|
||||
{
|
||||
gui_chat_printf (NULL,
|
||||
_("%sTheme \"%s\" not found"),
|
||||
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
|
||||
old_name);
|
||||
free (old_path);
|
||||
return WEECHAT_RC_ERROR;
|
||||
}
|
||||
|
||||
new_path = theme_user_file_path (new_name);
|
||||
if (!new_path)
|
||||
{
|
||||
free (old_path);
|
||||
return WEECHAT_RC_ERROR;
|
||||
}
|
||||
if (access (new_path, F_OK) == 0)
|
||||
{
|
||||
gui_chat_printf (NULL,
|
||||
_("%sTheme \"%s\" already exists"),
|
||||
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
|
||||
new_name);
|
||||
free (old_path);
|
||||
free (new_path);
|
||||
return WEECHAT_RC_ERROR;
|
||||
}
|
||||
|
||||
fin = fopen (old_path, "r");
|
||||
fout = (fin) ? fopen (new_path, "w") : NULL;
|
||||
if (!fin || !fout)
|
||||
{
|
||||
if (fin)
|
||||
fclose (fin);
|
||||
gui_chat_printf (NULL,
|
||||
_("%sFailed to rename theme \"%s\" to \"%s\""),
|
||||
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
|
||||
old_name, new_name);
|
||||
free (old_path);
|
||||
free (new_path);
|
||||
return WEECHAT_RC_ERROR;
|
||||
}
|
||||
|
||||
in_info = 0;
|
||||
name_done = 0;
|
||||
while (fgets (line, sizeof (line), fin))
|
||||
{
|
||||
trimmed = line;
|
||||
while (*trimmed == ' ' || *trimmed == '\t')
|
||||
trimmed++;
|
||||
if (*trimmed == '[')
|
||||
{
|
||||
in_info = (strncmp (trimmed, "[info]", 6) == 0);
|
||||
fputs (line, fout);
|
||||
continue;
|
||||
}
|
||||
if (in_info && !name_done
|
||||
&& trimmed[0] == 'n' && trimmed[1] == 'a'
|
||||
&& trimmed[2] == 'm' && trimmed[3] == 'e'
|
||||
&& (trimmed[4] == ' ' || trimmed[4] == '\t' || trimmed[4] == '='))
|
||||
{
|
||||
fprintf (fout, "name = \"%s\"\n", new_name);
|
||||
name_done = 1;
|
||||
continue;
|
||||
}
|
||||
fputs (line, fout);
|
||||
}
|
||||
fclose (fin);
|
||||
if (fclose (fout) != 0 || unlink (old_path) != 0)
|
||||
{
|
||||
unlink (new_path);
|
||||
gui_chat_printf (NULL,
|
||||
_("%sFailed to rename theme \"%s\" to \"%s\""),
|
||||
gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
|
||||
old_name, new_name);
|
||||
free (old_path);
|
||||
free (new_path);
|
||||
return WEECHAT_RC_ERROR;
|
||||
}
|
||||
|
||||
if (strcmp (CONFIG_STRING(config_look_theme), old_name) == 0)
|
||||
config_file_option_set (config_look_theme, new_name, 1);
|
||||
|
||||
gui_chat_printf (NULL,
|
||||
_("Theme \"%s\" renamed to \"%s\""),
|
||||
old_name, new_name);
|
||||
|
||||
free (old_path);
|
||||
free (new_path);
|
||||
return WEECHAT_RC_OK;
|
||||
}
|
||||
|
||||
/*
|
||||
* Delete a user theme file.
|
||||
*
|
||||
|
||||
@@ -71,6 +71,7 @@ extern struct t_arraylist *theme_list (void);
|
||||
extern int theme_apply (const char *name);
|
||||
extern int theme_reset (void);
|
||||
extern int theme_save (const char *name, int full);
|
||||
extern int theme_rename (const char *old_name, const char *new_name);
|
||||
extern int theme_delete (const char *name);
|
||||
extern char *theme_make_backup (void);
|
||||
extern char *theme_user_file_path (const char *name);
|
||||
|
||||
Reference in New Issue
Block a user