Skip to content

Instantly share code, notes, and snippets.

@baconpat
Created September 26, 2011 01:54
Show Gist options
  • Save baconpat/1241453 to your computer and use it in GitHub Desktop.
Save baconpat/1241453 to your computer and use it in GitHub Desktop.
Vim functions and AppleScript for running tests in an external Terminal using MacVim
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
on execInItermTab(_terminal, _session, _command)
tell application "iTerm"
activate
set current terminal to _terminal
tell _session
select _session
write text _command
end tell
end tell
end selectTerminalTab
on execInTerminalTab(_window, _tab, _command)
tell application "Terminal"
activate
set frontmost of _window to true
set selected of _tab to true
do script _command in _tab
end
end execInTerminalTab
on run argv
set _command to item 1 of argv
set _foundTab to false
-- Second argument should be the tty to look for
if length of argv is 2
if appIsRunning("iTerm") then
tell application "iTerm"
repeat with t in terminals
tell t
repeat with s in sessions
set _tty to (tty of s)
if _tty = (item 2 of argv) then
set _foundTab to true
set _session to s
set _terminal to t
exit repeat
end if
end repeat
end tell
if _foundTab then
exit repeat
end if
end repeat
end tell
if _foundTab then
execInItermTab(_terminal, _session, _command)
end if
end if
if not _foundTab and appIsRunning("Terminal") then
tell application "Terminal"
repeat with w in windows
tell w
repeat with t in tabs
set _tty to (tty of t)
if _tty = (item 2 of argv) then
set _foundTab to true
set _window to w
set _tab to t
exit repeat
end if
end repeat
end tell
if _foundTab then
exit repeat
end if
end repeat
end tell
if _foundTab then
execInTerminalTab(_window, _tab, _command)
end if
end if
end if
end run
function! RunInTerminal(file)
if match(a:file, '_spec\.rb') != -1
let l:command = 'bundle exec rspec'
elseif match(a:file, '\.feature') != -1
let l:command = 'bundle exec cucumber'
elseif match(a:file, '\.rb') != -1
let l:command = 'ruby'
endif
if exists("l:command")
let g:last_run_in_terminal = a:file
let l:run_script = "!osascript ~/.vim/tools/run_command.applescript"
silent execute ":up"
silent execute l:run_script . " '" . l:command . " " . a:file . "' " . g:vim_terminal . " &"
silent execute ":redraw!"
else
echo "Couldn't figure out how to run " . a:file
end
endfunction
function! RunFileInTerminal()
if exists("g:vim_terminal")
call RunInTerminal(expand("%"))
else
echo "You need to set g:vim_terminal to a valid TTY (e.g. /dev/ttys000)"
end
endfunction
function! RunFileAtLineInTerminal()
if exists("g:vim_terminal")
call RunInTerminal(expand("%") . ":" . line("."))
else
echo "You need to set g:vim_terminal to a valid TTY (e.g. /dev/ttys000)"
endif
endfunction
function! ReRunLastFileCommand()
if exists("g:vim_terminal") && exists("g:last_run_in_terminal")
call RunInTerminal(g:last_run_in_terminal)
endif
endfunction
command! RunFileInTerminal call RunFileInTerminal()
command! RunFileAtLineInTerminal call RunFileAtLineInTerminal()
command! ReRunLastFileCommand call ReRunLastFileCommand()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment