summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2023-08-24 10:38:18 +0200
committerSébastien Helleu <flashcode@flashtux.org>2023-08-24 11:49:29 +0200
commit303fe6744e13e148fe3876826139ca30e5a8353c (patch)
tree7df69f911bac5c938d0be2cbe522aa4dc391a187 /src/core
parent3aef8b7292330da6d4a7dd97ddb261baca3f33e3 (diff)
downloadweechat-303fe6744e13e148fe3876826139ca30e5a8353c.zip
core: add option `setauto` in command `/buffer` (issue #352)
Diffstat (limited to 'src/core')
-rw-r--r--src/core/wee-command.c42
-rw-r--r--src/core/wee-completion.c59
-rw-r--r--src/core/wee-config.c174
-rw-r--r--src/core/wee-config.h2
4 files changed, 238 insertions, 39 deletions
diff --git a/src/core/wee-command.c b/src/core/wee-command.c
index b9e802233..54209d505 100644
--- a/src/core/wee-command.c
+++ b/src/core/wee-command.c
@@ -642,7 +642,7 @@ COMMAND_CALLBACK(buffer)
long number, number1, number2, numbers[3];
char *error, *value, *pos, *str_number1, *pos_number2;
int i, count, prev_number, clear_number, list_size;
- int buffer_found, arg_name, type_free, switch_to_buffer;
+ int buffer_found, arg_name, type_free, switch_to_buffer, rc;
/* make C compiler happy */
(void) pointer;
@@ -1328,6 +1328,41 @@ COMMAND_CALLBACK(buffer)
return WEECHAT_RC_OK;
}
+ /*
+ * set a property on buffer, saved in config, auto-applied when the buffer
+ * is opened
+ */
+ if (string_strcmp (argv[1], "setauto") == 0)
+ {
+ COMMAND_MIN_ARGS(3, "setauto");
+ if (argc == 3)
+ {
+ /*
+ * default to empty value for valueless buffer "properties",
+ * e.g. localvar_del_xxx
+ */
+ rc = config_weechat_buffer_set (buffer, argv[2], "");
+ }
+ else
+ {
+ value = string_remove_quotes (argv_eol[3], "'\"");
+ rc = config_weechat_buffer_set (buffer,
+ argv[2],
+ (value) ? value : argv_eol[3]);
+ if (value)
+ free (value);
+ }
+ if (!rc)
+ {
+ gui_chat_printf (
+ NULL,
+ _("%sUnable to create option for buffer property \"%s\""),
+ gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
+ argv[2]);
+ }
+ return WEECHAT_RC_OK;
+ }
+
/* get a buffer property */
if (string_strcmp (argv[1], "get") == 0)
{
@@ -7758,6 +7793,7 @@ command_init ()
" || setvar <name> [<value>]"
" || delvar <name>"
" || set <property> [<value>]"
+ " || setauto <property> [<value>]"
" || get <property>"
" || jump smart|last_displayed|prev_visited|next_visited"
" || <number>|-|+|<name>"),
@@ -7795,6 +7831,9 @@ command_init ()
" setvar: set a local variable in the current buffer\n"
" delvar: delete a local variable from the current buffer\n"
" set: set a property in the current buffer\n"
+ " setauto: like \"set\" and also define option "
+ "\"weechat.buffer.<name>.<property>\" so that the property is saved "
+ "in configuration and applied each time this buffer is opened\n"
" get: display a property of current buffer\n"
" jump: jump to another buffer:\n"
" smart: next buffer with activity\n"
@@ -7861,6 +7900,7 @@ command_init ()
" || setvar %(buffer_local_variables) %(buffer_local_variable_value)"
" || delvar %(buffer_local_variables)"
" || set %(buffer_properties_set)"
+ " || setauto %(buffer_properties_setauto)"
" || get %(buffer_properties_get)"
" || jump smart|last_displayed|prev_visited|next_visited"
" || %(buffers_plugins_names)|%(buffers_names)|%(irc_channels)|"
diff --git a/src/core/wee-completion.c b/src/core/wee-completion.c
index cbd068b4d..9c4674af6 100644
--- a/src/core/wee-completion.c
+++ b/src/core/wee-completion.c
@@ -490,6 +490,62 @@ completion_list_add_buffer_properties_set_cb (const void *pointer, void *data,
}
/*
+ * Adds a buffer local variable to completions list (for `/buffer setauto`).
+ */
+
+void
+completion_list_map_buffer_local_variable_setauto_cb (void *data,
+ struct t_hashtable *hashtable,
+ const void *key,
+ const void *value)
+{
+ char str_localvar[4096];
+
+ /* make C compiler happy */
+ (void) hashtable;
+ (void) value;
+
+ snprintf (str_localvar, sizeof (str_localvar),
+ "localvar_set_%s", (const char *)key);
+ gui_completion_list_add ((struct t_gui_completion *)data,
+ str_localvar,
+ 0, WEECHAT_LIST_POS_SORT);
+}
+
+/*
+ * Adds buffer properties and local variables (that can be set) to completion
+ * list.
+ */
+
+int
+completion_list_add_buffer_properties_setauto_cb (const void *pointer, void *data,
+ const char *completion_item,
+ struct t_gui_buffer *buffer,
+ struct t_gui_completion *completion)
+{
+ int i;
+
+ /* make C compiler happy */
+ (void) pointer;
+ (void) data;
+ (void) completion_item;
+ (void) buffer;
+
+ for (i = 0; gui_buffer_properties_set[i]; i++)
+ {
+ gui_completion_list_add (completion,
+ gui_buffer_properties_set[i],
+ 0, WEECHAT_LIST_POS_SORT);
+ }
+
+ hashtable_map (completion->buffer->local_variables,
+ &completion_list_map_buffer_local_variable_setauto_cb,
+ completion);
+
+ return WEECHAT_RC_OK;
+}
+
+/*
* Adds buffer properties (that can be read) to completion list.
*/
@@ -2026,6 +2082,9 @@ completion_init ()
hook_completion (NULL, "buffer_properties_set",
N_("properties that can be set on a buffer"),
&completion_list_add_buffer_properties_set_cb, NULL, NULL);
+ hook_completion (NULL, "buffer_properties_setauto",
+ N_("properties that can be automatically set on a buffer"),
+ &completion_list_add_buffer_properties_setauto_cb, NULL, NULL);
hook_completion (NULL, "buffer_properties_get",
N_("properties that can be read on a buffer"),
&completion_list_add_buffer_properties_get_cb, NULL, NULL);
diff --git a/src/core/wee-config.c b/src/core/wee-config.c
index 583c6249d..1ab9a7195 100644
--- a/src/core/wee-config.c
+++ b/src/core/wee-config.c
@@ -36,6 +36,7 @@
#include <regex.h>
#include "weechat.h"
+#include "wee-arraylist.h"
#include "wee-config.h"
#include "wee-eval.h"
#include "wee-hashtable.h"
@@ -2468,19 +2469,54 @@ config_weechat_layout_write_cb (const void *pointer, void *data,
}
/*
- * Callback for changes on a notify option.
+ * Applies a buffer option to all matching buffers.
*/
void
-config_weechat_notify_change_cb (const void *pointer, void *data,
+config_weechat_buffer_apply_option (struct t_config_option *option)
+{
+ struct t_arraylist *all_buffers;
+ struct t_gui_buffer *ptr_buffer;
+ int i, list_size;
+
+ if (!option)
+ return;
+
+ all_buffers = arraylist_new (gui_buffers_count, 0, 0,
+ NULL, NULL, NULL, NULL);
+ if (!all_buffers)
+ return;
+
+ for (ptr_buffer = gui_buffers; ptr_buffer;
+ ptr_buffer = ptr_buffer->next_buffer)
+ {
+ arraylist_add (all_buffers, ptr_buffer);
+ }
+
+ list_size = arraylist_size (all_buffers);
+ for (i = 0; i < list_size; i++)
+ {
+ ptr_buffer = (struct t_gui_buffer *)arraylist_get (all_buffers, i);
+ if (gui_buffer_valid (ptr_buffer))
+ gui_buffer_apply_config_option_property (ptr_buffer, option);
+ }
+
+ arraylist_free (all_buffers);
+}
+
+/*
+ * Callback for changes on a buffer option.
+ */
+
+void
+config_weechat_buffer_change_cb (const void *pointer, void *data,
struct t_config_option *option)
{
/* make C compiler happy */
(void) pointer;
(void) data;
- (void) option;
- gui_buffer_notify_set_all ();
+ config_weechat_buffer_apply_option (option);
}
/*
@@ -2505,51 +2541,113 @@ config_weechat_buffer_create_option_cb (const void *pointer, void *data,
rc = WEECHAT_CONFIG_OPTION_SET_ERROR;
- if (option_name)
+ if (!option_name)
+ return rc;
+
+ ptr_option = config_file_search_option (config_file, section,
+ option_name);
+ if (ptr_option)
{
- ptr_option = config_file_search_option (config_file, section,
- option_name);
- if (ptr_option)
- {
- rc = config_file_option_set (ptr_option, value, 1);
- }
- else
+ rc = config_file_option_set (ptr_option, value, 1);
+ }
+ else
+ {
+ pos = strrchr (option_name, '.');
+ if (pos)
{
- pos = strrchr (option_name, '.');
- if (pos)
+ buffer_mask = strndup (option_name, pos - option_name);
+ if (buffer_mask)
{
- buffer_mask = strndup (option_name, pos - option_name);
- if (buffer_mask)
- {
- snprintf (description, sizeof (description),
- _("set property \"%s\" on any buffer matching "
- "mask \"%s\"; "
- "content is evaluated, see /help eval; "
- "${buffer} is a pointer to the buffer being "
- "opened"),
- pos + 1,
- buffer_mask);
- ptr_option = config_file_new_option (
- config_file, section,
- option_name, "string",
- description,
- "",
- 0, 0, "", value, 0,
- NULL, NULL, NULL,
- NULL, NULL, NULL,
- NULL, NULL, NULL);
- rc = (ptr_option) ?
- WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE : WEECHAT_CONFIG_OPTION_SET_ERROR;
- free (buffer_mask);
- }
+ snprintf (description, sizeof (description),
+ _("set property \"%s\" on any buffer matching "
+ "mask \"%s\"; "
+ "content is evaluated, see /help eval; "
+ "${buffer} is a pointer to the buffer being "
+ "opened"),
+ pos + 1,
+ buffer_mask);
+ ptr_option = config_file_new_option (
+ config_file, section,
+ option_name, "string",
+ description,
+ "",
+ 0, 0, "", value, 0,
+ NULL, NULL, NULL,
+ &config_weechat_buffer_change_cb, NULL, NULL,
+ NULL, NULL, NULL);
+ rc = (ptr_option) ?
+ WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE : WEECHAT_CONFIG_OPTION_SET_ERROR;
+ free (buffer_mask);
}
}
}
+ if (ptr_option)
+ config_weechat_buffer_apply_option (ptr_option);
+
return rc;
}
/*
+ * Sets a buffer property.
+ *
+ * Returns:
+ * 1: OK
+ * 0: error
+ */
+
+int
+config_weechat_buffer_set (struct t_gui_buffer *buffer,
+ const char *property, const char *value)
+{
+ char option_name[4096];
+ int rc;
+
+ if (!buffer || !property || !property[0])
+ return 0;
+
+ snprintf (option_name, sizeof (option_name),
+ "%s.%s",
+ buffer->full_name,
+ property);
+
+ /* create/update option */
+ rc = config_weechat_buffer_create_option_cb (
+ NULL, NULL,
+ weechat_config_file,
+ weechat_config_section_buffer,
+ option_name,
+ (value) ? value : "");
+
+ if (rc != WEECHAT_CONFIG_OPTION_SET_ERROR)
+ {
+ gui_chat_printf (
+ NULL,
+ _("Option \"weechat.buffer.%s\" has been set to \"%s\""),
+ option_name,
+ (value) ? value : "");
+ }
+
+ return (rc != WEECHAT_CONFIG_OPTION_SET_ERROR) ? 1 : 0;
+}
+
+/*
+ * Callback for changes on a notify option.
+ */
+
+void
+config_weechat_notify_change_cb (const void *pointer, void *data,
+ struct t_config_option *option)
+{
+ /* make C compiler happy */
+ (void) pointer;
+ (void) data;
+ (void) option;
+
+ gui_buffer_notify_set_all ();
+}
+
+/*
* Callback called when an option is created in section "notify".
*/
diff --git a/src/core/wee-config.h b/src/core/wee-config.h
index 4aa511f79..286a02c8b 100644
--- a/src/core/wee-config.h
+++ b/src/core/wee-config.h
@@ -401,6 +401,8 @@ extern struct t_config_option *config_weechat_debug_get (const char *plugin_name
extern int config_weechat_debug_set (const char *plugin_name,
const char *value);
extern void config_weechat_debug_set_all ();
+extern int config_weechat_buffer_set (struct t_gui_buffer *buffer,
+ const char *property, const char *value);
extern int config_weechat_notify_set (struct t_gui_buffer *buffer,
const char *notify);
extern void config_get_item_time (char *text_time, int max_length);