Skip to content

feat(eval): Add callback to listen to let expression #244

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/apitest/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@
#include "libvim.h"
#include "minunit.h"

void onEvalVariableSet(char_u *name, typval_T *val)
{
if (val->v_type == VAR_STRING)
{
printf("%s set: %s\n", name, val->vval.v_string);
}
else if (val->v_type == VAR_NUMBER)
{

printf("%s set: %ld\n", name, val->vval.v_number);
}
}

void test_setup(void)
{
vimKey("<esc>");
Expand Down Expand Up @@ -30,18 +43,30 @@ MU_TEST(test_empty)
mu_check(result == NULL);
}

MU_TEST(test_let_expression)
{
vimExecute("let mapleader = \"<space>\"");
vimExecute("let g:mapleader = \"<space>\"");
vimExecute("let mapleader = 1");

mu_check(TRUE);
}

MU_TEST_SUITE(test_suite)
{
MU_SUITE_CONFIGURE(&test_setup, &test_teardown);

MU_RUN_TEST(test_simple_addition);
MU_RUN_TEST(test_empty);
MU_RUN_TEST(test_let_expression);
}

int main(int argc, char **argv)
{
vimInit(argc, argv);

vimSetEvalVariableSetCallback(&onEvalVariableSet);

win_setwidth(5);
win_setheight(100);

Expand Down
8 changes: 7 additions & 1 deletion src/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2341,7 +2341,6 @@ set_var_lval(
int cc;
listitem_T *ri;
dictitem_T *di;

if (lp->ll_tv == NULL)
{
cc = *endp;
Expand Down Expand Up @@ -7893,6 +7892,13 @@ void set_var(
typval_T *tv,
int copy) /* make copy of value in "tv" */
{

if (name != NULL && tv != NULL)
{
//printf("lp->ll_exp_name: %s\n", lp->ll_exp_name);
evalVariableSetCallback(name, tv);
}

dictitem_T *v;
char_u *varname;
hashtab_T *ht;
Expand Down
1 change: 1 addition & 0 deletions src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ EXTERN BufferUpdateCallback bufferUpdateCallback INIT(= NULL);
EXTERN ClipboardGetCallback clipboardGetCallback INIT(= NULL);
EXTERN FileWriteFailureCallback fileWriteFailureCallback INIT(= NULL);
EXTERN DirectoryChangedCallback directoryChangedCallback INIT(= NULL);
EXTERN EvalVariableSetCallback evalVariableSetCallback INIT(= NULL);
EXTERN FormatCallback formatCallback INIT(= NULL);
EXTERN GotoCallback gotoCallback INIT(= NULL);
EXTERN TabPageCallback tabPageCallback INIT(= NULL);
Expand Down
5 changes: 5 additions & 0 deletions src/libvim.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ void vimSetDirectoryChangedCallback(DirectoryChangedCallback f)
directoryChangedCallback = f;
}

void vimSetEvalVariableSetCallback(EvalVariableSetCallback f)
{
evalVariableSetCallback = f;
}

void vimSetOptionSetCallback(OptionSetCallback f)
{
optionSetCallback = f;
Expand Down
2 changes: 2 additions & 0 deletions src/libvim.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ void vimCommandLineGetCompletions(char_u ***completions, int *count);
*/
char_u *vimEval(char_u *str);

void vimSetEvalVariableSetCallback(EvalVariableSetCallback callback);

/***
* Cursor Methods
***/
Expand Down
2 changes: 2 additions & 0 deletions src/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1390,6 +1390,8 @@ typedef struct
} vval;
} typval_T;

typedef void (*EvalVariableSetCallback)(char_u *name, typval_T *tv);

/* Values for "dv_scope". */
#define VAR_SCOPE 1 /* a:, v:, s:, etc. scope dictionaries */

Expand Down