noib3/nvim-oxi

Use LuaFunction as `rhs` for `buffer_keymap_set` that should capture local variable

patata3000 opened this issue · 2 comments

Hey there!

I've got this piece of code that doesn't work:

    let table_obj: LuaTable = build_table_output(buffer.clone(), buffer_content)?;
    let my_function = Function::from_fn({
        move |()| {
            print!("{:?}", table_obj.call_method("get_cell", ())?);
            Ok::<(), oxi::Error>(())
        }
    });
    buffer.set_keymap(Mode::Normal, "p", my_function, &keymap_opts)?;  // FORBIDDEN

I couldn't find a way to pass a lambda LuaFunction to a set_keymap.

What I could do is to define a LuaFunction in some module -> my_module => my_module.my_function and make

    buffer.set_keymap(Mode::Normal, "p", "<cmd>lua require'my_module.my_function()'<cr>", &keymap_opts)?;

There is still a problem though. I want it to either:

  • be able to capture the local variable table_obj,
  • pass the table_obj as an argument.

If it's possible, I can't find a way to do this.
If it's not possible, I may be using a bad pattern but I don't know how to solve this problem.

Do you have an explanation? Or any hint?

You can leave the rhs empty and pass the closure to the callback method of your keymap_opts builder.

I love you.