/
c.vim
101 lines (79 loc) · 2.63 KB
/
c.vim
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
setlocal tabstop=8
setlocal shiftwidth=8
setlocal noexpandtab
iabbr null NULL
nmap <buffer> gm :exe "Man ".expand("<cword>")<cr>
let b:outline_pattern = '^\k'
RunCommand !./a.out <args>
nnoremap <buffer> - :call <SID>ToggleAccessOperator()<cr>
xnoremap <buffer> - :call <SID>ToggleAccessOperator()<cr>
function! s:ToggleAccessOperator()
let saved_cursor = getpos('.')
let line = getline('.')
if line =~ '\k\+->\k'
s/\k\+\zs->\ze\k/./
elseif line =~ '\k\+\.\k'
s/\k\+\zs\.\ze\k/->/
endif
call setpos('.', saved_cursor)
endfunction
let s:type_pattern = '\%(\k\|*\|&\)\+'
let s:definition_pattern = '^'.s:type_pattern.'\s\+\(\k\+\)(.*$'
if !exists(':Implement')
command! Implement call s:Implement()
function! s:Implement()
let function_line = getline('.')
let function_name = substitute(function_line, s:definition_pattern, '\1', '')
let function_definition = substitute(function_line, ';$', ' {', '')
let nearby_function_name = ''
" First, check if there is a function declaration below, so we can put this
" one in the same place
if search(s:definition_pattern, 'W') > 0
let nearby_function_name = substitute(getline('.'), s:definition_pattern, '\1', '')
endif
" Go to the implementation file
A
" Position ourselves
normal! gg
if nearby_function_name != '' && search('^\s*'.s:type_pattern.'\s\+'.nearby_function_name, 'W') > 0
normal! gk
else
normal! Go
endif
" Add the skeleton of an implementation
call append(line('.'), [function_definition, '', '}', ''])
normal! gjgj
call feedkeys('cc')
endfunction
command! Update call s:Update()
function! s:Update()
let view = winsaveview()
try
if search(s:definition_pattern, 'Wb') <= 0
echohl WarningMsg | echomsg "Couldn't find a function around" | echohl NONE
return
endif
let function_line = getline('.')
let function_name = substitute(function_line, s:definition_pattern, '\1', '')
let function_declaration = substitute(function_line, '\s*{$', '', '').';'
" Go to the header file
A
" Find the function declaration
if search('^\s*'.s:type_pattern.'\s\+'.function_name, '') <= 0
echohl WarningMsg
echomsg "Couldn't find function in header. (Note: Should probably implement automatic adding)"
echohl NONE
A
return
endif
" Update!
call append(line('.'), function_declaration)
delete _
update
" Go back to the implementation
A
finally
call winrestview(view)
endtry
endfunction
endif