Skip to content

Commit 8cc4fe9

Browse files
authored
Merge pull request #183 from mvanderkamp/initialize_settings
2 parents 576b24c + f4df4c9 commit 8cc4fe9

File tree

2 files changed

+51
-49
lines changed

2 files changed

+51
-49
lines changed

doc/vimux.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,9 @@ extra return. Thanks to @trptcolin for discovering this issue.
287287
==============================================================================
288288
CONFIGURATION (4) *VimuxConfiguration*
289289

290-
You can configure Vimux like this:
290+
You can configure Vimux as follows. Note that all occurances of global
291+
variables `g:Vimux...` may also be set using buffer variables `b:Vimux...` to
292+
change the behavior of Vimux in just the current buffer.
291293

292294
------------------------------------------------------------------------------
293295
*VimuxConfiguration_height*

plugin/vimux.vim

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

6-
function! 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:tmuxCmd()
15-
return VimuxOption('g:VimuxTmuxCommand', 'tmux')
16-
endfunction
17-
18-
if !executable(s:tmuxCmd())
19-
echohl ErrorMsg | echomsg 'Failed to find executable '.s:tmuxCmd() | 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+
function! VimuxOption(name) abort
19+
return get(b:, a:name, get(g:, a:name))
20+
endfunction
21+
22+
if !executable(VimuxOption('VimuxTmuxCommand'))
23+
echohl ErrorMsg | echomsg 'Failed to find executable '.VimuxOption('VimuxTmuxCommand') | echohl None
2024
finish
2125
endif
2226

@@ -58,7 +62,7 @@ function! VimuxRunCommand(command, ...)
5862
if exists('a:1')
5963
let l:autoreturn = a:1
6064
endif
61-
let resetSequence = VimuxOption('g:VimuxResetSequence', 'q C-u')
65+
let resetSequence = VimuxOption('VimuxResetSequence')
6266
let g:VimuxLastCommand = a:command
6367
call VimuxSendKeys(resetSequence)
6468
call VimuxSendText(a:command)
@@ -81,66 +85,66 @@ endfunction
8185

8286
function! VimuxOpenRunner()
8387
let nearestIndex = s:nearestIndex()
84-
if VimuxOption('g:VimuxUseNearest', 1) ==# 1 && nearestIndex != -1
88+
if VimuxOption('VimuxUseNearest') ==# 1 && nearestIndex != -1
8589
let g:VimuxRunnerIndex = nearestIndex
8690
else
87-
let extraArguments = VimuxOption('g:VimuxOpenExtraArgs', '')
88-
if s:runnerType() ==# 'pane'
89-
let height = VimuxOption('g:VimuxHeight', 20)
90-
let orientation = VimuxOption('g:VimuxOrientation', 'v')
91+
let extraArguments = VimuxOption('VimuxOpenExtraArgs')
92+
if VimuxOption('VimuxRunnerType') ==# 'pane'
93+
let height = VimuxOption('VimuxHeight')
94+
let orientation = VimuxOption('VimuxOrientation')
9195
call VimuxTmux('split-window -p '.height.' -'.orientation.' '.extraArguments)
92-
elseif s:runnerType() ==# 'window'
96+
elseif VimuxOption('VimuxRunnerType') ==# 'window'
9397
call VimuxTmux('new-window '.extraArguments)
9498
endif
9599
let g:VimuxRunnerIndex = s:tmuxIndex()
96100
call s:setRunnerName()
97-
call VimuxTmux('last-'.s:runnerType())
101+
call VimuxTmux('last-'.VimuxOption('VimuxRunnerType'))
98102
endif
99103
endfunction
100104

101105
function! VimuxCloseRunner()
102106
if exists('g:VimuxRunnerIndex')
103-
call VimuxTmux('kill-'.s:runnerType().' -t '.g:VimuxRunnerIndex)
107+
call VimuxTmux('kill-'.VimuxOption('VimuxRunnerType').' -t '.g:VimuxRunnerIndex)
104108
unlet g:VimuxRunnerIndex
105109
endif
106110
endfunction
107111

108112
function! VimuxTogglePane()
109113
if exists('g:VimuxRunnerIndex')
110-
if s:runnerType() ==# 'window'
111-
call VimuxTmux('join-pane -d -s '.g:VimuxRunnerIndex.' -p '.VimuxOption('g:VimuxHeight', 20))
112-
let g:VimuxRunnerType = 'pane'
113-
elseif s:runnerType() ==# 'pane'
114+
if VimuxOption('VimuxRunnerType') ==# 'window'
115+
call VimuxTmux('join-pane -d -s '.g:VimuxRunnerIndex.' -p '.VimuxOption('VimuxHeight'))
116+
let VimuxOption('VimuxRunnerType') = 'pane'
117+
elseif VimuxOption('VimuxRunnerType') ==# 'pane'
114118
let g:VimuxRunnerIndex=substitute(VimuxTmux('break-pane -d -t '.g:VimuxRunnerIndex." -P -F '#{window_id}'"), '\n', '', '')
115-
let g:VimuxRunnerType = 'window'
119+
let VimuxOption('VimuxRunnerType') = 'window'
116120
endif
117121
endif
118122
endfunction
119123

120124
function! VimuxZoomRunner()
121125
if exists('g:VimuxRunnerIndex')
122-
if s:runnerType() ==# 'pane'
126+
if VimuxOption('VimuxRunnerType') ==# 'pane'
123127
call VimuxTmux('resize-pane -Z -t '.g:VimuxRunnerIndex)
124-
elseif s:runnerType() ==# 'window'
128+
elseif VimuxOption('VimuxRunnerType') ==# 'window'
125129
call VimuxTmux('select-window -t '.g:VimuxRunnerIndex)
126130
endif
127131
endif
128132
endfunction
129133

130134
function! VimuxInspectRunner()
131-
call VimuxTmux('select-'.s:runnerType().' -t '.g:VimuxRunnerIndex)
135+
call VimuxTmux('select-'.VimuxOption('VimuxRunnerType').' -t '.g:VimuxRunnerIndex)
132136
call VimuxTmux('copy-mode')
133137
endfunction
134138

135139
function! VimuxScrollUpInspect()
136140
call VimuxInspectRunner()
137-
call VimuxTmux('last-'.s:runnerType())
141+
call VimuxTmux('last-'.VimuxOption('VimuxRunnerType'))
138142
call VimuxSendKeys('C-u')
139143
endfunction
140144

141145
function! VimuxScrollDownInspect()
142146
call VimuxInspectRunner()
143-
call VimuxTmux('last-'.s:runnerType())
147+
call VimuxTmux('last-'.VimuxOption('VimuxRunnerType'))
144148
call VimuxSendKeys('C-d')
145149
endfunction
146150

@@ -162,23 +166,23 @@ endfunction
162166

163167
function! VimuxPromptCommand(...)
164168
let command = a:0 ==# 1 ? a:1 : ''
165-
let l:command = input(VimuxOption('g:VimuxPromptString', 'Command? '), command, 'shellcmd')
169+
let l:command = input(VimuxOption('VimuxPromptString'), command, 'shellcmd')
166170
call VimuxRunCommand(l:command)
167171
endfunction
168172

169173
function! VimuxTmux(arguments)
170-
if VimuxOption('g:VimuxDebug', 0) != 0
171-
echom s:tmuxCmd().' '.a:arguments
174+
if VimuxOption('VimuxDebug')
175+
echom VimuxOption('VimuxTmuxCommand').' '.a:arguments
172176
endif
173-
return system(s:tmuxCmd().' '.a:arguments)
177+
return system(VimuxOption('VimuxTmuxCommand').' '.a:arguments)
174178
endfunction
175179

176180
function! s:tmuxSession()
177181
return s:tmuxProperty('#S')
178182
endfunction
179183

180184
function! s:tmuxIndex()
181-
if s:runnerType() ==# 'pane'
185+
if VimuxOption('VimuxRunnerType') ==# 'pane'
182186
return s:tmuxPaneId()
183187
else
184188
return s:tmuxWindowId()
@@ -194,7 +198,7 @@ function! s:tmuxWindowId()
194198
endfunction
195199

196200
function! s:nearestIndex()
197-
let t = s:runnerType()
201+
let t = VimuxOption('VimuxRunnerType')
198202
let filter = s:getTargetFilter()
199203
let views = split(VimuxTmux('list-'.t."s -F '#{".t.'_active}:#{'.t."_id}'".filter), '\n')
200204
for view in views
@@ -206,11 +210,11 @@ function! s:nearestIndex()
206210
endfunction
207211

208212
function! s:getTargetFilter()
209-
let targetName = VimuxOption('g:VimuxRunnerName', '')
213+
let targetName = VimuxOption('VimuxRunnerName')
210214
if targetName ==# ''
211215
return ''
212216
endif
213-
let t = s:runnerType()
217+
let t = VimuxOption('VimuxRunnerType')
214218
if t ==# 'window'
215219
return " -f '#{==:#{window_name},".targetName."}'"
216220
elseif t ==# 'pane'
@@ -219,27 +223,23 @@ function! s:getTargetFilter()
219223
endfunction
220224

221225
function! s:setRunnerName()
222-
let targetName = VimuxOption('g:VimuxRunnerName', '')
226+
let targetName = VimuxOption('VimuxRunnerName')
223227
if targetName ==# ''
224228
return
225229
endif
226-
let t = s:runnerType()
230+
let t = VimuxOption('VimuxRunnerType')
227231
if t ==# 'window'
228232
call VimuxTmux('rename-window '.targetName)
229233
elseif t ==# 'pane'
230234
call VimuxTmux('select-pane -T '.targetName)
231235
endif
232236
endfunction
233237

234-
function! s:runnerType()
235-
return VimuxOption('g:VimuxRunnerType', 'pane')
236-
endfunction
237-
238238
function! s:tmuxProperty(property)
239239
return substitute(VimuxTmux("display -p '".a:property."'"), '\n$', '', '')
240240
endfunction
241241

242242
function! s:hasRunner(index)
243-
let t = s:runnerType()
243+
let t = VimuxOption('VimuxRunnerType')
244244
return match(VimuxTmux('list-'.t."s -F '#{".t."_id}'"), a:index)
245245
endfunction

0 commit comments

Comments
 (0)