Support tmux's target-pane and target-window syntax for finding an existing runner (#210)

fix-tmux-next-3dot4
Michael van der Kamp 2 years ago committed by GitHub
parent 89604a4464
commit 3daa0e91f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -447,5 +447,27 @@ previously-run commands in VimuxPromptCommand.
< <
Default: 1 Default: 1
------------------------------------------------------------------------------
*VimuxRunnerQuery*
4.13 g:VimuxRunnerQuery~
Set this option to define a query to use for looking up an existing runner
pane or window when initiating Vimux. Uses the tmux syntax for the target-pane
and target-window command arguments. (See the man page for tmux). It must be a
dictionary containing up to two keys, "pane" and "window", defining the query
to use for the respective runner types.
If no key exists for the current runner type, the search for an existing
runner falls back to the `VimuxUseNearest` option (and the related
`VimuxRunnerName`). If that option is false or either command fails, a new
runner is created instead, positioned according to `VimuxOrientation`.
>
let g:VimuxRunnerQuery = {
\ 'pane': '{down-of}',
\ 'window': 'vimux',
\}
<
Default: {}
============================================================================== ==============================================================================
vim:tw=78:ts=2:sw=2:expandtab:ft=help:norl: vim:tw=78:ts=2:sw=2:expandtab:ft=help:norl:

@ -92,9 +92,9 @@ function! VimuxSendKeys(keys) abort
endfunction endfunction
function! VimuxOpenRunner() abort function! VimuxOpenRunner() abort
let nearestIndex = s:nearestIndex() let existingId = s:existingRunnerId()
if VimuxOption('VimuxUseNearest') ==# 1 && nearestIndex != -1 if existingId !=# ''
let g:VimuxRunnerIndex = nearestIndex let g:VimuxRunnerIndex = existingId
else else
let extraArguments = VimuxOption('VimuxOpenExtraArgs') let extraArguments = VimuxOption('VimuxOpenExtraArgs')
if VimuxOption('VimuxRunnerType') ==# 'pane' if VimuxOption('VimuxRunnerType') ==# 'pane'
@ -123,7 +123,12 @@ function! VimuxTogglePane() abort
let g:VimuxRunnerIndex = s:tmuxIndex() let g:VimuxRunnerIndex = s:tmuxIndex()
call VimuxTmux('last-'.VimuxOption('VimuxRunnerType')) call VimuxTmux('last-'.VimuxOption('VimuxRunnerType'))
elseif VimuxOption('VimuxRunnerType') ==# 'pane' elseif VimuxOption('VimuxRunnerType') ==# 'pane'
let g:VimuxRunnerIndex=substitute(VimuxTmux('break-pane -d -s '.g:VimuxRunnerIndex." -P -F '#{window_id}'"), '\n', '', '') let g:VimuxRunnerIndex=substitute(
\ VimuxTmux('break-pane -d -s '.g:VimuxRunnerIndex." -P -F '#{window_id}'"),
\ '\n',
\ '',
\ ''
\)
let g:VimuxRunnerType = 'window' let g:VimuxRunnerType = 'window'
endif endif
endif endif
@ -222,16 +227,49 @@ function! s:vimuxPaneOptions() abort
return '-p '.height.' -'.orientation return '-p '.height.' -'.orientation
endfunction endfunction
function! s:nearestIndex() abort ""
let t = VimuxOption('VimuxRunnerType') " @return a string of the form '%4', the ID of the pane or window to use,
" or '' if no nearest pane or window is found.
function! s:existingRunnerId() abort
let runnerType = VimuxOption('VimuxRunnerType')
let query = get(VimuxOption('VimuxRunnerQuery'), runnerType, '')
if empty(query)
if !empty(VimuxOption('VimuxUseNearest'))
return s:nearestRunnerId()
endif
endif
" Try finding the runner using the provided query
let message = VimuxTmux('select-'.runnerType.' -t '.query.'')
if message ==# ''
" Success!
let runnerId = s:tmuxIndex()
call VimuxTmux('last-'.runnerType)
return runnerId
endif
return ''
endfunction
function! s:nearestRunnerId() abort
" Try finding the runner in the current window/session, optionally using a
" name/title filter
let runnerType = VimuxOption('VimuxRunnerType')
let filter = s:getTargetFilter() let filter = s:getTargetFilter()
let views = split(VimuxTmux('list-'.t."s -F '#{".t.'_active}:#{'.t."_id}'".filter), '\n') " list-panes -F '#{pane_active}:#{pane_id}' -f '#{==:#{pane_title}, " foo}'
" select-pane -t:.'{last}'
let views = split(
\ VimuxTmux(
\ 'list-'.runnerType.'s'
\ ." -F '#{".runnerType.'_active}:#{'.runnerType."_id}'"
\ .filter),
\ '\n')
" '1:' is the current active pane (the one with vim).
" Find the first non-active pane.
for view in views for view in views
if match(view, '1:') ==# -1 if match(view, '1:') ==# -1
return split(view, ':')[1] return split(view, ':')[1]
endif endif
endfor endfor
return -1 return ''
endfunction endfunction
function! s:getTargetFilter() abort function! s:getTargetFilter() abort
@ -239,10 +277,10 @@ function! s:getTargetFilter() abort
if targetName ==# '' if targetName ==# ''
return '' return ''
endif endif
let t = VimuxOption('VimuxRunnerType') let runnerType = VimuxOption('VimuxRunnerType')
if t ==# 'window' if runnerType ==# 'window'
return " -f '#{==:#{window_name},".targetName."}'" return " -f '#{==:#{window_name},".targetName."}'"
elseif t ==# 'pane' elseif runnerType ==# 'pane'
return " -f '#{==:#{pane_title},".targetName."}'" return " -f '#{==:#{pane_title},".targetName."}'"
endif endif
endfunction endfunction
@ -252,10 +290,10 @@ function! s:setRunnerName() abort
if targetName ==# '' if targetName ==# ''
return return
endif endif
let t = VimuxOption('VimuxRunnerType') let runnerType = VimuxOption('VimuxRunnerType')
if t ==# 'window' if runnerType ==# 'window'
call VimuxTmux('rename-window '.targetName) call VimuxTmux('rename-window '.targetName)
elseif t ==# 'pane' elseif runnerType ==# 'pane'
call VimuxTmux('select-pane -T '.targetName) call VimuxTmux('select-pane -T '.targetName)
endif endif
endfunction endfunction
@ -265,8 +303,8 @@ function! s:tmuxProperty(property) abort
endfunction endfunction
function! s:hasRunner(index) abort function! s:hasRunner(index) abort
let t = VimuxOption('VimuxRunnerType') let runnerType = VimuxOption('VimuxRunnerType')
return match(VimuxTmux('list-'.t."s -F '#{".t."_id}'"), a:index) return match(VimuxTmux('list-'.runnerType."s -F '#{".runnerType."_id}'"), a:index)
endfunction endfunction
function! s:autoclose() abort function! s:autoclose() abort

Loading…
Cancel
Save