Skip to content

Commit 748b54b

Browse files
mvanderkampalerque
authored andcommitted
Define options once at startup
This also removes a couple of "getter" functions that were being used to look up options and fall back to their defaults
1 parent 5f999f4 commit 748b54b

File tree

1 file changed

+42
-47
lines changed

1 file changed

+42
-47
lines changed

plugin/vimux.vim

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ if exists('g:loaded_vimux') || &compatible
33
endif
44
let g:loaded_vimux = 1
55

6-
function! s:VimuxOption(option, default)
7-
if exists(a:option)
8-
return eval(a:option)
9-
else
10-
return a:default
11-
endif
12-
endfunction
13-
14-
function! s:VimuxTmuxCmd()
15-
return s:VimuxOption('g:VimuxTmuxCommand', 'tmux')
16-
endfunction
17-
18-
if !executable(s:VimuxTmuxCmd())
19-
echohl ErrorMsg | echomsg 'Failed to find executable '.s:VimuxTmuxCmd() | echohl None
6+
" Set up all global options with defaults right away, in one place
7+
let g:VimuxDebug = get(g:, 'VimuxDebug', v:false)
8+
let g:VimuxHeight = get(g:, 'VimuxHeight', 20)
9+
let g:VimuxOpenExtraArgs = get(g:, 'VimuxOpenExtraArgs', '')
10+
let g:VimuxOrientation = get(g:, 'VimuxOrientation', 'v')
11+
let g:VimuxPromptString = get(g:, 'VimuxPromptString', 'Command? ')
12+
let g:VimuxResetSequence = get(g:, 'VimuxResetSequence', 'q C-u')
13+
let g:VimuxRunnerName = get(g:, 'VimuxRunnerName', '')
14+
let g:VimuxRunnerType = get(g:, 'VimuxRunnerType', 'pane')
15+
let g:VimuxTmuxCommand = get(g:, 'VimuxTmuxCommand', 'tmux')
16+
let g:VimuxUseNearest = get(g:, 'VimuxUseNearest', v:true)
17+
18+
if !executable(g:VimuxTmuxCommand)
19+
echohl ErrorMsg | echomsg 'Failed to find executable '.g:VimuxTmuxCommand | echohl None
2020
finish
2121
endif
2222

@@ -60,7 +60,7 @@ function! VimuxRunCommand(command, ...)
6060
let l:autoreturn = a:1
6161
endif
6262

63-
let resetSequence = s:VimuxOption('g:VimuxResetSequence', 'q C-u')
63+
let resetSequence = g:VimuxResetSequence
6464
let g:VimuxLastCommand = a:command
6565

6666
call VimuxSendKeys(resetSequence)
@@ -86,37 +86,37 @@ endfunction
8686
function! VimuxOpenRunner()
8787
let nearestIndex = s:VimuxNearestIndex()
8888

89-
if s:VimuxOption('g:VimuxUseNearest', 1) ==# 1 && nearestIndex != -1
89+
if g:VimuxUseNearest ==# 1 && nearestIndex != -1
9090
let g:VimuxRunnerIndex = nearestIndex
9191
else
92-
let extraArguments = s:VimuxOption('g:VimuxOpenExtraArgs', '')
93-
if s:VimuxRunnerType() ==# 'pane'
94-
let height = s:VimuxOption('g:VimuxHeight', 20)
95-
let orientation = s:VimuxOption('g:VimuxOrientation', 'v')
92+
let extraArguments = g:VimuxOpenExtraArgs
93+
if g:VimuxRunnerType ==# 'pane'
94+
let height = g:VimuxHeight
95+
let orientation = g:VimuxOrientation
9696
call s:VimuxTmux('split-window -p '.height.' -'.orientation.' '.extraArguments)
97-
elseif s:VimuxRunnerType() ==# 'window'
97+
elseif g:VimuxRunnerType ==# 'window'
9898
call s:VimuxTmux('new-window '.extraArguments)
9999
endif
100100

101101
let g:VimuxRunnerIndex = s:VimuxTmuxIndex()
102102
call s:VimuxSetRunnerName()
103-
call s:VimuxTmux('last-'.s:VimuxRunnerType())
103+
call s:VimuxTmux('last-'.g:VimuxRunnerType)
104104
endif
105105
endfunction
106106

107107
function! VimuxCloseRunner()
108108
if exists('g:VimuxRunnerIndex')
109-
call s:VimuxTmux('kill-'.s:VimuxRunnerType().' -t '.g:VimuxRunnerIndex)
109+
call s:VimuxTmux('kill-'.g:VimuxRunnerType.' -t '.g:VimuxRunnerIndex)
110110
unlet g:VimuxRunnerIndex
111111
endif
112112
endfunction
113113

114114
function! VimuxTogglePane()
115115
if exists('g:VimuxRunnerIndex')
116-
if s:VimuxRunnerType() ==# 'window'
117-
call s:VimuxTmux('join-pane -d -s '.g:VimuxRunnerIndex.' -p '.s:VimuxOption('g:VimuxHeight', 20))
116+
if g:VimuxRunnerType ==# 'window'
117+
call s:VimuxTmux('join-pane -d -s '.g:VimuxRunnerIndex.' -p '.g:VimuxHeight)
118118
let g:VimuxRunnerType = 'pane'
119-
elseif s:VimuxRunnerType() ==# 'pane'
119+
elseif g:VimuxRunnerType ==# 'pane'
120120
let g:VimuxRunnerIndex=substitute(s:VimuxTmux('break-pane -d -t '.g:VimuxRunnerIndex." -P -F '#{window_id}'"), '\n', '', '')
121121
let g:VimuxRunnerType = 'window'
122122
endif
@@ -125,28 +125,28 @@ endfunction
125125

126126
function! VimuxZoomRunner()
127127
if exists('g:VimuxRunnerIndex')
128-
if s:VimuxRunnerType() ==# 'pane'
128+
if g:VimuxRunnerType ==# 'pane'
129129
call s:VimuxTmux('resize-pane -Z -t '.g:VimuxRunnerIndex)
130-
elseif s:VimuxRunnerType() ==# 'window'
130+
elseif g:VimuxRunnerType ==# 'window'
131131
call s:VimuxTmux('select-window -t '.g:VimuxRunnerIndex)
132132
endif
133133
endif
134134
endfunction
135135

136136
function! VimuxInspectRunner()
137-
call s:VimuxTmux('select-'.s:VimuxRunnerType().' -t '.g:VimuxRunnerIndex)
137+
call s:VimuxTmux('select-'.g:VimuxRunnerType.' -t '.g:VimuxRunnerIndex)
138138
call s:VimuxTmux('copy-mode')
139139
endfunction
140140

141141
function! VimuxScrollUpInspect()
142142
call VimuxInspectRunner()
143-
call s:VimuxTmux('last-'.s:VimuxRunnerType())
143+
call s:VimuxTmux('last-'.g:VimuxRunnerType)
144144
call VimuxSendKeys('C-u')
145145
endfunction
146146

147147
function! VimuxScrollDownInspect()
148148
call VimuxInspectRunner()
149-
call s:VimuxTmux('last-'.s:VimuxRunnerType())
149+
call s:VimuxTmux('last-'.g:VimuxRunnerType)
150150
call VimuxSendKeys('C-d')
151151
endfunction
152152

@@ -168,23 +168,23 @@ endfunction
168168

169169
function! VimuxPromptCommand(...)
170170
let command = a:0 ==# 1 ? a:1 : ''
171-
let l:command = input(s:VimuxOption('g:VimuxPromptString', 'Command? '), command, 'shellcmd')
171+
let l:command = input(g:VimuxPromptString, command, 'shellcmd')
172172
call VimuxRunCommand(l:command)
173173
endfunction
174174

175175
function! s:VimuxTmux(arguments)
176-
if s:VimuxOption('g:VimuxDebug', 0) != 0
177-
echom s:VimuxTmuxCmd().' '.a:arguments
176+
if g:VimuxDebug
177+
echom g:VimuxTmuxCommand.' '.a:arguments
178178
endif
179-
return system(s:VimuxTmuxCmd().' '.a:arguments)
179+
return system(g:VimuxTmuxCommand.' '.a:arguments)
180180
endfunction
181181

182182
function! s:VimuxTmuxSession()
183183
return s:VimuxTmuxProperty('#S')
184184
endfunction
185185

186186
function! s:VimuxTmuxIndex()
187-
if s:VimuxRunnerType() ==# 'pane'
187+
if g:VimuxRunnerType ==# 'pane'
188188
return s:VimuxTmuxPaneId()
189189
else
190190
return s:VimuxTmuxWindowId()
@@ -200,7 +200,7 @@ function! s:VimuxTmuxWindowId()
200200
endfunction
201201

202202
function! s:VimuxNearestIndex()
203-
let t = s:VimuxRunnerType()
203+
let t = g:VimuxRunnerType
204204
let filter = s:VimuxGetTargetFilter()
205205
let views = split(s:VimuxTmux('list-'.t."s -F '#{".t.'_active}:#{'.t."_id}'".filter), '\n')
206206

@@ -214,11 +214,11 @@ function! s:VimuxNearestIndex()
214214
endfunction
215215

216216
function! s:VimuxGetTargetFilter()
217-
let targetName = s:VimuxOption('g:VimuxRunnerName', '')
217+
let targetName = g:VimuxRunnerName
218218
if targetName ==# ''
219219
return ''
220220
endif
221-
let t = s:VimuxRunnerType()
221+
let t = g:VimuxRunnerType
222222
if t ==# 'window'
223223
return " -f '#{==:#{window_name},".targetName."}'"
224224
elseif t ==# 'pane'
@@ -227,28 +227,23 @@ function! s:VimuxGetTargetFilter()
227227
endfunction
228228

229229
function! s:VimuxSetRunnerName()
230-
let targetName = s:VimuxOption('g:VimuxRunnerName', '')
230+
let targetName = g:VimuxRunnerName
231231
if targetName ==# ''
232232
return
233233
endif
234-
let t = s:VimuxRunnerType()
234+
let t = g:VimuxRunnerType
235235
if t ==# 'window'
236236
call s:VimuxTmux('rename-window '.targetName)
237237
elseif t ==# 'pane'
238238
call s:VimuxTmux('select-pane -T '.targetName)
239239
endif
240240
endfunction
241241

242-
243-
function! s:VimuxRunnerType()
244-
return s:VimuxOption('g:VimuxRunnerType', 'pane')
245-
endfunction
246-
247242
function! s:VimuxTmuxProperty(property)
248243
return substitute(s:VimuxTmux("display -p '".a:property."'"), '\n$', '', '')
249244
endfunction
250245

251246
function! s:VimuxHasRunner(index)
252-
let t = s:VimuxRunnerType()
247+
let t = g:VimuxRunnerType
253248
return match(s:VimuxTmux('list-'.t."s -F '#{".t."_id}'"), a:index)
254249
endfunction

0 commit comments

Comments
 (0)