Luc Hermitte's VIM C/C++ ftplugins | Wednesday 9th October 2002 |
I propose/present here a set of VIM ftplugins designed for editing C++ files and C files.
You will found all the links for downloading the described files at the end of this part.
c_set.vim
defines many things that I found useful when
programming in C. They could be used for C++ and Java programming as well.
Features and Originalities
if
, else
, elif
(I know it is not
a C keyword, but else if
are), for
, while
, switch
and main
. if
in insert mode, it will automatically
expand into ... if () { }... in respect of the context. I.e.: within comments or strings (delimited by single or double quotes)
if
is not expanded. If keyword characters precede the typing,
if
is not expanded as well. Thus variables like
tarif
can be used without getting any headache.,if
wraps the selection within the curly brackets and
inserts if ()
just before. In
normal mode ,if
does the same thing under the consideration
that the selection is considered to be the current line under the cursor.
Actually, it is not ,if
but <LocalLeader>if
, with
maplocalleader
assigned by default to ,
.More precisely, regarding the value of the buffer relative option
b:usemarks
(cf. common_brackets.vim
), if
could be
expanded into :
if () { «» }«»
I do not present more precisely the other mappings alike. There are twin mappings !
misc_map.vim
defines two functions used in
every mapping and abbreviation of the language keywords ; it enables to
define context dependent macros.
common_bracket.vim
defines a set of mappings
that ease the management of brackets, quotes, parenthesis, etc..
These mappings are customized for C & C++ programming.
a.vim
very nice when we want to swap between .h, .inl and
.c and .cpp files. Could be found of sourceforge -- not critical regarding the mappings defined.
LoadHeaderFile.vim
very nice in order to go to included
files. Could be found of sourceforge -- not critical regarding the mappings defined.
Classical features :
cpp_set.vim
cpp_set.vim
:
c_set.vim
,
try
& catch
;
public:
, protected:
, private:
, template<>
, virtual
and std::
;
/*<space>
that expands into :/** */«»
/**
that expands
into : /** */«»
<c-x>be
that expands
algo(container_expr§)
into algo(container_expr.begin(),
container_expr.end()§)
, where §
represents the current
cursor position. cpp_options.vim
that is the ideal place for custom C++ preferences.
cpp_BuildTemplates.vim
defines my own template preferences regarding the format of comments and
classes -- many headers plus some doxygen constructs.
cpp_FindContextClass.vim
extracts the name of the class the cursor is within.
cpp_InsertAccessors.vim
defines certainly one of the two most useful features : an easy way to add
(to a class) new attributes and their accessors and mutators (get and set
methods).
cpp_GotoFunctionImpl.vim
defines the second most useful feature : a way to jump from the prototype to
the implementation of a function, and if the implementation has not beeing
defined yet, an empty implementation is inserted in the .cpp
file. This file is the ideal place for custom C++ preferences like the prefix and
postfix notations used for member data and their accessors, the file where
inlines should go, if accessors must be inlined, etc. ; this file is
used to configure cpp_BuildTemplates.vim
,
cpp_InsertAccessors.vim
and
cpp_GotoFunctionImpl.vim
.
Drop it into your $$/ftplugin/cpp/
directory ; it would be
share by all your projects. And if for a particular project other rules are
required, copy and specialize this file into every directory containing the
C++ source files.
cpp_BuildTemplates.vim
This file defines my own template preferences regarding the format of comments and classes -- many "rulers" plus some doxygen constructs. You may not be directly interested in this file ; moreover, it will evolve to take advantage of Gergely Kontra's mu-template. The commands are :
;HE <main class name>
which
produces a template for the header file dedicated to the specified class
name.
;CL <main class name>
which
produces a class template for specified class name.
;BL <title>
which produces three
lines of '='
around the title -- BigLines.
;GR <group title>
which inserts
a Doc++/Doxygen group -- GRoup.
;MGR <group title>
which inserts
a Doc++/Doxygen group plus a comment line -- MegaGRoup.
;RI <class name>
which moves the cursor to
the desired (regarding cpp_options.vim::g:inlinesPlace
)
location for the implementation of the inlines of the current class --
ReachInlines. The functions that the command calls are quite
complex and heavily rely on the Big Line format. So if you want to
change the format, you will have to adapt the function
Cpp_search4line()
and some other script variables. All the
underlying functions used by this command are very important for the
cpp_InsertAccessors.vim::AddAccessor
command. So, be very
careful in your customizations. cpp_BuildTemplates.vim
needs an old version of a.vim
.
cpp_FindContextClass.vim
Given a line number, the function (in this ftplugin) extracts the name of the most embedded class around the specified line.
A this time, the template are not managed.
For something like :
1: 2: class A 3: { 4: // ... 5: class B { 6: // ... 7: }; 8: // ... 9: class C { 10: // ... 11: }; 12: }; 13:The possible retrieved class names are : "" [1, 13], "A" [2-4,8,12], "A::B" [5-7] and "A::C" [9-11].
This script is required by cpp_InsertAccessors.vim
.
cpp_InsertAccessors.vim
This file defines an easy way to add new attributes, their accessors (get
methods) and their mutators (set methods) to a class.
I may use
"accessor" to also designate the "mutator".
The built accessors are moreover const and ref. correct in the sense
of the item 43 of Herb Sutter's Exceptional C++. BTW, if you extend
(with typedefs) the list of base types, you will have to extend the function
Cpp_IsBaseType()
.
To use this feature, place the cursor where you want your attributes
(internal data members) to be written to, then press ;AA
. Then
first, you will be asked for the type of the new attribute and its name. And
then, you will be asked whether you want :
Regarding the customization facilities, through
cpp_options.vim
you can :
m_Foo
(Microsoft's one), Foo_
(Herb Sutter's one),
_Foo
(the one you should forget about),
Foo
(the very lazy one), etc..
Foo
could be
"GetFoo()
", or "getFoo()
", or
"get_Foo()
" and so on. And the same for the set and
the reference accessors.
Here is a little example of header file and the corresponding inline file generated for :
g:implPlace=1
-- implemented as inlines ;
g:inlinesPlace=1
-- inlines in a separate file ;
int
attribute named
AnInteger
for which I asked for a get, a set and a reference
accessor.
CComplexThing
attribute named AComplexThing
for which I asked for a proxy-get (of type CT_Proxy
), a set and
a reference accessor ; Note : the inline and the definition files don't need to exist. They are generated automatically.
cpp_GotoFunctionImpl.vim
This file defines an easy way to jump from a function-prototype to its implementation ; if the implementation does exist yet, a default one is generated on the fly.
This ftplugin has been first inspired by Leif Wickland's VIM TIP#335 (on SF) and Robert Kelly IV's improvments. Then I added what I've been wanting to do for a longtime: a way to jump the implementation of a function.
Some other features :
mu-template
: i.e. it won't
complains or do any weird and undesired thing.
cpp_options.vim
and can be overriden
when invoking the command. The ftplugin defines :
:GIMPL
(GOTO_IMPL
) that
will provoke the jump ; The cursor must be placed between the line of the
function's name and the corresponding closing parenthesis.
<C-X>i
;
;GI
and <C-W>i
. imap <buffer> whateverIWant <Plug>GotoImpl nmap <buffer> whateverIWant <Plug>GotoImpl
Several options are available (idea from Robert Kelly IV) :
ShowVirtual
(0/1) : determines whether we want the
virtuality of the member function to be reminded along with the
implementation ;
ShowStatic
(0/1) : determines whether we want the
staticity of the function to be reminded ;
ShowDefaultParam
(0/1/2) : determines whether we want the
default value of the parameters to be remininded to us. cpp_options.vim
that can define the variables :
g:cpp_ShowVirtual
, g:cpp_ShowStatic
and
g:cpp_ShowDefaultParams
. :GIMPL
, we can specify values for these different options. The
format beeing :
<name_of_the_option>(\d\+\|on\|off)
. For instance
: :GIMPL ShowVirtualoff ShowDefaultParams2
.
c_set.vim <raw file> |
The main C ftplugin. It defines several settings plus a lot of
mappings for C keywords.
|
Requires : misc_map.vim , common_bracket.vim . Wants
a.vim and LoadHeaderFile.vim . | |
cpp_set.vim <raw file> |
The main C++ ftplugin. It defines some new mappings for C++
keywords, enhanced the comments management and loads all the other C++
plugins.
|
Requires : c_set.vim ,
and every following cpp_.*.vim . | |
cpp_options.vim <raw file> |
Designated place for customizing the policies of the following
files.
|
Needed by : cpp_BuildTemplates.vim and
cpp_InsertAccessors.vim . | |
cpp_BuildTemplates.vim <raw file> |
Defines some template preference for the formats of comments and
classes.
|
Requires : a.vim | |
parse-class.sed <raw file> |
Multi-line extraction of class names. | Used to be required by cpp_FindContextClass.vim | |
cpp_FindContextClass.vim <raw file> |
Extracts name of classes.
|
||
cpp_InsertAccessors.vim <raw file> |
Enables to easily add attributes and their accessors.
|
Requires : cpp_options.vim ,
cpp_BuildTemplates.vim ,
cpp_FindContextClass.vim , a.vim . | |
cpp_GotoFunctionImpl.vim <raw file> |
Defines a command and some mappings to jump from a function
prototype to its implementation ; if the implementation does exist yet,
a default one is generated on the fly.
|
Requires : cpp_options.vim ,
cpp_FindContextClass.vim , a.vim . | |
lh-cpp.tar.gz <raw file> |
The tarball containing all the previous files and a copy of the ones they rely on. | ||
a.vim | Commands to switch between source files and header files. | Maintained on sourceforce. | |
LoadHeaderFile.vim | Loads a file from a #include line. |
Maintained on sourceforce. | |
mu-template.vim Local copy: mu-template.vim <raw file> Archive with some templates: mu-template22.tar.gz |
Gergely Kontra's script. It permits to (automatically) load template
files. Little to little, I will use this plugin to replace some of my
cpp_BuildTemplates.vim 's functions | .
Maintained on sourceforce; Can takes advantage of searchInRuntime.vim |
Back to the ftplugings page | hermitte@free.fr |