Reorganize initialization code

This commit is contained in:
Chris Toomey 2012-11-23 20:44:28 -05:00
parent 369fba87b5
commit 1c8250b229
2 changed files with 54 additions and 28 deletions

View File

@ -4,7 +4,8 @@ A simple, vimscript only, command runner for sending commands from vim to tmux
## Commands
This plugin exposes a command to open a small command runner window
This plugin exposes a command to open a small command runner window in a tmux
pane, then send commands to it.
- Open runner window
- Automatic cd to project root (via git cdup)
@ -12,9 +13,13 @@ This plugin exposes a command to open a small command runner window
- Enter tmux-copy mode in pane
- Maximize the runner pane
Full documentation can be found [here][].
## Inspiration
This plugin is heavily inspired by the functionality in the Vimux plugin. This
plugin aims to implement a similar feature set while not requiring Vim with
ruby requirement. In addition a few new commands not found in Vimux have been
added to provide additional control over the tmux runner pane.
[here]: https://github.com/christoomey/vim-tmux-runner/blob/master/doc/vim-tmux-runner.txt

View File

@ -1,13 +1,3 @@
" Function: s:initVariable() function {{{2
" This function is used to initialise a given variable to a given value. The
" variable is only initialised if it does not exist prior
"
" Args:
" var: the name of the var to be initialised
" value: the value to initialise var to
"
" Returns:
" 1 if the var is set, 0 otherwise
function! s:InitVariable(var, value)
if !exists(a:var)
let escaped_value = substitute(a:value, "'", "''", "g")
@ -22,9 +12,12 @@ function! s:InitializeVariables()
call s:InitVariable("g:VtrOrientation", "v")
call s:InitVariable("g:VtrInitialCommand", "")
call s:InitVariable("g:VtrClearBeforeSend", 1)
call s:InitVariable("g:VtrGitCdUpOnOpen", 1)
call s:InitVariable("g:VtrPrompt", "Command to run: ")
call s:InitVariable("g:VtrUseVtrMaps", 1)
call s:InitVariable("g:VtrClearOnResize", 1)
call s:InitVariable("g:VtrClearOnReorient", 1)
endfunction
call s:InitializeVariables()
function! s:OpenRunnerPane()
let s:vim_pane = s:ActiveTmuxPaneNumber()
@ -32,6 +25,9 @@ function! s:OpenRunnerPane()
call s:SendTmuxCommand(cmd)
let s:runner_pane = s:ActiveTmuxPaneNumber()
call s:FocusVimPane()
if g:VtrGitCdUpOnOpen
call s:GitCdUp()
endif
if g:VtrInitialCommand != ""
call s:SendKeys(g:VtrInitialCommand)
call s:SendClearSequence()
@ -66,7 +62,8 @@ function! s:RunnerPaneDimensions()
let panes = s:TmuxPanes()
for pane in panes
if pane =~ '^'.s:runner_pane
let pane_info = matchlist(pane, s:runner_pane.': [\(\d\+\)x\(\d\+\)\]')
let pattern = s:runner_pane.': [\(\d\+\)x\(\d\+\)\]'
let pane_info = matchlist(pane, pattern)
return {'width': pane_info[1], 'height': pane_info[2]}
endif
endfor
@ -75,7 +72,8 @@ endfunction
function! s:ResizeRunnerPane()
let new_percent = s:HighlightedPrompt("Runner screen percentage: ")
let pane_dimensions = s:RunnerPaneDimensions()
let inputs = [pane_dimensions['height'], '*', new_percent, '/', g:VtrPercentage]
let inputs = [pane_dimensions['height'], '*', new_percent,
\ '/', g:VtrPercentage]
let new_lines = eval(join(inputs)) " Not sure why I need to use eval...?
let lines_delta = abs(pane_dimensions['height'] - new_lines)
let move_down = (eval(join([new_percent, '<', g:VtrPercentage])))
@ -84,11 +82,11 @@ function! s:ResizeRunnerPane()
let full_command = join([targeted_cmd, direction, lines_delta])
let g:VtrPercentage = new_percent
call s:SendTmuxCommand(full_command)
if g:VtrClearOnResize
call s:SendClearSequence()
endif
endfunction
command! VTRResizePane :call s:ResizeRunnerPane()
nmap ,rr :VTRResizePane<cr>
function! s:FocusRunnerPane()
call s:FocusTmuxPane(s:runner_pane)
endfunction
@ -122,6 +120,14 @@ function! s:SendClearSequence()
sleep 50m
endfunction
function! s:GitCdUp()
let git_repo_check = "git rev-parse --git-dir > /dev/null 2>&1"
let cdup_cmd = "cd './'$(git rev-parse --show-cdup)"
let cmd = shellescape(join([git_repo_check, '&&', cdup_cmd]))
call s:SendKeys(cmd)
call s:SendClearSequence()
endfunction
function! s:FocusVimPane()
call s:FocusTmuxPane(s:vim_pane)
endfunction
@ -147,7 +153,9 @@ function! s:ReorientRunner()
let join_cmd = join(["join-pane", "-s", ":".temp_window.".0",
\ "-p", g:VtrPercentage, "-".g:VtrOrientation])
call s:SendTmuxCommand(join_cmd)
call s:SendClearSequence()
if g:VtrClearOnReorient
call s:SendClearSequence()
endif
call s:FocusVimPane()
endfunction
@ -164,13 +172,26 @@ function! s:SendCommandToRunner()
call s:SendKeys(user_command)
endfunction
command! VTROpenRunner :call s:OpenRunnerPane()
command! VTRKillRunner :call s:KillRunnerPane()
command! VTRFocusRunnerPane :call s:FocusRunnerPane()
command! VTRSendCommandToRunner :call s:SendCommandToRunner()
command! VTRReorientRunner :call s:ReorientRunner()
nmap ,ror :VTRReorientRunner<cr>
nmap ,sc :VTRSendCommandToRunner<cr>
nmap ,or :VTROpenRunner<cr>
nmap ,kr :VTRKillRunner<cr>
nmap ,fr :VTRFocusRunnerPane<cr>
function! s:DefineCommands()
command! VTROpenRunner :call s:OpenRunnerPane()
command! VTRKillRunner :call s:KillRunnerPane()
command! VTRFocusRunnerPane :call s:FocusRunnerPane()
command! VTRSendCommandToRunner :call s:SendCommandToRunner()
command! VTRReorientRunner :call s:ReorientRunner()
command! VTRResizePane :call s:ResizeRunnerPane()
endfunction
function! s:DefineKeymaps()
if g:VtrUseVtrMaps
nmap ,rr :VTRResizePane<cr>
nmap ,ror :VTRReorientRunner<cr>
nmap ,sc :VTRSendCommandToRunner<cr>
nmap ,or :VTROpenRunner<cr>
nmap ,kr :VTRKillRunner<cr>
nmap ,fr :VTRFocusRunnerPane<cr>
endif
endfunction
call s:InitializeVariables()
call s:DefineCommands()
call s:DefineKeymaps()