--- /usr/share/vim/vimfiles/plugin/supertab.vim 2012-06-10 07:08:20.000000000 -0400 +++ /home/dbb/.vim/bundle/supertab/plugin/supertab.vim 2014-04-05 03:00:11.000000000 -0400 @@ -13,7 +13,7 @@ " }}} " " License: {{{ -" Copyright (c) 2002 - 2012 +" Copyright (c) 2002 - 2014 " All rights reserved. " " Redistribution and use of this software in source and binary forms, with @@ -48,7 +48,7 @@ " }}} " " Testing Info: {{{ -" Running vim + supertab with the absolute bar minimum settings: +" Running vim + supertab with the absolute bare minimum settings: " $ vim -u NONE -U NONE -c "set nocp | runtime plugin/supertab.vim" " }}} @@ -60,6 +60,11 @@ finish endif +if exists("loaded_supertab") + finish +endif +let loaded_supertab = 1 + let s:save_cpo=&cpo set cpo&vim @@ -95,7 +100,7 @@ if exists("g:SuperTabLeadingSpaceCompletion") && g:SuperTabLeadingSpaceCompletion let g:SuperTabNoCompleteAfter = [] else - let g:SuperTabNoCompleteAfter = ['\s'] + let g:SuperTabNoCompleteAfter = ['^', '\s'] endif endif @@ -126,6 +131,10 @@ let g:SuperTabClosePreviewOnPopupClose = 0 endif + if !exists("g:SuperTabUndoBreak") + let g:SuperTabUndoBreak = 0 + endif + " }}} " Script Variables {{{ @@ -160,10 +169,10 @@ " }}} -" SuperTabSetDefaultCompletionType(type) {{{ -" Globally available function that users can use to set the default -" completion type for the current buffer, like in an ftplugin. -function! SuperTabSetDefaultCompletionType(type) +function! SuperTabSetDefaultCompletionType(type) " {{{ + " Globally available function that users can use to set the default + " completion type for the current buffer, like in an ftplugin. + " init hack for workaround. let b:complCommandLine = 0 @@ -173,36 +182,36 @@ call SuperTabSetCompletionType(b:SuperTabDefaultCompletionType) endfunction " }}} -" SuperTabSetCompletionType(type) {{{ -" Globally available function that users can use to create mappings to quickly -" switch completion modes. Useful when a user wants to restore the default or -" switch to another mode without having to kick off a completion of that type -" or use SuperTabHelp. Note, this function only changes the current -" completion type, not the default, meaning that the default will still be -" restored once the configured retension duration has been met (see -" g:SuperTabRetainCompletionDuration). To change the default for the current -" buffer, use SuperTabDefaultCompletionType(type) instead. Example mapping to -" restore SuperTab default: -" nmap :call SetSuperTabCompletionType("") -function! SuperTabSetCompletionType(type) +function! SuperTabSetCompletionType(type) " {{{ + " Globally available function that users can use to create mappings to quickly + " switch completion modes. Useful when a user wants to restore the default or + " switch to another mode without having to kick off a completion of that type + " or use SuperTabHelp. Note, this function only changes the current + " completion type, not the default, meaning that the default will still be + " restored once the configured retension duration has been met (see + " g:SuperTabRetainCompletionDuration). To change the default for the current + " buffer, use SuperTabDefaultCompletionType(type) instead. Example mapping to + " restore SuperTab default: + " nmap :call SetSuperTabCompletionType("") + call s:InitBuffer() exec "let b:complType = \"" . escape(a:type, '<') . "\"" endfunction " }}} -" SuperTabAlternateCompletion(type) {{{ -" Function which can be mapped to a key to kick off an alternate completion -" other than the default. For instance, if you have 'context' as the default -" and want to map ctrl+space to issue keyword completion. -" Note: due to the way vim expands ctrl characters in mappings, you cannot -" create the alternate mapping like so: -" imap =SuperTabAlternateCompletion("") -" instead, you have to use \ to prevent vim from expanding the key -" when creating the mapping. -" gvim: -" imap =SuperTabAlternateCompletion("\c-p>") -" console: -" imap =SuperTabAlternateCompletion("\c-p>") -function! SuperTabAlternateCompletion(type) +function! SuperTabAlternateCompletion(type) " {{{ + " Function which can be mapped to a key to kick off an alternate completion + " other than the default. For instance, if you have 'context' as the default + " and want to map ctrl+space to issue keyword completion. + " Note: due to the way vim expands ctrl characters in mappings, you cannot + " create the alternate mapping like so: + " imap =SuperTabAlternateCompletion("") + " instead, you have to use \ to prevent vim from expanding the key + " when creating the mapping. + " gvim: + " imap =SuperTabAlternateCompletion("\c-p>") + " console: + " imap =SuperTabAlternateCompletion("\c-p>") + call SuperTabSetCompletionType(a:type) " end any current completion before attempting to start the new one. " use feedkeys to prevent possible remapping of from causing issues. @@ -215,19 +224,17 @@ return '' endfunction " }}} -" SuperTabLongestHighlight(dir) {{{ -" When longest highlight is enabled, this function is used to do the actual -" selection of the completion popup entry. -function! SuperTabLongestHighlight(dir) +function! SuperTabLongestHighlight(dir) " {{{ + " When longest highlight is enabled, this function is used to do the actual + " selection of the completion popup entry. + if !pumvisible() return '' endif return a:dir == -1 ? "\" : "\" endfunction " }}} -" s:Init {{{ -" Global initilization when supertab is loaded. -function! s:Init() +function! s:Init() " {{{ " Setup mechanism to restore original completion type upon leaving insert " mode if configured to do so if g:SuperTabRetainCompletionDuration == 'insert' @@ -238,9 +245,7 @@ endif endfunction " }}} -" s:InitBuffer {{{ -" Per buffer initilization. -function! s:InitBuffer() +function! s:InitBuffer() " {{{ if exists('b:SuperTabNoCompleteBefore') return endif @@ -277,15 +282,17 @@ endif endfunction " }}} -" s:ManualCompletionEnter() {{{ -" Handles manual entrance into completion mode. -function! s:ManualCompletionEnter() +function! s:ManualCompletionEnter() " {{{ + " Handles manual entrance into completion mode. + if &smd echo '' | echohl ModeMsg | echo '-- ^X++ mode (' . s:modes . ')' | echohl None endif let complType = nr2char(getchar()) if stridx(s:types, complType) != -1 - let b:supertab_close_preview = 1 + if !exists('b:supertab_close_preview') + let b:supertab_close_preview = !s:IsPreviewOpen() + endif if stridx("\\", complType) != -1 " no memory, just scroll... return "\" . complType @@ -309,6 +316,9 @@ " optionally enable enhanced longest completion if g:SuperTabLongestEnhanced && &completeopt =~ 'longest' call s:EnableLongestEnhancement() + " handle backspacing which triggers g:SuperTabNoCompleteAfter match + elseif s:IsNoCompleteAfterReset() + call s:EnableNoCompleteAfterReset() endif if g:SuperTabLongestHighlight && @@ -326,10 +336,10 @@ return complType endfunction " }}} -" s:SetCompletionType() {{{ -" Sets the completion type based on what the user has chosen from the help -" buffer. -function! s:SetCompletionType() +function! s:SetCompletionType() " {{{ + " Sets the completion type based on what the user has chosen from the help + " buffer. + let chosen = substitute(getline('.'), '.*|\(.*\)|.*', '\1', '') if chosen != getline('.') let winnr = b:winnr @@ -346,23 +356,34 @@ endif endfunction " }}} -" s:SuperTab(command) {{{ -" Used to perform proper cycle navigation as the user requests the next or -" previous entry in a completion list, and determines whether or not to simply -" retain the normal usage of based on the cursor position. -function! s:SuperTab(command) +function! SuperTab(command) " {{{ + " Used to perform proper cycle navigation as the user requests the next or + " previous entry in a completion list, and determines whether or not to simply + " retain the normal usage of based on the cursor position. + if exists('b:SuperTabDisabled') && b:SuperTabDisabled - return "\" + if exists('s:Tab') + return s:Tab() + endif + return ( + \ g:SuperTabMappingForward ==? '' || + \ g:SuperTabMappingBackward ==? '' + \ ) ? "\" : '' endif call s:InitBuffer() if s:WillComplete() - let b:supertab_close_preview = 1 + if !exists('b:supertab_close_preview') + let b:supertab_close_preview = !s:IsPreviewOpen() + endif " optionally enable enhanced longest completion if g:SuperTabLongestEnhanced && &completeopt =~ 'longest' call s:EnableLongestEnhancement() + " handle backspacing which triggers g:SuperTabNoCompleteAfter match + elseif s:IsNoCompleteAfterReset() + call s:EnableNoCompleteAfterReset() endif if !pumvisible() @@ -390,9 +411,9 @@ elseif pumvisible() && !b:complReset let type = b:complType == 'context' ? b:complTypeContext : b:complType if a:command == 'n' - return type == "\" ? "\" : "\" + return type == "\" || type == "\\" ? "\" : "\" endif - return type == "\" ? "\" : "\" + return type == "\" || type == "\\" ? "\" : "\" endif " handle 'context' completion. @@ -411,6 +432,15 @@ let complType = b:complType endif + " switch / completion in mode + if a:command == 'p' + if complType == "\\" + let complType = "\\" + elseif complType == "\\" + let complType = "\\" + endif + endif + " highlight first result if longest enabled if g:SuperTabLongestHighlight && \ &completeopt =~ 'longest' && @@ -429,15 +459,25 @@ endif endif + if g:SuperTabUndoBreak && !pumvisible() + return "\u" . complType + endif + return complType endif - return "\" + if exists('s:Tab') + return s:Tab() + endif + return ( + \ g:SuperTabMappingForward ==? '' || + \ g:SuperTabMappingBackward ==? '' + \ ) ? "\" : '' endfunction " }}} -" s:SuperTabHelp() {{{ -" Opens a help window where the user can choose a completion type to enter. -function! s:SuperTabHelp() +function! s:SuperTabHelp() " {{{ + " Opens a help window where the user can choose a completion type to enter. + let winnr = winnr() if bufwinnr("SuperTabHelp") == -1 botright split SuperTabHelp @@ -446,13 +486,10 @@ setlocal buftype=nowrite setlocal bufhidden=delete - let saved = @" - let @" = s:tabHelp - silent put + silent put =s:tabHelp call cursor(1, 1) - silent 1,delete + silent 1,delete _ call cursor(4, 1) - let @" = saved exec "resize " . line('$') syntax match Special "|.\{-}|" @@ -468,37 +505,50 @@ let b:winnr = winnr endfunction " }}} -" s:WillComplete() {{{ -" Determines if completion should be kicked off at the current location. -function! s:WillComplete() - if pumvisible() +function! s:WillComplete(...) " {{{ + " Determines if completion should be kicked off at the current location. + " Optional arg: + " col: The column to check at, otherwise use the current column. + + " if an arg was supplied, then we will re-check even if already in + " completion mode. + if pumvisible() && !a:0 return 1 endif let line = getline('.') - let cnum = col('.') - - " Start of line. - if line =~ '^\s*\%' . cnum . 'c' - return 0 - endif + let cnum = a:0 ? a:1 : col('.') " honor SuperTabNoCompleteAfter - let pre = line[:cnum - 2] - for pattern in b:SuperTabNoCompleteAfter - if pre =~ pattern . '$' - return 0 - endif - endfor + let pre = cnum >= 2 ? line[:cnum - 2] : '' + let complAfterType = type(b:SuperTabNoCompleteAfter) + if complAfterType == 3 + " the option was provided as a list of patterns + for pattern in b:SuperTabNoCompleteAfter + if pre =~ pattern . '$' + return 0 + endif + endfor + elseif complAfterType == 2 + " the option was provided as a funcref + return !b:SuperTabNoCompleteAfter(pre) + endif " honor SuperTabNoCompleteBefore " Within a word, but user does not have mid word completion enabled. let post = line[cnum - 1:] - for pattern in b:SuperTabNoCompleteBefore - if post =~ '^' . pattern - return 0 - endif - endfor + let complBeforeType = type(b:SuperTabNoCompleteBefore) + if complBeforeType == 3 + " a list of patterns + for pattern in b:SuperTabNoCompleteBefore + if post =~ '^' . pattern + return 0 + endif + endfor + elseif complBeforeType == 2 + " the option was provided as a funcref + return !b:SuperTabNoCompleteBefore(post) + endif return 1 endfunction " }}} @@ -512,14 +562,58 @@ call s:CaptureKeyPresses() endfunction " }}} +function! s:IsNoCompleteAfterReset() " {{{ + " if the user has g:SuperTabNoCompleteAfter set, then re-map so that + " backspacing to a point where one of the g:SuperTabNoCompleteAfter + " entries matches will cause completion mode to exit. + let complAfterType = type(b:SuperTabNoCompleteAfter) + if complAfterType == 2 + return 1 + endif + return len(g:SuperTabNoCompleteAfter) && g:SuperTabNoCompleteAfter != ['^', '\s'] +endfunction " }}} + +function! s:EnableNoCompleteAfterReset() " {{{ + augroup supertab_reset + autocmd! + autocmd InsertLeave,CursorMovedI + \ call s:ReleaseKeyPresses() | autocmd! supertab_reset + augroup END + + " short version of s:CaptureKeyPresses + if !exists('b:capturing') || !b:capturing + let b:capturing = 1 + let b:capturing_start = col('.') + let b:captured = { + \ '': maparg('', 'i'), + \ '': maparg('', 'i'), + \ } + imap =CompletionReset("\bs>") + imap =CompletionReset("\c-h>") + endif +endfunction " }}} + function! s:CompletionReset(char) " {{{ let b:complReset = 1 + + " handle exiting completion mode if user has g:SuperTabNoCompleteAfter set + " and they are about to backspace to a point where that maches one of the + " entries in that var. + if (a:char == "\" || a:char == "\") && s:IsNoCompleteAfterReset() + if !s:WillComplete(col('.') - 1) + " exit from completion mode then issue the currently requested backspace + call feedkeys("\\\", 'n') + return '' + endif + endif + return a:char endfunction " }}} function! s:CaptureKeyPresses() " {{{ if !exists('b:capturing') || !b:capturing let b:capturing = 1 + let b:capturing_start = col('.') " save any previous mappings " TODO: capture additional info provided by vim 7.3.032 and up. let b:captured = { @@ -528,6 +622,8 @@ \ } " TODO: use &keyword to get an accurate list of chars to map for c in split('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_', '.\zs') + let existing = maparg(c, 'i') + let b:captured[c] = existing exec 'imap ' . c . ' =CompletionReset("' . c . '")' endfor imap =CompletionReset("\bs>") @@ -535,6 +631,18 @@ endif endfunction " }}} +function! s:IsPreviewOpen() " {{{ + let wins = tabpagewinnr(tabpagenr(), '$') + let winnr = 1 + while winnr <= wins + if getwinvar(winnr, '&previewwindow') == 1 + return 1 + endif + let winnr += 1 + endwhile + return 0 +endfunction " }}} + function! s:ClosePreview() " {{{ if exists('b:supertab_close_preview') && b:supertab_close_preview let preview = 0 @@ -546,21 +654,23 @@ endfor if preview pclose + try + doautocmd supertab_preview_closed User + catch /E216/ + " ignore: no autocmds defined + endtry endif - unlet b:supertab_close_preview endif + silent! unlet b:supertab_close_preview endfunction " }}} function! s:ReleaseKeyPresses() " {{{ if exists('b:capturing') && b:capturing let b:capturing = 0 - for c in split('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_', '.\zs') + for c in keys(b:captured) exec 'iunmap ' . c endfor - iunmap - iunmap - " restore any previous mappings for [key, rhs] in items(b:captured) if rhs != '' @@ -570,23 +680,24 @@ let expr = substitute(rhs, '\(.*\)".\{-}"\(.*\)', '\1%s\2', '') let rhs = printf(expr, args) endif - exec printf("imap %s %s", key, rhs) + exec printf("imap %s %s", key, rhs) endif endfor unlet b:captured - if mode() == 'i' && &completeopt =~ 'menu' + if mode() == 'i' && &completeopt =~ 'menu' && b:capturing_start != col('.') " force full exit from completion mode (don't exit insert mode since " that will break repeating with '.') call feedkeys("\\", 'n') endif + unlet b:capturing_start endif endfunction " }}} -" s:CommandLineCompletion() {{{ -" Hack needed to account for apparent bug in vim command line mode completion -" when invoked via = -function! s:CommandLineCompletion() +function! s:CommandLineCompletion() " {{{ + " Hack needed to account for apparent bug in vim command line mode completion + " when invoked via = + " This hack will trigger InsertLeave which will then invoke " s:SetDefaultCompletionType. To prevent default completion from being " restored prematurely, set an internal flag for s:SetDefaultCompletionType @@ -647,7 +758,7 @@ \ ((has('win32') || has('win64')) && curline =~ '.*\\\w*\%' . cnum . 'c') return "\\" - elseif curline =~ '.*\(\w\|[\])]\)\(\.\|::\|->\)\w*\%' . cnum . 'c' && + elseif curline =~ '.*\(\w\|[\])]\)\(\.\|>\?::\|->\)\w*\%' . cnum . 'c' && \ synname !~ '\(String\|Comment\)' let omniPrecedence = exists('g:SuperTabContextTextOmniPrecedence') ? \ g:SuperTabContextTextOmniPrecedence : ['&completefunc', '&omnifunc'] @@ -675,8 +786,10 @@ endfunction " }}} function! SuperTabChain(completefunc, completekeys) " {{{ - let b:SuperTabChain = [a:completefunc, a:completekeys] - setlocal completefunc=SuperTabCodeComplete + if a:completefunc != 'SuperTabCodeComplete' + let b:SuperTabChain = [a:completefunc, a:completekeys] + setlocal completefunc=SuperTabCodeComplete + endif endfunction " }}} function! SuperTabCodeComplete(findstart, base) " {{{ @@ -725,50 +838,41 @@ " map a regular tab to ctrl-tab (note: doesn't work in console vim) exec 'inoremap ' . g:SuperTabMappingTabLiteral . ' ' - imap =ManualCompletionEnter() + imap =ManualCompletionEnter() - imap