vim: add include guards to C/C++ header files
[shutils.git] / dotfiles / vim / .vimrc
index a68602e9d83d09117f284bd94698d742a1ef8110..5da54996af79d7730b9e6dfb1fec05781fb8b839 100644 (file)
@@ -168,6 +168,19 @@ function EscapeHexToChar()
     execute '%s/^\(.*\)$/"\1"/'
 endfunction
 
+function InsertIncludeGuardsWithoutEndif()
+       let gatename = substitute(expand("%:t"), "\\.", "_", "g") . '_' . strpart(system('pwgen -c 16 1'), 0, 16)
+       execute "normal! i#ifndef " . gatename
+       execute "normal!   o#define " . gatename
+endfunction
+
+function AddIncludeGuards()
+       execute "normal! Go#endif"
+       execute "normal! gg"
+       call InsertIncludeGuardsWithoutEndif()
+endfunction
+
+autocmd BufNewFile *.{h,hpp} call AddIncludeGuards()
 
 
 """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""