VimCompletesMe.vim 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. " VimCompletesMe.vim - For super simple tab completion
  2. " Maintainer: Akshay Hegde <http://github.com/ajh17>
  3. " Version: 1.2.1
  4. " Website: <http://github.com/ajh17/VimCompletesMe>
  5. " Vimscript Setup: {{{1
  6. if exists("g:loaded_VimCompletesMe") || v:version < 703 || &compatible
  7. finish
  8. endif
  9. let g:loaded_VimCompletesMe = 1
  10. " Options: {{{1
  11. if !exists('g:vcm_s_tab_behavior')
  12. let g:vcm_s_tab_behavior = 0
  13. endif
  14. if !exists('g:vcm_direction')
  15. let g:vcm_direction = 'n'
  16. endif
  17. if !exists('g:vcm_default_maps')
  18. let g:vcm_default_maps = 1
  19. endif
  20. " Functions: {{{1
  21. function! s:vim_completes_me(shift_tab)
  22. let dirs = ["\<c-p>", "\<c-n>"]
  23. let dir = g:vcm_direction =~? '[nf]'
  24. let map = exists('b:vcm_tab_complete') ? b:vcm_tab_complete : ''
  25. if pumvisible()
  26. if a:shift_tab
  27. return dirs[!dir]
  28. else
  29. return dirs[dir]
  30. endif
  31. endif
  32. " Figure out whether we should indent.
  33. let pos = getpos('.')
  34. let substr = matchstr(strpart(getline(pos[1]), 0, pos[2]-1), "[^ \t]*$")
  35. if strlen(substr) == 0
  36. return (a:shift_tab && !g:vcm_s_tab_behavior) ? "\<C-d>" : "\<Tab>"
  37. endif
  38. " Figure out if user has started typing a path or a period
  39. let period = match(substr, '\.') != -1
  40. let file_path = (has('win32') || has('win64')) ? '\\' : '\/'
  41. let file_pattern = match(substr, file_path) != -1
  42. if file_pattern
  43. return "\<C-x>\<C-f>"
  44. elseif period && (&omnifunc != '')
  45. if get(b:, 'tab_complete_pos', []) == pos
  46. let exp = "\<C-x>" . dirs[!dir]
  47. else
  48. let exp = "\<C-x>\<C-o>"
  49. endif
  50. let b:tab_complete_pos = pos
  51. return exp
  52. endif
  53. " First fallback to keyword completion if special completion was already tried.
  54. if exists('b:completion_tried') && b:completion_tried
  55. let b:completion_tried = 0
  56. return "\<C-e>" . dirs[!dir]
  57. endif
  58. " Fallback
  59. let b:completion_tried = 1
  60. if map ==? "user"
  61. return "\<C-x>\<C-u>"
  62. elseif map ==? "tags"
  63. return "\<C-x>\<C-]>"
  64. elseif map ==? "omni"
  65. return "\<C-x>\<C-o>"
  66. elseif map ==? "dict"
  67. return "\<C-x>\<C-k>"
  68. elseif map ==? "vim"
  69. return "\<C-x>\<C-v>"
  70. else
  71. return "\<C-x>" . dirs[!dir]
  72. endif
  73. endfunction
  74. inoremap <expr> <plug>vim_completes_me_forward <sid>vim_completes_me(0)
  75. inoremap <expr> <plug>vim_completes_me_backward <sid>vim_completes_me(1)
  76. " Maps: {{{1
  77. if g:vcm_default_maps
  78. imap <Tab> <plug>vim_completes_me_forward
  79. imap <S-Tab> <plug>vim_completes_me_backward
  80. endif
  81. " Autocmds {{{1
  82. augroup VCM
  83. autocmd!
  84. autocmd InsertEnter * let b:completion_tried = 0
  85. if v:version > 703 || v:version == 703 && has('patch598')
  86. autocmd CompleteDone * let b:completion_tried = 0
  87. endif
  88. augroup END