forked from vim-scripts/Vim-R-plugin
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtmux_split.vim
More file actions
205 lines (187 loc) · 7.31 KB
/
Copy pathtmux_split.vim
File metadata and controls
205 lines (187 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
" Adapted from screen plugin:
function TmuxActivePane()
let line = system("tmux list-panes | grep \'(active)$'")
let paneid = matchstr(line, '\v\%\d+ \(active\)')
if !empty(paneid)
return matchstr(paneid, '\v^\%\d+')
else
return matchstr(line, '\v^\d+')
endif
endfunction
function StartR_TmuxSplit(rcmd)
let g:rplugin_vim_pane = TmuxActivePane()
let tmuxconf = ['set-environment VIMRPLUGIN_TMPDIR "' . g:rplugin_tmpdir . '"',
\ 'set-environment VIMR_COMPLDIR "' . substitute(g:rplugin_compldir, ' ', '\\ ', "g") . '"',
\ 'set-environment VIMEDITOR_SVRNM ' . $VIMEDITOR_SVRNM ,
\ 'set-environment VIMINSTANCEID ' . $VIMINSTANCEID ,
\ 'set-environment VIMR_SECRET ' . $VIMR_SECRET ,
\ 'set-environment R_DEFAULT_PACKAGES ' . $R_DEFAULT_PACKAGES ]
if &t_Co == 256
call extend(tmuxconf, ['set default-terminal "' . $TERM . '"'])
endif
call writefile(tmuxconf, g:rplugin_tmpdir . "/tmux" . $VIMINSTANCEID . ".conf")
call system("tmux source-file '" . g:rplugin_tmpdir . "/tmux" . $VIMINSTANCEID . ".conf" . "'")
call delete(g:rplugin_tmpdir . "/tmux" . $VIMINSTANCEID . ".conf")
let tcmd = "tmux split-window "
if g:vimrplugin_vsplit
if g:vimrplugin_rconsole_width == -1
let tcmd .= "-h"
else
let tcmd .= "-h -l " . g:vimrplugin_rconsole_width
endif
else
let tcmd .= "-l " . g:vimrplugin_rconsole_height
endif
" Let Tmux automatically kill the panel when R quits.
let tcmd .= " '" . a:rcmd . "'"
let rlog = system(tcmd)
if v:shell_error
call RWarningMsg(rlog)
return
endif
let g:rplugin_rconsole_pane = TmuxActivePane()
let rlog = system("tmux select-pane -t " . g:rplugin_vim_pane)
if v:shell_error
call RWarningMsg(rlog)
return
endif
let g:SendCmdToR = function('SendCmdToR_TmuxSplit')
let g:rplugin_last_rcmd = a:rcmd
if g:vimrplugin_tmux_title != "automatic" && g:vimrplugin_tmux_title != ""
call system("tmux rename-window " . g:vimrplugin_tmux_title)
endif
if WaitVimComStart()
if g:vimrplugin_after_start != ''
call system(g:vimrplugin_after_start)
endif
endif
endfunction
function StartObjBrowser_Tmux()
if b:rplugin_extern_ob
" This is the Object Browser
echoerr "StartObjBrowser_Tmux() called."
return
endif
" Don't start the Object Browser if it already exists
if IsExternalOBRunning()
return
endif
let objbrowserfile = g:rplugin_tmpdir . "/objbrowserInit"
let tmxs = " "
call writefile([
\ 'let g:rplugin_vim_pane = "' . g:rplugin_vim_pane . '"',
\ 'let g:rplugin_rconsole_pane = "' . g:rplugin_rconsole_pane . '"',
\ 'let $VIMINSTANCEID = "' . $VIMINSTANCEID . '"',
\ 'let $VIMCOMPORT = "' . g:rplugin_vimcomport . '"',
\ 'let showmarks_enable = 0',
\ 'let g:rplugin_tmuxsname = "' . g:rplugin_tmuxsname . '"',
\ 'let b:rscript_buffer = "' . bufname("%") . '"',
\ 'set filetype=rbrowser',
\ 'let g:rplugin_vimcom_home = "' . g:rplugin_vimcom_home . '"',
\ 'let g:rplugin_vclntsrvr = "' . g:rplugin_vclntsrvr . '"',
\ 'let b:objbrtitle = "' . b:objbrtitle . '"',
\ 'let b:rplugin_extern_ob = 1',
\ 'set shortmess=atI',
\ 'set rulerformat=%3(%l%)',
\ 'set laststatus=0',
\ 'set noruler',
\ 'set updatetime=100',
\ 'let g:SendCmdToR = function("SendCmdToR_TmuxSplit")',
\ 'let g:rplugin_vimcomport = ' . g:rplugin_vimcomport,
\ 'let vcs_job = job_start([g:rplugin_vclntsrvr], {"out_cb": "ROnJobStdout", "err_cb": "ROnJobStderr"})',
\ 'let g:rplugin_channel = job_getchannel(vcs_job)',
\ 'sleep 150m'],
\ objbrowserfile)
if g:vimrplugin_objbr_place =~ "left"
let panw = system("tmux list-panes | cat")
if g:vimrplugin_objbr_place =~ "console"
" Get the R Console width:
let panw = substitute(panw, '.*[0-9]: \[\([0-9]*\)x[0-9]*.\{-}' . g:rplugin_rconsole_pane . '\>.*', '\1', "")
else
" Get the Vim width
let panw = substitute(panw, '.*[0-9]: \[\([0-9]*\)x[0-9]*.\{-}' . g:rplugin_vim_pane . '\>.*', '\1', "")
endif
let panewidth = panw - g:vimrplugin_objbr_w
" Just to be safe: If the above code doesn't work as expected
" and we get a spurious value:
if panewidth < 30 || panewidth > 180
let panewidth = 80
endif
else
let panewidth = g:vimrplugin_objbr_w
endif
if g:vimrplugin_objbr_place =~ "console"
let obpane = g:rplugin_rconsole_pane
else
let obpane = g:rplugin_vim_pane
endif
if g:rplugin_is_darwin && has("gui_macvim")
let vimexec = substitute($VIMRUNTIME, "/MacVim.app/Contents/.*", "", "") . "/MacVim.app/Contents/MacOS/Vim"
let vimexec = substitute(vimexec, ' ', '\\ ', 'g')
else
let vimexec = "vim"
endif
let cmd = "tmux split-window -h -l " . panewidth . " -t " . obpane . ' "' . vimexec . " -c 'source " . substitute(objbrowserfile, ' ', '\\ ', 'g') . "'" . '"'
let rlog = system(cmd)
if v:shell_error
let rlog = substitute(rlog, '\n', ' ', 'g')
let rlog = substitute(rlog, '\r', ' ', 'g')
call RWarningMsg(rlog)
let g:rplugin_running_objbr = 0
return 0
endif
let g:rplugin_ob_pane = TmuxActivePane()
let rlog = system("tmux select-pane -t " . g:rplugin_vim_pane)
if v:shell_error
call RWarningMsg(rlog)
return 0
endif
if g:vimrplugin_objbr_place =~ "left"
if g:vimrplugin_objbr_place =~ "console"
call system("tmux swap-pane -d -s " . g:rplugin_rconsole_pane . " -t " . g:rplugin_ob_pane)
else
call system("tmux swap-pane -d -s " . g:rplugin_vim_pane . " -t " . g:rplugin_ob_pane)
endif
endif
endfunction
function SendCmdToR_TmuxSplit(cmd)
if g:vimrplugin_ca_ck
let cmd = "\001" . "\013" . a:cmd
else
let cmd = a:cmd
endif
if !exists("g:rplugin_rconsole_pane")
" Should never happen
call RWarningMsg("Missing internal variable: g:rplugin_rconsole_pane")
endif
let str = substitute(cmd, "'", "'\\\\''", "g")
if str =~ '^-'
let str = ' ' . str
endif
let scmd = "tmux set-buffer '" . str . "\<C-M>' && tmux paste-buffer -t " . g:rplugin_rconsole_pane
let rlog = system(scmd)
if v:shell_error
let rlog = substitute(rlog, "\n", " ", "g")
let rlog = substitute(rlog, "\r", " ", "g")
call RWarningMsg(rlog)
call ClearRInfo()
return 0
endif
return 1
endfunction
function CloseExternalOB()
if IsExternalOBRunning()
call system("tmux kill-pane -t " . g:rplugin_ob_pane)
unlet g:rplugin_ob_pane
sleep 250m
endif
endfunction
function IsExternalOBRunning()
if exists("g:rplugin_ob_pane")
let plst = system("tmux list-panes | cat")
if plst =~ g:rplugin_ob_pane
return 1
endif
endif
return 0
endfunction