Skip to content

Commit 7dead51

Browse files
Takeshi NISHIDAvim-scripts
authored andcommitted
Version 1.2: - Fixed bugs related to 'completeopt'.
1 parent c900e31 commit 7dead51

File tree

1 file changed

+90
-55
lines changed

1 file changed

+90
-55
lines changed

plugin/autocomplpop.vim

Lines changed: 90 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
" autocomplpop.vim - Automatically open the popup menu for completion.
33
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
44
"
5-
" Last Change: 09-Nov-2007.
5+
" Last Change: 12-Nov-2007.
66
" Author: Takeshi Nishida <ns9tks(at)ns9tks.net>
77
" Version: 1.1, for Vim 7.0
88
" Licence: MIT Licence
@@ -63,6 +63,9 @@
6363
"
6464
"-----------------------------------------------------------------------------
6565
" ChangeLog:
66+
" 1.2:
67+
" - Fixed bugs related to 'completeopt'.
68+
"
6669
" 1.1:
6770
" - Added g:AutoComplPop_IgnoreCaseOption option.
6871
" - Added g:AutoComplPop_NotEnableAtStartup option.
@@ -158,6 +161,14 @@ function! <SID>Initialize()
158161
command! -bar -narg=0 AutoComplPopLock call <SID>Lock()
159162
command! -bar -narg=0 AutoComplPopUnlock call <SID>Unlock()
160163

164+
"-------------------------------------------------------------------------
165+
" AUTOCOMMANDS
166+
augroup AutoComplPop_GlobalAutoCommand
167+
autocmd!
168+
autocmd CursorMovedI * call <SID>OnCursorMovedI()
169+
autocmd InsertLeave * call <SID>OnInsertLeave()
170+
augroup END
171+
161172
"-------------------------------------------------------------------------
162173
" ETC
163174
if !g:AutoComplPop_NotEnableAtStartup
@@ -171,59 +182,6 @@ endfunction
171182
" FUNCTIONS:
172183
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
173184

174-
"-----------------------------------------------------------------------------
175-
function! g:AutoComplPop_OpenPopupMenu(nRetry)
176-
let s:_completeopt = &completeopt
177-
let &completeopt = 'menuone'
178-
let s:_complete = &complete
179-
let &complete = g:AutoComplPop_CompleteOption
180-
let s:_ignorecase = &ignorecase
181-
let &ignorecase = g:AutoComplPop_IgnoreCaseOption
182-
183-
return g:AutoComplPop_PopupCmd . "\<C-r>=g:AutoComplPop_AfterOpenPopupMenu(" . a:nRetry . ")\<CR>"
184-
endfunction
185-
186-
187-
"-----------------------------------------------------------------------------
188-
function! g:AutoComplPop_AfterOpenPopupMenu(nRetry)
189-
let &completeopt = s:_completeopt
190-
let &complete = s:_complete
191-
let &ignorecase = s:_ignorecase
192-
193-
if pumvisible()
194-
" a command to restore to original text and select the first match
195-
return "\<C-p>\<Down>"
196-
elseif a:nRetry > 0
197-
" In case of dividing words by symbols while popup menu is visible,
198-
" popup is not available unless input <C-e> (e.g. 'for(int', 'a==b')
199-
return "\<C-e>\<C-r>=g:AutoComplPop_OpenPopupMenu(" . (a:nRetry - 1) . ")\<CR>"
200-
else
201-
return "\<C-e>"
202-
endif
203-
endfunction
204-
205-
206-
"-----------------------------------------------------------------------------
207-
function! <SID>InsertAndPopup(input)
208-
if s:lock_count > 0 || pumvisible()
209-
return a:input
210-
endif
211-
212-
let last_word_len = len(matchstr(strpart(getline('.'), 0, col('.') - 1) . a:input, '\k*$'))
213-
if last_word_len < g:AutoComplPop_MinLength || last_word_len > g:AutoComplPop_MaxLength
214-
return a:input
215-
endif
216-
217-
if last_word_len == g:AutoComplPop_MinLength
218-
let nRetry = 1
219-
else
220-
let nRetry = 0
221-
endif
222-
223-
return a:input . "\<C-r>=g:AutoComplPop_OpenPopupMenu(" . nRetry . ")\<CR>"
224-
endfunction
225-
226-
227185
"-----------------------------------------------------------------------------
228186
function! <SID>Enable()
229187
if !empty(s:map_list)
@@ -233,7 +191,7 @@ function! <SID>Enable()
233191
let s:map_list = deepcopy(g:AutoComplPop_MapList)
234192

235193
for item in s:map_list
236-
execute 'inoremap <silent> <expr> ' . item . ' <SID>InsertAndPopup("'. item . '")'
194+
execute 'inoremap <silent> <expr> ' . item . ' <SID>FeedKeysAndPopup("' . item . '")'
237195
endfor
238196
endfunction
239197

@@ -265,6 +223,83 @@ function! <SID>Unlock()
265223
endfunction
266224

267225

226+
"-----------------------------------------------------------------------------
227+
function! <SID>FeedKeysAndPopup(keys)
228+
let last_word_len = len(<SID>GetLastWord() . a:keys)
229+
if s:lock_count == 0 && !pumvisible() && last_word_len >= g:AutoComplPop_MinLength &&
230+
\ last_word_len <= g:AutoComplPop_MaxLength
231+
call <SID>SetOrRestoreOption(1)
232+
233+
let s:popup_fed = 1
234+
endif
235+
236+
return a:keys
237+
endfunction
238+
239+
240+
"-----------------------------------------------------------------------------
241+
function! g:AutoComplPop_HandlePopupMenu(retry)
242+
echo ""
243+
if pumvisible()
244+
" a command to restore to original text and select the first match
245+
return "\<C-p>\<Down>"
246+
elseif a:retry > 0
247+
" In case of dividing words by symbols while popup menu is visible,
248+
" popup is not available unless input <C-e> (e.g. "for(int", "a==b")
249+
return "\<C-e>" . g:AutoComplPop_PopupCmd . "\<C-r>=g:AutoComplPop_HandlePopupMenu(" . (a:retry - 1) . ")\<CR>"
250+
else
251+
return "\<C-e>"
252+
endif
253+
endfunction
254+
255+
256+
"-----------------------------------------------------------------------------
257+
function! <SID>GetLastWord()
258+
return matchstr(strpart(getline('.'), 0, col('.') - 1), '\k*$')
259+
endfunction
260+
261+
262+
"-----------------------------------------------------------------------------
263+
function! <SID>SetOrRestoreOption(set_or_restore)
264+
if a:set_or_restore && !exists('s:_completeopt')
265+
let s:_completeopt = &completeopt
266+
let &completeopt = 'menuone'
267+
let s:_complete = &complete
268+
let &complete = g:AutoComplPop_CompleteOption
269+
let s:_ignorecase = &ignorecase
270+
let &ignorecase = g:AutoComplPop_IgnoreCaseOption
271+
elseif !a:set_or_restore && exists('s:_completeopt')
272+
let &completeopt = s:_completeopt
273+
unlet s:_completeopt
274+
let &complete = s:_complete
275+
unlet s:_complete
276+
let &ignorecase = s:_ignorecase
277+
unlet s:_ignorecase
278+
endif
279+
endfunction
280+
281+
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
282+
" EVENT HANDLER:
283+
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
284+
285+
"-----------------------------------------------------------------------------
286+
function! <SID>OnCursorMovedI()
287+
if exists('s:popup_fed')
288+
unlet s:popup_fed
289+
let retry = (len(<SID>GetLastWord()) == g:AutoComplPop_MinLength ? 1 : 0)
290+
call feedkeys(g:AutoComplPop_PopupCmd . "\<C-r>=g:AutoComplPop_HandlePopupMenu(" . retry . ")\<CR>", 'n')
291+
elseif !pumvisible()
292+
call <SID>SetOrRestoreOption(0)
293+
endif
294+
endfunction
295+
296+
297+
"-----------------------------------------------------------------------------
298+
function! <SID>OnInsertLeave()
299+
call <SID>SetOrRestoreOption(0)
300+
endfunction
301+
302+
268303
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
269304
" INITIALIZE:
270305
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

0 commit comments

Comments
 (0)