1

I want to extend the easymotion Plugin to take some action after the jump. To explain to those who are not familiar with this plugin, I will use the following simplified example.

Say in a plugin UsefulPlugin the following map is defined:

function! s:Goto()
    let char = nr2char(getchar())
    return "f" . char
endfunction
nnoremap <expr> <Plug>(goto) s:Goto()

which does almost the same as the unmapped f: jump to the next char input by getchar.

Now as a user, I want to define a map that uses <Plug>(goto) and after the user input the char, jump to that char and delete until end of line. As an illustration: the effect of this mapping should be equivalent to

function! s:GotoAndMore()
    let char = nr2char(getchar())
    return "f" . char . "D"
endfunction
nnoremap <expr> fD s:GotoAndMore()

My question is: how can I achieve this with <Plug>(goto)?

  • you are missing <expr> in the first example, is that intentional? – Mass Jan 4 at 3:12
  • Can you call s:Goto from s:GotoAndMore()? Are they in the same script? – Ralf Jan 4 at 6:23
  • no, s:Goto is provided by another plugin. My intention is to extend easymotion. It uses a very complex internal function. – Liu Sha Jan 4 at 6:42
2

This is hackish and I'm not sure how this works if s:Goto calls another script-internal function.

I assume that the s:Goto() is already defined in another script.

function! GotoAndMore()
    " Get the mapping for <Plug>(goto)
    redir => xx | silent map <Plug>(goto) | redir end
    " extract the function name (incl script number)
    let funcName = matchstr(xx, "<SNR>[^(]*")
    " create a funcref for it
    let Func = function(funcName)
    " return function return value with following "D" 
    return Func() . "D"
endfunction
nnoremap <expr> fD GotoAndMore()

Test thoroughly!

  • The "D" will be consumed by the getchar() inside Func – Liu Sha Jan 4 at 9:10
  • @LiuSha No, The function is called and returns a String. E.g. "fU", if you entered 'U'. Then 'D' is appended to the returned string. The result "fuD" is then returned by GotoAndMore(). – Ralf Jan 4 at 10:19
  • You are right, But what I am going to deal with is a function in easymotion. And the function not only returns the expression but also has a lot of side effects updating the display. This approach does not work. Anyway, thank you very much for your reply. This would be helpful for other scenarios. I upvoted your answer. – Liu Sha Jan 4 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.