-
Notifications
You must be signed in to change notification settings - Fork 7
/
keybinding-helpers.vim
199 lines (179 loc) Β· 7.67 KB
/
keybinding-helpers.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
"
" Helper functions for setting up key bindings and groups
"
" Cleaned up API.
command! -nargs=+ -bar SpBind call SpaceNeovimBufBind(<args>)
command! -nargs=+ -bar SpMap call SpaceNeovimBufMap(<args>)
command! -nargs=+ -bar SpNMap call SpaceNeovimBufNMap(<args>)
command! -nargs=+ -bar SpVMap call SpaceNeovimBufVMap(<args>)
command! -nargs=+ -bar SpFileTypeBind call SpaceNeovimFTBind(<args>)
command! -nargs=+ -bar SpFileTypeMap call SpaceNeovimFTMap(<args>)
command! -nargs=+ -bar SpFileTypeNMap call SpaceNeovimFTNMap(<args>)
" FIXME: Currently these do not take effect, so they are disabled for now.
command! -nargs=+ -bar SpFileTypeGroup call SpaceNeovimFileTypeGroup(<args>)
command! -nargs=+ -bar SpFileTypeGroup2 call SpaceNeovimFileTypeGroup2(<args>)
command! -nargs=+ -bar SpFileTypeGroup3 call SpaceNeovimFileTypeGroup3(<args>)
""
" Debug messages to the console.
"
function! s:debug(msg)
if g:dotspaceneovim_layer_debug
echom a:msg
endif
endfunction
" NOTE: Use `SpaceNeovimBufBind` instead since this section is currently broken.
" See https://github.com/Tehnix/spaceneovim/issues/23 for more info.
" Mapping functions for normal commands {{{
function! SpaceNeovimBind(key_map, binding, name, value, isCmd)
call s:debug('+++ Adding mapping to key ' . a:binding . ' for cmd: ' . a:name)
" Register keybindings for the various modes.
if a:isCmd
let l:value = ':' . a:value . '<cr>'
else
let l:value = a:value
endif
if a:key_map ==# 'map' && maparg('<Leader>' . a:binding, '') ==# ''
let l:noremap = 'noremap'
elseif a:key_map ==# 'nmap' && maparg('<Leader>' . a:binding, 'n') ==# ''
let l:noremap = 'nnoremap'
elseif a:key_map ==# 'vmap' && maparg('<Leader>' . a:binding, 'v') ==# ''
let l:noremap = 'vnoremap'
elseif a:key_map ==# 'tmap' && maparg('<Leader>' . a:binding, 't') ==# ''
let l:noremap = 'tnoremap'
else
let l:noremap = ''
endif
if l:noremap !=# ''
execute l:noremap . ' <silent> <SID>' . a:name . '# ' . l:value
execute a:key_map . ' <Leader>' . a:binding . ' <SID>' . a:name . '#'
endif
endfunction
function! SpaceNeovimMap(binding, name, value, ...)
" Convenience function for SpaceNeovimBind using map/noremap.
let l:isCmd = 1
if a:0 > 0
let l:isCmd = a:1
endif
call SpaceNeovimBind('map', a:binding, a:name, a:value, l:isCmd)
endfunction
function! SpaceNeovimNMap(binding, name, value, ...)
" Convenience function for SpaceNeovimBind using nmap/nnoremap.
let l:isCmd = 1
if a:0 > 0
let l:isCmd = a:1
endif
call SpaceNeovimBind('nmap', a:binding, a:name, a:value, l:isCmd)
endfunction
" }}}
" Mapping functions for file type specific commands, on `au VimEnter` {{{
function! SpaceNeovimBufBind(map, binding, name, value, isCmd)
call s:debug('+++ Adding BufEnter mapping to key ' . a:binding . ' for cmd: ' . a:name)
" Register keybindings for a specific filetype for the various modes.
if a:isCmd
let l:value = ':' . a:value . '<cr>'
else
let l:value = a:value
endif
if a:map ==# 'map' && maparg('<Leader>' . a:binding, '') ==# ''
let l:noremap = 'noremap'
elseif a:map ==# 'nmap' && maparg('<Leader>' . a:binding, 'n') ==# ''
let l:noremap = 'nnoremap'
elseif a:map ==# 'vmap' && maparg('<Leader>' . a:binding, 'v') ==# ''
let l:noremap = 'vnoremap'
elseif a:map ==# 'tmap' && maparg('<Leader>' . a:binding, 't') ==# ''
let l:noremap = 'tnoremap'
else
let l:noremap = ''
endif
if l:noremap !=# ''
execute 'au VimEnter * ' . l:noremap . ' <SID>' . a:name . '# ' . l:value
execute 'au VimEnter * ' . a:map . ' <Leader>' . a:binding . ' <SID>' . a:name . '#'
endif
endfunction
function! SpaceNeovimBufMap(binding, name, value, ...)
" Convenience function for SpaceNeovimBind using map/noremap.
let l:isCmd = 1
if a:0 > 0
let l:isCmd = a:1
endif
call SpaceNeovimBufBind('map', a:binding, a:name, a:value, l:isCmd)
endfunction
function! SpaceNeovimBufNMap(binding, name, value, ...)
" Convenience function for SpaceNeovimBind using nmap/nnoremap.
let l:isCmd = 1
if a:0 > 0
let l:isCmd = a:1
endif
call SpaceNeovimBufBind('nmap', a:binding, a:name, a:value, l:isCmd)
endfunction
function! SpaceNeovimBufVMap(binding, name, value, ...)
" Convenience function for SpaceNeovimBind using vmap/vnoremap.
let l:isCmd = 1
if a:0 > 0
let l:isCmd = a:1
endif
call SpaceNeovimBufBind('vmap', a:binding, a:name, a:value, l:isCmd)
endfunction
" }}}
" Mapping functions for file type specific commands {{{
function! SpaceNeovimFTBind(ft, map, binding, name, value, isCmd)
call s:debug('-+- Adding FT mapping to key ' . a:binding . ' for cmd: ' . a:name . ' for file type ' . a:ft)
" Register keybindings for a specific filetype for the various modes.
if a:isCmd
let l:value = ':' . a:value . '<cr>'
else
let l:value = a:value
endif
if a:map ==# 'map' && maparg('<Leader>' . a:binding, '') ==# ''
let l:noremap = 'noremap'
elseif a:map ==# 'nmap' && maparg('<Leader>' . a:binding, 'n') ==# ''
let l:noremap = 'nnoremap'
elseif a:map ==# 'vmap' && maparg('<Leader>' . a:binding, 'v') ==# ''
let l:noremap = 'vnoremap'
elseif a:map ==# 'tmap' && maparg('<Leader>' . a:binding, 't') ==# ''
let l:noremap = 'tnoremap'
else
let l:noremap = ''
endif
if l:noremap !=# ''
execute 'au VimEnter * if &ft ==? "' . a:ft . '" | ' . l:noremap . ' <buffer> <SID>' . a:name . '# ' . l:value . ' | endif '
execute 'au VimEnter * if &ft ==? "' . a:ft . '" | ' . a:map . ' <buffer> <Leader>' . a:binding . ' <SID>' . a:name . '# | endif '
execute 'au FileType ' . a:ft . ' ' . l:noremap . ' <buffer> <SID>' . a:name . '# ' . l:value
execute 'au FileType ' . a:ft . ' ' . a:map . ' <buffer> <Leader>' . a:binding . ' <SID>' . a:name . '#'
endif
endfunction
function! SpaceNeovimFTMap(tfile, binding, name, value, ...)
" Convenience function for SpaceNeovimBind using map/noremap.
let l:isCmd = 1
if a:0 > 0
let l:isCmd = a:1
endif
call SpaceNeovimFTBind(a:tfile, 'map', a:binding, a:name, a:value, l:isCmd)
endfunction
function! SpaceNeovimFTNMap(tfile, binding, name, value, ...)
" Convenience function for SpaceNeovimBind using nmap/nnoremap.
let l:isCmd = 1
if a:0 > 0
let l:isCmd = a:1
endif
call SpaceNeovimFTBind(a:tfile, 'nmap', a:binding, a:name, a:value, l:isCmd)
endfunction
" }}}
" Grouping functions for file type groups (under SPC m) {{{
function! SpaceNeovimFileTypeGroup(ft, group, name)
" Register keybindings for a specific filetype for the various modes.
" execute 'au VimEnter * if &ft ==? "' . a:ft . '" | let g:lmap.m.' . a:group . ' = { "name": "' . a:name . '" } | endif '
" execute 'au FileType ' . a:ft . ' let g:lmap.m.' . a:group . ' = { "name": "' . a:name . '" } '
execute 'au FileType ' . a:ft . ' let g:lmap.m.' . a:group . ' = get(g:lmap, "' . a:group . '", { "name": "' . a:name . '" })'
endfunction
function! SpaceNeovimFileTypeGroup2(ft, group1, group2, name)
" Register keybindings for a specific filetype for the various modes.
execute 'au VimEnter * if &ft ==? "' . a:ft . '" | let g:lmap.m.' . a:group1 . '.' . a:group2 . ' = { "name": "' . a:name . '" } | endif '
execute 'au BufEnter * if &ft ==? "' . a:ft . '" | let g:lmap.m.' . a:group1 . '.' . a:group2 . ' = { "name": "' . a:name . '" } | endif '
endfunction
function! SpaceNeovimFileTypeGroup3(ft, group1, group2, group3, name)
" Register keybindings for a specific filetype for the various modes.
execute 'au VimEnter * if &ft ==? "' . a:ft . '" | let g:lmap.m.' . a:group1 . '.' . a:group2 . '.' . a:group3 . ' = { "name": "' . a:name . '" } | endif '
execute 'au BufEnter * if &ft ==? "' . a:ft . '" | let g:lmap.m.' . a:group1 . '.' . a:group2 . '.' . a:group3 . ' = { "name": "' . a:name . '" } | endif '
endfunction
" }}}