2

Consider the following vim code.

function! s:foo()
    if s:bar()
        let a = 1
    endif
endfunction

function! s:bar()
python << EOF
vim.command('echoerr "blabla"')
EOF
endfunction

command! Foo call s:foo()
map <F7> :Foo<CR>

Open a vim session and press F7. Vim wil then throw an error saying E171: Missing :endif. How is this possible and how can I correctly send an error message inside the python part?

2 Answers 2

4

Inside a function, :echoerr works like :throw and raises an exception. Normal control flow is interrupted and Vim complains it didn't see the :endif. :echoerr is practically useless inside a function, also because an entire multi-line stacktrace is shown.

I would solve this by just setting v:errmsg (or any other custom variable or flag), and then check for and report the error directly at the conclusion of your function:

function! s:foo()
    let v:errmsg = ''
    if s:bar()
        let a = 1
    endif
endfunction

function! s:bar()
python << EOF
vim.command('let v:errmsg = "blabla"')
EOF
endfunction

command! Foo call s:foo() | if ! empty(v:errmsg) | echoerr v:errmsg | endif
map <F7> :Foo<CR>
5
  • On a side note: !empty(string) is very inefficient, since it has to convert string to a list first. Consider using len(string) instead, or just string !=# ''. It's a widespread mistake...
    – lcd047
    May 22, 2015 at 21:27
  • Sorry, len(string) should be strlen(string).
    – lcd047
    May 23, 2015 at 7:27
  • @lcd047: Have you actually measured it? I don't see any remarkable difference, and I don't think there will be an explicit conversion to a List. Oh, and len() and strlen() are identical for Strings. May 23, 2015 at 10:01
  • 1
    I looked at the sources; Vim doesn't try to optimise anything, even inroads like this. This approach makes sense from a developer's point of view. Not so much from a user's point of view. len(), just like empty(), is a list function. The speed difference doesn't matter when you call it once; it might start to add up when you call it from a sort function. But even if it's a pointless optimisation, it's still a small detail in understanding the language (at least it is to me). shrug
    – lcd047
    May 23, 2015 at 13:05
  • 1
    @lcd047 empty() has understood how to efficiently check for an empty string since it was first implemented in 2005.
    – jamessan
    Jan 12, 2017 at 17:56
2

if s:bar() expects s:bar() to return a value, but s:bar() doesn't. Hence the "missing :endif".

On a side note, you also didn't import vim for the Python code (not sure that's still required though).

Update: echoerr also raises a vim.error. Try this instead:

function! s:bar()
    python <<EOF
import vim

vim.command('normal \<Esc>')
vim.command('echohl ErrorMsg')
vim.command('echomsg "blabla"')
vim.command('echohl None')
EOF
    return 1
endfunction

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.