Extending with Python
- vim module
- functions
- Python Extensions (if_pyth.html)
vim.command()
vim.eval()
- A bit brain damaged – it returns a
string
if the Vim expression evaluates to astring
or a number.
- A bit brain damaged – it returns a
vim.error
vim.buffers
vim.windows
vim.current
Transform Visual Selection
Two methods. The first one just does a transform. The second one feeds the transform as if types - so things like current paste mode make a difference.
@VimFunction(name="TranformVisualUsingUserFunction") def TranformVisualUsingUserFunction(): def callback(user_input): selection = VimGetVisualSelection() text = selection.text lines = selection.lines result = eval(user_input) if callable(result): result = result(text) if isinstance(result, basestring): result = [ result ] else: result = list(result) lines = [] for line in result: lines.extend(line.split(u"\n")) VimSetVisualSelection(selection._replace(lines=lines)) CallWithUserInput(callback, prompt_string=">>> ") vim.command("vmap ,: :call TranformVisualUsingUserFunction()<CR>") @VimFunction(name="TranformVisualUsingUserFunction_FeedKeys") def TranformVisualUsingUserFunction_FeedKeys(): # Calling this function will return the results. temp_func_name = "G_TranformVisualUsingUserFunction_FeedKeys" key_control_r = chr(18) key_enter = chr(13) key_escape = chr(27) return_value = "c%s=%s()%s%s" % (key_control_r, temp_func_name, key_enter, key_escape) selection = VimGetVisualSelection() def callback(user_input): # Keep text and lines in locals() here # so that the user supplied expression # can reference it. text = selection.text lines = selection.lines result = eval(user_input) if callable(result): result = result(text) VimFunction(name=temp_func_name)(lambda: result) CallWithUserInput(callback, prompt_string=">>> ") return return_value vim.command("vmap <expr> ,; TranformVisualUsingUserFunction_FeedKeys()")