How to encapsulate Vim plugin code written in Lua / Python / Ruby?

In Vimscript, the scope of script s: can be used to avoid name conflicts between plugins. I am writing a Vim plugin in Lua, and I noticed that Vim uses all of its Lua code in a common area. This means that my Lua plugin functions are visible to any other plugin using Lua, and it looks like a name clash is expected.

Although my example includes Lua, this question also applies when developing Vim plugins in Python or Ruby. I could just prefix all my Lua functions with the name of the plugin, but is there a more reliable / standard way to encapsulate the Vim plugin code when using these languages?

+6
source share
1 answer

I don't have much experience with lua, but for python, everything is similar too, especially if you use 'pyfile' (luafile is probably very similar). The best, recommended approach, especially for python, would look something like this:

 if !exists('g:audiobox_py_loaded') python import sys, vim python if vim.eval('expand("<sfile>:p:h")') not in sys.path: \ sys.path.append(vim.eval('expand("<sfile>:p:h")')) python import audiobox endif 

Thus, even if you have top-level functions in the audiobox.py file, they will get the names placed in the path to the โ€œaudio blockโ€ and, therefore, can now be accessed through the audiobox. I am sure similar idioms should be available for lua as well.

For my AudioBox plugin, which I created in my free time to find out how I can interact with python, I took this to the next level and wrapped my necessary functionality in a class and exposed the object with the same setup () method. You can take a look at the code to get a better idea.

NOTE. I am not a python expert in any way, so don't judge my code, this is more about a hobby :).

+1
source

All Articles