Skip to content

Commit

Permalink
core: evaluate expressions even when the suffix is missing (issue #2042
Browse files Browse the repository at this point in the history
…, issue #1714)
  • Loading branch information
flashcode committed Nov 23, 2023
1 parent 87f74e9 commit 479ab5b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 26 deletions.
1 change: 1 addition & 0 deletions ChangeLog.adoc
Expand Up @@ -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)
Expand Down
13 changes: 7 additions & 6 deletions src/core/wee-eval.c
Expand Up @@ -1472,8 +1472,8 @@ eval_string_hdata (const char *text, struct t_eval_context *eval_context)
*/

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;

Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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)
Expand Down
23 changes: 12 additions & 11 deletions src/core/wee-string.c
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/core/wee-string.h
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/core/test-core-eval.cpp
Expand Up @@ -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}");
Expand Down
21 changes: 14 additions & 7 deletions tests/unit/core/test-core-string.cpp
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 479ab5b

Please sign in to comment.