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