-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.vim
More file actions
57 lines (47 loc) · 1.39 KB
/
plugin.vim
File metadata and controls
57 lines (47 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
" Entry point for the plugin
function! FileNotEmpty(name)
echom a:name
if(!filereadable(a:name))
return 0
endif
let l:data = readfile(a:name)
for d in data
if(d == '')
continue
endif
return 1
endfor
return 0
endfunction
function! TryHeaderInsertion(file_path, file_name)
if(FileNotEmpty(a:file_path))
return
endif
let l:file_parts = split(a:file_name, "[.]")
if(len(l:file_parts) > 1 && file_parts[len(l:file_parts)-1][0] == 'h')
call append(0,"#ifndef " . toupper(l:file_parts[0]) . "_H_DEFINE")
call append(1,"#define " . toupper(l:file_parts[0]) . "_H_DEFINE")
call append(3,"#endif")
endif
endfunction
function! MakeMain()
execute "normal! G"
call setline('.', ["#include <iostream>", "","using namespace std;","","int main(int argc, char** argv) {", "", "\treturn 0;", "", "}"])
endfunction
function TryMainInsertion(file_path, file_name)
if(FileNotEmpty(a:file_path))
return
endif
if(tolower(a:file_name) == "main.cpp")
call MakeMain()
endif
endfunction
function! CheckForNewBuffer()
augroup MyGroup
autocmd!
autocmd BufRead,BufNewFile main.cpp call TryMainInsertion(expand("%:p"), expand("%:t"))
autocmd BufRead,BufNewFile *.h* call TryHeaderInsertion(expand("%:p"), expand("%:t"))
augroup END
endfunction
command! Main : if confirm("Are you sure you want a main function?") | call MakeMain() | endif
call CheckForNewBuffer()