supertab.vim 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. " Author: Eric Van Dewoestine <ervandew@gmail.com>
  2. " Original concept and versions up to 0.32 written by
  3. " Gergely Kontra <kgergely@mcl.hu>
  4. " Version: 2.1
  5. " GetLatestVimScripts: 1643 1 :AutoInstall: supertab.vim
  6. "
  7. " Description: {{{
  8. " Use your tab key to do all your completion in insert mode!
  9. " You can cycle forward and backward with the <Tab> and <S-Tab> keys
  10. " Note: you must press <Tab> once to be able to cycle back
  11. "
  12. " http://www.vim.org/scripts/script.php?script_id=1643
  13. " }}}
  14. "
  15. " License: {{{
  16. " Copyright (c) 2002 - 2016
  17. " All rights reserved.
  18. "
  19. " Redistribution and use of this software in source and binary forms, with
  20. " or without modification, are permitted provided that the following
  21. " conditions are met:
  22. "
  23. " * Redistributions of source code must retain the above
  24. " copyright notice, this list of conditions and the
  25. " following disclaimer.
  26. "
  27. " * Redistributions in binary form must reproduce the above
  28. " copyright notice, this list of conditions and the
  29. " following disclaimer in the documentation and/or other
  30. " materials provided with the distribution.
  31. "
  32. " * Neither the name of Gergely Kontra or Eric Van Dewoestine nor the names
  33. " of its contributors may be used to endorse or promote products derived
  34. " from this software without specific prior written permission of Gergely
  35. " Kontra or Eric Van Dewoestine.
  36. "
  37. " THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  38. " IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  39. " THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  40. " PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  41. " CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  42. " EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  43. " PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  44. " PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  45. " LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  46. " NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  47. " SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  48. " }}}
  49. "
  50. " Testing Info: {{{
  51. " Running vim + supertab with the absolute bare minimum settings:
  52. " $ vim -u NONE -U NONE -c "set nocp | runtime plugin/supertab.vim"
  53. " }}}
  54. if v:version < 700
  55. finish
  56. endif
  57. if exists('complType') " Integration with other completion functions.
  58. finish
  59. endif
  60. if exists("loaded_supertab")
  61. finish
  62. endif
  63. let loaded_supertab = 1
  64. let s:save_cpo=&cpo
  65. set cpo&vim
  66. " Global Variables {{{
  67. if !exists("g:SuperTabDefaultCompletionType")
  68. let g:SuperTabDefaultCompletionType = "<c-p>"
  69. endif
  70. if !exists("g:SuperTabContextDefaultCompletionType")
  71. let g:SuperTabContextDefaultCompletionType = "<c-p>"
  72. endif
  73. if !exists("g:SuperTabContextTextMemberPatterns")
  74. let g:SuperTabContextTextMemberPatterns = ['\.', '>\?::', '->']
  75. endif
  76. if !exists("g:SuperTabCompletionContexts")
  77. let g:SuperTabCompletionContexts = ['s:ContextText']
  78. endif
  79. if !exists("g:SuperTabRetainCompletionDuration")
  80. let g:SuperTabRetainCompletionDuration = 'insert'
  81. endif
  82. if !exists("g:SuperTabNoCompleteBefore")
  83. " retain backwards compatability
  84. if exists("g:SuperTabMidWordCompletion") && !g:SuperTabMidWordCompletion
  85. let g:SuperTabNoCompleteBefore = ['\k']
  86. else
  87. let g:SuperTabNoCompleteBefore = []
  88. endif
  89. endif
  90. if !exists("g:SuperTabNoCompleteAfter")
  91. " retain backwards compatability
  92. if exists("g:SuperTabLeadingSpaceCompletion") && g:SuperTabLeadingSpaceCompletion
  93. let g:SuperTabNoCompleteAfter = []
  94. else
  95. let g:SuperTabNoCompleteAfter = ['^', '\s']
  96. endif
  97. endif
  98. if !exists("g:SuperTabMappingForward")
  99. let g:SuperTabMappingForward = '<tab>'
  100. endif
  101. if !exists("g:SuperTabMappingBackward")
  102. let g:SuperTabMappingBackward = '<s-tab>'
  103. endif
  104. if !exists("g:SuperTabMappingTabLiteral")
  105. let g:SuperTabMappingTabLiteral = '<c-tab>'
  106. endif
  107. if !exists("g:SuperTabLongestEnhanced")
  108. let g:SuperTabLongestEnhanced = 0
  109. endif
  110. if !exists("g:SuperTabLongestHighlight")
  111. let g:SuperTabLongestHighlight = 0
  112. endif
  113. if !exists("g:SuperTabCrMapping")
  114. let g:SuperTabCrMapping = 0
  115. endif
  116. if !exists("g:SuperTabClosePreviewOnPopupClose")
  117. let g:SuperTabClosePreviewOnPopupClose = 0
  118. endif
  119. if !exists("g:SuperTabUndoBreak")
  120. let g:SuperTabUndoBreak = 0
  121. endif
  122. if !exists("g:SuperTabCompleteCase")
  123. let g:SuperTabCompleteCase = 'inherit'
  124. endif
  125. " }}}
  126. " Script Variables {{{
  127. " construct the help text.
  128. let s:tabHelp =
  129. \ "Hit <CR> or CTRL-] on the completion type you wish to switch to.\n" .
  130. \ "Use :help ins-completion for more information.\n" .
  131. \ "\n" .
  132. \ "|<c-n>| - Keywords in 'complete' searching down.\n" .
  133. \ "|<c-p>| - Keywords in 'complete' searching up (SuperTab default).\n" .
  134. \ "|<c-x><c-l>| - Whole lines.\n" .
  135. \ "|<c-x><c-n>| - Keywords in current file.\n" .
  136. \ "|<c-x><c-k>| - Keywords in 'dictionary'.\n" .
  137. \ "|<c-x><c-t>| - Keywords in 'thesaurus', thesaurus-style.\n" .
  138. \ "|<c-x><c-i>| - Keywords in the current and included files.\n" .
  139. \ "|<c-x><c-]>| - Tags.\n" .
  140. \ "|<c-x><c-f>| - File names.\n" .
  141. \ "|<c-x><c-d>| - Definitions or macros.\n" .
  142. \ "|<c-x><c-v>| - Vim command-line.\n" .
  143. \ "|<c-x><c-u>| - User defined completion.\n" .
  144. \ "|<c-x><c-o>| - Omni completion.\n" .
  145. \ "|<c-x>s| - Spelling suggestions."
  146. " set the available completion types and modes.
  147. let s:types =
  148. \ "\<c-e>\<c-y>\<c-l>\<c-n>\<c-k>\<c-t>\<c-i>\<c-]>" .
  149. \ "\<c-f>\<c-d>\<c-v>\<c-n>\<c-p>\<c-u>\<c-o>\<c-n>\<c-p>s"
  150. let s:modes = '/^E/^Y/^L/^N/^K/^T/^I/^]/^F/^D/^V/^P/^U/^O/s'
  151. let s:types = s:types . "np"
  152. let s:modes = s:modes . '/n/p'
  153. " }}}
  154. function! SuperTabSetDefaultCompletionType(type) " {{{
  155. " Globally available function that users can use to set the default
  156. " completion type for the current buffer, like in an ftplugin.
  157. " don't allow overriding what SuperTabChain has set, otherwise chaining may
  158. " not work.
  159. if exists('b:SuperTabChain')
  160. return
  161. endif
  162. " init hack for <c-x><c-v> workaround.
  163. let b:complCommandLine = 0
  164. let b:SuperTabDefaultCompletionType = a:type
  165. " set the current completion type to the default
  166. call SuperTabSetCompletionType(b:SuperTabDefaultCompletionType)
  167. endfunction " }}}
  168. function! SuperTabSetCompletionType(type) " {{{
  169. " Globally available function that users can use to create mappings to quickly
  170. " switch completion modes. Useful when a user wants to restore the default or
  171. " switch to another mode without having to kick off a completion of that type
  172. " or use SuperTabHelp. Note, this function only changes the current
  173. " completion type, not the default, meaning that the default will still be
  174. " restored once the configured retension duration has been met (see
  175. " g:SuperTabRetainCompletionDuration). To change the default for the current
  176. " buffer, use SuperTabDefaultCompletionType(type) instead. Example mapping to
  177. " restore SuperTab default:
  178. " nmap <F6> :call SetSuperTabCompletionType("<c-p>")<cr>
  179. " don't allow overriding what SuperTabChain has set, otherwise chaining may
  180. " not work.
  181. if exists('b:SuperTabChain')
  182. return
  183. endif
  184. call s:InitBuffer()
  185. exec "let b:complType = \"" . escape(a:type, '<') . "\""
  186. endfunction " }}}
  187. function! SuperTabAlternateCompletion(type) " {{{
  188. " Function which can be mapped to a key to kick off an alternate completion
  189. " other than the default. For instance, if you have 'context' as the default
  190. " and want to map ctrl+space to issue keyword completion.
  191. " Note: due to the way vim expands ctrl characters in mappings, you cannot
  192. " create the alternate mapping like so:
  193. " imap <c-space> <c-r>=SuperTabAlternateCompletion("<c-p>")<cr>
  194. " instead, you have to use \<lt> to prevent vim from expanding the key
  195. " when creating the mapping.
  196. " gvim:
  197. " imap <c-space> <c-r>=SuperTabAlternateCompletion("\<lt>c-p>")<cr>
  198. " console:
  199. " imap <nul> <c-r>=SuperTabAlternateCompletion("\<lt>c-p>")<cr>
  200. call SuperTabSetCompletionType(a:type)
  201. " end any current completion before attempting to start the new one.
  202. " use feedkeys to prevent possible remapping of <c-e> from causing issues.
  203. "call feedkeys("\<c-e>", 'n')
  204. " ^ since we can't detect completion mode vs regular insert mode, we force
  205. " vim into keyword completion mode and end that mode to prevent the regular
  206. " insert behavior of <c-e> from occurring.
  207. call feedkeys("\<c-x>\<c-p>\<c-e>", 'n')
  208. call feedkeys(b:complType, 'n')
  209. return ''
  210. endfunction " }}}
  211. function! SuperTabLongestHighlight(dir) " {{{
  212. " When longest highlight is enabled, this function is used to do the actual
  213. " selection of the completion popup entry.
  214. if !pumvisible()
  215. return ''
  216. endif
  217. return a:dir == -1 ? "\<up>" : "\<down>"
  218. endfunction " }}}
  219. function! s:Init() " {{{
  220. " Setup mechanism to restore original completion type upon leaving insert
  221. " mode if configured to do so
  222. if g:SuperTabRetainCompletionDuration == 'insert'
  223. augroup supertab_retain
  224. autocmd!
  225. autocmd InsertLeave * call s:SetDefaultCompletionType()
  226. augroup END
  227. endif
  228. endfunction " }}}
  229. function! s:InitBuffer() " {{{
  230. if exists('b:SuperTabNoCompleteBefore')
  231. return
  232. endif
  233. let b:complReset = 0
  234. let b:complTypeManual = !exists('b:complTypeManual') ? '' : b:complTypeManual
  235. let b:complTypeContext = ''
  236. " init hack for <c-x><c-v> workaround.
  237. let b:complCommandLine = 0
  238. if !exists('b:SuperTabNoCompleteBefore')
  239. let b:SuperTabNoCompleteBefore = g:SuperTabNoCompleteBefore
  240. endif
  241. if !exists('b:SuperTabNoCompleteAfter')
  242. let b:SuperTabNoCompleteAfter = g:SuperTabNoCompleteAfter
  243. endif
  244. if !exists('b:SuperTabDefaultCompletionType')
  245. let b:SuperTabDefaultCompletionType = g:SuperTabDefaultCompletionType
  246. endif
  247. if !exists('b:SuperTabContextDefaultCompletionType')
  248. let b:SuperTabContextDefaultCompletionType =
  249. \ g:SuperTabContextDefaultCompletionType
  250. endif
  251. " set the current completion type to the default
  252. call SuperTabSetCompletionType(b:SuperTabDefaultCompletionType)
  253. " hack to programatically revert a change to snipmate that breaks supertab
  254. " but which the new maintainers don't care about:
  255. " http://github.com/garbas/vim-snipmate/issues/37
  256. let snipmate = maparg('<tab>', 'i')
  257. if snipmate =~ '<C-G>u' && g:SuperTabMappingForward =~? '<tab>'
  258. let snipmate = substitute(snipmate, '<C-G>u', '', '')
  259. iunmap <tab>
  260. exec "inoremap <silent> <tab> " . snipmate
  261. endif
  262. endfunction " }}}
  263. function! s:ManualCompletionEnter() " {{{
  264. " Handles manual entrance into completion mode.
  265. if &smd
  266. echo '' | echohl ModeMsg | echo '-- ^X++ mode (' . s:modes . ')' | echohl None
  267. endif
  268. let complType = nr2char(getchar())
  269. if stridx(s:types, complType) != -1
  270. if !exists('b:supertab_close_preview')
  271. let b:supertab_close_preview = !s:IsPreviewOpen()
  272. endif
  273. if stridx("\<c-e>\<c-y>", complType) != -1 " no memory, just scroll...
  274. return "\<c-x>" . complType
  275. elseif stridx('np', complType) != -1
  276. let complType = nr2char(char2nr(complType) - 96)
  277. else
  278. let complType = "\<c-x>" . complType
  279. endif
  280. let b:complTypeManual = complType
  281. if index(['insert', 'session'], g:SuperTabRetainCompletionDuration) != -1
  282. let b:complType = complType
  283. endif
  284. " Hack to workaround bug when invoking command line completion via <c-r>=
  285. if complType == "\<c-x>\<c-v>"
  286. return s:CommandLineCompletion()
  287. endif
  288. call s:InitBuffer()
  289. " optionally enable enhanced longest completion
  290. if g:SuperTabLongestEnhanced && &completeopt =~ 'longest'
  291. call s:EnableLongestEnhancement()
  292. " handle backspacing which triggers g:SuperTabNoCompleteAfter match
  293. elseif s:IsNoCompleteAfterReset()
  294. call s:EnableNoCompleteAfterReset()
  295. endif
  296. if g:SuperTabLongestHighlight &&
  297. \ &completeopt =~ 'longest' &&
  298. \ &completeopt =~ 'menu' &&
  299. \ !s:CompletionMode()
  300. let dir = (complType == "\<c-x>\<c-p>") ? -1 : 1
  301. call feedkeys("\<c-r>=SuperTabLongestHighlight(" . dir . ")\<cr>", 'n')
  302. endif
  303. call s:StartCompletionMode()
  304. return complType
  305. endif
  306. echohl "Unknown mode"
  307. return complType
  308. endfunction " }}}
  309. function! s:SetCompletionType() " {{{
  310. " Sets the completion type based on what the user has chosen from the help
  311. " buffer.
  312. let chosen = substitute(getline('.'), '.*|\(.*\)|.*', '\1', '')
  313. if chosen != getline('.')
  314. let winnr = b:winnr
  315. close
  316. exec winnr . 'winc w'
  317. call SuperTabSetCompletionType(chosen)
  318. endif
  319. endfunction " }}}
  320. function! s:SetDefaultCompletionType() " {{{
  321. if exists('b:SuperTabDefaultCompletionType') &&
  322. \ (!exists('b:complCommandLine') || !b:complCommandLine)
  323. call SuperTabSetCompletionType(b:SuperTabDefaultCompletionType)
  324. endif
  325. endfunction " }}}
  326. function! SuperTab(command) " {{{
  327. " Used to perform proper cycle navigation as the user requests the next or
  328. " previous entry in a completion list, and determines whether or not to simply
  329. " retain the normal usage of <tab> based on the cursor position.
  330. if exists('b:SuperTabDisabled') && b:SuperTabDisabled
  331. if exists('s:Tab')
  332. return s:Tab()
  333. endif
  334. return (
  335. \ g:SuperTabMappingForward ==? '<tab>' ||
  336. \ g:SuperTabMappingBackward ==? '<tab>'
  337. \ ) ? "\<tab>" : ''
  338. endif
  339. call s:InitBuffer()
  340. if s:WillComplete()
  341. if !exists('b:supertab_close_preview')
  342. let b:supertab_close_preview = !s:IsPreviewOpen()
  343. endif
  344. " optionally enable enhanced longest completion
  345. if g:SuperTabLongestEnhanced && &completeopt =~ 'longest'
  346. call s:EnableLongestEnhancement()
  347. " handle backspacing which triggers g:SuperTabNoCompleteAfter match
  348. elseif s:IsNoCompleteAfterReset()
  349. call s:EnableNoCompleteAfterReset()
  350. endif
  351. if !s:CompletionMode()
  352. let b:complTypeManual = ''
  353. endif
  354. " exception: if in <c-p> mode, then <c-n> should move up the list, and
  355. " <c-p> down the list.
  356. if a:command == 'p' && !b:complReset &&
  357. \ (b:complType == "\<c-p>" ||
  358. \ (b:complType == 'context' &&
  359. \ b:complTypeManual == '' &&
  360. \ b:complTypeContext == "\<c-p>"))
  361. return "\<c-n>"
  362. elseif a:command == 'p' && !b:complReset &&
  363. \ (b:complType == "\<c-n>" ||
  364. \ (b:complType == 'context' &&
  365. \ b:complTypeManual == '' &&
  366. \ b:complTypeContext == "\<c-n>"))
  367. return "\<c-p>"
  368. " already in completion mode and not resetting for longest enhancement, so
  369. " just scroll to next/previous
  370. elseif s:CompletionMode() && !b:complReset
  371. let type = b:complType == 'context' ? b:complTypeContext : b:complType
  372. if a:command == 'n'
  373. return type == "\<c-p>" || type == "\<c-x>\<c-p>" ? "\<c-p>" : "\<c-n>"
  374. endif
  375. return type == "\<c-p>" || type == "\<c-x>\<c-p>" ? "\<c-n>" : "\<c-p>"
  376. endif
  377. " handle 'context' completion.
  378. if b:complType == 'context'
  379. let complType = s:ContextCompletion()
  380. if complType == ''
  381. exec "let complType = \"" .
  382. \ escape(b:SuperTabContextDefaultCompletionType, '<') . "\""
  383. endif
  384. let b:complTypeContext = complType
  385. " Hack to workaround bug when invoking command line completion via <c-r>=
  386. elseif b:complType == "\<c-x>\<c-v>"
  387. let complType = s:CommandLineCompletion()
  388. else
  389. let complType = b:complType
  390. endif
  391. " switch <c-x><c-p> / <c-x><c-n> completion in <c-p> mode
  392. if a:command == 'p'
  393. if complType == "\<c-x>\<c-p>"
  394. let complType = "\<c-x>\<c-n>"
  395. elseif complType == "\<c-x>\<c-n>"
  396. let complType = "\<c-x>\<c-p>"
  397. endif
  398. endif
  399. " highlight first result if longest enabled
  400. if g:SuperTabLongestHighlight &&
  401. \ &completeopt =~ 'longest' &&
  402. \ &completeopt =~ 'menu' &&
  403. \ (!s:CompletionMode() || b:complReset)
  404. let dir = (complType == "\<c-p>") ? -1 : 1
  405. call feedkeys("\<c-r>=SuperTabLongestHighlight(" . dir . ")\<cr>", 'n')
  406. endif
  407. if b:complReset
  408. let b:complReset = 0
  409. " not an accurate condition for everyone, but better than sending <c-e>
  410. " at the wrong time.
  411. if s:CompletionMode()
  412. return "\<c-e>" . complType
  413. endif
  414. endif
  415. if g:SuperTabUndoBreak && !s:CompletionMode()
  416. call s:StartCompletionMode()
  417. return "\<c-g>u" . complType
  418. endif
  419. if g:SuperTabCompleteCase == 'ignore' ||
  420. \ g:SuperTabCompleteCase == 'match'
  421. if exists('##CompleteDone')
  422. let ignorecase = g:SuperTabCompleteCase == 'ignore' ? 1 : 0
  423. if &ignorecase != ignorecase
  424. let b:supertab_ignorecase_save = &ignorecase
  425. let &ignorecase = ignorecase
  426. augroup supertab_ignorecase
  427. autocmd CompleteDone <buffer>
  428. \ let &ignorecase = b:supertab_ignorecase_save |
  429. \ unlet b:supertab_ignorecase_save |
  430. \ autocmd! supertab_ignorecase
  431. augroup END
  432. endif
  433. endif
  434. endif
  435. call s:StartCompletionMode()
  436. return complType
  437. endif
  438. if (a:command == 'n' && g:SuperTabMappingForward ==? '<tab>') ||
  439. \ (a:command == 'p' && g:SuperTabMappingBackward ==? '<tab>')
  440. " trigger our func ref to the smart tabs plugin if present.
  441. if exists('s:Tab')
  442. return s:Tab()
  443. endif
  444. return "\<tab>"
  445. endif
  446. if (a:command == 'n' && g:SuperTabMappingForward ==? '<s-tab>') ||
  447. \ (a:command == 'p' && g:SuperTabMappingBackward ==? '<s-tab>')
  448. " support triggering <s-tab> mappings users might have.
  449. if exists('s:ShiftTab')
  450. if type(s:ShiftTab) == 2
  451. return s:ShiftTab()
  452. else
  453. call feedkeys(s:ShiftTab, 'n')
  454. endif
  455. endif
  456. endif
  457. return ''
  458. endfunction " }}}
  459. function! s:SuperTabHelp() " {{{
  460. " Opens a help window where the user can choose a completion type to enter.
  461. let winnr = winnr()
  462. if bufwinnr("SuperTabHelp") == -1
  463. keepalt botright split SuperTabHelp
  464. setlocal noswapfile
  465. setlocal buftype=nowrite
  466. setlocal bufhidden=wipe
  467. silent put =s:tabHelp
  468. call cursor(1, 1)
  469. silent 1,delete _
  470. call cursor(4, 1)
  471. exec "resize " . line('$')
  472. syntax match Special "|.\{-}|"
  473. setlocal readonly
  474. setlocal nomodifiable
  475. nmap <silent> <buffer> <cr> :call <SID>SetCompletionType()<cr>
  476. nmap <silent> <buffer> <c-]> :call <SID>SetCompletionType()<cr>
  477. else
  478. exec bufwinnr("SuperTabHelp") . "winc w"
  479. endif
  480. let b:winnr = winnr
  481. endfunction " }}}
  482. function! s:CompletionMode() " {{{
  483. return pumvisible() || exists('b:supertab_completion_mode')
  484. endfunction " }}}
  485. function! s:StartCompletionMode() " {{{
  486. if exists('##CompleteDone')
  487. let b:supertab_completion_mode = 1
  488. augroup supertab_completion_mode
  489. autocmd CompleteDone <buffer>
  490. \ if exists('b:supertab_completion_mode') |
  491. \ unlet b:supertab_completion_mode |
  492. \ endif |
  493. \ autocmd! supertab_completion_mode
  494. augroup END
  495. endif
  496. endfunction " }}}
  497. function! s:WillComplete(...) " {{{
  498. " Determines if completion should be kicked off at the current location.
  499. " Optional arg:
  500. " col: The column to check at, otherwise use the current column.
  501. " if an arg was supplied, then we will re-check even if already in
  502. " completion mode.
  503. if s:CompletionMode() && !a:0
  504. return 1
  505. endif
  506. let line = getline('.')
  507. let cnum = a:0 ? a:1 : col('.')
  508. " honor SuperTabNoCompleteAfter
  509. let pre = cnum >= 2 ? line[:cnum - 2] : ''
  510. let complAfterType = type(b:SuperTabNoCompleteAfter)
  511. if complAfterType == 3
  512. " the option was provided as a list of patterns
  513. for pattern in b:SuperTabNoCompleteAfter
  514. if pre =~ pattern . '$'
  515. return 0
  516. endif
  517. endfor
  518. elseif complAfterType == 2
  519. " the option was provided as a funcref
  520. return !b:SuperTabNoCompleteAfter(pre)
  521. endif
  522. " honor SuperTabNoCompleteBefore
  523. " Within a word, but user does not have mid word completion enabled.
  524. let post = line[cnum - 1:]
  525. let complBeforeType = type(b:SuperTabNoCompleteBefore)
  526. if complBeforeType == 3
  527. " a list of patterns
  528. for pattern in b:SuperTabNoCompleteBefore
  529. if post =~ '^' . pattern
  530. return 0
  531. endif
  532. endfor
  533. elseif complBeforeType == 2
  534. " the option was provided as a funcref
  535. return !b:SuperTabNoCompleteBefore(post)
  536. endif
  537. return 1
  538. endfunction " }}}
  539. function! s:EnableLongestEnhancement() " {{{
  540. augroup supertab_reset
  541. autocmd!
  542. autocmd InsertLeave,CursorMovedI <buffer>
  543. \ call s:ReleaseKeyPresses() | autocmd! supertab_reset
  544. augroup END
  545. call s:CaptureKeyPresses()
  546. endfunction " }}}
  547. function! s:IsNoCompleteAfterReset() " {{{
  548. " if the user has g:SuperTabNoCompleteAfter set, then re-map <bs> so that
  549. " backspacing to a point where one of the g:SuperTabNoCompleteAfter
  550. " entries matches will cause completion mode to exit.
  551. let complAfterType = type(b:SuperTabNoCompleteAfter)
  552. if complAfterType == 2
  553. return 1
  554. endif
  555. return len(g:SuperTabNoCompleteAfter) && g:SuperTabNoCompleteAfter != ['^', '\s']
  556. endfunction " }}}
  557. function! s:EnableNoCompleteAfterReset() " {{{
  558. augroup supertab_reset
  559. autocmd!
  560. autocmd InsertLeave,CursorMovedI <buffer>
  561. \ call s:ReleaseKeyPresses() | autocmd! supertab_reset
  562. augroup END
  563. " short version of s:CaptureKeyPresses
  564. if !exists('b:capturing') || !b:capturing
  565. let b:capturing = 1
  566. let b:capturing_start = col('.')
  567. let b:captured = {
  568. \ '<bs>': s:CaptureKeyMap('<bs>'),
  569. \ '<c-h>': s:CaptureKeyMap('<c-h>'),
  570. \ }
  571. imap <buffer> <bs> <c-r>=<SID>CompletionReset("\<lt>bs>")<cr>
  572. imap <buffer> <c-h> <c-r>=<SID>CompletionReset("\<lt>c-h>")<cr>
  573. endif
  574. endfunction " }}}
  575. function! s:CompletionReset(char) " {{{
  576. let b:complReset = 1
  577. " handle exiting completion mode if user has g:SuperTabNoCompleteAfter set
  578. " and they are about to backspace to a point where that maches one of the
  579. " entries in that var.
  580. if (a:char == "\<bs>" || a:char == "\<c-h>") && s:IsNoCompleteAfterReset()
  581. if !s:WillComplete(col('.') - 1)
  582. " Exit from completion mode then issue the currently requested
  583. " backspace (mapped).
  584. call feedkeys("\<space>\<bs>", 'n')
  585. call s:ReleaseKeyPresses()
  586. call feedkeys("\<bs>", 'mt')
  587. return ''
  588. endif
  589. endif
  590. return a:char
  591. endfunction " }}}
  592. function! s:CaptureKeyPresses() " {{{
  593. if !exists('b:capturing') || !b:capturing
  594. let b:capturing = 1
  595. let b:capturing_start = col('.')
  596. " save any previous mappings
  597. let b:captured = {
  598. \ '<bs>': s:CaptureKeyMap('<bs>'),
  599. \ '<c-h>': s:CaptureKeyMap('<c-h>'),
  600. \ }
  601. " TODO: use &keyword to get an accurate list of chars to map
  602. for c in split('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_', '.\zs')
  603. let existing = s:CaptureKeyMap(c)
  604. let b:captured[c] = existing
  605. exec 'imap <buffer> ' . c . ' <c-r>=<SID>CompletionReset("' . c . '")<cr>'
  606. endfor
  607. imap <buffer> <bs> <c-r>=<SID>CompletionReset("\<lt>bs>")<cr>
  608. imap <buffer> <c-h> <c-r>=<SID>CompletionReset("\<lt>c-h>")<cr>
  609. endif
  610. endfunction " }}}
  611. function! s:CaptureKeyMap(key) " {{{
  612. " as of 7.3.032 maparg supports obtaining extended information about the
  613. " mapping.
  614. if s:has_dict_maparg
  615. return maparg(a:key, 'i', 0, 1)
  616. endif
  617. return maparg(a:key, 'i')
  618. endfunction " }}}
  619. function! s:IsPreviewOpen() " {{{
  620. let wins = tabpagewinnr(tabpagenr(), '$')
  621. let winnr = 1
  622. while winnr <= wins
  623. if getwinvar(winnr, '&previewwindow') == 1
  624. return 1
  625. endif
  626. let winnr += 1
  627. endwhile
  628. return 0
  629. endfunction " }}}
  630. function! s:ClosePreview() " {{{
  631. if exists('b:supertab_close_preview') && b:supertab_close_preview
  632. let preview = 0
  633. for bufnum in tabpagebuflist()
  634. if getwinvar(bufwinnr(bufnum), '&previewwindow')
  635. let preview = 1
  636. break
  637. endif
  638. endfor
  639. if preview
  640. pclose
  641. try
  642. doautocmd <nomodeline> supertab_preview_closed User <supertab>
  643. catch /E216/
  644. " ignore: no autocmds defined
  645. endtry
  646. endif
  647. endif
  648. silent! unlet b:supertab_close_preview
  649. endfunction " }}}
  650. function! s:ReleaseKeyPresses() " {{{
  651. if exists('b:capturing') && b:capturing
  652. let b:capturing = 0
  653. for c in keys(b:captured)
  654. exec 'iunmap <buffer> ' . c
  655. endfor
  656. " restore any previous mappings
  657. for [key, mapping] in items(b:captured)
  658. if !len(mapping)
  659. continue
  660. endif
  661. if type(mapping) == 4
  662. let restore = mapping.noremap ? "inoremap" : "imap"
  663. let restore .= " <buffer>"
  664. if mapping.silent
  665. let restore .= " <silent>"
  666. endif
  667. if mapping.expr
  668. let restore .= " <expr>"
  669. endif
  670. let rhs = substitute(mapping.rhs, '<SID>\c', '<SNR>' . mapping.sid . '_', 'g')
  671. let restore .= ' ' . key . ' ' . rhs
  672. exec restore
  673. elseif type(c) == 1
  674. let args = substitute(mapping, '.*\(".\{-}"\).*', '\1', '')
  675. if args != mapping
  676. let args = substitute(args, '<', '<lt>', 'g')
  677. let expr = substitute(mapping, '\(.*\)".\{-}"\(.*\)', '\1%s\2', '')
  678. let mapping = printf(expr, args)
  679. endif
  680. exec printf("imap <silent> <buffer> %s %s", key, mapping)
  681. endif
  682. endfor
  683. unlet b:captured
  684. if mode() == 'i' && &completeopt =~ 'menu' && b:capturing_start != col('.')
  685. " force full exit from completion mode (don't exit insert mode since
  686. " that will break repeating with '.')
  687. call feedkeys("\<space>\<bs>", 'n')
  688. endif
  689. unlet b:capturing_start
  690. endif
  691. endfunction " }}}
  692. function! s:CommandLineCompletion() " {{{
  693. " Hack needed to account for apparent bug in vim command line mode completion
  694. " when invoked via <c-r>=
  695. " This hack will trigger InsertLeave which will then invoke
  696. " s:SetDefaultCompletionType. To prevent default completion from being
  697. " restored prematurely, set an internal flag for s:SetDefaultCompletionType
  698. " to check for.
  699. let b:complCommandLine = 1
  700. return "\<c-\>\<c-o>:call feedkeys('\<c-x>\<c-v>\<c-v>', 'n') | " .
  701. \ "let b:complCommandLine = 0\<cr>"
  702. endfunction " }}}
  703. function! s:ContextCompletion() " {{{
  704. let contexts = exists('b:SuperTabCompletionContexts') ?
  705. \ b:SuperTabCompletionContexts : g:SuperTabCompletionContexts
  706. for context in contexts
  707. try
  708. let Context = function(context)
  709. let complType = Context()
  710. unlet Context
  711. if type(complType) == 1 && complType != ''
  712. return complType
  713. endif
  714. catch /E700/
  715. echohl Error
  716. echom 'supertab: no context function "' . context . '" found.'
  717. echohl None
  718. endtry
  719. endfor
  720. return ''
  721. endfunction " }}}
  722. function! s:ContextDiscover() " {{{
  723. let discovery = exists('g:SuperTabContextDiscoverDiscovery') ?
  724. \ g:SuperTabContextDiscoverDiscovery : []
  725. " loop through discovery list to find the default
  726. if !empty(discovery)
  727. for pair in discovery
  728. let var = substitute(pair, '\(.*\):.*', '\1', '')
  729. let type = substitute(pair, '.*:\(.*\)', '\1', '')
  730. exec 'let value = ' . var
  731. if value !~ '^\s*$' && value != '0'
  732. exec "let complType = \"" . escape(type, '<') . "\""
  733. return complType
  734. endif
  735. endfor
  736. endif
  737. endfunction " }}}
  738. function! s:ContextText() " {{{
  739. let exclusions = exists('g:SuperTabContextTextFileTypeExclusions') ?
  740. \ g:SuperTabContextTextFileTypeExclusions : []
  741. if index(exclusions, &ft) == -1
  742. let curline = getline('.')
  743. let cnum = col('.')
  744. let synname = synIDattr(synID(line('.'), cnum - 1, 1), 'name')
  745. let member_patterns = exists('b:SuperTabContextTextMemberPatterns') ?
  746. \ b:SuperTabContextTextMemberPatterns : g:SuperTabContextTextMemberPatterns
  747. let member_pattern = join(member_patterns, '\|')
  748. " don't kick off file completion if the pattern is '</' (to account for
  749. " sgml languanges), that's what the following <\@<! pattern is doing.
  750. if curline =~ '<\@<!/\.\?\w*\%' . cnum . 'c' ||
  751. \ ((has('win32') || has('win64')) && curline =~ '\\\w*\%' . cnum . 'c')
  752. return "\<c-x>\<c-f>"
  753. elseif curline =~ '\(' . member_pattern . '\)\w*\%' . cnum . 'c' &&
  754. \ synname !~ '\(String\|Comment\)'
  755. let omniPrecedence = exists('g:SuperTabContextTextOmniPrecedence') ?
  756. \ g:SuperTabContextTextOmniPrecedence : ['&completefunc', '&omnifunc']
  757. let omniPrecedence = exists('b:SuperTabContextTextOmniPrecedence') ?
  758. \ b:SuperTabContextTextOmniPrecedence : omniPrecedence
  759. for omniFunc in omniPrecedence
  760. if omniFunc !~ '^&'
  761. let omniFunc = '&' . omniFunc
  762. endif
  763. if getbufvar(bufnr('%'), omniFunc) != ''
  764. return omniFunc == '&omnifunc' ? "\<c-x>\<c-o>" : "\<c-x>\<c-u>"
  765. endif
  766. endfor
  767. endif
  768. endif
  769. endfunction " }}}
  770. function! s:ExpandMap(map) " {{{
  771. let map = a:map
  772. if map =~ '<Plug>'
  773. let plug = substitute(map, '.\{-}\(<Plug>\w\+\).*', '\1', '')
  774. let plug_map = maparg(plug, 'i')
  775. let map = substitute(map, '.\{-}\(<Plug>\w\+\).*', plug_map, '')
  776. endif
  777. return map
  778. endfunction " }}}
  779. function! SuperTabChain(completefunc, completekeys, ...) " {{{
  780. if a:completefunc != 'SuperTabCodeComplete'
  781. call s:InitBuffer()
  782. if (a:0 && a:1) || (!a:0 && b:SuperTabDefaultCompletionType == 'context')
  783. let b:SuperTabContextTextOmniPrecedence = ['&completefunc', '&omnifunc']
  784. call SuperTabSetDefaultCompletionType("context")
  785. else
  786. call SuperTabSetDefaultCompletionType("<c-x><c-u>")
  787. endif
  788. let b:SuperTabChain = [a:completefunc, a:completekeys]
  789. setlocal completefunc=SuperTabCodeComplete
  790. endif
  791. endfunction " }}}
  792. function! SuperTabCodeComplete(findstart, base) " {{{
  793. if !exists('b:SuperTabChain')
  794. echoe 'No completion chain has been set.'
  795. return -2
  796. endif
  797. if len(b:SuperTabChain) != 2
  798. echoe 'Completion chain can only be used with 1 completion function ' .
  799. \ 'and 1 fallback completion key binding.'
  800. return -2
  801. endif
  802. let Func = function(b:SuperTabChain[0])
  803. if a:findstart
  804. let start = Func(a:findstart, a:base)
  805. if start >= 0
  806. return start
  807. endif
  808. return col('.') - 1
  809. endif
  810. let results = Func(a:findstart, a:base)
  811. " Handle dict case, with 'words' and 'refresh' (optional).
  812. " This is used by YouCompleteMe. (See complete-functions).
  813. if type(results) == type({}) && has_key(results, 'words')
  814. if len(results.words)
  815. return results
  816. endif
  817. elseif len(results)
  818. return results
  819. endif
  820. exec 'let keys = "' . escape(b:SuperTabChain[1], '<') . '"'
  821. " <c-e>: stop completion and go back to the originally typed text.
  822. call feedkeys("\<c-e>" . keys, 'nt')
  823. return []
  824. endfunction " }}}
  825. " Autocmds {{{
  826. if g:SuperTabClosePreviewOnPopupClose
  827. augroup supertab_close_preview
  828. autocmd!
  829. autocmd InsertLeave,CursorMovedI * call s:ClosePreview()
  830. augroup END
  831. endif
  832. " }}}
  833. " Key Mappings {{{
  834. " map a regular tab to ctrl-tab (note: doesn't work in console vim)
  835. exec 'inoremap ' . g:SuperTabMappingTabLiteral . ' <tab>'
  836. inoremap <silent> <c-x> <c-r>=<SID>ManualCompletionEnter()<cr>
  837. imap <script> <Plug>SuperTabForward <c-r>=SuperTab('n')<cr>
  838. imap <script> <Plug>SuperTabBackward <c-r>=SuperTab('p')<cr>
  839. let s:has_dict_maparg = v:version > 703 || (v:version == 703 && has('patch32'))
  840. " support delegating to smart tabs plugin
  841. if g:SuperTabMappingForward ==? '<tab>' || g:SuperTabMappingBackward ==? '<tab>'
  842. let existing_tab = maparg('<tab>', 'i')
  843. if existing_tab =~ '\d\+_InsertSmartTab()$'
  844. let s:Tab = function(substitute(existing_tab, '()$', '', ''))
  845. endif
  846. endif
  847. " save user's existing <s-tab> mapping if they have one.
  848. " Note: this could cause more problems than it solves if it picks up <s-tab>
  849. " mappings from other plugins and misinterprets them, etc, so this block is
  850. " experimental and could be removed later.
  851. if g:SuperTabMappingForward ==? '<s-tab>' || g:SuperTabMappingBackward ==? '<s-tab>'
  852. let stab = maparg('<s-tab>', 'i')
  853. if s:has_dict_maparg
  854. let existing_stab = maparg('<s-tab>', 'i', 0, 1)
  855. if len(existing_stab) && existing_stab.expr
  856. let stab = substitute(stab, '<SID>\c', '<SNR>' . existing_stab.sid . '_', '')
  857. let stab = substitute(stab, '()$', '', '')
  858. let s:ShiftTab = function(stab)
  859. let stab = ''
  860. endif
  861. endif
  862. if stab != ''
  863. let stab = substitute(stab, '\(<[-a-zA-Z0-9]\+>\)', '\\\1', 'g')
  864. exec "let stab = \"" . stab . "\""
  865. let s:ShiftTab = stab
  866. endif
  867. endif
  868. exec 'imap ' . g:SuperTabMappingForward . ' <Plug>SuperTabForward'
  869. exec 'imap ' . g:SuperTabMappingBackward . ' <Plug>SuperTabBackward'
  870. if g:SuperTabCrMapping
  871. let expr_map = 0
  872. if s:has_dict_maparg
  873. let map_dict = maparg('<cr>', 'i', 0, 1)
  874. let expr_map = has_key(map_dict, 'expr') && map_dict.expr
  875. else
  876. let expr_map = maparg('<cr>', 'i') =~? '\<cr>'
  877. endif
  878. redir => iabbrevs
  879. silent iabbrev
  880. redir END
  881. let iabbrev_map = iabbrevs =~? '\<cr>'
  882. if expr_map
  883. " Not compatible w/ expr mappings. This is most likely a user mapping,
  884. " typically with the same functionality anyways.
  885. let g:SuperTabCrMapping = 0
  886. elseif iabbrev_map
  887. " Not compatible w/ insert abbreviations containing <cr>
  888. let g:SuperTabCrMapping = 0
  889. elseif maparg('<CR>', 'i') =~ '<Plug>delimitMateCR'
  890. " Not compatible w/ delimitMate since it doesn't play well with others
  891. " and will always return a <cr> which we don't want when selecting a
  892. " completion.
  893. let g:SuperTabCrMapping = 0
  894. elseif maparg('<CR>', 'i') =~ '<CR>'
  895. let map = maparg('<cr>', 'i')
  896. let cr = !(map =~? '\(^\|[^)]\)<cr>' || map =~ 'ExpandCr')
  897. let map = s:ExpandMap(map)
  898. exec "inoremap <script> <cr> <c-r>=<SID>SelectCompletion(" . cr . ")<cr>" . map
  899. else
  900. inoremap <silent> <cr> <c-r>=<SID>SelectCompletion(1)<cr>
  901. endif
  902. function! s:SelectCompletion(cr)
  903. " selecting a completion
  904. if s:CompletionMode()
  905. " ugly hack to let other <cr> mappings for other plugins cooperate
  906. " with supertab
  907. let b:supertab_pumwasvisible = 1
  908. " close the preview window if configured to do so
  909. if &completeopt =~ 'preview' && g:SuperTabClosePreviewOnPopupClose
  910. if !exists('b:supertab_close_preview')
  911. let b:supertab_close_preview = !s:IsPreviewOpen()
  912. endif
  913. call s:ClosePreview()
  914. endif
  915. return "\<c-y>"
  916. endif
  917. " only needed when chained with other mappings and one of them will
  918. " issue a <cr>.
  919. if exists('b:supertab_pumwasvisible') && !a:cr
  920. unlet b:supertab_pumwasvisible
  921. return ''
  922. endif
  923. " not so pleasant hack to keep <cr> working for abbreviations
  924. let word = substitute(getline('.'), '^.*\s\+\(.*\%' . col('.') . 'c\).*', '\1', '')
  925. let result = maparg(word, 'i', 1)
  926. if result != ''
  927. let bs = ""
  928. let i = 0
  929. while i < len(word)
  930. let bs .= "\<bs>"
  931. let i += 1
  932. endwhile
  933. " escape keys
  934. let result = substitute(result, '\(<[a-zA-Z][-a-zA-Z]*>\)', '\\\1', 'g')
  935. " ensure escaped keys are properly recognized
  936. exec 'let result = "' . escape(result, '"') . '"'
  937. return bs . result . (a:cr ? "\<cr>" : "")
  938. endif
  939. " only return a cr if nothing else is mapped to it since we don't want
  940. " to duplicate a cr returned by another mapping.
  941. return a:cr ? "\<cr>" : ""
  942. endfunction
  943. endif
  944. " }}}
  945. " Command Mappings {{{
  946. if !exists(":SuperTabHelp")
  947. command SuperTabHelp :call <SID>SuperTabHelp()
  948. endif
  949. " }}}
  950. call s:Init()
  951. function! TestSuperTabCodeComplete(findstart, base) " {{{
  952. " Test supertab completion chaining w/ a minimal vim environment:
  953. " $ vim -u NONE -U NONE \
  954. " --cmd "set nocp | sy on" \
  955. " -c "so ~/.vim/plugin/supertab.vim" \
  956. " -c "let g:SuperTabDefaultCompletionType = '<c-x><c-u>'" \
  957. " -c "set completefunc=TestSuperTabCodeComplete" \
  958. " -c "call SuperTabChain(&completefunc, '<c-p>')"
  959. if a:findstart
  960. let line = getline('.')
  961. let start = col('.') - 1
  962. if line[start] =~ '\.'
  963. let start -= 1
  964. endif
  965. while start > 0 && line[start - 1] =~ '\w'
  966. let start -= 1
  967. endwhile
  968. return start
  969. else
  970. let completions = []
  971. if getline('.') =~ 'TestC'
  972. call add(completions, {
  973. \ 'word': 'test1(',
  974. \ 'kind': 'm',
  975. \ 'menu': 'test1(...)',
  976. \ })
  977. call add(completions, {
  978. \ 'word': 'testing2(',
  979. \ 'kind': 'm',
  980. \ 'menu': 'testing2(...)',
  981. \ })
  982. endif
  983. return completions
  984. endif
  985. endfunction " }}}
  986. let &cpo = s:save_cpo
  987. " vim:ft=vim:fdm=marker