-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautocomp.vim
436 lines (395 loc) · 14.2 KB
/
autocomp.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
" Vim global plugin for auto-completion
" Last Change: 2010 Oct 22
" Maintainer: André Luiz dos Santos <[email protected]>
" License: This file is placed in the public domain.
"{{{ Documentation
"
" Installation
"
" Copy this file to the VIM plugin's directory.
" On Windows, this would be: ...\vimfiles\plugin\.
" On Linux: ~/.vim/plugin/.
"
" Usage
"
" Press F5 on VIM instances where you want the auto-complete feature on, and
" wait for the auto-complete window to appear.
" You can choose another key with the following in your VIM rc file:
" nmap <unique> <...> <Plug>AutocompStart
"
" Type the first letter of the word that you want. If the desired word appears
" on the auto-complete window, type its number. Otherwise, type the second
" letter of the word that you want. And so on.
" If you want to type the number and not auto-complete, type the number twice.
"
" Numbers above 10 are typed with the help of the Alt key. Example:
" 10 => Alt-0, 11 => Alt-1, etc.
"
" TODO / Possibly needs fixing
"
" If this plug-in ever gains a second user, making it a bit more configurable
" might be necessary.
"
" I think that the following code may not be a good idea, since other plug-ins
" may also set it or depend on its default value:
" setlocal updatetime=100
"
" Figure out how to invoke VIM's auto-complete functions manually.
" Making the code smarter about what possible auto-completions to present
" would be nice, too.
"
"}}}
"{{{ Initialization
if exists('loaded_autocomp')
finish
endif
let loaded_autocomp = 1
" This variable is used inside pattern brackets.
" If you want to match a close square bracket ( ] ), it must be the first character.
" Backslashes must be doubled.
let s:letters = '_a-zA-Z'
" Only two values are valid for this variable: 0 and 1.
" Switch between them and see which mode you like best.
let s:programmerMode = 1
" Search this many lines, above and below the current line, for possible
" auto-completions.
let s:scanRange = 200
highlight AutoCompCommonPrefix term=inverse cterm=inverse gui=inverse
if !hasmapto('<Plug>AutocompStart')
map <unique> <F5> <Plug>AutocompStart
endif
noremap <unique> <script> <Plug>AutocompStart <SID>Start
noremap <SID>Start :call <SID>StartPlugin()<CR>
" Everything below this point is script specific.
function s:StartPlugin()
" Create the AutoComplete buffer if it doesn't exist yet.
if !bufexists('AutoComplete')
call s:CreateWindow()
augroup autocomp
autocmd!
autocmd CursorMovedI * call <SID>CursorMovedIEvent()
autocmd CursorHoldI * call <SID>CursorHoldIEvent()
autocmd BufEnter * call <SID>BufEnterEvent()
augroup END
call s:BufEnterEvent()
endif
endfunction
function s:CreateWindow()
" Create the AutoComplete buffer.
silent vertical leftabove 25new AutoComplete
setlocal buftype=nofile noswapfile nonumber nowrap winfixwidth
""nobuflisted
call matchadd('AutoCompCommonPrefix', '\v^-\[.*\]-*$')
" Go back to the previous window.
wincmd p
endfunction
"}}}
"{{{ Word list
" Find words beginning with 'filter' on 'lnum'.
function s:CountWordsInLine(words, filter, lnum)
for l:word in split(getline(a:lnum), '\V\[^' . s:letters . ']\+')
" Pay attention to the pair of consecutive dots in the following line. It
" means that the filter must be followed by at least two characters to
" match.
if match(l:word, '\V\^' . a:filter . '\.\.') >= 0
let a:words[l:word] = get(a:words, l:word, 0) + 1
endif
endfor
endfunction
" Find words beginning with 'filter' in the current buffer.
" Up to ('scanRange' * 2 + 1) lines are scanned for words.
function s:CountWordsInBuffer(words, filter)
let l:curLine = line('.')
call s:CountWordsInLine(a:words, a:filter, l:curLine)
for index in range(1, s:scanRange)
call s:CountWordsInLine(a:words, a:filter, l:curLine - l:index)
call s:CountWordsInLine(a:words, a:filter, l:curLine + l:index)
if len(a:words) > 20
" Enough words found.
return
endif
endfor
endfunction
function s:WordSortAlg(a, b)
return a:b[1] - a:a[1]
endfunction
" Store 20 words for auto-completion in b:autocomp.words.
function s:UpdateWords(filter)
let l:words = {}
call s:CountWordsInBuffer(l:words, a:filter)
" Create a sorted list of the 20 most commonly used words. {{{
" items() ->
" [ ['word', 3], ['wand', 5] ]
" 1st sort() ->
" [ ['wand', 5], ['word', 3] ]
" map() ->
" [ 'wand', 'word' ]
" 2nd sort()
" }}}
let b:autocomp.words = sort(map(sort(items(l:words), 's:WordSortAlg')[:20-1], 'v:val[0]'))
endfunction
function s:FindCommonPrefixes()
if s:programmerMode == 0
" Find the most commonly used prefixes.
let l:prefix = {} " dict value = use count.
for w in b:autocomp.words
for i in range(1, len(l:w) - len(b:autocomp.curWord))
let l:key = strpart(l:w, len(b:autocomp.curWord), l:i)
let l:prefix[l:key] = get(l:prefix, l:key, 0) + 1
endfor
endfor
" Do not even consider prefixes not used this many times.
let l:plist = reverse(sort(map(filter(items(l:prefix), 'v:val[1] >= 2'), 'v:val[0]')))
if len(l:plist) == 0
" A regular expression that will never match.
return '\V\^\$'
else
return '\V\^' . b:autocomp.curWord . '\(' . join(l:plist, '\|') . '\)\(\.\+\)\$'
endif
elseif s:programmerMode == 1
let l:prefixList = []
let l:curWordSize = len(b:autocomp.curWord)
for prefix in [ 'no', 'auto', 'end', 'get', 'set' ]
if strpart(l:prefix, 0, l:curWordSize) == b:autocomp.curWord
let l:prefixList += [strpart(l:prefix, len(b:autocomp.curWord))]
endif
endfor
for word in b:autocomp.words
for [pattern, offset] in [ ['\v._.', 1], ['\v[a-z][A-Z]', 1] ]
let l:startAt = 0
for dummy in range(4)
let l:index = match(l:word, l:pattern, l:startAt)
if l:index == -1
break
endif
let l:index += l:offset
let l:prefixList += [strpart(l:word, l:curWordSize, l:index - l:curWordSize)]
let l:startAt = l:index + 1
endfor
endfor
endfor
return '\V\^' . b:autocomp.curWord . '\(' . join(l:prefixList, '\|') . '\)\(\.\+\)\$'
endif
endfunction
function s:ClearWords()
let b:autocomp.words = []
""call s:UnmapAutoCompleteKeys()
call s:WriteAutoCompleteBuffer()
endfunction
"}}}
"{{{ WriteAutoCompleteBuffer()
function s:WriteAutoCompleteBuffer()
let l:wn = bufwinnr('AutoComplete')
if l:wn != -1
" Save buffer variables into local variables so they remain accessible
" after the "wincmd" command below.
let l:autocomp = b:autocomp
let l:prefix_re = s:FindCommonPrefixes()
" Save the current and the previous window numbers.
" Restoration is done below. Necessary for the Project plug-in.
" TODO: I feel like there must be an easier way to do this. :-)
let [l:curWindow, l:prevWindow] = [winnr(), winnr('#')]
" Move the cursor to the first window showing the AutoComplete buffer.
" Makes the AutoComplete buffer the current buffer.
execute l:wn . 'wincmd w'
" Clear the AutoComplete buffer.
" gg (go to beginning) "_ (don't save deleted text) dG (delete until the end of the buffer)
normal gg"_dG
" Show the list of words.
let l:lines = []
if empty(l:autocomp.words)
let l:lines += ['---']
else
" Populate wordsByPrefix.
let l:rootPrefix = l:autocomp.curWord
let l:wordsByPrefix = {} " e => { 'e': [ 'xact', 'at' ], 'end': [ 'if', 'for' ] }
for word in l:autocomp.words
let l:ml = matchlist(l:word, l:prefix_re)
if len(l:ml) > 0
let [l:key, l:value] = [l:rootPrefix . l:ml[1], l:ml[2]]
else
let [l:key, l:value] = [l:rootPrefix, strpart(l:word, len(l:rootPrefix))]
endif
let l:wordsByPrefix[l:key] = get(l:wordsByPrefix, l:key, []) + [l:value]
endfor
if s:programmerMode == 0
" Remove prefixes that have only one word.
" Move removed words to the closest existing parent prefix.
for prefix in reverse(sort(keys(l:wordsByPrefix)))
if l:prefix != l:rootPrefix && len(l:wordsByPrefix[l:prefix]) == 1
for j in range(len(l:prefix) - 1, 1, -1)
let l:parentPrefix = strpart(l:prefix, 0, l:j)
if has_key(l:wordsByPrefix, l:parentPrefix)
let l:loneWord = strpart(l:prefix, len(l:parentPrefix)) . remove(l:wordsByPrefix, l:prefix)[0]
let l:wordsByPrefix[l:parentPrefix] += [l:loneWord]
break
endif
endfor
endif
endfor
endif
" Prepare lines for the auto-complete buffer.
" Reorder b:autocomp.words.
let l:newWordsList = []
for prefix in sort(keys(l:wordsByPrefix))
let l:lines += ['-[' . l:prefix . ']' . repeat('-', winwidth(0) - 3 - len(l:prefix))]
for word in sort(l:wordsByPrefix[l:prefix])
if l:prefix == l:autocomp.curWord
let l:lines += [len(l:newWordsList) . ' ' . l:prefix . l:word]
else
let l:lines += [len(l:newWordsList) . ' ' . l:word]
endif
let l:newWordsList += [l:prefix . l:word]
endfor
endfor
let l:autocomp.words = l:newWordsList
endif
call setline(1, l:lines)
" Restore windows.
execute l:prevWindow . 'wincmd w'
execute l:curWindow . 'wincmd w'
endif
endfunction
"}}}
"{{{ Event Handlers
function s:CursorMovedIEvent()
if exists('b:autocomp') && len(b:autocomp.words) > 0
let [l:curY, l:curX] = getpos('.')[1:2]
if b:autocomp.lastPos.x != l:curX || b:autocomp.lastPos.y != l:curY
let [b:autocomp.lastPos.y, b:autocomp.lastPos.x] = getpos('.')[1:2]
call s:ClearWords()
endif
endif
endfunction
function s:CursorHoldIEvent()
if exists('b:autocomp')
call s:LetterKeyPressed('')
call s:WriteAutoCompleteBuffer()
let [b:autocomp.lastPos.y, b:autocomp.lastPos.x] = getpos('.')[1:2]
endif
endfunction
function s:BufEnterEvent()
if bufname('%') == 'AutoComplete'
return
elseif !exists('b:autocomp')
setlocal updatetime=100
let b:autocomp = {'lastPos': {'x': -1, 'y': -1}, 'words': [], 'curWord': '', 'keysMapped': {'autoComplete': 0}, 'lastCompletion': {'key': '', 'line': '', 'x': -1, 'y': -1, 'addedChars': ''}}
endif
call s:WriteAutoCompleteBuffer()
endfunction
function s:LetterKeyPressed(key)
" Don't do anything if the AutoComplete buffer is not shown.
let l:wn = bufwinnr('AutoComplete')
if l:wn == -1
call s:UnmapAutoCompleteKeys()
return
endif
" Get the current word. (The word under the cursor)
let [l:curRow, l:curCol] = getpos('.')[1:2]
let l:curLine = getline(l:curRow)
let b:autocomp.curWord = matchstr(strpart(l:curLine, 0, l:curCol - 1), '\V\[' . s:letters . ']\*\$')
" Search for words that start with the current word.
" If there is no current word, then do nothing.
if b:autocomp.curWord == ''
let b:autocomp.words = []
else
call s:UpdateWords(b:autocomp.curWord)
endif
if empty(b:autocomp.words)
""call s:UnmapAutoCompleteKeys()
else
call s:MapAutoCompleteKeys()
endif
endfunction
function s:AutoCompleteKeyPressed(key)
let [l:curY, l:curX] = getpos('.')[1:2]
if b:autocomp.lastCompletion.key == a:key && b:autocomp.lastCompletion.line == getline('.') && b:autocomp.lastCompletion.y == l:curY && b:autocomp.lastCompletion.x == l:curX
" Undo auto-completion and type the number.
call s:UnmapAutoCompleteKeys()
let l:s = len(b:autocomp.lastCompletion.addedChars)
execute 'normal a' . a:key
execute 'normal ' . l:s . 'h' . l:s . 'x'
let b:autocomp.lastCompletion.key = ''
elseif a:key >= len(b:autocomp.words)
" No word for the typed number, enter the number instead.
call s:UnmapAutoCompleteKeys()
if a:key < 10
execute 'normal a' . a:key
endif
let b:autocomp.lastCompletion.key = ''
else
let l:chosenWord = b:autocomp.words[a:key]
let l:newChars = strpart(l:chosenWord, len(b:autocomp.curWord))
execute 'normal a' . l:newChars
"{{{ OUT: Uses getline() and setline().
"" let l:curLine = getline('.')
"" let [l:curRow, l:curCol] = getpos('.')[1:2]
"" let l:newChars = strpart(l:chosenWord, len(b:autocomp.curWord))
"" call setline(l:curRow, strpart(l:curLine, 0, l:curCol) . l:newChars . strpart(l:curLine, l:curCol))
"" call cursor(l:curRow, l:curCol + len(l:newChars))
"}}}
let [b:autocomp.lastCompletion.key, b:autocomp.lastCompletion.line] = [a:key, getline('.')]
let b:autocomp.lastCompletion.addedChars = l:newChars
let [b:autocomp.lastCompletion.y, b:autocomp.lastCompletion.x] = getpos('.')[1:2]
endif
endfunction
function s:EscapeKeyPressed()
call s:ClearWords()
endfunction
"}}}
"{{{ Map and Unmap Keys
function s:MapAutoCompleteKeys()
if !b:autocomp.keysMapped.autoComplete
let b:autocomp.keysMapped.autoComplete = 1
for i in range(0,9)
execute 'inoremap <silent> <buffer> ' . i . ' <Esc>:call <SID>AutoCompleteKeyPressed(' . i . ')<CR>a'
execute 'inoremap <silent> <buffer> <M-' . i . '> <Esc>:call <SID>AutoCompleteKeyPressed(' . (i + 10) . ')<CR>a'
endfor
inoremap <silent> <buffer> <Esc> <Esc>:call <SID>EscapeKeyPressed()<CR>
endif
endfunction
function s:UnmapAutoCompleteKeys()
if b:autocomp.keysMapped.autoComplete
let b:autocomp.keysMapped.autoComplete = 0
for i in range(0,9)
execute 'iunmap <silent> <buffer> ' . i
execute 'iunmap <silent> <buffer> <M-' . i . '>'
endfor
""iunmap <silent> <buffer> <Esc>
endif
endfunction
"}}}
"{{{ Delete This!
"let b:autocomp.words = sort(map(sort(filter(items(b:wordCount), 'v:val[0] =~ "^' . a:filter . '.."'), 's:WordSortAlg')[:20-1], 'v:val[0]'))
"http://vimdoc.sourceforge.net/htmldoc/usr_27.html
" Patterns are almost like regular expressions, but they have some weird
" differences. For example: a+ should be written as a\+.
""after: \v \m \M \V matches
"" 'magic' 'nomagic'
"" $ $ $ \$ matches end-of-line
"" . . \. \. matches any character
"" * * \* \* any number of the previous atom
"" () \(\) \(\) \(\) grouping into an atom
"" | \| \| \| separating alternatives
"" \a \a \a \a alphabetic character
"" \\ \\ \\ \\ literal backslash
"" \. \. . . literal dot
"" \{ { { { literal '{'
"" a a a a literal 'a'
""
"inoremap <F5> <C-R>=ListMonths()<CR>
" func! ListMonths()
" call complete(col('.'), ['January', 'February', 'March',
" \ 'April', 'May', 'June', 'July', 'August', 'September',
" \ 'October', 'November', 'December'])
" return ''
" endfunc
""b:name variable local to a buffer
"" w:name variable local to a window
"" g:name global variable (also in a function)
"" v:name variable predefined by Vi
"":if my_changedtick != b:changedtick
"" : let my_changedtick = b:changedtick
"" : call My_Update()
"" :endif
"}}}