-
Notifications
You must be signed in to change notification settings - Fork 6
/
Keybinds.vim
305 lines (275 loc) · 13.5 KB
/
Keybinds.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
function! autopairs#Keybinds#IgnoreInsertEnter(f) abort
" TODO: Change this to use <cmd> when support for vim 8.2.19xx is dropped
let l:pre = "\<C-r>=autopairs#Keybinds#SetEventignore()\<CR>"
let l:val = call(function(a:f), a:000)
let l:post = "\<C-r>=autopairs#Keybinds#ResetEventignore()\<CR>"
return l:pre . l:val . l:post
endfunction
function! autopairs#Keybinds#IgnoreInsertEnterCmd(cmd) abort
call autopairs#Keybinds#SetEventignore()
normal a:cmd
call autopairs#Keybinds#ResetEventignore()
return ''
endfunction
function! autopairs#Keybinds#SetEventignore()
" TODO: Add InsertLeavePre when we know how to check version correctly on nvim
" or when support for vim 8.2.1873 and below is dropped
set eventignore+=InsertEnter,InsertLeave
return ''
endfunction
function! autopairs#Keybinds#ResetEventignore()
set eventignore-=InsertEnter,InsertLeave
return ''
endfunction
" Always silent the command
inoremap <silent> <SID>AutoPairsReturn <C-r>=autopairs#Keybinds#IgnoreInsertEnter('autopairs#AutoPairsReturn')<cr>
imap <Plug>AutoPairsReturn <SID>AutoPairsReturn
func! autopairs#Keybinds#ExpandMap(map)
let map = a:map
let map = substitute(map, '\(<Plug>\w\+\)', '\=maparg(submatch(1), "i")', 'g')
let map = substitute(map, '\(<Plug>([^)]*)\)', '\=maparg(submatch(1), "i")', 'g')
return map
endf
fun! autopairs#Keybinds#mapPairKeybinds()
for [open, close] in items(b:AutoPairs)
let o = autopairs#Strings#GetLastUnicodeChar(open)
" We wanna proxy the string value of close so we can start converting
" to a different format. There's _way_ too many formats, admittedly,
" but this means we can sort shit into opt instead, which is already
" present in the system. This also means close gets a canonical
" meaning, and we don't need to rewrite other bits of the code to
" add a proxy for something we already have.
let stringClose = ""
if type(close) == v:t_dict
if !has_key(close, "close")
" Let's silently make sure we have a close.
let close["close"] = ""
endif
" Objects store it in a key
let stringClose = close["close"]
else
" Strings store it in itself, for obvious reasons.
let stringClose = close
endif
" This line right here is part of why we filter it out this early.
let c = autopairs#Strings#GetFirstUnicodeChar(stringClose)
" TODO: link some global options against (some of) these
let opt = {'mapclose': 1,
\ 'alwaysmapdefaultclose': 1,
\ 'delete': 1, 'multiline': 1,
\ 'passiveclose': 1,
\ 'balancebyclose': 0 }
" Default: set key = c
let opt['key'] = c
if o == c || len(c) == 0
let opt['multiline'] = 0
elseif type(close) == v:t_dict && has_key(close, 'multiline')
let opt['multiline'] = close['multiline']
endif
if type(close) == v:t_dict
" We have a brand fucking new object!
" Let's handle mappings first
if (has_key(close, "mapclose"))
let mc = close["mapclose"]
if type(mc) == v:t_number
let opt["mapclose"] = mc
else
let opt["key"] = mc
" This is largely a compat util; if the key is empty, it's
" equivalent to setting it to 0
if (mc != "")
let opt["mapclose"] = 1
else
let opt["mapclose"] = 0
endif
endif
endif
" We've handled multiline earlier, so we only need to handle
" delete.
" Filetype is only handled in intialization methods (and is purely
" syntactic sugar for using a different variable), and therefore
" isn't used here.
if has_key(close, "delete")
let opt["delete"] = close["delete"]
endif
let opt["alwaysmapdefaultclose"] = get(close, 'alwaysmapdefaultclose', 1)
let opt["passiveclose"] = get(close, "passiveclose", 1)
let opt["balancebyclose"] = get(close, "balancebyclose", 0)
endif
call autopairs#AutoPairsMap(o)
if o != c && c != '' && opt['mapclose']
if opt["key"] != c && opt["alwaysmapdefaultclose"]
call autopairs#AutoPairsMap(c)
endif
call autopairs#AutoPairsMap(opt["key"], opt["key"] != c && opt["passiveclose"])
end
" Krasjet: add any non-string closing characters to a list
let b:AutoPairsList += [[open, stringClose, opt]]
" What in the fuck is this?
" This is arguably Krasjet's least documented feature. Figure out what
" it does pl0x
if stringClose !=? '' && stringClose !~# '\V\[' .. escape(join(b:AutoPairsQuoteClosingChar, ''), '\') .. ']'
let b:autopairs_next_char_whitelist += [escape(stringClose, '\')]
end
endfor
" sort pairs by length, longer pair should have higher priority
let b:AutoPairsList = sort(b:AutoPairsList, "autopairs#Strings#sortByLength")
" Krasjet: add whitelisted strings to the list
for str in b:AutoPairsNextCharWhitelist
let b:autopairs_next_char_whitelist += [escape(str,'\')]
endfor
" Krasjet: construct a regex for whitelisted strings
if empty(b:autopairs_next_char_whitelist)
let b:autopairs_next_char_whitelist = '^$'
else
let b:autopairs_next_char_whitelist = '^\V\(' . join(b:autopairs_next_char_whitelist, '\|') . '\)'
endif
" Krasjet: add blacklisted open strings to the list
let b:autopairs_open_blacklist = []
for str in b:AutoPairsOpenBalanceBlacklist
let b:autopairs_open_blacklist += [escape(str,'\')]
endfor
if empty(b:autopairs_open_blacklist)
let b:autopairs_open_blacklist = '^$'
else
let b:autopairs_open_blacklist = '\V\('.join(b:autopairs_open_blacklist, '\|') . '\)'
endif
for item in b:AutoPairsList
let [open, close, opt] = item
" Note to self: this is the bit that's responsible for checking
" whether a single-quote is in a word or not.
" Olivia: altered to allow three different modes, to prevent issues
" with things like string types in some languages (Python)
" where the programmers either can't use anything but single
" quotes, or (ew) decide to use single-quotes when
" double-quotes are possible
if open == "'" && open == close
if b:AutoPairsSingleQuoteMode == -1
let item[0] = '\v\zs'''
elseif b:AutoPairsSingleQuoteMode == 0
let item[0] = '\v(' . b:AutoPairsSingleQuotePrefixGroup . ')\zs'''
elseif b:AutoPairsSingleQuoteMode == 1
let item[0] = '\v(' . b:AutoPairsSingleQuotePrefixGroup . ')\w?\zs'''
elseif b:AutoPairsSingleQuoteMode == 2
" Note that g:AutoPairsSingleQuoteExpandFor is a separate
" group to make sure prefix conditions still hold. This means
" it still works for normal characters, and shouldn't expand
" for i.e. blahf'
" Largely quality of life; can be worked around with
" |b:AutoPairsSingleQuotePrefixGroup| and mode == 0 if other
" behavior is desired.
let item[0] = '\v(' . b:AutoPairsSingleQuotePrefixGroup . ')[' . b:AutoPairsSingleQuoteExpandFor . ']?\zs'''
else
echoerr 'Invalid b:AutoPairsSingleQuoteMode: ' . b:AutoPairsSingleQuoteMode
\ . ". Only -1, 0, 1, and 2 are allowed values."
endif
let opt["balancebyclose"] = 1
end
endfor
endfun
fun! autopairs#Keybinds#mapKeys()
" TODO: decode this comment
" for auto-pairs starts with 'a', so the priority is higher than supertab and vim-endwise
"
" vim-endwise doesn't support <Plug>AutoPairsReturn
" when use <Plug>AutoPairsReturn will cause <Plug> isn't expanded
"
" supertab doesn't support <SID>AutoPairsReturn
" when use <SID>AutoPairsReturn will cause Duplicated <CR>
"
" and when load after vim-endwise will cause unexpected endwise inserted.
" so always load AutoPairs at last
" Buffer level keys mapping
" comptible with other plugin
if b:AutoPairsMapCR
if v:version == 703 && has('patch32') || v:version > 703
" VIM 7.3 supports advancer maparg which could get <expr> info
" then auto-pairs could remap <CR> in any case.
let info = maparg(g:AutoPairsCRKey, 'i', 0, 1)
if empty(info)
" Not _entirely_ sure if this should be <CR> or
" g:AutoPairsCRKey.
let old_cr = '<CR>'
let is_expr = 0
else
let old_cr = info['rhs']
let old_cr = autopairs#Keybinds#ExpandMap(old_cr)
let old_cr = substitute(old_cr, '<SID>', '<SNR>' . info['sid'] . '_', 'g')
let is_expr = info['expr']
let wrapper_name = '<SID>AutoPairsOldCRWrapper73'
endif
else
" VIM version less than 7.3
" the mapping's <expr> info is lost, so guess it is expr or not, it's
" not accurate.
let old_cr = maparg(b:AutoPairsCRKey, 'i')
if old_cr == ''
let old_cr = '<CR>'
let is_expr = 0
else
let old_cr = autopairs#Keybinds#ExpandMap(old_cr)
" old_cr contain (, I guess the old cr is in expr mode
let is_expr = old_cr =~ '\V(' && toupper(old_cr) !~ '\V<C-R>'
" The old_cr start with " it must be in expr mode
let is_expr = is_expr || old_cr =~ '\v^"'
let wrapper_name = '<SID>AutoPairsOldCRWrapper'
endif
endif
if old_cr !~ 'AutoPairsReturn'
if is_expr
" remap <expr> to `name` to avoid mix expr and non-expr mode
execute 'inoremap <buffer> <expr> <script> '. wrapper_name . ' ' . old_cr
let old_cr = wrapper_name
end
" Always silent mapping
execute 'imap <script> <buffer> <silent> ' . b:AutoPairsCRKey . ' ' .old_cr.'<SID>AutoPairsReturn'
endif
endif
if b:AutoPairsMoveExpression != ""
for key in split(b:AutoPairsMoveCharacter, '\s*')
let escaped_key = substitute(key, "'", "''", 'g')
execute 'inoremap <silent> <buffer> ' . substitute(b:AutoPairsMoveExpression, "%key", key, "") . " <C-R>=autopairs#Keybinds#IgnoreInsertEnter('autopairs#AutoPairsMoveCharacter', '".escaped_key."')<CR>"
endfor
endif
" Still use <buffer> level mapping for <BS> <SPACE>
if b:AutoPairsMapBS
" Use <C-R> instead of <expr> for issue #14 sometimes press BS output strange words
execute 'inoremap <buffer> <silent> <BS> <C-R>=autopairs#AutoPairsDelete()<CR>'
end
if b:AutoPairsMapSpace
" Try to respect abbreviations on a <SPACE>
let do_abbrev = ""
" neovim appears to set v:version to 800, so it should be compatible
" with this.
" Admittedly, probably not compatible with the same version checks,
" but hey, it's fine.
if v:version == 703 && has("patch489") || v:version > 703
let do_abbrev = "<C-]>"
endif
execute 'inoremap <buffer> <silent> <SPACE> ' . do_abbrev . '<C-R>=autopairs#AutoPairsSpace()<CR>'
end
if b:AutoPairsShortcutFastWrap != ''
execute 'inoremap <buffer> <silent> ' . b:AutoPairsShortcutFastWrap . ' <C-R>=autopairs#AutoPairsFastWrap()<CR>'
end
if b:AutoPairsFlyMode && b:AutoPairsShortcutBackInsert != ''
execute 'inoremap <buffer> <silent> ' . b:AutoPairsShortcutBackInsert . ' <C-R>=autopairs#AutoPairsBackInsert()<CR>'
end
if b:AutoPairsShortcutToggle != ''
" use <expr> to ensure showing the status when toggle
execute 'inoremap <buffer> <silent> <expr> ' . b:AutoPairsShortcutToggle . ' autopairs#AutoPairsToggle()'
execute 'noremap <buffer> <silent> ' . b:AutoPairsShortcutToggle . ' :call autopairs#AutoPairsToggle()<CR>'
end
if b:AutoPairsShortcutToggleMultilineClose != ''
execute 'inoremap <buffer> <silent> <expr> ' . b:AutoPairsShortcutToggleMultilineClose . ' autopairs#AutoPairsToggleMultilineClose()'
execute 'noremap <buffer> <silent> ' . b:AutoPairsShortcutToggleMultilineClose . ' :call autopairs#AutoPairsToggleMultilineClose()<CR>'
endif
if b:AutoPairsShortcutJump != ''
" execute 'inoremap <buffer> <silent> ' . b:AutoPairsShortcutJump . ' <cmd>set eventignore+=InsertEnter,InsertLeavePre,InsertLeave<CR><ESC>:call autopairs#AutoPairsJump()<CR>a<cmd>set eventignore-=InsertEnter,InsertLeavePre,InsertLeave<CR>'
execute 'inoremap <buffer> <silent> ' . b:AutoPairsShortcutJump . ' <C-r>=autopairs#Keybinds#IgnoreInsertEnterCmd("<ESC>:call autopairs#AutoPairsJump()<CR>a")'
execute 'inoremap <buffer> <silent> ' . b:AutoPairsShortcutJump . ' <cmd>call autopairs#AutoPairsJump()<CR>'
execute 'noremap <buffer> <silent> ' . b:AutoPairsShortcutJump . ' :call autopairs#AutoPairsJump()<CR>'
end
if b:AutoPairsShortcutIgnore != ''
execute 'inoremap <buffer> <silent> ' .. b:AutoPairsShortcutIgnore .. ' <C-r>=autopairs#AutoPairsIgnore()<cr>'
end
endfun