Cleanup public/private API‌ split & coding style (#184)

* feat: Make s:VimuxOption (was _VimuxOption) public VimuxOption
* refactor: Simplify script-local private function names
* style: Format each function as a paragraph with no blanks
* feat: Make s:tmuxExec (was _VimuxTmux) public VimuxTmux
This commit is contained in:
Caleb Maclennan 2021-02-16 15:50:49 +03:00 committed by GitHub
commit 576b24c7a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,7 +3,7 @@ if exists('g:loaded_vimux') || &compatible
endif
let g:loaded_vimux = 1
function! s:VimuxOption(option, default)
function! VimuxOption(option, default)
if exists(a:option)
return eval(a:option)
else
@ -11,12 +11,12 @@ function! s:VimuxOption(option, default)
endif
endfunction
function! s:VimuxTmuxCmd()
return s:VimuxOption('g:VimuxTmuxCommand', 'tmux')
function! s:tmuxCmd()
return VimuxOption('g:VimuxTmuxCommand', 'tmux')
endfunction
if !executable(s:VimuxTmuxCmd())
echohl ErrorMsg | echomsg 'Failed to find executable '.s:VimuxTmuxCmd() | echohl None
if !executable(s:tmuxCmd())
echohl ErrorMsg | echomsg 'Failed to find executable '.s:tmuxCmd() | echohl None
finish
endif
@ -51,21 +51,17 @@ function! VimuxRunLastCommand()
endfunction
function! VimuxRunCommand(command, ...)
if !exists('g:VimuxRunnerIndex') || s:VimuxHasRunner(g:VimuxRunnerIndex) ==# -1
if !exists('g:VimuxRunnerIndex') || s:hasRunner(g:VimuxRunnerIndex) ==# -1
call VimuxOpenRunner()
endif
let l:autoreturn = 1
if exists('a:1')
let l:autoreturn = a:1
endif
let resetSequence = s:VimuxOption('g:VimuxResetSequence', 'q C-u')
let resetSequence = VimuxOption('g:VimuxResetSequence', 'q C-u')
let g:VimuxLastCommand = a:command
call VimuxSendKeys(resetSequence)
call VimuxSendText(a:command)
if l:autoreturn ==# 1
call VimuxSendKeys('Enter')
endif
@ -77,47 +73,45 @@ endfunction
function! VimuxSendKeys(keys)
if exists('g:VimuxRunnerIndex')
call s:VimuxTmux('send-keys -t '.g:VimuxRunnerIndex.' '.a:keys)
call VimuxTmux('send-keys -t '.g:VimuxRunnerIndex.' '.a:keys)
else
echo 'No vimux runner pane/window. Create one with VimuxOpenRunner'
endif
endfunction
function! VimuxOpenRunner()
let nearestIndex = s:VimuxNearestIndex()
if s:VimuxOption('g:VimuxUseNearest', 1) ==# 1 && nearestIndex != -1
let nearestIndex = s:nearestIndex()
if VimuxOption('g:VimuxUseNearest', 1) ==# 1 && nearestIndex != -1
let g:VimuxRunnerIndex = nearestIndex
else
let extraArguments = s:VimuxOption('g:VimuxOpenExtraArgs', '')
if s:VimuxRunnerType() ==# 'pane'
let height = s:VimuxOption('g:VimuxHeight', 20)
let orientation = s:VimuxOption('g:VimuxOrientation', 'v')
call s:VimuxTmux('split-window -p '.height.' -'.orientation.' '.extraArguments)
elseif s:VimuxRunnerType() ==# 'window'
call s:VimuxTmux('new-window '.extraArguments)
let extraArguments = VimuxOption('g:VimuxOpenExtraArgs', '')
if s:runnerType() ==# 'pane'
let height = VimuxOption('g:VimuxHeight', 20)
let orientation = VimuxOption('g:VimuxOrientation', 'v')
call VimuxTmux('split-window -p '.height.' -'.orientation.' '.extraArguments)
elseif s:runnerType() ==# 'window'
call VimuxTmux('new-window '.extraArguments)
endif
let g:VimuxRunnerIndex = s:VimuxTmuxIndex()
call s:VimuxSetRunnerName()
call s:VimuxTmux('last-'.s:VimuxRunnerType())
let g:VimuxRunnerIndex = s:tmuxIndex()
call s:setRunnerName()
call VimuxTmux('last-'.s:runnerType())
endif
endfunction
function! VimuxCloseRunner()
if exists('g:VimuxRunnerIndex')
call s:VimuxTmux('kill-'.s:VimuxRunnerType().' -t '.g:VimuxRunnerIndex)
call VimuxTmux('kill-'.s:runnerType().' -t '.g:VimuxRunnerIndex)
unlet g:VimuxRunnerIndex
endif
endfunction
function! VimuxTogglePane()
if exists('g:VimuxRunnerIndex')
if s:VimuxRunnerType() ==# 'window'
call s:VimuxTmux('join-pane -d -s '.g:VimuxRunnerIndex.' -p '.s:VimuxOption('g:VimuxHeight', 20))
if s:runnerType() ==# 'window'
call VimuxTmux('join-pane -d -s '.g:VimuxRunnerIndex.' -p '.VimuxOption('g:VimuxHeight', 20))
let g:VimuxRunnerType = 'pane'
elseif s:VimuxRunnerType() ==# 'pane'
let g:VimuxRunnerIndex=substitute(s:VimuxTmux('break-pane -d -t '.g:VimuxRunnerIndex." -P -F '#{window_id}'"), '\n', '', '')
elseif s:runnerType() ==# 'pane'
let g:VimuxRunnerIndex=substitute(VimuxTmux('break-pane -d -t '.g:VimuxRunnerIndex." -P -F '#{window_id}'"), '\n', '', '')
let g:VimuxRunnerType = 'window'
endif
endif
@ -125,28 +119,28 @@ endfunction
function! VimuxZoomRunner()
if exists('g:VimuxRunnerIndex')
if s:VimuxRunnerType() ==# 'pane'
call s:VimuxTmux('resize-pane -Z -t '.g:VimuxRunnerIndex)
elseif s:VimuxRunnerType() ==# 'window'
call s:VimuxTmux('select-window -t '.g:VimuxRunnerIndex)
if s:runnerType() ==# 'pane'
call VimuxTmux('resize-pane -Z -t '.g:VimuxRunnerIndex)
elseif s:runnerType() ==# 'window'
call VimuxTmux('select-window -t '.g:VimuxRunnerIndex)
endif
endif
endfunction
function! VimuxInspectRunner()
call s:VimuxTmux('select-'.s:VimuxRunnerType().' -t '.g:VimuxRunnerIndex)
call s:VimuxTmux('copy-mode')
call VimuxTmux('select-'.s:runnerType().' -t '.g:VimuxRunnerIndex)
call VimuxTmux('copy-mode')
endfunction
function! VimuxScrollUpInspect()
call VimuxInspectRunner()
call s:VimuxTmux('last-'.s:VimuxRunnerType())
call VimuxTmux('last-'.s:runnerType())
call VimuxSendKeys('C-u')
endfunction
function! VimuxScrollDownInspect()
call VimuxInspectRunner()
call s:VimuxTmux('last-'.s:VimuxRunnerType())
call VimuxTmux('last-'.s:runnerType())
call VimuxSendKeys('C-d')
endfunction
@ -162,63 +156,61 @@ endfunction
function! VimuxClearRunnerHistory()
if exists('g:VimuxRunnerIndex')
call s:VimuxTmux('clear-history -t '.g:VimuxRunnerIndex)
call VimuxTmux('clear-history -t '.g:VimuxRunnerIndex)
endif
endfunction
function! VimuxPromptCommand(...)
let command = a:0 ==# 1 ? a:1 : ''
let l:command = input(s:VimuxOption('g:VimuxPromptString', 'Command? '), command, 'shellcmd')
let l:command = input(VimuxOption('g:VimuxPromptString', 'Command? '), command, 'shellcmd')
call VimuxRunCommand(l:command)
endfunction
function! s:VimuxTmux(arguments)
if s:VimuxOption('g:VimuxDebug', 0) != 0
echom s:VimuxTmuxCmd().' '.a:arguments
function! VimuxTmux(arguments)
if VimuxOption('g:VimuxDebug', 0) != 0
echom s:tmuxCmd().' '.a:arguments
endif
return system(s:VimuxTmuxCmd().' '.a:arguments)
return system(s:tmuxCmd().' '.a:arguments)
endfunction
function! s:VimuxTmuxSession()
return s:VimuxTmuxProperty('#S')
function! s:tmuxSession()
return s:tmuxProperty('#S')
endfunction
function! s:VimuxTmuxIndex()
if s:VimuxRunnerType() ==# 'pane'
return s:VimuxTmuxPaneId()
function! s:tmuxIndex()
if s:runnerType() ==# 'pane'
return s:tmuxPaneId()
else
return s:VimuxTmuxWindowId()
return s:tmuxWindowId()
end
endfunction
function! s:VimuxTmuxPaneId()
return s:VimuxTmuxProperty('#{pane_id}')
function! s:tmuxPaneId()
return s:tmuxProperty('#{pane_id}')
endfunction
function! s:VimuxTmuxWindowId()
return s:VimuxTmuxProperty('#{window_id}')
function! s:tmuxWindowId()
return s:tmuxProperty('#{window_id}')
endfunction
function! s:VimuxNearestIndex()
let t = s:VimuxRunnerType()
let filter = s:VimuxGetTargetFilter()
let views = split(s:VimuxTmux('list-'.t."s -F '#{".t.'_active}:#{'.t."_id}'".filter), '\n')
function! s:nearestIndex()
let t = s:runnerType()
let filter = s:getTargetFilter()
let views = split(VimuxTmux('list-'.t."s -F '#{".t.'_active}:#{'.t."_id}'".filter), '\n')
for view in views
if match(view, '1:') ==# -1
return split(view, ':')[1]
endif
endfor
return -1
endfunction
function! s:VimuxGetTargetFilter()
let targetName = s:VimuxOption('g:VimuxRunnerName', '')
function! s:getTargetFilter()
let targetName = VimuxOption('g:VimuxRunnerName', '')
if targetName ==# ''
return ''
endif
let t = s:VimuxRunnerType()
let t = s:runnerType()
if t ==# 'window'
return " -f '#{==:#{window_name},".targetName."}'"
elseif t ==# 'pane'
@ -226,29 +218,28 @@ function! s:VimuxGetTargetFilter()
endif
endfunction
function! s:VimuxSetRunnerName()
let targetName = s:VimuxOption('g:VimuxRunnerName', '')
function! s:setRunnerName()
let targetName = VimuxOption('g:VimuxRunnerName', '')
if targetName ==# ''
return
return
endif
let t = s:VimuxRunnerType()
let t = s:runnerType()
if t ==# 'window'
call s:VimuxTmux('rename-window '.targetName)
call VimuxTmux('rename-window '.targetName)
elseif t ==# 'pane'
call s:VimuxTmux('select-pane -T '.targetName)
call VimuxTmux('select-pane -T '.targetName)
endif
endfunction
function! s:VimuxRunnerType()
return s:VimuxOption('g:VimuxRunnerType', 'pane')
function! s:runnerType()
return VimuxOption('g:VimuxRunnerType', 'pane')
endfunction
function! s:VimuxTmuxProperty(property)
return substitute(s:VimuxTmux("display -p '".a:property."'"), '\n$', '', '')
function! s:tmuxProperty(property)
return substitute(VimuxTmux("display -p '".a:property."'"), '\n$', '', '')
endfunction
function! s:VimuxHasRunner(index)
let t = s:VimuxRunnerType()
return match(s:VimuxTmux('list-'.t."s -F '#{".t."_id}'"), a:index)
function! s:hasRunner(index)
let t = s:runnerType()
return match(VimuxTmux('list-'.t."s -F '#{".t."_id}'"), a:index)
endfunction