author | Mahlon E. Smith <mahlon@martini.nu> |
Thu, 10 Sep 2009 20:50:11 -0700 | |
branch | vim-stuff |
changeset 11 | a9ebb6c22d14 |
parent 8 | 2e76f5b2a40f |
child 19 | 763cef799c74 |
permissions | -rw-r--r-- |
2 | 1 |
" vim: set noet nosta sw=4 ts=4 fdm=marker : |
2 |
" |
|
3 |
" Specky! |
|
4 |
" Mahlon E. Smith <mahlon@martini.nu> |
|
5 |
" $Id$ |
|
6 |
" |
|
7 |
||
8 | 8 |
|
2 | 9 |
" Hook up the functions to the user supplied key bindings. {{{ |
10 |
" |
|
11 |
if exists( 'g:speckySpecSwitcherKey' ) |
|
8 | 12 |
execute 'map ' . g:speckySpecSwitcherKey . ' :call <SID>SpecSwitcher()<CR>' |
13 |
" map &g:speckySpecSwitcherKey <SID>SpecSwitcher() |
|
2 | 14 |
endif |
15 |
||
16 |
if exists( 'g:speckyQuoteSwitcherKey' ) |
|
8 | 17 |
execute 'map ' . g:speckyQuoteSwitcherKey . ' :call <SID>QuoteSwitcher()<CR>' |
2 | 18 |
endif |
19 |
||
3 | 20 |
if exists( 'g:speckyBannerKey' ) |
8 | 21 |
execute 'map ' . g:speckyBannerKey . ' :call <SID>MakeBanner()<CR>' |
3 | 22 |
endif |
23 |
||
2 | 24 |
if exists( 'g:speckyRunSpecKey' ) |
8 | 25 |
execute 'map ' . g:speckyRunSpecKey . ' :call <SID>RunSpec()<CR>' |
2 | 26 |
endif |
27 |
||
28 |
if exists( 'g:speckyRunRdocKey' ) |
|
8 | 29 |
execute 'map ' . g:speckyRunRdocKey . ' :call <SID>RunRdoc()<CR>' |
2 | 30 |
endif |
31 |
||
32 |
if exists( 'specky_loaded' ) |
|
33 |
finish |
|
34 |
endif |
|
11
a9ebb6c22d14
Alter the build Makefiles to implement a suggestion from Antoine Mechelynck.
Mahlon E. Smith <mahlon@martini.nu>
parents:
8
diff
changeset
|
35 |
let specky_loaded = '$Rev$' |
2 | 36 |
|
37 |
||
38 |
"}}} |
|
39 |
" Menu configuration {{{ |
|
40 |
" |
|
41 |
let s:menuloc = '&Plugin.&specky' |
|
42 |
execute 'menu ' . s:menuloc . '.&Jump\ to\ code/spec :call <SID>SpecSwitcher()<CR>' |
|
43 |
execute 'menu ' . s:menuloc . '.Run\ &spec :call <SID>RunSpec()<CR>' |
|
44 |
execute 'menu ' . s:menuloc . '.&RDoc\ lookup :call <SID>RunRdoc()<CR>' |
|
45 |
execute 'menu ' . s:menuloc . '.Rotate\ "e\ style :call <SID>QuoteSwitcher()<CR>' |
|
3 | 46 |
execute 'menu ' . s:menuloc . '.Make\ a\ &banner :call <SID>MakeBanner()<CR>' |
2 | 47 |
|
48 |
||
49 |
" }}} |
|
50 |
" SpecSwitcher() {{{ |
|
51 |
" |
|
52 |
" When in ruby code or an rspec BDD file, try and search recursively through |
|
53 |
" the filesystem (within the current working directory) to find the |
|
54 |
" respectively matching file. (code to spec, spec to code.) |
|
55 |
" |
|
56 |
" This operates under the assumption that you've used chdir() to put vim into |
|
57 |
" the top level directory of your project. |
|
58 |
" |
|
59 |
function! <SID>SpecSwitcher() |
|
60 |
||
5 | 61 |
" If we aren't in a ruby or rspec file then we probably don't care |
62 |
" too much about this function. |
|
2 | 63 |
" |
5 | 64 |
if &ft != 'ruby' && &ft != 'rspec' |
65 |
call s:err( "Not currently in ruby or rspec mode." ) |
|
2 | 66 |
return |
67 |
endif |
|
68 |
||
69 |
" Ensure that we can always search recursively for files to open. |
|
70 |
" |
|
71 |
let l:orig_path = &path |
|
72 |
set path=** |
|
73 |
||
74 |
" Get the current buffer name, and determine if it is a spec file. |
|
75 |
" |
|
76 |
" /tmp/something/whatever/rubycode.rb ---> rubycode.rb |
|
77 |
" A requisite of the specfiles is that they match to the class/code file, |
|
78 |
" this emulates the eigenclass stuff, but doesn't require the same |
|
79 |
" directory structures. |
|
80 |
" |
|
81 |
" rubycode.rb ---> rubycode_spec.rb |
|
82 |
" |
|
83 |
let l:filename = matchstr( bufname('%'), '[0-9A-Za-z_.-]*$' ) |
|
84 |
let l:is_spec_file = match( l:filename, '_spec.rb$' ) == -1 ? 0 : 1 |
|
85 |
||
86 |
if l:is_spec_file |
|
87 |
let l:other_file = substitute( l:filename, '_spec\.rb$', '\.rb', '' ) |
|
88 |
else |
|
89 |
let l:other_file = substitute( l:filename, '\.rb$', '_spec\.rb', '' ) |
|
90 |
endif |
|
91 |
||
92 |
let l:bufnum = bufnr( l:other_file ) |
|
93 |
if l:bufnum == -1 |
|
94 |
" The file isn't currently open, so let's search for it. |
|
95 |
execute 'find ' . l:other_file |
|
96 |
else |
|
97 |
" We've already got an open buffer with this file, just go to it. |
|
98 |
execute 'buffer' . l:bufnum |
|
99 |
endif |
|
100 |
||
101 |
" Restore the original path. |
|
102 |
" |
|
103 |
execute 'set path=' . l:orig_path |
|
104 |
endfunction |
|
105 |
||
106 |
||
107 |
" }}} |
|
108 |
" QuoteSwitcher() {{{ |
|
109 |
" |
|
110 |
" Wrap the word under the cursor in quotes. If in ruby mode, |
|
111 |
" cycle between quoting styles and symbols. |
|
112 |
" |
|
113 |
" variable -> "variable" -> 'variable' -> :variable |
|
114 |
" |
|
115 |
function! <SID>QuoteSwitcher() |
|
116 |
let l:type = strpart( expand("<cWORD>"), 0, 1 ) |
|
117 |
let l:word = expand("<cword>") |
|
118 |
||
119 |
if l:type == '"' |
|
120 |
" Double quote to single |
|
121 |
" |
|
3 | 122 |
execute ":normal viWc'" . l:word . "'" |
2 | 123 |
|
124 |
elseif l:type == "'" |
|
5 | 125 |
if &ft == 'ruby' || &ft == 'rspec' |
2 | 126 |
" Single quote to symbol |
127 |
" |
|
3 | 128 |
execute ':normal viWc:' . l:word |
2 | 129 |
else |
130 |
" Single quote to double |
|
131 |
" |
|
3 | 132 |
execute ':normal viWc"' . l:word . '"' |
2 | 133 |
end |
134 |
||
135 |
else |
|
136 |
" Whatever to double quote |
|
137 |
" |
|
3 | 138 |
execute ':normal viWc"' . l:word . '"' |
2 | 139 |
endif |
140 |
||
141 |
" Move the cursor back into the cl:word |
|
142 |
" |
|
143 |
call cursor( 0, getpos('.')[2] - 1 ) |
|
144 |
endfunction |
|
145 |
||
146 |
||
147 |
" }}} |
|
3 | 148 |
" MakeBanner() {{{ |
149 |
" |
|
150 |
" Create a quick banner from the current line's text. |
|
151 |
" |
|
152 |
function! <SID>MakeBanner() |
|
153 |
let l:banner_text = toupper(join( split( getline('.'), '\zs' ), ' ' )) |
|
154 |
let l:banner_text = substitute( l:banner_text, '^\s\+', '', '' ) |
|
6 | 155 |
let l:sep = repeat( '#', &textwidth == 0 ? 72 : &textwidth ) |
3 | 156 |
let l:line = line('.') |
157 |
||
158 |
call setline( l:line, l:sep ) |
|
159 |
call append( l:line, [ '### ' . l:banner_text, l:sep ] ) |
|
6 | 160 |
execute 'normal 3==' |
3 | 161 |
call cursor( l:line + 3, 0 ) |
162 |
endfunction |
|
163 |
||
164 |
||
165 |
" }}} |
|
2 | 166 |
" RunSpec() {{{ |
167 |
" |
|
168 |
" Run this function while in a spec file to run the specs within vim. |
|
169 |
" |
|
170 |
function! <SID>RunSpec() |
|
171 |
||
172 |
" If we're in the code instead of the spec, try and switch |
|
173 |
" before running tests. |
|
174 |
" |
|
175 |
let l:filename = matchstr( bufname('%'), '[0-9A-Za-z_.-]*$' ) |
|
176 |
let l:is_spec_file = match( l:filename, '_spec.rb$' ) == -1 ? 0 : 1 |
|
177 |
if ( ! l:is_spec_file ) |
|
178 |
silent call <SID>SpecSwitcher() |
|
179 |
endif |
|
180 |
||
181 |
let l:spec = bufname('%') |
|
182 |
let l:buf = 'specky:specrun' |
|
183 |
let l:bufnum = bufnr( l:buf ) |
|
184 |
||
185 |
" Squash the old buffer, if it exists. |
|
186 |
" |
|
187 |
if buflisted( l:buf ) |
|
188 |
execute 'bd! ' . l:buf |
|
189 |
endif |
|
190 |
||
6 | 191 |
execute <SID>NewWindowCmd() . l:buf |
2 | 192 |
setlocal buftype=nofile bufhidden=delete noswapfile filetype=specrun |
193 |
set foldtext='--'.getline(v:foldstart).v:folddashes |
|
194 |
||
195 |
" Set up some convenient keybindings. |
|
196 |
" |
|
197 |
nnoremap <silent> <buffer> q :close<CR> |
|
198 |
nnoremap <silent> <buffer> e :call <SID>FindSpecError(1)<CR> |
|
199 |
nnoremap <silent> <buffer> r :call <SID>FindSpecError(-1)<CR> |
|
200 |
nnoremap <silent> <buffer> E :call <SID>FindSpecError(0)<CR> |
|
201 |
nnoremap <silent> <buffer> <C-e> :let b:err_line=1<CR> |
|
202 |
||
203 |
" Default cmd for spec |
|
204 |
" |
|
205 |
if !exists( 'g:speckyRunSpecCmd' ) |
|
206 |
let g:speckyRunSpecCmd = 'spec -fs' |
|
207 |
endif |
|
208 |
||
209 |
" Call spec and gather up the output |
|
210 |
" |
|
211 |
let l:cmd = g:speckyRunSpecCmd . ' ' . l:spec |
|
212 |
let l:output = system( l:cmd ) |
|
213 |
call append( 0, split( l:output, "\n" ) ) |
|
214 |
call append( 0, '' ) |
|
215 |
call append( 0, 'Output of: ' . l:cmd ) |
|
216 |
normal gg |
|
217 |
||
218 |
" Lockdown the buffer |
|
219 |
" |
|
220 |
setlocal nomodifiable |
|
221 |
endfunction |
|
222 |
||
223 |
||
224 |
" }}} |
|
225 |
" RunRdoc() {{{ |
|
226 |
" |
|
227 |
" Get documentation for the word under the cursor. |
|
228 |
" |
|
229 |
function! <SID>RunRdoc() |
|
230 |
||
231 |
" If we aren't in a ruby file (specs are ruby-mode too) then we probably |
|
232 |
" don't care too much about this function. |
|
233 |
" |
|
6 | 234 |
if ( &ft != 'ruby' && &ft != 'rdoc' && &ft != 'rspec' ) |
235 |
call s:err( "Not currently in a rubyish-mode." ) |
|
2 | 236 |
return |
237 |
endif |
|
238 |
||
239 |
" Set defaults |
|
240 |
" |
|
241 |
if !exists( 'g:speckyRunRdocCmd' ) |
|
242 |
let g:speckyRunRdocCmd = 'ri' |
|
243 |
endif |
|
244 |
||
245 |
let l:buf = 'specky:rdoc' |
|
246 |
let l:bufname = bufname('%') |
|
247 |
||
248 |
if ( match( l:bufname, l:buf ) != -1 ) |
|
249 |
" Already in the rdoc buffer. This allows us to lookup |
|
250 |
" something like Kernel#require. |
|
251 |
" |
|
252 |
let l:word = expand('<cWORD>') |
|
253 |
else |
|
254 |
" Not in the rdoc buffer. This allows us to lookup |
|
255 |
" something like 'each' in some_hash.each { ... } |
|
256 |
" |
|
257 |
let l:word = expand('<cword>') |
|
258 |
endif |
|
259 |
||
260 |
" Squash the old buffer, if it exists. |
|
261 |
" |
|
262 |
if buflisted( l:buf ) |
|
263 |
execute 'bd! ' . l:buf |
|
264 |
endif |
|
265 |
||
266 |
" With multiple matches, strip the comams from the cWORD. |
|
267 |
" |
|
268 |
let l:word = substitute( l:word, ',', '', 'eg' ) |
|
269 |
||
6 | 270 |
execute <SID>NewWindowCmd() . l:buf |
2 | 271 |
setlocal buftype=nofile bufhidden=delete noswapfile filetype=rdoc |
272 |
nnoremap <silent> <buffer> q :close<CR> |
|
273 |
||
274 |
" Call the documentation and gather up the output |
|
275 |
" |
|
276 |
let l:cmd = g:speckyRunRdocCmd . ' ' . l:word |
|
277 |
let l:output = system( l:cmd ) |
|
278 |
call append( 0, split( l:output, "\n" ) ) |
|
279 |
execute 'normal gg' |
|
280 |
||
281 |
" Lockdown the buffer |
|
282 |
" |
|
283 |
execute 'setlocal nomodifiable' |
|
284 |
endfunction |
|
285 |
||
286 |
||
287 |
" }}} |
|
288 |
" FindSpecError( detail ) {{{ |
|
289 |
" |
|
290 |
" Convenience searches for jumping to spec failures. |
|
291 |
" |
|
292 |
function! <SID>FindSpecError( detail ) |
|
293 |
||
294 |
let l:err_str = '(FAILED\|ERROR - \d\+)$' |
|
295 |
||
296 |
if ( a:detail == 0 ) |
|
297 |
" Find the detailed failure text for the current failure line, |
|
298 |
" and unfold it. |
|
299 |
" |
|
300 |
let l:orig_so = &so |
|
301 |
set so=100 |
|
302 |
call search('^' . matchstr(getline('.'),'\d\+)$') ) |
|
303 |
if has('folding') |
|
304 |
silent! normal za |
|
305 |
endif |
|
306 |
execute 'set so=' . l:orig_so |
|
307 |
||
308 |
else |
|
309 |
" Find the 'regular' failure line |
|
310 |
" |
|
311 |
if exists( 'b:err_line' ) |
|
312 |
call cursor( b:err_line, a:detail == -1 ? 1 : strlen(getline(b:err_line)) ) |
|
313 |
endif |
|
314 |
call search( l:err_str, a:detail == -1 ? 'b' : '' ) |
|
315 |
let b:err_line = line('.') |
|
316 |
nohl |
|
317 |
||
318 |
endif |
|
319 |
endfunction |
|
320 |
||
6 | 321 |
" }}} |
322 |
" NewWindowCmd() {{{ |
|
323 |
" |
|
324 |
" Return the stringified command for a new window, based on user preferences. |
|
325 |
" |
|
326 |
function! <SID>NewWindowCmd() |
|
327 |
if ( ! exists('g:speckyWindowType' ) ) |
|
328 |
return 'tabnew ' |
|
329 |
endif |
|
330 |
||
331 |
if ( g:speckyWindowType == 1 ) |
|
332 |
return 'new ' |
|
333 |
elseif ( g:speckyWindowType == 2 ) |
|
334 |
return 'vert new ' |
|
335 |
else |
|
336 |
return 'tabnew ' |
|
337 |
endif |
|
338 |
endfunction |
|
2 | 339 |
|
340 |
" }}} |
|
341 |
" s:err( msg ) "{{{ |
|
342 |
" Notify of problems in a consistent fashion. |
|
343 |
" |
|
344 |
function! s:err( msg ) |
|
345 |
echohl WarningMsg|echomsg 'specky: ' . a:msg|echohl None |
|
346 |
endfunction " }}} |
|
347 |