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