Luc Hermitte's VIM Ftplugins Emulation | Monday 25th August 2003 |
This paragraph only concerns those who want to emulate the file type plugins mechanism with version of VIM prior to the version 6.
In a few words, what I've understood of this new feature is that it enables
to easily load (source) VIM files -- containing functions, mappings and
other customizations -- and associate its definitions to a buffer regarding
the filetype of what is edited within the buffer.
Thus, it enables to define completely redundant mappings and associate them
to different filetypes. For instance, the mapping of if
could
be expanded to if<cr>endif
when editing VIM script files,
to if () {<cr>}
when editing C/C++/Java files, and not
defined at all when editing HTML files.
There is no further need to define everything within the .vimrc
file -- note also that writing all the autocommands within the
.vimrc
file obfuscates it and causes (me) headaches while it
increases the complexity the maintenance of the whole stuff.
The filetypes plugins mechanism is thus very useful but it was not present in VIM until the version 6. Follows a way to emulate this feature with the previous versions of our favorite editor. What I propose seems to work since the version 5.6 and may be the previous ones -- don't remember.
The first smart thing to do is to split all the definitions regarding the
filetype of the edited buffers ; it is a matter of clarity. Then quickly,
several files emerge. One for LaTeX editing, one for HTML editing, one for C
editing, and so on.
As the .vim
extension is already used by the syntax
files, I gave another extension to these files in order not to mess
things up when managing completely different files. Years ago, I chose
.set
that stands for setting files. It is not perfect
but at least I can't mix them up.
The second not so smart thing not to do is to load these files from
syntax files : when VIM is upgraded from ver 5.x to 5.x+1,
the syntax files are overloaded and all the modifications are lost.
A far more better solution is to add corresponding autocommands to the
.vimrc
. But, it is not said that filetypes defined within
filetype.vim
and myfiletype.vim
are consistent
with the autocommands present within .vimrc
.
I opted for a solution directly inspired from the one adopted with syntax
files. I copied and adapted syntaxfile.vim
into
mysettingfile.vim
.
At first, it defines a command SetAuQuick
that will ease the
writing of all the autocommands.
command -nargs=1 SetAuQuick au Syntax <args> so $VIMRUNTIME/settings/<args>.setThen, for each filetype we have a dedicated macros file, the only thing to do is to call the command for the specific filetype. For instance, for e-mails composition, the line is :
SetAuQuick mailNo more need to know exactly the definition of the filetype pattern.
Of course, this new file has to be called. I am not sure is it the better
place, but I chose to source it just after the syntax activation command :
syntax
on
from my .vimrc
(actually it is vimrc_core.vim
in my configuration).
Unfortunately my trick is not activated when the syntax colorization is
off. If you have any idea in order to enhance this, please let me know !
At this point, every thing is nice until different types of buffers are loaded within the same VIM session. It becomes very annoying when VIM complains about redundant or inconsistent mappings. I observed this behavior a lot of times mainly when I want to update my set of abbreviations and mistyping corrections from the same session than my LaTeX one, or when I generate HTML files corresponding to different kind of source files (with 2HTML).
In order to correct this, the mapping abbreviations and other stuffs must
become buffer relative. I found the solution to this issue thanks to the
wonderful scripts buffoption.vim
from Michael Geddes.
Using my scripts, the command to use in order to issue buffer relative
loading of pseuso-ftplugins is SetAu
. After a caching
operation, the command issue a call to ReadFileTypeMap()
. If
you look closely at my mysettingfile.vim
, you will notice that
I load my e-mails composing plugins with SetAuQuick
; I
consider that it is very unlikely to mix e-mail with any other kind of
files.
Regarding buffoption.vim
, you will be able to found the latest
version on sourceforge. I
understood that it enable to use the new form of mapping (with the
<buffer>
tag) and thus
load real ftplugins files with VIM v5.x ; I haven't tested it until now.
Nevertheless, I proposed a little patch
over an older version of the script. The patch focuses on several issues :
noremap
, noreab
and
noremenu
commands,
FileType__ExeLine(...)
,
Last note : there are some drawbacks either inherent to the technique used or because I miss things (I guess that I missed something regarding syntax on or off). The points are :
common_brackets.vim
do not work properly when switching
buffers. From my point of view, this incompatibility with
Triggers.vim
is a real issue.
<buffer>
tags). In
order to do so, I will have to merge the latest
buffoptions.vim
from Michael Geddes with my patch. But
well ... I lack time and by then I think that I will have definitively
moved to the version 6.
mysettingfile.vim <raw file> Warning: filemtime() [function.filemtime]: Stat failed for ../vim/ressources/dollar_VIM/mysettingfile.vim (errno=2 - No such file or directory) in /mnt/133/sdb/f/b/hermitte/vim/dateOfScript.php on line 3 1st January 1970 |
file in charge of the loading of the right script file according to the filetype of the buffer to be edited |
buffoptions2.vim <raw file> Warning: filemtime() [function.filemtime]: Stat failed for ../vim/ressources/dollar_VIM/vim57/macros/buffoptions2.vim (errno=2 - No such file or directory) in /mnt/133/sdb/f/b/hermitte/vim/dateOfScript.php on line 3 1st January 1970 buffoptions_mac2.vim <raw file> Warning: filemtime() [function.filemtime]: Stat failed for ../vim/ressources/dollar_VIM/vim57/macros/buffoptions_mac2.vim (errno=2 - No such file or directory) in /mnt/133/sdb/f/b/hermitte/vim/dateOfScript.php on line 3 1st January 1970 |
files that enable to operate buffer relative loading of scripts |
vimrc_core.vim |
the root file (.vimrc ) in charge of loading
mysettingfile.vim |
Back to the VIM Page | hermitte at free.fr |