macro: Robustify InsertIncludeGuardsWithoutEndif
authorStefan Huber <stefan.huber@fh-salzburg.ac.at>
Fri, 15 Oct 2021 13:58:29 +0000 (15:58 +0200)
committerStefan Huber <stefan.huber@fh-salzburg.ac.at>
Fri, 15 Oct 2021 13:58:29 +0000 (15:58 +0200)
Also replace - by a _ in the include guard.

macros.vim

index b33bf4da65992365e37c3886d23716cc993d43ce..d74406055ece5b04f6c1184bcbfe2f7c9340adfd 100644 (file)
@@ -78,9 +78,12 @@ 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
+  " Build name of guard: Take filename, replace some chars by _ and
+  " prepend a random sequence to make guard robust against file name
+  " collisions.
+  let guardname = substitute(expand("%:t"), "[\\.-]", "_", "g") . '_' . strpart(system('pwgen -c 16 1'), 0, 16)
+  execute "normal! i#ifndef " . guardname
+  execute "normal! o#define " . guardname
 endfunction