1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-12 14:14:48 +02:00

core: evaluate expressions even when the suffix is missing (issue #2042, issue #1714)

This commit is contained in:
Sébastien Helleu
2023-11-23 08:20:01 +01:00
parent 87f74e9f95
commit 479ab5bc58
6 changed files with 40 additions and 26 deletions
+1
View File
@@ -15,6 +15,7 @@ For a list of important changes that require manual actions, please look at rele
New features::
* core: evaluate expressions even when the suffix is missing ("}" by default) (issue #2042, issue #1714)
* core: add syntax highlighting in evaluation of expressions with `raw_hl:string` and `hl:string`, add option weechat.color.eval_syntax_colors (issue #2042)
* core: add option `search_history` in command `/input`, add key kbd:[Ctrl+r] to search in commands history, add key context "histsearch" (issue #2040)
* core: add option weechat.look.buffer_search_history (issue #2040)
+7 -6
View File
@@ -1472,8 +1472,8 @@ end:
*/
char *
eval_syntax_highlight_add_markers (const char *text,
struct t_eval_context *eval_context)
eval_syntax_highlight_add_markers (const char *prefix, const char *text,
const char *suffix)
{
char **value;
@@ -1482,10 +1482,10 @@ eval_syntax_highlight_add_markers (const char *text,
return NULL;
string_dyn_concat (value, EVAL_SYNTAX_HL_INC, -1);
string_dyn_concat (value, eval_context->prefix, -1);
string_dyn_concat (value, prefix, -1);
if (text)
string_dyn_concat (value, text, -1);
string_dyn_concat (value, eval_context->suffix, -1);
string_dyn_concat (value, suffix, -1);
string_dyn_concat (value, EVAL_SYNTAX_HL_DEC, -1);
return string_dyn_free (value, 0);
@@ -1625,7 +1625,8 @@ eval_syntax_highlight (const char *text, struct t_eval_context *eval_context)
*/
char *
eval_replace_vars_cb (void *data, const char *text)
eval_replace_vars_cb (void *data,
const char *prefix, const char *text, const char *suffix)
{
struct t_eval_context *eval_context;
struct t_config_option *ptr_option;
@@ -1641,7 +1642,7 @@ eval_replace_vars_cb (void *data, const char *text)
EVAL_DEBUG_MSG(1, "eval_replace_vars_cb(\"%s\")", text);
if (eval_context->syntax_highlight)
return eval_syntax_highlight_add_markers (text, eval_context);
return eval_syntax_highlight_add_markers (prefix, text, suffix);
/* raw text (no evaluation at all), with syntax highlighting */
if (strncmp (text, "raw_hl:", 7) == 0)
+12 -11
View File
@@ -4139,7 +4139,10 @@ string_replace_with_callback (const char *string,
const char *prefix,
const char *suffix,
const char **list_prefix_no_replace,
char *(*callback)(void *data, const char *text),
char *(*callback)(void *data,
const char *prefix,
const char *text,
const char *suffix),
void *callback_data,
int *errors)
{
@@ -4196,14 +4199,6 @@ string_replace_with_callback (const char *string,
}
pos_end_name++;
}
/* prefix without matching suffix => error! */
if (!pos_end_name[0])
{
result[index_result] = '\0';
if (errors)
(*errors)++;
return result;
}
key = string_strndup (string + index_string + length_prefix,
pos_end_name - (string + index_string + length_prefix));
if (key)
@@ -4241,7 +4236,10 @@ string_replace_with_callback (const char *string,
key = key2;
}
}
value = (*callback) (callback_data, (key) ? key : "");
value = (*callback) (callback_data,
prefix,
(key) ? key : "",
(pos_end_name[0]) ? suffix : "");
if (value)
{
length_value = strlen (value);
@@ -4262,7 +4260,10 @@ string_replace_with_callback (const char *string,
strcpy (result + index_result, value);
index_result += length_value;
}
index_string = pos_end_name - string + length_suffix;
if (pos_end_name[0])
index_string = pos_end_name - string + length_suffix;
else
index_string = pos_end_name - string;
free (value);
}
else
+4 -1
View File
@@ -147,7 +147,10 @@ extern char *string_replace_with_callback (const char *string,
const char *prefix,
const char *suffix,
const char **list_prefix_no_replace,
char *(*callback)(void *data, const char *text),
char *(*callback)(void *data,
const char *prefix,
const char *text,
const char *suffix),
void *callback_data,
int *errors);
extern void string_get_priority_and_name (const char *string,
+2 -1
View File
@@ -988,7 +988,8 @@ TEST(CoreEval, EvalExpression)
hashtable_set (pointers, "my_null_pointer", (const void *)0x0);
hashtable_set (pointers, "my_buffer_pointer", gui_buffers);
hashtable_set (pointers, "my_other_pointer", (const void *)0x1234abcd);
WEE_CHECK_EVAL("x", "x${buffer.number");
WEE_CHECK_EVAL("x", "x${buffer.numbe");
WEE_CHECK_EVAL("x1", "x${buffer.number");
WEE_CHECK_EVAL("x${buffer.number}1",
"x\\${buffer.number}${buffer.number}");
WEE_CHECK_EVAL("1", "${buffer.number}");
+14 -7
View File
@@ -1308,20 +1308,23 @@ TEST(CoreString, Highlight)
/*
* Test callback for function string_replace_with_callback.
*
* It replaces "abc" by "def", "xxx" by empty string, and for any other value
* It replaces "abc" by "def", "empty" by empty string, and for any other value
* it returns NULL (so the value is kept as-is).
*/
char *
test_replace_cb (void *data, const char *text)
test_replace_cb (void *data,
const char *prefix, const char *text, const char *suffix)
{
/* make C++ compiler happy */
(void) data;
(void) prefix;
(void) suffix;
if (strcmp (text, "abc") == 0)
return strdup ("def");
if (strcmp (text, "xxx") == 0)
if (strcmp (text, "empty") == 0)
return strdup ("");
if (strncmp (text, "no_replace:", 11) == 0)
@@ -1446,13 +1449,17 @@ TEST(CoreString, ReplaceWithCallback)
&test_replace_cb, NULL, &errors);
WEE_REPLACE_CB("test def", 0, "test ${abc}", "${", "}", NULL,
&test_replace_cb, NULL, &errors);
WEE_REPLACE_CB("test ", 0, "test ${xxx}", "${", "}", NULL,
WEE_REPLACE_CB("test ", 0, "test ${empty}", "${", "}", NULL,
&test_replace_cb, NULL, &errors);
WEE_REPLACE_CB("test ${aaa", 1, "test ${aaa", "${", "}", NULL,
&test_replace_cb, NULL, &errors);
WEE_REPLACE_CB("test ", 0, "test ${empty", "${", "}", NULL,
&test_replace_cb, NULL, &errors);
WEE_REPLACE_CB("test ${aaa}", 1, "test ${aaa}", "${", "}", NULL,
&test_replace_cb, NULL, &errors);
WEE_REPLACE_CB("test def ${aaa}", 1, "test ${abc} ${xxx} ${aaa}",
WEE_REPLACE_CB("test def ${aaa}", 1, "test ${abc} ${empty} ${aaa}",
"${", "}", NULL, &test_replace_cb, NULL, &errors);
WEE_REPLACE_CB("test ", 1, "test ${abc", "${", "}", NULL,
WEE_REPLACE_CB("test def", 0, "test ${abc", "${", "}", NULL,
&test_replace_cb, NULL, &errors);
WEE_REPLACE_CB("test abc}", 0, "test abc}", "${", "}", NULL,
&test_replace_cb, NULL, &errors);
@@ -1462,7 +1469,7 @@ TEST(CoreString, ReplaceWithCallback)
&test_replace_cb, NULL, &errors);
WEE_REPLACE_CB("def", 0, "${abc}", "${", "}", NULL,
&test_replace_cb, NULL, &errors);
WEE_REPLACE_CB("", 0, "${xxx}", "${", "}", NULL,
WEE_REPLACE_CB("", 0, "${empty}", "${", "}", NULL,
&test_replace_cb, NULL, &errors);
WEE_REPLACE_CB("${aaa}", 1, "${aaa}", "${", "}", NULL,
&test_replace_cb, NULL, &errors);