Skip to content
This repository has been archived by the owner on Jun 24, 2023. It is now read-only.

Map commands to Python functions feature. #28

Open
rajaravivarma-r opened this issue Aug 19, 2018 · 0 comments
Open

Map commands to Python functions feature. #28

rajaravivarma-r opened this issue Aug 19, 2018 · 0 comments

Comments

@rajaravivarma-r
Copy link

I wanted a way to call Python functions through vim commands, and I have wrote one helper function in my .vimrc.py,

def command_map(command, nargs="*"):
    def decorator(func):
        def wrapper(*args):
            args = snake.vim.eval("a:000")
            return func(*args)

        wrapper_func = snake.register_fn(wrapper)
        function_name = f"Snake_{func.__name__}"
        snake.command(
            f"""
            function! {function_name}(...)
                :{snake.PYTHON_CMD} {wrapper_func}
            endfunction
            """
        )
        snake.command(
            f"command! -nargs={nargs} {command} call {function_name}(<f-args>)"
        )
        return wrapper

    return decorator


@command_map("Pwf")
def copy_current_file_path(*args, **kwargs):
    current_buffer = snake.vim.current.buffer
    if current_buffer.name is None:
        snake_echo("No file loaded in current buffer")
    else:
        pyperclip.copy(current_buffer.name)
        snake_echo(current_buffer.name)

Do you think this feature could get into the core? I would like to contribute with a PR if you wish.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant