citadel

My dotfiles, scripts and nix configs
git clone git://jb55.com/citadel
Log | Files | Refs | README | LICENSE

supertab.vba (30804B)


      1 " Vimball Archiver by Charles E. Campbell, Jr., Ph.D.
      2 UseVimball
      3 finish
      4 doc/supertab.txt	[[[1
      5 308
      6 *supertab.txt*
      7 
      8 Authors:
      9   Original: Gergely Kontra <kgergely@mcl.hu>
     10   Current:  Eric Van Dewoestine <ervandew@gmail.com> (as of version 0.4)
     11 
     12 Contributors:
     13   Christophe-Marie Duquesne <chm.duquesne@gmail.com> (documentation)
     14 
     15 Please direct all correspondence to Eric.
     16 
     17 This plugin is licensed under the terms of the BSD License.  Please see
     18 supertab.vim for the license in its entirety.
     19 
     20 ==============================================================================
     21 Supertab                                    *supertab*
     22 
     23 1. Introduction                         |supertab-intro|
     24 2. Supertab Usage                       |supertab-usage|
     25 3. Supertab Options                     |supertab-options|
     26     Default completion type             |supertab-defaultcompletion|
     27     Secondary default completion type   |supertab-contextdefault|
     28     Completion contexts                 |supertab-completioncontexts|
     29         Context text                    |supertab-contexttext|
     30         Context Discover                |supertab-contextdiscover|
     31         Example                         |supertab-contextexample|
     32     Completion Duration                 |supertab-duration|
     33     Midword completion                  |supertab-midword|
     34     Changing default mapping            |supertab-forwardbackward|
     35     Inserting true tabs                 |supertab-mappingtabliteral|
     36     Preselecting the first entry        |supertab-longesthighlight|
     37 
     38 ==============================================================================
     39 1. Introduction                             *supertab-intro*
     40 
     41 Supertab is a plugin which allows you to perform all your insert completion
     42 (|ins-completion|) using the tab key.
     43 
     44 Supertab requires Vim version 7.0 or above.
     45 
     46 ==============================================================================
     47 2. Supertab usage                           *supertab-usage*
     48 
     49 Using Supertab is as easy as hitting <Tab> or <S-Tab> (shift+tab) while in
     50 insert mode, with at least one non whitespace character before the cursor, to
     51 start the completion and then <Tab> or <S-Tab> again to cycle forwards or
     52 backwards through the available completions.
     53 
     54 Example ('|' denotes the cursor location):
     55 
     56 bar
     57 baz
     58 b|<Tab>    Hitting <Tab> here will start the completion, allowing you to
     59            then cycle through the suggested words ('bar' and 'baz').
     60 
     61 ==============================================================================
     62 3. Supertab Options                         *supertab-options*
     63 
     64 Supertab is configured via several global variables that you can set in your
     65 |vimrc| file according to your needs. Below is a comprehensive list of
     66 the variables available.
     67 
     68 g:SuperTabDefaultCompletionType             |supertab-defaultcompletion|
     69   The default completion type to use. If you program in languages that support
     70   omni or user completions, it is highly recommended setting this to
     71   'context'.
     72 
     73   For help about built in completion types in vim, see |i_CTRL-X_index|.
     74 
     75 g:SuperTabContextDefaultCompletionType      |supertab-contextdefault|
     76   The default completion type to use when 'context' is the global default, but
     77   context completion has determined that neither omni, user, or file
     78   completion should be used in the current context.
     79 
     80 g:SuperTabCompletionContexts                |supertab-completioncontexts|
     81   Used to configure a list of function names which are used when the global
     82   default type is 'context'. These functions will be consulted in order to
     83   determine which completion type to use. Advanced users can plug in their own
     84   functions here to customize their 'context' completion.
     85 
     86 g:SuperTabRetainCompletionDuration          |supertab-duration|
     87   This setting determines how long a non-default completion type should be
     88   retained as the temporary default. By default supertab will retain the
     89   alternate completion type until you leave insert mode.
     90 
     91 g:SuperTabMidWordCompletion                 |supertab-midword|
     92   This can be used to turn off completion if you are in the middle of a word
     93   (word characters immediately preceding and following the cursor).
     94 
     95 g:SuperTabMappingForward                    |supertab-forwardbackward|
     96 g:SuperTabMappingBackward                   |supertab-forwardbackward|
     97   If using the tab key for completion isn't for you, then you can use these to
     98   set an alternate key to be used for your insert completion needs.
     99 
    100 g:SuperTabMappingTabLiteral                 |supertab-mappingtabliteral|
    101   For those rare cases where supertab would normal want to start insert
    102   completion, but you just want to insert a tab, this setting is used to
    103   define the key combination to use to do just that.  By default Ctrl-Tab is
    104   used.
    105 
    106 g:SuperTabLongestHighlight                  |supertab-longesthighlight|
    107   When enabled and you have the completion popup enable and 'longest' in your
    108   completeopt, supertab will auto highlight the first selection in the popup.
    109 
    110 
    111 Default Completion Type             *supertab-defaultcompletion*
    112                                     *g:SuperTabDefaultCompletionType*
    113 
    114 g:SuperTabDefaultCompletionType (default value: "<c-p>")
    115 
    116 Used to set the default completion type. There is no need to escape this
    117 value as that will be done for you when the type is set.
    118 
    119   Example: setting the default completion to 'user' completion:
    120 
    121     let g:SuperTabDefaultCompletionType = "<c-x><c-u>"
    122 
    123 Note: a special value of 'context' is supported which will result in
    124 super tab attempting to use the text preceding the cursor to decide which
    125 type of completion to attempt.  Currently super tab can recognize method
    126 calls or attribute references via '.', '::' or '->', and file path
    127 references containing '/'.
    128 
    129     let g:SuperTabDefaultCompletionType = "context"
    130 
    131     /usr/l<tab>     # will use filename completion
    132     myvar.t<tab>    # will use user completion if completefunc set,
    133                     # or omni completion if omnifunc set.
    134     myvar-><tab>    # same as above
    135 
    136 When using context completion, super tab will fall back to a secondary default
    137 completion type set by |g:SuperTabContextDefaultCompletionType|.
    138 
    139 Note: once the buffer has been initialized, changing the value of this setting
    140 will not change the default complete type used.  If you want to change the
    141 default completion type for the current buffer after it has been set, perhaps
    142 in an ftplugin, you'll need to call SuperTabSetDefaultCompletionType like so,
    143 supplying the completion type you wish to switch to:
    144 
    145     call SuperTabSetDefaultCompletionType("<c-x><c-u>")
    146 
    147 
    148 Secondary default completion type   *supertab-contextdefault*
    149                                     *g:SuperTabContextDefaultCompletionType*
    150 
    151 g:SuperTabContextDefaultCompletionType (default value: "<c-p>")
    152 
    153 Sets the default completion type used when g:SuperTabDefaultCompletionType is
    154 set to 'context' and no completion type is returned by any of the configured
    155 contexts.
    156 
    157 
    158 Completion contexts                 *supertab-completioncontexts*
    159                                     *g:SuperTabCompletionContexts*
    160 
    161 g:SuperTabCompletionContexts (default value: ['s:ContextText'])
    162 
    163 Sets the list of contexts used for context completion.  This value should
    164 be a list of function names which provide the context implementation.
    165 
    166 When supertab starts the default completion, each of these contexts will be
    167 consulted, in the order they were supplied, to determine the completion type
    168 to use.  If a context returns a completion type, that type will be used,
    169 otherwise the next context in the list will be consulted.  If after executing
    170 all the context functions, no completion type has been determined, then the
    171 value of g:SuperTabContextDefaultCompletionType will be used.
    172 
    173 Built in completion contexts:
    174 
    175   s:ContextText                     *supertab-contexttext*
    176 
    177   The text context will examine the text near the cursor to decide which type
    178   of completion to attempt.  Currently the text context can recognize method
    179   calls or attribute references via '.', '::' or '->', and file path
    180   references containing '/'.
    181 
    182       /usr/l<tab>  # will use filename completion
    183       myvar.t<tab> # will use user completion if completefunc set, or
    184                    # omni completion if omnifunc set.
    185       myvar-><tab> # same as above
    186 
    187   Supported configuration attributes:
    188 
    189     g:SuperTabContextTextFileTypeExclusions
    190     List of file types for which the text context will be skipped.
    191 
    192     g:SuperTabContextTextOmniPrecedence
    193     List of omni completion option names in the order of precedence that they
    194     should be used if available. By default, user completion will be given
    195     precedence over omni completion, but you can use this variable to give
    196     omni completion higher precedence by placing it first in the list.
    197 
    198   s:ContextDiscover                 *supertab-contextdiscover*
    199 
    200   This context will use the 'g:SuperTabContextDiscoverDiscovery' variable to
    201   determine the completion type to use.  It will evaluate each value, in the
    202   order they were defined, until a variable evaluates to a non-zero or
    203   non-empty value, then the associated completion type is used.
    204 
    205   Supported configuration properties:
    206 
    207     g:SuperTabContextDiscoverDiscovery
    208     List of variable:completionType mappings.
    209 
    210   Example context configuration:    *supertab-contextexample*
    211 
    212     let g:SuperTabCompletionContexts = ['s:ContextText', 's:ContextDiscover']
    213     let g:SuperTabContextTextOmniPrecedence = ['&omnifunc', '&completefunc']
    214     let g:SuperTabContextDiscoverDiscovery =
    215         \ ["&completefunc:<c-x><c-u>", "&omnifunc:<c-x><c-o>"]
    216 
    217   In addition to the default completion contexts, you can plug in your own
    218   implementation by creating a globally accessible function that returns
    219   the completion type to use (eg. "\<c-x>\<c-u>").
    220 
    221     function MyTagContext()
    222       if filereadable(expand('%:p:h') . '/tags')
    223         return "\<c-x>\<c-]>"
    224       endif
    225       " no return will result in the evaluation of the next
    226       " configured context
    227     endfunction
    228     let g:SuperTabCompletionContexts =
    229         \ ['MyTagContext', 's:ContextText', 's:ContextDiscover']
    230 
    231   Note: supertab also supports the b:SuperTabCompletionContexts variable
    232   allowing you to set the list of contexts separately for the current buffer,
    233   like from an ftplugin for example.
    234 
    235 
    236 Completion Duration                 *supertab-duration*
    237                                     *g:SuperTabRetainCompletionDuration*
    238 
    239 g:SuperTabRetainCompletionDuration (default value: 'insert')
    240 
    241 Determines if, and for how long, the current completion type is retained.
    242 The possible values include:
    243 'completion' - The current completion type is only retained for the
    244                current completion.  Once you have chosen a completion
    245                result or exited the completion mode, the default
    246                completion type is restored.
    247 'insert'     - The current completion type is saved until you exit insert
    248                mode (via ESC).  Once you exit insert mode the default
    249                completion type is restored. (supertab default)
    250 'session'    - The current completion type is saved for the duration of
    251                your vim session or until you enter a different completion
    252                mode.
    253 
    254 
    255 Midword completion                  *supertab-midword*
    256                                     *g:SuperTabMidWordCompletion*
    257 
    258 g:SuperTabMidWordCompletion (default value: 1)
    259 
    260 Sets whether or not mid word completion is enabled. When enabled, <tab> will
    261 kick off completion when ever a non whitespace character is to the left of the
    262 cursor.  When disabled, completion will only occur if the char to the left is
    263 non whitespace char and the char to the right is not a keyword character (you
    264 are at the end of the word).
    265 
    266 
    267 Changing the default mapping        *supertab-forwardbackward*
    268                                     *g:SuperTabMappingForward*
    269                                     *g:SuperTabMappingBackward*
    270 
    271 g:SuperTabMappingForward  (default value: '<tab>')
    272 g:SuperTabMappingBackward (default value: '<s-tab>')
    273 
    274 These two variables allow you to set the keys used to kick off the current
    275 completion.  By default this is <tab> and <s-tab>.  To change to something
    276 like <c-space> and <s-c-space>, you can add the following to your |vimrc|.
    277 
    278         let g:SuperTabMappingForward = '<c-space>'
    279         let g:SuperTabMappingBackward = '<s-c-space>'
    280 
    281 Note: if the above does not have the desired effect (which may happen in
    282 console version of vim), you can try the following mappings.  Although the
    283 backwards mapping still doesn't seem to work in the console for me, your
    284 milage may vary.
    285 
    286         let g:SuperTabMappingForward = '<nul>'
    287         let g:SuperTabMappingBackward = '<s-nul>'
    288 
    289 
    290 Inserting true tabs                 *supertab-mappingtabliteral*
    291                                     *g:SuperTabMappingTabLiteral*
    292 
    293 g:SuperTabMappingTabLiteral (default value: '<c-tab>')
    294 
    295 Sets the key mapping used to insert a literal tab where supertab would
    296 otherwise attempt to kick off insert completion. The default is '<c-tab>'
    297 (ctrl-tab) which unfortunately might not work at the console. So if you are
    298 using a console vim and want this functionality, you may have to change it to
    299 something that is supported.  Alternatively, you can escape the <tab> with
    300 <c-v> (see |i_CTRL-V| for more infos).
    301 
    302 
    303 Preselecting the first entry        *supertab-longesthighlight*
    304                                     *g:SuperTabLongestHighlight*
    305 
    306 g:SuperTabLongestHighlight (default value: 0)
    307 
    308 Sets whether or not to pre-highlight the first match when completeopt has the
    309 popup menu enabled and the 'longest' option as well. When enabled, <tab> will
    310 kick off completion and pre-select the first entry in the popup menu, allowing
    311 you to simply hit <enter> to use it.
    312 
    313 vim:tw=78:ts=8:ft=help:norl:
    314 plugin/supertab.vim	[[[1
    315 510
    316 " Author:
    317 "   Original: Gergely Kontra <kgergely@mcl.hu>
    318 "   Current:  Eric Van Dewoestine <ervandew@gmail.com> (as of version 0.4)
    319 "   Please direct all correspondence to Eric.
    320 " Version: 1.0
    321 " GetLatestVimScripts: 1643 1 :AutoInstall: supertab.vim
    322 "
    323 " Description: {{{
    324 "   Use your tab key to do all your completion in insert mode!
    325 "   You can cycle forward and backward with the <Tab> and <S-Tab> keys
    326 "   Note: you must press <Tab> once to be able to cycle back
    327 "
    328 "   http://www.vim.org/scripts/script.php?script_id=1643
    329 " }}}
    330 "
    331 " License: {{{
    332 "   Software License Agreement (BSD License)
    333 "
    334 "   Copyright (c) 2002 - 2009
    335 "   All rights reserved.
    336 "
    337 "   Redistribution and use of this software in source and binary forms, with
    338 "   or without modification, are permitted provided that the following
    339 "   conditions are met:
    340 "
    341 "   * Redistributions of source code must retain the above
    342 "     copyright notice, this list of conditions and the
    343 "     following disclaimer.
    344 "
    345 "   * Redistributions in binary form must reproduce the above
    346 "     copyright notice, this list of conditions and the
    347 "     following disclaimer in the documentation and/or other
    348 "     materials provided with the distribution.
    349 "
    350 "   * Neither the name of Gergely Kontra or Eric Van Dewoestine nor the names
    351 "   of its contributors may be used to endorse or promote products derived
    352 "   from this software without specific prior written permission of Gergely
    353 "   Kontra or Eric Van Dewoestine.
    354 "
    355 "   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
    356 "   IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
    357 "   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    358 "   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    359 "   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    360 "   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    361 "   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    362 "   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    363 "   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    364 "   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    365 "   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    366 " }}}
    367 "
    368 " Testing Info: {{{
    369 "   Running vim + supertab with the absolute bar minimum settings:
    370 "     $ vim -u NONE -U NONE -c "set nocp | runtime plugin/supertab.vim"
    371 " }}}
    372 
    373 if v:version < 700
    374   finish
    375 endif
    376 
    377 if exists('complType') " Integration with other completion functions.
    378   finish
    379 endif
    380 
    381 let s:save_cpo=&cpo
    382 set cpo&vim
    383 
    384 " Global Variables {{{
    385 
    386   if !exists("g:SuperTabDefaultCompletionType")
    387     let g:SuperTabDefaultCompletionType = "<c-p>"
    388   endif
    389 
    390   if !exists("g:SuperTabContextDefaultCompletionType")
    391     let g:SuperTabContextDefaultCompletionType = "<c-p>"
    392   endif
    393 
    394   if !exists("g:SuperTabCompletionContexts")
    395     let g:SuperTabCompletionContexts = ['s:ContextText']
    396   endif
    397 
    398   if !exists("g:SuperTabRetainCompletionDuration")
    399     let g:SuperTabRetainCompletionDuration = 'insert'
    400   endif
    401 
    402   if !exists("g:SuperTabMidWordCompletion")
    403     let g:SuperTabMidWordCompletion = 1
    404   endif
    405 
    406   if !exists("g:SuperTabMappingForward")
    407     let g:SuperTabMappingForward = '<tab>'
    408   endif
    409   if !exists("g:SuperTabMappingBackward")
    410     let g:SuperTabMappingBackward = '<s-tab>'
    411   endif
    412 
    413   if !exists("g:SuperTabMappingTabLiteral")
    414     let g:SuperTabMappingTabLiteral = '<c-tab>'
    415   endif
    416 
    417   if !exists("g:SuperTabLongestHighlight")
    418     let g:SuperTabLongestHighlight = 0
    419   endif
    420 
    421 " }}}
    422 
    423 " Script Variables {{{
    424 
    425   " construct the help text.
    426   let s:tabHelp =
    427     \ "Hit <CR> or CTRL-] on the completion type you wish to switch to.\n" .
    428     \ "Use :help ins-completion for more information.\n" .
    429     \ "\n" .
    430     \ "|<c-n>|      - Keywords in 'complete' searching down.\n" .
    431     \ "|<c-p>|      - Keywords in 'complete' searching up (SuperTab default).\n" .
    432     \ "|<c-x><c-l>| - Whole lines.\n" .
    433     \ "|<c-x><c-n>| - Keywords in current file.\n" .
    434     \ "|<c-x><c-k>| - Keywords in 'dictionary'.\n" .
    435     \ "|<c-x><c-t>| - Keywords in 'thesaurus', thesaurus-style.\n" .
    436     \ "|<c-x><c-i>| - Keywords in the current and included files.\n" .
    437     \ "|<c-x><c-]>| - Tags.\n" .
    438     \ "|<c-x><c-f>| - File names.\n" .
    439     \ "|<c-x><c-d>| - Definitions or macros.\n" .
    440     \ "|<c-x><c-v>| - Vim command-line.\n" .
    441     \ "|<c-x><c-u>| - User defined completion.\n" .
    442     \ "|<c-x><c-o>| - Omni completion.\n" .
    443     \ "|<c-x>s|     - Spelling suggestions."
    444 
    445   " set the available completion types and modes.
    446   let s:types =
    447     \ "\<c-e>\<c-y>\<c-l>\<c-n>\<c-k>\<c-t>\<c-i>\<c-]>" .
    448     \ "\<c-f>\<c-d>\<c-v>\<c-n>\<c-p>\<c-u>\<c-o>\<c-n>\<c-p>s"
    449   let s:modes = '/^E/^Y/^L/^N/^K/^T/^I/^]/^F/^D/^V/^P/^U/^O/s'
    450   let s:types = s:types . "np"
    451   let s:modes = s:modes . '/n/p'
    452 
    453 " }}}
    454 
    455 " SuperTabSetDefaultCompletionType(type) {{{
    456 " Globally available function that users can use to set the default
    457 " completion type for the current buffer, like in an ftplugin.
    458 function! SuperTabSetDefaultCompletionType(type)
    459   " init hack for <c-x><c-v> workaround.
    460   let b:complCommandLine = 0
    461 
    462   let b:SuperTabDefaultCompletionType = a:type
    463 
    464   " set the current completion type to the default
    465   call SuperTabSetCompletionType(b:SuperTabDefaultCompletionType)
    466 endfunction " }}}
    467 
    468 " SuperTabSetCompletionType(type) {{{
    469 " Globally available function that users can use to create mappings to quickly
    470 " switch completion modes.  Useful when a user wants to restore the default or
    471 " switch to another mode without having to kick off a completion of that type
    472 " or use SuperTabHelp.  Note, this function only changes the current
    473 " completion type, not the default, meaning that the default will still be
    474 " restored once the configured retension duration has been met (see
    475 " g:SuperTabRetainCompletionDuration).  To change the default for the current
    476 " buffer, use SuperTabDefaultCompletionType(type) instead.  Example mapping to
    477 " restore SuperTab default:
    478 "   nmap <F6> :call SetSuperTabCompletionType("<c-p>")<cr>
    479 function! SuperTabSetCompletionType(type)
    480   exec "let b:complType = \"" . escape(a:type, '<') . "\""
    481 endfunction " }}}
    482 
    483 " SuperTabAlternateCompletion(type) {{{
    484 " Function which can be mapped to a key to kick off an alternate completion
    485 " other than the default.  For instance, if you have 'context' as the default
    486 " and want to map ctrl+space to issue keyword completion.
    487 " Note: due to the way vim expands ctrl characters in mappings, you cannot
    488 " create the alternate mapping like so:
    489 "    imap <c-space> <c-r>=SuperTabAlternateCompletion("<c-p>")<cr>
    490 " instead, you have to use \<lt> to prevent vim from expanding the key
    491 " when creating the mapping.
    492 "    gvim:
    493 "      imap <c-space> <c-r>=SuperTabAlternateCompletion("\<lt>c-p>")<cr>
    494 "    console:
    495 "      imap <nul> <c-r>=SuperTabAlternateCompletion("\<lt>c-p>")<cr>
    496 function! SuperTabAlternateCompletion(type)
    497   call SuperTabSetCompletionType(a:type)
    498   " end any current completion before attempting to start the new one.
    499   " use feedkeys to prevent possible remapping of <c-e> from causing issues.
    500   "call feedkeys("\<c-e>", 'n')
    501   " ^ since we can't detect completion mode vs regular insert mode, we force
    502   " vim into keyword completion mode and end that mode to prevent the regular
    503   " insert behavior of <c-e> from occurring.
    504   call feedkeys("\<c-x>\<c-p>\<c-e>", 'n')
    505   call feedkeys(b:complType)
    506   return ''
    507 endfunction " }}}
    508 
    509 " s:Init {{{
    510 " Global initilization when supertab is loaded.
    511 function! s:Init()
    512   augroup supertab_init
    513     autocmd!
    514     autocmd BufEnter * call <SID>InitBuffer()
    515   augroup END
    516 
    517   " ensure InitBuffer gets called for the first buffer, after the ftplugins
    518   " have been called.
    519   augroup supertab_init_first
    520     autocmd!
    521     autocmd FileType <buffer> call <SID>InitBuffer()
    522   augroup END
    523 
    524   " Setup mechanism to restore orignial completion type upon leaving insert
    525   " mode if configured to do so
    526   if g:SuperTabRetainCompletionDuration == 'insert'
    527     augroup supertab_retain
    528       autocmd!
    529       autocmd InsertLeave * call s:SetDefaultCompletionType()
    530     augroup END
    531   endif
    532 endfunction " }}}
    533 
    534 " s:InitBuffer {{{
    535 " Per buffer initilization.
    536 function! s:InitBuffer()
    537   if exists("b:complType")
    538     return
    539   endif
    540 
    541   " init hack for <c-x><c-v> workaround.
    542   let b:complCommandLine = 0
    543 
    544   let b:SuperTabDefaultCompletionType = g:SuperTabDefaultCompletionType
    545 
    546   " set the current completion type to the default
    547   call SuperTabSetCompletionType(b:SuperTabDefaultCompletionType)
    548 endfunction " }}}
    549 
    550 " s:ManualCompletionEnter() {{{
    551 " Handles manual entrance into completion mode.
    552 function! s:ManualCompletionEnter()
    553   if &smd
    554     echo '' | echohl ModeMsg | echo '-- ^X++ mode (' . s:modes . ')' | echohl None
    555   endif
    556   let complType = nr2char(getchar())
    557   if stridx(s:types, complType) != -1
    558     if stridx("\<c-e>\<c-y>", complType) != -1 " no memory, just scroll...
    559       return "\<c-x>" . complType
    560     elseif stridx('np', complType) != -1
    561       let complType = nr2char(char2nr(complType) - 96)
    562     else
    563       let complType = "\<c-x>" . complType
    564     endif
    565 
    566     if index(['insert', 'session'], g:SuperTabRetainCompletionDuration) != -1
    567       let b:complType = complType
    568     endif
    569 
    570     " Hack to workaround bug when invoking command line completion via <c-r>=
    571     if complType == "\<c-x>\<c-v>"
    572       return s:CommandLineCompletion()
    573     endif
    574 
    575     return complType
    576   endif
    577 
    578   echohl "Unknown mode"
    579   return complType
    580 endfunction " }}}
    581 
    582 " s:SetCompletionType() {{{
    583 " Sets the completion type based on what the user has chosen from the help
    584 " buffer.
    585 function! s:SetCompletionType()
    586   let chosen = substitute(getline('.'), '.*|\(.*\)|.*', '\1', '')
    587   if chosen != getline('.')
    588     let winnr = b:winnr
    589     close
    590     exec winnr . 'winc w'
    591     call SuperTabSetCompletionType(chosen)
    592   endif
    593 endfunction " }}}
    594 
    595 " s:SetDefaultCompletionType() {{{
    596 function! s:SetDefaultCompletionType()
    597   if exists('b:SuperTabDefaultCompletionType') &&
    598   \ (!exists('b:complCommandLine') || !b:complCommandLine)
    599     call SuperTabSetCompletionType(b:SuperTabDefaultCompletionType)
    600   endif
    601 endfunction " }}}
    602 
    603 " s:SuperTab(command) {{{
    604 " Used to perform proper cycle navigation as the user requests the next or
    605 " previous entry in a completion list, and determines whether or not to simply
    606 " retain the normal usage of <tab> based on the cursor position.
    607 function! s:SuperTab(command)
    608   if s:WillComplete()
    609     " rare case where no autocmds have fired for this buffer to initialize the
    610     " supertab vars.
    611     call s:InitBuffer()
    612 
    613     let key = ''
    614     " highlight first result if longest enabled
    615     if g:SuperTabLongestHighlight && !pumvisible() && &completeopt =~ 'longest'
    616       let key = (b:complType == "\<c-p>") ? "\<c-p>" : "\<c-n>"
    617     endif
    618 
    619     " exception: if in <c-p> mode, then <c-n> should move up the list, and
    620     " <c-p> down the list.
    621     if a:command == 'p' &&
    622       \ (b:complType == "\<c-p>" ||
    623       \   (b:complType == 'context' &&
    624       \    tolower(g:SuperTabContextDefaultCompletionType) == '<c-p>'))
    625       return "\<c-n>"
    626     elseif a:command == 'p' &&
    627       \ (b:complType == "\<c-n>" ||
    628       \   (b:complType == 'context' &&
    629       \    tolower(g:SuperTabContextDefaultCompletionType) == '<c-n>'))
    630       return "\<c-p>"
    631     endif
    632 
    633     " handle 'context' completion.
    634     if b:complType == 'context'
    635       let complType = s:ContextCompletion()
    636       if complType == ''
    637         exec "let complType = \"" .
    638           \ escape(g:SuperTabContextDefaultCompletionType, '<') . "\""
    639       endif
    640       return complType . key
    641     endif
    642 
    643     " Hack to workaround bug when invoking command line completion via <c-r>=
    644     if b:complType == "\<c-x>\<c-v>"
    645       return s:CommandLineCompletion()
    646     endif
    647     return b:complType . key
    648   endif
    649 
    650   return "\<tab>"
    651 endfunction " }}}
    652 
    653 " s:SuperTabHelp() {{{
    654 " Opens a help window where the user can choose a completion type to enter.
    655 function! s:SuperTabHelp()
    656   let winnr = winnr()
    657   if bufwinnr("SuperTabHelp") == -1
    658     botright split SuperTabHelp
    659 
    660     setlocal noswapfile
    661     setlocal buftype=nowrite
    662     setlocal bufhidden=delete
    663 
    664     let saved = @"
    665     let @" = s:tabHelp
    666     silent put
    667     call cursor(1, 1)
    668     silent 1,delete
    669     call cursor(4, 1)
    670     let @" = saved
    671     exec "resize " . line('$')
    672 
    673     syntax match Special "|.\{-}|"
    674 
    675     setlocal readonly
    676     setlocal nomodifiable
    677 
    678     nmap <silent> <buffer> <cr> :call <SID>SetCompletionType()<cr>
    679     nmap <silent> <buffer> <c-]> :call <SID>SetCompletionType()<cr>
    680   else
    681     exec bufwinnr("SuperTabHelp") . "winc w"
    682   endif
    683   let b:winnr = winnr
    684 endfunction " }}}
    685 
    686 " s:WillComplete() {{{
    687 " Determines if completion should be kicked off at the current location.
    688 function! s:WillComplete()
    689   let line = getline('.')
    690   let cnum = col('.')
    691 
    692   " Start of line.
    693   let prev_char = strpart(line, cnum - 2, 1)
    694   if prev_char =~ '^\s*$'
    695     return 0
    696   endif
    697 
    698   " Within a word, but user does not have mid word completion enabled.
    699   let next_char = strpart(line, cnum - 1, 1)
    700   if !g:SuperTabMidWordCompletion && next_char =~ '\k'
    701     return 0
    702   endif
    703 
    704   " In keyword completion mode and no preceding word characters.
    705   "if (b:complType == "\<c-n>" || b:complType == "\<c-p>") && prev_char !~ '\k'
    706   "  return 0
    707   "endif
    708 
    709   return 1
    710 endfunction " }}}
    711 
    712 " s:CommandLineCompletion() {{{
    713 " Hack needed to account for apparent bug in vim command line mode completion
    714 " when invoked via <c-r>=
    715 function! s:CommandLineCompletion()
    716   " This hack will trigger InsertLeave which will then invoke
    717   " s:SetDefaultCompletionType.  To prevent default completion from being
    718   " restored prematurely, set an internal flag for s:SetDefaultCompletionType
    719   " to check for.
    720   let b:complCommandLine = 1
    721   return "\<c-\>\<c-o>:call feedkeys('\<c-x>\<c-v>\<c-v>', 'n') | " .
    722     \ "let b:complCommandLine = 0\<cr>"
    723 endfunction " }}}
    724 
    725 " s:ContextCompletion() {{{
    726 function! s:ContextCompletion()
    727   let contexts = exists('b:SuperTabCompletionContexts') ?
    728     \ b:SuperTabCompletionContexts : g:SuperTabCompletionContexts
    729 
    730   for context in contexts
    731     try
    732       let Context = function(context)
    733       let complType = Context()
    734       unlet Context
    735       if type(complType) == 1 && complType != ''
    736         return complType
    737       endif
    738     catch /E700/
    739       echohl Error
    740       echom 'supertab: no context function "' . context . '" found.'
    741       echohl None
    742     endtry
    743   endfor
    744   return ''
    745 endfunction " }}}
    746 
    747 " s:ContextDiscover() {{{
    748 function! s:ContextDiscover()
    749   let discovery = exists('g:SuperTabContextDiscoverDiscovery') ?
    750     \ g:SuperTabContextDiscoverDiscovery : []
    751 
    752   " loop through discovery list to find the default
    753   if !empty(discovery)
    754     for pair in discovery
    755       let var = substitute(pair, '\(.*\):.*', '\1', '')
    756       let type = substitute(pair, '.*:\(.*\)', '\1', '')
    757       exec 'let value = ' . var
    758       if value !~ '^\s*$' && value != '0'
    759         exec "let complType = \"" . escape(type, '<') . "\""
    760         return complType
    761       endif
    762     endfor
    763   endif
    764 endfunction " }}}
    765 
    766 " s:ContextText() {{{
    767 function! s:ContextText()
    768   let exclusions = exists('g:SuperTabContextTextFileTypeExclusions') ?
    769     \ g:SuperTabContextTextFileTypeExclusions : []
    770 
    771   if index(exclusions, &ft) == -1
    772     let curline = getline('.')
    773     let cnum = col('.')
    774     let synname = synIDattr(synID(line('.'), cnum - 1, 1), 'name')
    775     if curline =~ '.*/\w*\%' . cnum . 'c' ||
    776       \ ((has('win32') || has('win64')) && curline =~ '.*\\\w*\%' . cnum . 'c')
    777       return "\<c-x>\<c-f>"
    778 
    779     elseif curline =~ '.*\(\w\|[\])]\)\(\.\|::\|->\)\w*\%' . cnum . 'c' &&
    780       \ synname !~ '\(String\|Comment\)'
    781       let omniPrecedence = exists('g:SuperTabContextTextOmniPrecedence') ?
    782         \ g:SuperTabContextTextOmniPrecedence : ['&completefunc', '&omnifunc']
    783 
    784       for omniFunc in omniPrecedence
    785         if omniFunc !~ '^&'
    786           let omniFunc = '&' . omniFunc
    787         endif
    788         if getbufvar(bufnr('%'), omniFunc) != ''
    789           return omniFunc == '&omnifunc' ? "\<c-x>\<c-o>" : "\<c-x>\<c-u>"
    790         endif
    791       endfor
    792     endif
    793   endif
    794 endfunction " }}}
    795 
    796 " Key Mappings {{{
    797   " map a regular tab to ctrl-tab (note: doesn't work in console vim)
    798   exec 'inoremap ' . g:SuperTabMappingTabLiteral . ' <tab>'
    799 
    800   imap <c-x> <c-r>=<SID>ManualCompletionEnter()<cr>
    801 
    802   " From the doc |insert.txt| improved
    803   exec 'imap ' . g:SuperTabMappingForward . ' <c-n>'
    804   exec 'imap ' . g:SuperTabMappingBackward . ' <c-p>'
    805 
    806   " After hitting <Tab>, hitting it once more will go to next match
    807   " (because in XIM mode <c-n> and <c-p> mappings are ignored)
    808   " and wont start a brand new completion
    809   " The side effect, that in the beginning of line <c-n> and <c-p> inserts a
    810   " <Tab>, but I hope it may not be a problem...
    811   inoremap <c-n> <c-r>=<SID>SuperTab('n')<cr>
    812   inoremap <c-p> <c-r>=<SID>SuperTab('p')<cr>
    813 " }}}
    814 
    815 " Command Mappings {{{
    816   if !exists(":SuperTabHelp")
    817     command SuperTabHelp :call <SID>SuperTabHelp()
    818   endif
    819 " }}}
    820 
    821 call s:Init()
    822 
    823 let &cpo = s:save_cpo
    824 
    825 " vim:ft=vim:fdm=marker