Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Perform hdata_search in /eval #1891

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

talisein
Copy link
Contributor

@talisein talisein commented Mar 13, 2023

Without search, hdata lists are cumbersome to access in triggers. This PR introduces a search operator:
hdata[list?search_condition].var1.var2...
The search condition can contain subexpressions, but instead of bracketed ${foo}, a parenthesized $(foo) must be used. For example:

Fetch the description of a loaded plugin with name == "irc"

>> ${plugin[weechat_plugins?$(plugin.name) == irc].description}
== [IRC (Internet Relay Chat) protocol]

Fetch the irc server capability string for the server who's name matches the current buffer's name split on periods:

>> ${irc_server[irc_servers?$(irc_server.name) == $(split:1,.,,$(buffer.name))].cap_ls.keys()}
== account-notify,away-notify,chghost,extended-join,multi-prefix,sasl,tls,account-tag,cap-notify,echo-message,server-time,solanum.chat/identify-msg,solanum.chat/oper,solanum.chat/realhost

@flashcode
Copy link
Member

Hi,

Thanks for this PR.

I think it's not a good idea to use another prefix/suffix for subexpressions when searching in hdata, for 2 reasons:

  • the prefix and suffix can be customized in eval: not directly with /eval but when using API function string_eval_expression, so for example if you choose $( as prefix and ) as suffix, this will conflict with the ones you just added for hdata search ;
  • it's confusing for the user to know when to use one prefix or the other one.

Now the problem is how to have an evaluated condition that is not immediately evaluated.

Something similar is done with the format ${if:condition?true:false}, where the condition evaluation is delayed:

weechat/src/core/wee-eval.c

Lines 1883 to 1902 in 3e5d77f

char *
eval_replace_vars (const char *expr, struct t_eval_context *eval_context)
{
const char *no_replace_prefix_list[] = { "if:", "raw:", NULL };
char *result;
int debug_id;
EVAL_DEBUG_MSG(1, "eval_replace_vars(\"%s\")", expr);
eval_context->recursion_count++;
if (eval_context->recursion_count < EVAL_RECURSION_MAX)
{
result = string_replace_with_callback (expr,
eval_context->prefix,
eval_context->suffix,
no_replace_prefix_list,
&eval_replace_vars_cb,
eval_context,
NULL);

Maybe you could do something similar, but this could require to change the format.
For example using: ${hdata_search:search_condition,hdata[list].var1.var2...}.

Other ideas welcome!

@flashcode flashcode self-assigned this Mar 19, 2023
@flashcode flashcode added feature New feature request waiting info Waiting for info from author of issue labels Mar 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature request waiting info Waiting for info from author of issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants