1

How do I check if Lua is supported?

From my .vimrc, I only want to put an if around a Lua heredoc so that my config works in plain vim or versions of neovim which didn't support Lua (pre 0.5.0?).

What's the best way to check for Lua support in VimL?

2
  • 1
    if has('lua') ...
    – Heptite
    Jul 22, 2023 at 15:20
  • @Heptite has('lua') returns 0 in NVIM v0.9.0. Surely this is a bug?
    – Tom Hale
    Jul 24, 2023 at 12:34

2 Answers 2

3

All Neovim versions do support Lua. For Vim, you can check has("lua") return value. That is, if has("nvim") || has("lua") should work in most cases.

However, Vim may specifically depend on the presence of external Lua library in run-time. So executing Lua code may still fail even if has("lua") returns one.

So if you want to be 100% sure, you have to try it.

function! LuaWorks()
    try | return luaeval("1")
    catch | endtry
endfunction
3
  • 1
    Didn't nvim prior to v0.5.0 only have partial Lua support?
    – Tom Hale
    Jul 24, 2023 at 12:22
  • 1
    I just tested, and in the catch case, nothing is returned, which has 0 (the default) returned.
    – Tom Hale
    Jul 24, 2023 at 12:40
  • @TomHale 1) What you mean by "partial"? The Lua engine was always a builtin in Neovim. If it was missing some feature(s) you personally need then check for has("nvim-0.5.0") or such specifically. Otherwise, I don't have idea what are you talking about. 2) Yes, in VimScript zero is default return value. This is why I had it omitted. Putting it after "catch" doesn't really change anything.
    – Matt
    Jul 24, 2023 at 13:21
1

Based on Matt's answer, I came up with this:

" Check for a working Lua:
function! HasLua()
  if has('nvim-0.5')
    return 1
  endif
  if ! has('lua')
    return 0
  endif
  " Check external lua works correctly
  try
    return luaeval("1")
  catch
    return 0
  endtry
endfunction
1
  • It's probably a good idea to specify the exception(s) for catch (:help :catch). For example, I think as written if I Ctrl-C while that function happens to be executing it would give the wrong result instead of bubbling up the interrupt. Probably not what you want.
    – D. Ben Knoble
    Jul 24, 2023 at 13:26

Your Answer

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

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