2
2
" autocomplpop.vim - Automatically open the popup menu for completion.
3
3
" """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
4
4
"
5
- " Last Change: 09 -Nov-2007.
5
+ " Last Change: 12 -Nov-2007.
6
6
" Author: Takeshi Nishida <ns9tks(at)ns9tks.net>
7
7
" Version: 1.1, for Vim 7.0
8
8
" Licence: MIT Licence
63
63
"
64
64
" -----------------------------------------------------------------------------
65
65
" ChangeLog:
66
+ " 1.2:
67
+ " - Fixed bugs related to 'completeopt'.
68
+ "
66
69
" 1.1:
67
70
" - Added g:AutoComplPop_IgnoreCaseOption option.
68
71
" - Added g:AutoComplPop_NotEnableAtStartup option.
@@ -158,6 +161,14 @@ function! <SID>Initialize()
158
161
command ! - bar -narg =0 AutoComplPopLock call <SID> Lock ()
159
162
command ! - bar -narg =0 AutoComplPopUnlock call <SID> Unlock ()
160
163
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
+
161
172
" -------------------------------------------------------------------------
162
173
" ETC
163
174
if ! g: AutoComplPop_NotEnableAtStartup
@@ -171,59 +182,6 @@ endfunction
171
182
" FUNCTIONS:
172
183
" """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
173
184
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
-
227
185
" -----------------------------------------------------------------------------
228
186
function ! <SID> Enable ()
229
187
if ! empty (s: map_list )
@@ -233,7 +191,7 @@ function! <SID>Enable()
233
191
let s: map_list = deepcopy (g: AutoComplPop_MapList )
234
192
235
193
for item in s: map_list
236
- execute ' inoremap <silent> <expr> ' . item . ' <SID>InsertAndPopup ("' . item . ' ")'
194
+ execute ' inoremap <silent> <expr> ' . item . ' <SID>FeedKeysAndPopup ("' . item . ' ")'
237
195
endfor
238
196
endfunction
239
197
@@ -265,6 +223,83 @@ function! <SID>Unlock()
265
223
endfunction
266
224
267
225
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
+
268
303
" """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
269
304
" INITIALIZE:
270
305
" """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
0 commit comments