Vim Tips Wiki
mNo edit summary
 
(Middlemousebutton...)
Tags: Visual edit apiedit
 
(18 intermediate revisions by 10 users not shown)
Line 1: Line 1:
  +
{{TipImported
 
{{review}}
 
{{Tip
 
 
|id=514
 
|id=514
  +
|previous=510
|title=Automatic insertion of C/C__PLUS____PLUS__ header gates
 
  +
|next=515
|created=July 21, 2003 14:12
+
|created=July 21, 2003
 
|complexity=basic
 
|complexity=basic
 
|author=Morten Fjord-Larsen
 
|author=Morten Fjord-Larsen
 
|version=5.7
 
|version=5.7
 
|rating=63/30
 
|rating=63/30
  +
|category1=C
|text=
 
  +
|category2=C++
C/C++ header files should be guarded against multiple inclusions using preprocessor directives, e.g.:
 
  +
|category3=Templates
 
}}
 
C/C++ header files should be guarded against multiple inclusions using preprocessor directives, e.g.:
   
  +
<pre>
 
#ifndef FOO_H
 
#define FOO_H
   
 
/* Declarations. */
   
 
#endif
&#35;ifndef FOO_H
 
  +
</pre>
   
 
Placing the following snippet in your vimrc file, makes Vim insert these preprocessor gates automatically, when a new header file is created:
&#35;define FOO_H
 
   
  +
<pre>
  +
function! s:insert_gates()
 
let gatename = substitute(toupper(expand("%:t")), "\\.", "_", "g")
 
execute "normal! i#ifndef " . gatename
 
execute "normal! o#define " . gatename . " "
 
execute "normal! Go#endif /* " . gatename . " */"
 
normal! kk
 
endfunction
 
autocmd BufNewFile *.{h,hpp} call <SID>insert_gates()
  +
</pre>
   
 
==Comments==
  +
Here's my variation.
   
  +
Pull up foo.c or foo.h, hit your key sequence to trigger New_Class(), and bang, there's the class structure for you.
/* Declarations. */
 
   
  +
<pre>
  +
function New_Class_C(l_class_name, u_class_name)
  +
insert
  +
#include "l_class_name.h"
   
  +
u_class_name::u_class_name(
  +
)
  +
{
  +
;
  +
}
   
  +
u_class_name::~u_class_name(
&#35;endif
 
  +
)
  +
{
  +
;
  +
}
  +
.
  +
%s/l_class_name/\=a:l_class_name/g
  +
%s/u_class_name/\=a:u_class_name/g
  +
endfunction
   
  +
function New_Class_H(l_class_name, u_class_name)
  +
insert
  +
#if !defined(INCLUDED_u_class_name_H)
  +
#define INCLUDED_u_class_name_H
   
  +
class u_class_name {
  +
public:
  +
u_class_name();
  +
~u_class_name();
  +
};
   
  +
#else
Placing the following snippet in your .vimrc file, makes vim insert these preprocessor gates automatically, when a new header file is created:
 
   
  +
class u_class_name;
   
  +
#endif
  +
.
  +
%s/u_class_name/\=a:u_class_name/g
  +
endfunction
   
function! s:insert_gates()
+
function New_Class()
  +
let class_name = expand("%:r")
  +
let file_type = expand("%:e")
  +
let l_class_name = tolower(class_name)
  +
let u_class_name = toupper(class_name)
   
  +
if file_type =~# "c"
let gatename = substitute(toupper(expand("%:t")), "\\.", "_", "g")
 
  +
call New_Class_C(l_class_name, u_class_name)
  +
else
  +
call New_Class_H(l_class_name, u_class_name)
  +
endif
  +
endfunction
   
execute "normal i&#35;ifndef " . gatename
 
   
  +
</pre>
execute "normal o&#35;define " . gatename . " "
 
   
execute "normal Go&#35;endif /* " . gatename . " */"
 
   
  +
{{Todo}}
normal kk
 
  +
The following is from a proposed new tip that has now been deleted. What is required to make this work? It assumes you start with something?
   
  +
Here is an alternative, intended to produce:
endfunction
 
  +
<pre>
  +
#ifndef __FILENAME_H
  +
#define __FILENAME_H
  +
#endif //__FILENAME_H
  +
</pre>
   
  +
This will add this kind of block at the top of a new .h file at the press of a key. Add to your vimrc file:
   
  +
<pre>
  +
nnoremap <F12> "%phr_I#ifndef __<Esc>gUwyypldwidefine <Esc>yypldwiendif //<Esc>O<Esc>
  +
</pre>
   
  +
This is another alternative, also a mapping, intended to produce more verbosely-commented boilerplate but otherwise similar to the above:
autocmd BufNewFile *.{h,hpp} call &lt;SID&gt;insert_gates()
 
   
  +
<pre>
  +
:nnoremap <C-F12> "%phr_g0gUw<Esc>I#ifndef __<Esc>g$yiwo<Esc>pI# define <Esc>gg<Esc>A /* Guard against multiple
  +
header inclusion error */<Esc>ggjo<Esc>I#endif /* end if-not-def <Esc>pA */<Esc>2O<Esc>
  +
</pre>
  +
Produces when run on "Phoohaa.h" (must be in that file's working directory):
  +
<pre>
  +
#ifndef __PHOOHAA_H /* Guard against multiple header instance error */
  +
# define __PHOOHAA_H
   
}}
 
   
  +
#endif /* end if-not-def __PHOOHAA_H */
== Comments ==
 
  +
</pre>
<!-- parsed by vimtips.py in 0.274583 seconds-->
 
  +
  +
Please note however that using double underscores anywhere in your include guard names is not recommended, and symbols starting with a double underscore including defines are reserved for use by the compiler. So in the above example it is best to use:
  +
  +
<pre>
  +
#ifndef FILENAME_H
  +
#define FILENAME_H
  +
....
  +
  +
</pre>
  +
  +
For more information, see: http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier
  +
  +
=== Simplistic ===
  +
<pre>
  +
function g:MyAddGuard(s)<br>
  +
    call append(0, ["#ifndef " . a:s, "#define " . a:s, ""])
  +
   call append(line("$"), ["#endif /*" . a:s . "*/"])
  +
endfunction
  +
</pre>
  +
To add a guard to the current file, just do:
  +
<pre>:call MyAddGuard("MACRONAME")</pre>
  +
Choosing an appropriate name for the function will allow you to easily tab-complete this line.
  +
  +
See also https://github.com/drmikehenry/vim-headerguard/blob/master/plugin/headerguard.vim.
  +
----

Latest revision as of 09:53, 17 June 2015

Tip 514 Printable Monobook Previous Next

created July 21, 2003 · complexity basic · author Morten Fjord-Larsen · version 5.7


C/C++ header files should be guarded against multiple inclusions using preprocessor directives, e.g.:

#ifndef FOO_H
#define FOO_H

/* Declarations. */

#endif

Placing the following snippet in your vimrc file, makes Vim insert these preprocessor gates automatically, when a new header file is created:

function! s:insert_gates()
  let gatename = substitute(toupper(expand("%:t")), "\\.", "_", "g")
  execute "normal! i#ifndef " . gatename
  execute "normal! o#define " . gatename . " "
  execute "normal! Go#endif /* " . gatename . " */"
  normal! kk
endfunction
autocmd BufNewFile *.{h,hpp} call <SID>insert_gates()

Comments[]

Here's my variation.

Pull up foo.c or foo.h, hit your key sequence to trigger New_Class(), and bang, there's the class structure for you.

function New_Class_C(l_class_name, u_class_name)
   insert
#include "l_class_name.h"

u_class_name::u_class_name(
)
{
   ;
}

u_class_name::~u_class_name(
)
{
   ;
}
.
   %s/l_class_name/\=a:l_class_name/g
   %s/u_class_name/\=a:u_class_name/g
endfunction

function New_Class_H(l_class_name, u_class_name)
   insert
#if !defined(INCLUDED_u_class_name_H)
#define INCLUDED_u_class_name_H

class u_class_name {
public:
   u_class_name();
   ~u_class_name();
};

#else

class u_class_name;

#endif
.
   %s/u_class_name/\=a:u_class_name/g
endfunction

function New_Class()
   let class_name = expand("%:r")
   let file_type = expand("%:e")
   let l_class_name = tolower(class_name)
   let u_class_name = toupper(class_name)

   if file_type =~# "c"
      call New_Class_C(l_class_name, u_class_name)
   else
      call New_Class_H(l_class_name, u_class_name)
   endif
endfunction



 TO DO 
The following is from a proposed new tip that has now been deleted. What is required to make this work? It assumes you start with something?

Here is an alternative, intended to produce:

#ifndef __FILENAME_H
#define __FILENAME_H
#endif //__FILENAME_H

This will add this kind of block at the top of a new .h file at the press of a key. Add to your vimrc file:

nnoremap <F12> "%phr_I#ifndef __<Esc>gUwyypldwidefine <Esc>yypldwiendif //<Esc>O<Esc>

This is another alternative, also a mapping, intended to produce more verbosely-commented boilerplate but otherwise similar to the above:

:nnoremap <C-F12> "%phr_g0gUw<Esc>I#ifndef __<Esc>g$yiwo<Esc>pI#  define <Esc>gg<Esc>A /* Guard against multiple
 header inclusion error */<Esc>ggjo<Esc>I#endif /* end if-not-def <Esc>pA */<Esc>2O<Esc>

Produces when run on "Phoohaa.h" (must be in that file's working directory):

#ifndef __PHOOHAA_H /* Guard against multiple header instance error */
#  define __PHOOHAA_H


#endif /* end if-not-def __PHOOHAA_H */

Please note however that using double underscores anywhere in your include guard names is not recommended, and symbols starting with a double underscore including defines are reserved for use by the compiler. So in the above example it is best to use:

#ifndef FILENAME_H
#define FILENAME_H
....

For more information, see: http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier

Simplistic[]

function g:MyAddGuard(s)<br>
    call append(0, ["#ifndef " . a:s, "#define " . a:s, ""])
    call append(line("$"), ["#endif /*" . a:s . "*/"])
endfunction

To add a guard to the current file, just do:

:call MyAddGuard("MACRONAME")

Choosing an appropriate name for the function will allow you to easily tab-complete this line.

See also https://github.com/drmikehenry/vim-headerguard/blob/master/plugin/headerguard.vim.