From ad6a9d4c10c8a4248fef492fb8302bd5930b7032 Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Thu, 28 Jun 2012 17:33:45 -0400 Subject: [PATCH] Upgrade supertab --- .vim/plugin/supertab.vim | 890 ++++++++++++++++++++++++++++++--------- 1 file changed, 699 insertions(+), 191 deletions(-) diff --git a/.vim/plugin/supertab.vim b/.vim/plugin/supertab.vim index c88ee1a..3ced8d6 100644 --- a/.vim/plugin/supertab.vim +++ b/.vim/plugin/supertab.vim @@ -1,20 +1,19 @@ -" Author: -" Original: Gergely Kontra -" Current: Eric Van Dewoestine (as of version 0.4) -" Please direct all correspondence to Eric. -" Version: 0.43 +" Author: Eric Van Dewoestine +" Original concept and versions up to 0.32 written by +" Gergely Kontra +" Version: 2.0 +" GetLatestVimScripts: 1643 1 :AutoInstall: supertab.vim " " Description: {{{ " Use your tab key to do all your completion in insert mode! " You can cycle forward and backward with the and keys -" ( will not work in the console version) " Note: you must press once to be able to cycle back +" +" http://www.vim.org/scripts/script.php?script_id=1643 " }}} " " License: {{{ -" Software License Agreement (BSD License) -" -" Copyright (c) 2002 - 2007 +" Copyright (c) 2002 - 2012 " All rights reserved. " " Redistribution and use of this software in source and binary forms, with @@ -47,41 +46,84 @@ " NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS " SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. " }}} +" +" Testing Info: {{{ +" Running vim + supertab with the absolute bar minimum settings: +" $ vim -u NONE -U NONE -c "set nocp | runtime plugin/supertab.vim" +" }}} + +if v:version < 700 + finish +endif -if !exists('complType') "Integration with other completion functions. +if exists('complType') " Integration with other completion functions. + finish +endif + +let s:save_cpo=&cpo +set cpo&vim " Global Variables {{{ - " Used to set the default completion type. - " There is no need to escape this value as that will be done for you when - " the type is set. - " Ex. let g:SuperTabDefaultCompletionType = "" if !exists("g:SuperTabDefaultCompletionType") - let g:SuperTabDefaultCompletionType = "" + let g:SuperTabDefaultCompletionType = "" + endif + + if !exists("g:SuperTabContextDefaultCompletionType") + let g:SuperTabContextDefaultCompletionType = "" + endif + + if !exists("g:SuperTabCompletionContexts") + let g:SuperTabCompletionContexts = ['s:ContextText'] endif - " Determines if, and for how long, the current completion type is retained. - " The possible values include: - " 0 - The current completion type is only retained for the current completion. - " Once you have chosen a completion result or exited the completion - " mode, the default completion type is restored. - " 1 - The current completion type is saved for the duration of your vim - " session or until you enter a different completion mode. - " (SuperTab default). - " 2 - The current completion type is saved until you exit insert mode (via - " ESC). Once you exit insert mode the default completion type is - " restored. - if !exists("g:SuperTabRetainCompletionType") - let g:SuperTabRetainCompletionType = 1 + if !exists("g:SuperTabRetainCompletionDuration") + let g:SuperTabRetainCompletionDuration = 'insert' endif - " Sets whether or not mid word completion is enabled. - " When enabled, will kick off completion when ever a word character is - " to the left of the cursor. When disabled, completion will only occur if - " the char to the left is a word char and the char to the right is not (you - " are at the end of the word). - if !exists("g:SuperTabMidWordCompletion") - let g:SuperTabMidWordCompletion = 1 + if !exists("g:SuperTabNoCompleteBefore") + " retain backwards compatability + if exists("g:SuperTabMidWordCompletion") && !g:SuperTabMidWordCompletion + let g:SuperTabNoCompleteBefore = ['\k'] + else + let g:SuperTabNoCompleteBefore = [] + endif + endif + + if !exists("g:SuperTabNoCompleteAfter") + " retain backwards compatability + if exists("g:SuperTabLeadingSpaceCompletion") && g:SuperTabLeadingSpaceCompletion + let g:SuperTabNoCompleteAfter = [] + else + let g:SuperTabNoCompleteAfter = ['^', '\s'] + endif + endif + + if !exists("g:SuperTabMappingForward") + let g:SuperTabMappingForward = '' + endif + if !exists("g:SuperTabMappingBackward") + let g:SuperTabMappingBackward = '' + endif + + if !exists("g:SuperTabMappingTabLiteral") + let g:SuperTabMappingTabLiteral = '' + endif + + if !exists("g:SuperTabLongestEnhanced") + let g:SuperTabLongestEnhanced = 0 + endif + + if !exists("g:SuperTabLongestHighlight") + let g:SuperTabLongestHighlight = 0 + endif + + if !exists("g:SuperTabCrMapping") + let g:SuperTabCrMapping = 1 + endif + + if !exists("g:SuperTabClosePreviewOnPopupClose") + let g:SuperTabClosePreviewOnPopupClose = 0 endif " }}} @@ -93,119 +135,310 @@ if !exists('complType') "Integration with other completion functions. \ "Hit or CTRL-] on the completion type you wish to switch to.\n" . \ "Use :help ins-completion for more information.\n" . \ "\n" . - \ "|| - Keywords in 'complete' searching down.\n" . - \ "|| - Keywords in 'complete' searching up (SuperTab default).\n" . - \ "|| - Whole lines.\n" . - \ "|| - Keywords in current file.\n" . - \ "|| - Keywords in 'dictionary'.\n" . - \ "|| - Keywords in 'thesaurus', thesaurus-style.\n" . - \ "|| - Keywords in the current and included files.\n" . - \ "|| - Tags.\n" . - \ "|| - File names.\n" . - \ "|| - Definitions or macros.\n" . - \ "|| - Vim command-line." - if v:version >= 700 - let s:tabHelp = s:tabHelp . "\n" . - \ "|| - User defined completion.\n" . - \ "|| - Omni completion.\n" . - \ "|s| - Spelling suggestions." - endif + \ "|| - Keywords in 'complete' searching down.\n" . + \ "|| - Keywords in 'complete' searching up (SuperTab default).\n" . + \ "|| - Whole lines.\n" . + \ "|| - Keywords in current file.\n" . + \ "|| - Keywords in 'dictionary'.\n" . + \ "|| - Keywords in 'thesaurus', thesaurus-style.\n" . + \ "|| - Keywords in the current and included files.\n" . + \ "|| - Tags.\n" . + \ "|| - File names.\n" . + \ "|| - Definitions or macros.\n" . + \ "|| - Vim command-line.\n" . + \ "|| - User defined completion.\n" . + \ "|| - Omni completion.\n" . + \ "|s| - Spelling suggestions." " set the available completion types and modes. let s:types = - \ "\\\\\\\\\\\\\" - let s:modes = '/^E/^Y/^L/^N/^K/^T/^I/^]/^F/^D/^V/^P' - if v:version >= 700 - let s:types = s:types . "\\\\s" - let s:modes = s:modes . '/^U/^O/s' - endif + \ "\\\\\\\\" . + \ "\\\\\\\\\s" + let s:modes = '/^E/^Y/^L/^N/^K/^T/^I/^]/^F/^D/^V/^P/^U/^O/s' let s:types = s:types . "np" let s:modes = s:modes . '/n/p' " }}} -" CtrlXPP() {{{ -" Handles entrance into completion mode. -function! CtrlXPP() +" 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) + " init hack for workaround. + let b:complCommandLine = 0 + + let b:SuperTabDefaultCompletionType = a:type + + " set the current completion type to the default + 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) + 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) + 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. + "call feedkeys("\", 'n') + " ^ since we can't detect completion mode vs regular insert mode, we force + " vim into keyword completion mode and end that mode to prevent the regular + " insert behavior of from occurring. + call feedkeys("\\\", 'n') + call feedkeys(b:complType, 'n') + 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) + if !pumvisible() + return '' + endif + return a:dir == -1 ? "\" : "\" +endfunction " }}} + +" s:Init {{{ +" Global initilization when supertab is loaded. +function! s:Init() + " Setup mechanism to restore original completion type upon leaving insert + " mode if configured to do so + if g:SuperTabRetainCompletionDuration == 'insert' + augroup supertab_retain + autocmd! + autocmd InsertLeave * call s:SetDefaultCompletionType() + augroup END + endif +endfunction " }}} + +" s:InitBuffer {{{ +" Per buffer initilization. +function! s:InitBuffer() + if exists('b:SuperTabNoCompleteBefore') + return + endif + + let b:complReset = 0 + let b:complTypeManual = !exists('b:complTypeManual') ? '' : b:complTypeManual + let b:complTypeContext = '' + + " init hack for workaround. + let b:complCommandLine = 0 + + if !exists('b:SuperTabNoCompleteBefore') + let b:SuperTabNoCompleteBefore = g:SuperTabNoCompleteBefore + endif + if !exists('b:SuperTabNoCompleteAfter') + let b:SuperTabNoCompleteAfter = g:SuperTabNoCompleteAfter + endif + + if !exists('b:SuperTabDefaultCompletionType') + let b:SuperTabDefaultCompletionType = g:SuperTabDefaultCompletionType + endif + + " set the current completion type to the default + call SuperTabSetCompletionType(b:SuperTabDefaultCompletionType) + + " hack to programatically revert a change to snipmate that breaks supertab + " but which the new maintainers don't care about: + " http://github.com/garbas/vim-snipmate/issues/37 + let snipmate = maparg('', 'i') + if snipmate =~ 'u' && g:SuperTabMappingForward =~? '' + let snipmate = substitute(snipmate, 'u', '', '') + iunmap + exec "inoremap " . snipmate + endif +endfunction " }}} + +" s:ManualCompletionEnter() {{{ +" Handles manual entrance into completion mode. +function! s:ManualCompletionEnter() if &smd - echo '' | echo '-- ^X++ mode (' . s:modes . ')' + echo '' | echohl ModeMsg | echo '-- ^X++ mode (' . s:modes . ')' | echohl None endif let complType = nr2char(getchar()) if stridx(s:types, complType) != -1 - if stridx("\\", complType) != -1 " no memory, just scroll... - return "\" . complType + let b:supertab_close_preview = 1 + + if stridx("\\", complType) != -1 " no memory, just scroll... + return "\" . complType elseif stridx('np', complType) != -1 - let complType = nr2char(char2nr(complType) - 96) " char2nr('n')-char2nr("\= + if complType == "\\" + return s:CommandLineCompletion() + endif + + " optionally enable enhanced longest completion + if g:SuperTabLongestEnhanced && &completeopt =~ 'longest' + call s:EnableLongestEnhancement() + endif + + if g:SuperTabLongestHighlight && + \ &completeopt =~ 'longest' && + \ &completeopt =~ 'menu' && + \ !pumvisible() + let dir = (complType == "\\") ? -1 : 1 + call feedkeys("\=SuperTabLongestHighlight(" . dir . ")\", 'n') endif - return complType - else - echohl "Unknown mode" return complType endif + + echohl "Unknown mode" + return complType endfunction " }}} -" SuperTabSetCompletionType(type) {{{ -" Globally available function that user's 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. -" Example mapping to restore SuperTab default: -" nmap :call SetSuperTabCompletionType("") -function! SuperTabSetCompletionType (type) - exec "let g:complType = \"" . escape(a:type, '<') . "\"" +" s:SetCompletionType() {{{ +" Sets the completion type based on what the user has chosen from the help +" buffer. +function! s:SetCompletionType() + let chosen = substitute(getline('.'), '.*|\(.*\)|.*', '\1', '') + if chosen != getline('.') + let winnr = b:winnr + close + exec winnr . 'winc w' + call SuperTabSetCompletionType(chosen) + endif endfunction " }}} -" s:Init {{{ -" Initializes super tab according to user defined settings. -function! s:Init () - " set the default completion type. - call SuperTabSetCompletionType(g:SuperTabDefaultCompletionType) - - " Setup mechanism to restore orignial completion type upon leaving insert - " mode if g:SuperTabDefaultCompletionType == 2 - if g:SuperTabRetainCompletionType == 2 - " pre vim 7, must map - if v:version < 700 - im - \ :call SuperTabSetCompletionType(g:SuperTabDefaultCompletionType) - - " since vim 7, we can use InsertLeave autocmd. - else - augroup supertab - autocmd InsertLeave * - \ call SuperTabSetCompletionType(g:SuperTabDefaultCompletionType) - augroup END - endif +function! s:SetDefaultCompletionType() " {{{ + if exists('b:SuperTabDefaultCompletionType') && + \ (!exists('b:complCommandLine') || !b:complCommandLine) + call SuperTabSetCompletionType(b:SuperTabDefaultCompletionType) endif endfunction " }}} " s:SuperTab(command) {{{ -" Used to perform proper cycle navigtion as the user request the next or -" previous entry in a completion list, and determines whether or not so simply +" 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) + if exists('b:SuperTabDisabled') && b:SuperTabDisabled + return g:SuperTabMappingForward == '' ? "\" : '' + endif + + call s:InitBuffer() + if s:WillComplete() + let b:supertab_close_preview = 1 + + " optionally enable enhanced longest completion + if g:SuperTabLongestEnhanced && &completeopt =~ 'longest' + call s:EnableLongestEnhancement() + endif + + if !pumvisible() + let b:complTypeManual = '' + endif + " exception: if in mode, then should move up the list, and " down the list. - if a:command == 'p' && g:complType == "\" - return "\" + if a:command == 'p' && !b:complReset && + \ (b:complType == "\" || + \ (b:complType == 'context' && + \ b:complTypeManual == '' && + \ b:complTypeContext == "\")) + return "\" + + elseif a:command == 'p' && !b:complReset && + \ (b:complType == "\" || + \ (b:complType == 'context' && + \ b:complTypeManual == '' && + \ b:complTypeContext == "\")) + return "\" + + " already in completion mode and not resetting for longest enhancement, so + " just scroll to next/previous + elseif pumvisible() && !b:complReset + let type = b:complType == 'context' ? b:complTypeContext : b:complType + if a:command == 'n' + return type == "\" ? "\" : "\" + endif + return type == "\" ? "\" : "\" endif - return g:complType - else - return "\" + + " handle 'context' completion. + if b:complType == 'context' + let complType = s:ContextCompletion() + if complType == '' + exec "let complType = \"" . + \ escape(g:SuperTabContextDefaultCompletionType, '<') . "\"" + endif + let b:complTypeContext = complType + + " Hack to workaround bug when invoking command line completion via = + elseif b:complType == "\\" + let complType = s:CommandLineCompletion() + else + let complType = b:complType + endif + + " highlight first result if longest enabled + if g:SuperTabLongestHighlight && + \ &completeopt =~ 'longest' && + \ &completeopt =~ 'menu' && + \ (!pumvisible() || b:complReset) + let dir = (complType == "\") ? -1 : 1 + call feedkeys("\=SuperTabLongestHighlight(" . dir . ")\", 'n') + endif + + if b:complReset + let b:complReset = 0 + " not an accurate condition for everyone, but better than sending + " at the wrong time. + if pumvisible() + return "\" . complType + endif + endif + + return complType endif + + return g:SuperTabMappingForward == '' ? "\" : '' endfunction " }}} " s:SuperTabHelp() {{{ " Opens a help window where the user can choose a completion type to enter. function! s:SuperTabHelp() + let winnr = winnr() if bufwinnr("SuperTabHelp") == -1 botright split SuperTabHelp @@ -232,122 +465,360 @@ function! s:SuperTabHelp() else exec bufwinnr("SuperTabHelp") . "winc w" endif + let b:winnr = winnr endfunction " }}} -" s:SetCompletionType() {{{ -" Sets the completion type based on what the user has chosen from the help -" buffer. -function! s:SetCompletionType () - let chosen = substitute(getline('.'), '.*|\(.*\)|.*', '\1', '') - if chosen != getline('.') - call SuperTabSetCompletionType(chosen) - close - winc p +" s:WillComplete() {{{ +" Determines if completion should be kicked off at the current location. +function! s:WillComplete() + if pumvisible() + return 1 endif -endfunction " }}} -" s:WillComplete () {{{ -" Determines if completion should be kicked off at the current location. -function! s:WillComplete () let line = getline('.') let cnum = col('.') - " Start of line. - let prev_char = strpart(line, cnum - 2, 1) - if prev_char =~ '^\s*$' - return 0 - endif + " honor SuperTabNoCompleteAfter + let pre = cnum >= 2 ? line[:cnum - 2] : '' + for pattern in b:SuperTabNoCompleteAfter + if pre =~ pattern . '$' + return 0 + endif + endfor + " honor SuperTabNoCompleteBefore " Within a word, but user does not have mid word completion enabled. - let next_char = strpart(line, cnum - 1, 1) - if !g:SuperTabMidWordCompletion && s:IsWordChar(next_char) - return 0 - endif - - " In keyword completion mode and no preceding word characters. - "if (g:complType == "\" || g:complType == "\") && !s:IsWordChar(prev_char) - " return 0 - "endif + let post = line[cnum - 1:] + for pattern in b:SuperTabNoCompleteBefore + if post =~ '^' . pattern + return 0 + endif + endfor return 1 endfunction " }}} -" s:IsWordChar(char) {{{ -" Determines if the supplied character is a word character or matches value -" defined by 'iskeyword'. -function! s:IsWordChar (char) - if a:char =~ '\w' - return 1 +function! s:EnableLongestEnhancement() " {{{ + augroup supertab_reset + autocmd! + autocmd InsertLeave,CursorMovedI + \ call s:ReleaseKeyPresses() | autocmd! supertab_reset + augroup END + call s:CaptureKeyPresses() +endfunction " }}} + +function! s:CompletionReset(char) " {{{ + let b:complReset = 1 + 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 = { + \ '': maparg('', 'i'), + \ '': maparg('', 'i'), + \ } + " TODO: use &keyword to get an accurate list of chars to map + for c in split('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_', '.\zs') + exec 'imap ' . c . ' =CompletionReset("' . c . '")' + endfor + imap =CompletionReset("\bs>") + imap =CompletionReset("\c-h>") endif +endfunction " }}} - " check against 'iskeyword' - let values = &iskeyword - let index = stridx(values, ',') - while index > 0 || values != '' - if index > 0 - let value = strpart(values, 0, index) - let values = strpart(values, index + 1) - else - let value = values - let values = '' +function! s:ClosePreview() " {{{ + if exists('b:supertab_close_preview') && b:supertab_close_preview + let preview = 0 + for bufnum in tabpagebuflist() + if getwinvar(bufwinnr(bufnum), '&previewwindow') + let preview = 1 + break + endif + endfor + if preview + pclose endif + unlet b:supertab_close_preview + endif +endfunction " }}} - " exception case for '^,' - if value == '^' - let value = '^,' - - " execption case for ',' - elseif value =~ '^,,' - let values .= strpart(value, 2) - let value = ',' +function! s:ReleaseKeyPresses() " {{{ + if exists('b:capturing') && b:capturing + let b:capturing = 0 + for c in split('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_', '.\zs') + exec 'iunmap ' . c + endfor + + iunmap + iunmap + + " restore any previous mappings + for [key, rhs] in items(b:captured) + if rhs != '' + let args = substitute(rhs, '.*\(".\{-}"\).*', '\1', '') + if args != rhs + let args = substitute(args, '<', '', 'g') + let expr = substitute(rhs, '\(.*\)".\{-}"\(.*\)', '\1%s\2', '') + let rhs = printf(expr, args) + endif + exec printf("imap %s %s", key, rhs) + endif + endfor + unlet b:captured - " execption case after a ^, - elseif value =~ '^,' - let value = strpart(value, 1) + 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 " }}} - " keyword values is an ascii number range - if value =~ '[0-9]\+-[0-9]\+' - let charnum = char2nr(a:char) - exec 'let start = ' . substitute(value, '\([0-9]\+\)-.*', '\1', '') - exec 'let end = ' . substitute(value, '.*-\([0-9]\+\)', '\1', '') +" s:CommandLineCompletion() {{{ +" Hack needed to account for apparent bug in vim command line mode completion +" when invoked via = +function! s:CommandLineCompletion() + " 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 + " to check for. + let b:complCommandLine = 1 + return "\\:call feedkeys('\\\', 'n') | " . + \ "let b:complCommandLine = 0\" +endfunction " }}} - if charnum >= start && charnum <= end - return 1 +function! s:ContextCompletion() " {{{ + let contexts = exists('b:SuperTabCompletionContexts') ? + \ b:SuperTabCompletionContexts : g:SuperTabCompletionContexts + + for context in contexts + try + let Context = function(context) + let complType = Context() + unlet Context + if type(complType) == 1 && complType != '' + return complType endif + catch /E700/ + echohl Error + echom 'supertab: no context function "' . context . '" found.' + echohl None + endtry + endfor + return '' +endfunction " }}} - " keyword value is a set of include or exclude characters - else - let include = 1 - if value =~ '^\^' - let value = strpart(value, 1) - let include = 0 +function! s:ContextDiscover() " {{{ + let discovery = exists('g:SuperTabContextDiscoverDiscovery') ? + \ g:SuperTabContextDiscoverDiscovery : [] + + " loop through discovery list to find the default + if !empty(discovery) + for pair in discovery + let var = substitute(pair, '\(.*\):.*', '\1', '') + let type = substitute(pair, '.*:\(.*\)', '\1', '') + exec 'let value = ' . var + if value !~ '^\s*$' && value != '0' + exec "let complType = \"" . escape(type, '<') . "\"" + return complType endif + endfor + endif +endfunction " }}} - if a:char =~ '[' . escape(value, '[]') . ']' - return include - endif +function! s:ContextText() " {{{ + let exclusions = exists('g:SuperTabContextTextFileTypeExclusions') ? + \ g:SuperTabContextTextFileTypeExclusions : [] + + if index(exclusions, &ft) == -1 + let curline = getline('.') + let cnum = col('.') + let synname = synIDattr(synID(line('.'), cnum - 1, 1), 'name') + if curline =~ '.*/\w*\%' . cnum . 'c' || + \ ((has('win32') || has('win64')) && curline =~ '.*\\\w*\%' . cnum . 'c') + return "\\" + + elseif curline =~ '.*\(\w\|[\])]\)\(\.\|::\|->\)\w*\%' . cnum . 'c' && + \ synname !~ '\(String\|Comment\)' + let omniPrecedence = exists('g:SuperTabContextTextOmniPrecedence') ? + \ g:SuperTabContextTextOmniPrecedence : ['&completefunc', '&omnifunc'] + + for omniFunc in omniPrecedence + if omniFunc !~ '^&' + let omniFunc = '&' . omniFunc + endif + if getbufvar(bufnr('%'), omniFunc) != '' + return omniFunc == '&omnifunc' ? "\\" : "\\" + endif + endfor endif - let index = stridx(values, ',') - endwhile + endif +endfunction " }}} + +function! s:ExpandMap(map) " {{{ + let map = a:map + if map =~ '' + let plug = substitute(map, '.\{-}\(\w\+\).*', '\1', '') + let plug_map = maparg(plug, 'i') + let map = substitute(map, '.\{-}\(\w\+\).*', plug_map, '') + endif + return map +endfunction " }}} - return 0 +function! SuperTabChain(completefunc, completekeys) " {{{ + let b:SuperTabChain = [a:completefunc, a:completekeys] + setlocal completefunc=SuperTabCodeComplete endfunction " }}} +function! SuperTabCodeComplete(findstart, base) " {{{ + if !exists('b:SuperTabChain') + echoe 'No completion chain has been set.' + return -2 + endif + + if len(b:SuperTabChain) != 2 + echoe 'Completion chain can only be used with 1 completion function ' . + \ 'and 1 fallback completion key binding.' + return -2 + endif + + let Func = function(b:SuperTabChain[0]) + + if a:findstart + let start = Func(a:findstart, a:base) + if start >= 0 + return start + endif + + return col('.') - 1 + endif + + let results = Func(a:findstart, a:base) + if len(results) + return results + endif + + exec 'let keys = "' . escape(b:SuperTabChain[1], '<') . '"' + call feedkeys("\" . keys, 'nt') + return [] +endfunction " }}} + +" Autocmds {{{ + if g:SuperTabClosePreviewOnPopupClose + augroup supertab_close_preview + autocmd! + autocmd InsertLeave,CursorMovedI * call s:ClosePreview() + augroup END + endif +" }}} + " Key Mappings {{{ - im =CtrlXPP() + " map a regular tab to ctrl-tab (note: doesn't work in console vim) + exec 'inoremap ' . g:SuperTabMappingTabLiteral . ' ' + + imap =ManualCompletionEnter() + + imap