3
function! DebugC()
    let l:debugStr = "printf(\"" . s:debugPrefixStr . s:debugCounter . "\");"

    if g:debugstringAlwaysIncludeHeader
        let l:incStr = "#include <stdio.h>; "
        let l:debugStr = l:incStr . l:debugStr
    endif

    :put=l:debugStr
endfunc " }}}

Method dumps the text correctly but if I use execute instead of call, it always moves the cursor to the top of the

" doesn't move to top of file
autocmd Filetype c,cpp nnoremap <buffer> <silent> <Plug>DumpDebugString  :call DebugC()<CR>

" moves cursor to the top of the file
autocmd Filetype c,cpp nnoremap <buffer> <silent> <Plug>DumpDebugString  :exe DebugC()<CR>

Adding getcurpos(), setpos() at the start and end of the function doesn't seem to solve the problem.

Any pointers?

1 Answer 1

7

In vim, a function which does not explicitly return a value actually returns 0. So, your code is equivalent to exe 0, which is like :0 meaning move the cursor to the first line.

5
  • Wow, so troublesome, yet so simple of an explanation! So I get I can solve it just by return getcurpos()[1] + 1 or something like that.. Do you think there is a more elegant solution to the problem? Jan 1, 2018 at 19:41
  • 1
    Is there a reason you are using exe instead of call? It is not clear from your question why call doesn't work. And you can just return empty string '', exe '' should do nothing.
    – Mass
    Jan 1, 2018 at 20:02
  • The actual call is a little more complicated, something like this: autocmd Filetype c,cpp nnoremap <buffer> <silent> <Plug>DumpDebugString :<C-U>exe <SID>debugFunctionWrapper("s:debugC")<CR> You're right on the second remark though, returning ' ' does the job... Jan 1, 2018 at 20:05
  • You can use call in that example. Actually, :<c-u>call s:function()<cr> works too. But, the alternative solution, if required, is just return empty string return '' in debugFunctionWrapper.
    – Mass
    Jan 1, 2018 at 20:11
  • Ah, yeah you're right again.. call does work in this case as well.. Thanks again! Jan 1, 2018 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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