Function defined in ftplugins
In ftplugins we can define script local functions and global functions that are defined only when a buffer of the given filtetype is loaded.
In the second case, the function is still global and can be hidden by any other script.
In both cases, the functions will be redefined every time a new buffer of the same filetype is opened, unless we use another global guard to avoid loading some global part of the ftplugin. Either way, the ftplugin is bigger and it'll take slightly more time to be loaded.
Global functions that support multiple filetypes. (your option 1)
This approach makes sense when defining a same function that could be used from different filetypes, and its behaviour needs to be adapted to the current filetype (or any filetype passed as parameter).
Autoloaded functions
Nowadays (i.e. since I'm sure the version of Vim I'm using is >= 7.0), instead of global functions I prefer autoloaded functions. They are still some kind of global functions anybody could override (that's why I use my initials as a prefix to avoid name clashes).
The big advantage is that they are (lazily) loaded on the fly, and once -- no need for guards anymore, and they are only loaded when used. For instance, ftplugin file could define the command, but the function behind the command would be defined only on first use.
Also, I find it easier to test them, and to update their definition when working on their implementation.
When the function can be used from buffers of different filetypes, I use a global name (lh#somefunc()
). When the function only makes sense from, let's say C++, I use the filetype in the name of the function (e.g. lh#cpp#somefunc()
)
Filetype-polymorphism.
It's possible to emulate some kind of filetype polymorphism and call functions named foo#{ft}#bar#func()
. I've defined a convoluted framework for this purpose. I mainly use it from my refactoring plugin as variables declarations, getter signatures, and so on, will change according to the filetype while the main procedure stays the same.
-buffer
in theftplugin/*.vim
) rather than functions themselves. – Andrew Ho-Lee Mar 1 at 19:19