-
Notifications
You must be signed in to change notification settings - Fork 56
Additional decorators - events and opfuncs #15
Comments
from snake import *
from time import time
def my_python_func():
print("moved! at %d" % time())
call = register_fn(my_python_func)
command("autocmd CursorMoved * :python %s" % call)) that's how you would do it. you could easily make a helper that registered a function to an autocmd event. |
Okay, I think that would be a good fit for this library--I can make a PR with decorators for the basic events if you agree. How about custom operators? It would be cool to be able to define them in the same fashion. There is a project for a vim shortcut here: https://github.com/tommcdo/vim-express |
Decorators would be great. If you make a generalized autocmd helper that takes an event name and a python function, and does all the things to register that function to that event, that would make it super reusable. Then we could make more specific decorators for specific events like CursorMoved, if they make sense. Not sure about your operator question. I'm not sure what a custom operator is. |
Custom Operators allow you to apply a function to text selected by an arbitrary motion. The selected text then serves as the "noun" the function "verbs" with. They seem to be called by a number of different names and the straight VimL implementation is quite obscure.
Example from my blog post :nmap <silent> <F4> :set opfunc=Echo<CR>g@
:vmap <silent> <F4> :<C-U>call Echo(visualmode(), 1)<CR> The function definition (you can see it's quite a mess of boilerplate): " uses [/] marks along with visual mode to yank a custom selection of text.
fun! Echo(type, ...)
"backticks=' (goto mark), have to avoid out-quoting string
"clear the @q register for use in this function.
"@q is the reserved phonim register.
let @q=""
" see :h g@ for more info and how to save and restore a register, which
" would allow us to use 'q' only temporarily and then restore it
if a:0 " Invoked from Visual mode, use '< and '> marks.
silent exe "normal! `<" . a:type . '`>"qy'
elseif a:type == 'line'
silent exe 'normal! `[V`]"qy'
elseif a:type == 'block' " column ('block') selection.
silent exe 'normal! `[\<C-V>`]"qy'
"v -> visual mode but stay in-line
else " Stay in-line
silent exe 'normal! `[v`]"qy'
endif
echo @q
endfun . . . . . |
Could you provide a use-case that would be difficult to achieve another way? I'm still having trouble grasping how this would be used. |
The use case in vim-express is to comment out (e.g. with In some (all?) cases you could have a |
Interesting. In theory it sounds like it could be implemented in snake. I'd be for merging it if you can pull it off in a PR. Writing a test might be hard though |
just as there is a
key_map
decorator it would be great if there was a decorator for things like:Is that feasible?
The text was updated successfully, but these errors were encountered: