Mercurial > dotfiles
comparison .vim/plugin/supertab.vim @ 0:c30d68fbd368
Initial import from svn.
author | Augie Fackler <durin42@gmail.com> |
---|---|
date | Wed, 26 Nov 2008 10:56:09 -0600 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c30d68fbd368 |
---|---|
1 " Author: Gergely Kontra <kgergely@mcl.hu> | |
2 " You may direct issues regarding version 0.4+ to | |
3 " Eric Van Dewoestine (ervandew@yahoo.com). | |
4 " Version: 0.41 | |
5 " Description: | |
6 " Use your tab key to do all your completion in insert mode! | |
7 " The script remembers the last completion type, and applies that. | |
8 " Eg.: You want to enter /usr/local/lib/povray3/ | |
9 " You type (in insert mode): | |
10 " /u<C-x><C-f>/l<Tab><Tab><Tab>/p<Tab>/i<Tab> | |
11 " You can also manipulate the completion type used by changing g:complType | |
12 " variable. | |
13 " You can cycle forward and backward with the <Tab> and <S-Tab> keys | |
14 " (<S-Tab> will not work in the console version) | |
15 " Note: you must press <Tab> once to be able to cycle back | |
16 " History: | |
17 " 0.41 Fixed couple bugs introduced in last version (Eric Van Dewoestine). | |
18 " 0.4 Added the following functionality (Eric Van Dewoestine) | |
19 " - support for vim 7 omni, user, and spelling completion modes | |
20 " (should be backwards compatible with vim 6.x). | |
21 " - command :SuperTabHelp which opens a window with available | |
22 " completion types that the user can choose from. | |
23 " - variable g:SuperTabRetainCompletionType setting for determining if | |
24 " and for how long to retain completion type. | |
25 " 0.32 Corrected tab-insertion/completing decidion (thx to: Lorenz Wegener) | |
26 " 0.31 Added <S-Tab> for backward cycling. (req by: Peter Chun) | |
27 " 0.3 Back to the roots. Autocompletion is another story... | |
28 " Now the prompt appears, when showmode is on | |
29 | |
30 if !exists('complType') "Integration with other completion functions... | |
31 | |
32 " This variable determines if, and for how long, the current completion type | |
33 " is retained. The possible values include: | |
34 " 0 - The current completion type is only retained for the current completion. | |
35 " Once you have chosen a completion result or exited the completion | |
36 " mode, the default completion type is restored. | |
37 " 1 - The current completion type is saved for the duration of your vim | |
38 " session or until you enter a different completion mode. | |
39 " (SuperTab default). | |
40 " 2 - The current completion type is saved until you exit insert mode (via | |
41 " ESC). Once you exit insert mode the default completion type is | |
42 " restored. | |
43 if !exists("g:SuperTabRetainCompletionType") | |
44 let g:SuperTabRetainCompletionType = 1 | |
45 endif | |
46 | |
47 " This variable is used to set the default completion type. | |
48 " There is no need to escape this value as that will be done for you when | |
49 " the type is set. | |
50 " Ex. let g:SuperTabDefaultCompletionType = "<C-X><C-U>" | |
51 if !exists("g:SuperTabDefaultCompletionType") | |
52 let g:SuperTabDefaultCompletionType = "<C-P>" | |
53 endif | |
54 | |
55 " construct the help text. | |
56 let s:tabHelp = | |
57 \ "Hit <CR> or CTRL-] on the completion type you wish to swith to.\n" . | |
58 \ "Use :help ins-completion for more information.\n" . | |
59 \ "\n" . | |
60 \ "|<C-N>| - Keywords in 'complete' searching down.\n" . | |
61 \ "|<C-P>| - Keywords in 'complete' searching up (SuperTab default).\n" . | |
62 \ "|<C-X><C-L>| - Whole lines.\n" . | |
63 \ "|<C-X><C-N>| - Keywords in current file.\n" . | |
64 \ "|<C-X><C-K>| - Keywords in 'dictionary'.\n" . | |
65 \ "|<C-X><C-T>| - Keywords in 'thesaurus', thesaurus-style.\n" . | |
66 \ "|<C-X><C-I>| - Keywords in the current and included files.\n" . | |
67 \ "|<C-X><C-]>| - Tags.\n" . | |
68 \ "|<C-X><C-F>| - File names.\n" . | |
69 \ "|<C-X><C-D>| - Definitions or macros.\n" . | |
70 \ "|<C-X><C-V>| - Vim command-line." | |
71 if v:version >= 700 | |
72 let s:tabHelp = s:tabHelp . "\n" . | |
73 \ "|<C-X><C-U>| - User defined completion.\n" . | |
74 \ "|<C-X><C-O>| - Occult completion.\n" . | |
75 \ "|<C-X>s| - Spelling suggestions." | |
76 endif | |
77 | |
78 " set the available completion types and modes. | |
79 let s:types = | |
80 \ "\<C-E>\<C-Y>\<C-L>\<C-N>\<C-K>\<C-T>\<C-I>\<C-]>\<C-F>\<C-D>\<C-V>\<C-N>\<C-P>" | |
81 let s:modes = '/^E/^Y/^L/^N/^K/^T/^I/^]/^F/^D/^V/^P' | |
82 if v:version >= 700 | |
83 let s:types = s:types . "\<C-U>\<C-O>\<C-N>\<C-P>s" | |
84 let s:modes = s:modes . '/^U/^O/s' | |
85 endif | |
86 let s:types = s:types . "np" | |
87 let s:modes = s:modes . '/n/p' | |
88 | |
89 " Globally available function that user's can use to create mappings to | |
90 " quickly switch completion modes. Useful when a user wants to restore the | |
91 " default or switch to another mode without having to kick off a completion | |
92 " of that type or use SuperTabHelp. | |
93 " Example mapping to restore SuperTab default: | |
94 " nmap <F6> :call SetSuperTabCompletionType("<C-P>")<cr> | |
95 fu! SuperTabSetCompletionType (type) | |
96 exec "let g:complType = \"" . escape(a:type, '<') . "\"" | |
97 endf | |
98 | |
99 call SuperTabSetCompletionType(g:SuperTabDefaultCompletionType) | |
100 | |
101 im <C-X> <C-r>=CtrlXPP()<CR> | |
102 | |
103 " Setup mechanism to restore orignial completion type upon leaving insert | |
104 " mode if g:SuperTabDefaultCompletionType == 2 | |
105 if g:SuperTabRetainCompletionType == 2 | |
106 " pre vim 7, must map <esc> | |
107 if v:version < 700 | |
108 im <silent> <ESC> | |
109 \ <ESC>:call SuperTabSetCompletionType(g:SuperTabDefaultCompletionType)<cr> | |
110 | |
111 " since vim 7, we can use InsertLeave autocmd. | |
112 else | |
113 augroup supertab | |
114 autocmd InsertLeave * | |
115 \ call SuperTabSetCompletionType(g:SuperTabDefaultCompletionType) | |
116 augroup END | |
117 endif | |
118 endif | |
119 | |
120 fu! CtrlXPP() | |
121 if &smd | |
122 echo '' | echo '-- ^X++ mode (' . s:modes . ')' | |
123 endif | |
124 let complType=nr2char(getchar()) | |
125 if stridx(s:types, complType) != -1 | |
126 if stridx("\<C-E>\<C-Y>",complType)!=-1 " no memory, just scroll... | |
127 return "\<C-x>".complType | |
128 elseif stridx('np',complType)!=-1 | |
129 let complType=nr2char(char2nr(complType)-96) " char2nr('n')-char2nr("\<C-n") | |
130 else | |
131 let complType="\<C-x>".complType | |
132 endif | |
133 | |
134 if g:SuperTabRetainCompletionType | |
135 let g:complType = complType | |
136 endif | |
137 | |
138 return complType | |
139 else | |
140 echohl "Unknown mode" | |
141 return complType | |
142 endif | |
143 endf | |
144 | |
145 " From the doc |insert.txt| improved | |
146 im <Tab> <C-n> | |
147 inore <S-Tab> <C-p> | |
148 | |
149 " This way after hitting <Tab>, hitting it once more will go to next match | |
150 " (because in XIM mode <C-n> and <C-p> mappings are ignored) | |
151 " and wont start a brand new completion | |
152 " The side effect, that in the beginning of line <C-n> and <C-p> inserts a | |
153 " <Tab>, but I hope it may not be a problem... | |
154 ino <C-n> <C-R>=<SID>SuperTab('n')<CR> | |
155 ino <C-p> <C-R>=<SID>SuperTab('p')<CR> | |
156 | |
157 fu! <SID>SuperTab(command) | |
158 if (strpart(getline('.'),col('.')-2,1)=~'^\s\?$') | |
159 return "\<Tab>" | |
160 else | |
161 " exception: if in <c-p> mode, then <c-n> should move up the list, and | |
162 " <c-p> down the list. | |
163 if a:command == 'p' && g:complType == "\<C-P>" | |
164 return "\<C-N>" | |
165 endif | |
166 return g:complType | |
167 endif | |
168 endf | |
169 | |
170 fu! <SID>SuperTabHelp() | |
171 if bufwinnr("SuperTabHelp") == -1 | |
172 botright split SuperTabHelp | |
173 | |
174 setlocal noswapfile | |
175 setlocal buftype=nowrite | |
176 setlocal bufhidden=delete | |
177 | |
178 let saved = @" | |
179 let @" = s:tabHelp | |
180 silent put | |
181 call cursor(1,1) | |
182 silent 1,delete | |
183 call cursor(4,1) | |
184 let @" = saved | |
185 exec "resize " . line('$') | |
186 | |
187 syntax match Special "|.\{-}|" | |
188 | |
189 setlocal readonly | |
190 setlocal nomodifiable | |
191 | |
192 nmap <silent> <buffer> <cr> :call <SID>SetCompletionType()<cr> | |
193 nmap <silent> <buffer> <c-]> :call <SID>SetCompletionType()<cr> | |
194 else | |
195 exec bufwinnr("SuperTabHelp") . "winc w" | |
196 endif | |
197 endf | |
198 | |
199 fu! s:SetCompletionType () | |
200 let chosen = substitute(getline('.'), '.*|\(.*\)|.*', '\1', '') | |
201 if chosen != getline('.') | |
202 call SuperTabSetCompletionType(chosen) | |
203 close | |
204 winc p | |
205 endif | |
206 endf | |
207 | |
208 if !exists(":SuperTabHelp") | |
209 command SuperTabHelp :call <SID>SuperTabHelp() | |
210 endif | |
211 en |