From f44a6b50b7cce2f1026e10015a14d722853cdaeb Mon Sep 17 00:00:00 2001 From: Matthias Bilger Date: Wed, 7 Oct 2015 19:07:35 +0200 Subject: [PATCH] Added possibility to choose a differen session --- doc/vimux.txt | 14 +++++++++- plugin/vimux.vim | 70 ++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/doc/vimux.txt b/doc/vimux.txt index 395b90b..a09c026 100644 --- a/doc/vimux.txt +++ b/doc/vimux.txt @@ -351,9 +351,21 @@ Options: Default: "pane" +------------------------------------------------------------------------------ + *VimuxTargetSession* +2.7 g:VimuxTargetSession + +The session the tmux window should belong to. Works only with VimuxRunnerType +"window". If the session is available the current active window will be used, +else the session will be created. + + let g:VimuxRunnerType = "VimuxOut" + +Default: "" + ------------------------------------------------------------------------------ *VimuxTmuxCommand* -2.7 g:VimuxTmuxCommand~ +2.8 g:VimuxTmuxCommand~ The command that Vimux runs when it calls out to tmux. It may be useful to redefine this if you're using something like tmate. diff --git a/plugin/vimux.vim b/plugin/vimux.vim index 9170668..55c3ede 100644 --- a/plugin/vimux.vim +++ b/plugin/vimux.vim @@ -65,24 +65,49 @@ function! VimuxSendKeys(keys) endfunction function! VimuxOpenRunner() + let activeSession = _VimuxGetTargetSession() let nearestIndex = _VimuxNearestIndex() - if _VimuxOption("g:VimuxUseNearest", 1) == 1 && nearestIndex != -1 - let g:VimuxRunnerIndex = nearestIndex + if _VimuxOption("g:VimuxUseNearest", 1) == 1 && nearestIndex != -1 && activeSession != -1 + let g:VimuxRunnerIndex = activeSession.':'.nearestIndex else if _VimuxRunnerType() == "pane" let height = _VimuxOption("g:VimuxHeight", 20) let orientation = _VimuxOption("g:VimuxOrientation", "v") call _VimuxTmux("split-window -p ".height." -".orientation) elseif _VimuxRunnerType() == "window" - call _VimuxTmux("new-window") + if activeSession == -1 + let activeSession = _VimuxOption("g:VimuxTargetSession", '') + call _VimuxTmuxSpecial("new-session -d -n \"vimux\" -s \"".activeSession."\"") + else + call _VimuxTmux("new-window -t ".activeSession) + endif endif - let g:VimuxRunnerIndex = _VimuxTmuxIndex() + let g:VimuxRunnerIndex = activeSession.':'._VimuxTmuxIndex() call _VimuxTmux("last-"._VimuxRunnerType()) endif endfunction + +function! _VimuxGetTargetSession() + let targetSession = '' + if _VimuxOption("g:VimuxTargetSession", '') != '' + let targetSession = g:VimuxTargetSession + let sessions = split(_VimuxTmux("list-sessions"), "\n") + + for session in sessions + if match(session, targetSession) != -1 + return targetSession + endif + endfor + return -1 + else + let targetSession = substitute(_VimuxTmux("display -p '#S'"), '\n$', '', '') + endif + return targetSession +endfunction + function! VimuxCloseRunner() if exists("g:VimuxRunnerIndex") call _VimuxTmux("kill-"._VimuxRunnerType()." -t ".g:VimuxRunnerIndex) @@ -147,9 +172,20 @@ endfunction function! _VimuxTmux(arguments) let l:command = _VimuxOption("g:VimuxTmuxCommand", "tmux") + if _VimuxOption("g:VimuxDebug", 0) != 0 + echo l:command." ".a:arguments + endif return system(l:command." ".a:arguments) endfunction +function! _VimuxTmuxSpecial(arguments) + let l:command = _VimuxOption("g:VimuxTmuxCommand", "tmux") + if _VimuxOption("g:VimuxDebug", 0) != 0 + echo "TMUX= ".l:command." ".a:arguments + endif + return system("TMUX= ".l:command." ".a:arguments) +endfunction + function! _VimuxTmuxSession() return _VimuxTmuxProperty("#S") endfunction @@ -171,13 +207,25 @@ function! _VimuxTmuxWindowIndex() endfunction function! _VimuxNearestIndex() - let views = split(_VimuxTmux("list-"._VimuxRunnerType()."s"), "\n") - - for view in views - if match(view, "(active)") == -1 - return split(view, ":")[0] - endif - endfor + let targetSession = '' + if _VimuxOption("g:VimuxTargetSession", '') != '' + let targetSession = ' -t '.g:VimuxTargetSession + endif + let views = split(_VimuxTmux("list-"._VimuxRunnerType()."s".targetSession ), "\n") + + if _VimuxOption("g:VimuxTargetSession", '') != '' + for view in views + if match(view, "(active)") != -1 + return split(view, ":")[0] + endif + endfor + else + for view in views + if match(view, "(active)") == -1 + return split(view, ":")[0] + endif + endfor + endif return -1 endfunction