-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathtmux_split.vim
More file actions
115 lines (107 loc) · 3.9 KB
/
Copy pathtmux_split.vim
File metadata and controls
115 lines (107 loc) · 3.9 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
if $TMUX == ''
finish
endif
if exists("*TmuxActivePane")
finish
endif
let g:R_external_term = 1
let g:R_applescript = 0
let g:rplugin.tmux_split = 1
let g:R_tmux_title = get(g:, 'R_tmux_title', 'VimR')
" 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
" Replace StartR_ExternalTerm with a function that starts R in a Tmux split pane
function! StartR_ExternalTerm(rcmd)
let g:rplugin.editor_pane = $TMUX_PANE
let tmuxconf = ['set-environment VIMR_TMPDIR "' . g:rplugin.tmpdir . '"',
\ 'set-environment VIMR_COMPLDIR "' . substitute(g:rplugin.compldir, ' ', '\\ ', "g") . '"',
\ 'set-environment VIMR_ID ' . $VIMR_ID ,
\ 'set-environment VIMR_SECRET ' . $VIMR_SECRET ,
\ 'set-environment VIMR_PORT ' . g:rplugin.myport ,
\ 'set-environment R_DEFAULT_PACKAGES ' . $R_DEFAULT_PACKAGES ]
if $NVIM_IP_ADDRESS != ""
call extend(tmuxconf, ['set-environment NVIM_IP_ADDRESS ' . $NVIM_IP_ADDRESS ])
endif
if $R_LIBS_USER != ""
call extend(tmuxconf, ['set-environment R_LIBS_USER ' . $R_LIBS_USER ])
endif
if &t_Co == 256
call extend(tmuxconf, ['set default-terminal "' . $TERM . '"'])
endif
call writefile(tmuxconf, g:rplugin.tmpdir . "/tmux" . $VIMR_ID . ".conf")
call system("tmux source-file '" . g:rplugin.tmpdir . "/tmux" . $VIMR_ID . ".conf" . "'")
call delete(g:rplugin.tmpdir . "/tmux" . $VIMR_ID . ".conf")
let tcmd = "tmux split-window "
if g:R_rconsole_width > 0 && winwidth(0) > (g:R_rconsole_width + g:R_min_editor_width + 1 + (&number * &numberwidth))
if g:R_rconsole_width == -1
let tcmd .= "-h"
else
let tcmd .= "-h -l " . g:R_rconsole_width
endif
else
let tcmd .= "-l " . g:R_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.editor_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:R_tmux_title != "automatic" && g:R_tmux_title != ""
call system("tmux rename-window " . g:R_tmux_title)
endif
call WaitVimcomStart()
" Environment variables persist across Tmux windows.
" Unset VIMR_TMPDIR to avoid vimcom loading its C library
" when R was not started by Vim:
call system("tmux set-environment -u VIMR_TMPDIR")
" Also unset R_DEFAULT_PACKAGES so that other R instances do not
" load vimcom unnecessarily
call system("tmux set-environment -u R_DEFAULT_PACKAGES")
endfunction
function SendCmdToR_TmuxSplit(...)
if g:R_clear_line
if g:R_editing_mode == "emacs"
let cmd = "\001\013" . a:1
else
let cmd = "\x1b0Da" . a:1
endif
else
let cmd = a:1
endif
let str = substitute(cmd, "'", "'\\\\''", "g")
if str =~ '^-'
let str = ' ' . str
endif
if a:0 == 2 && a:2 == 0
let scmd = "tmux set-buffer '" . str . "' ; tmux paste-buffer -t " . g:rplugin.rconsole_pane
else
let scmd = "tmux set-buffer '" . str . "\<CR>' ; tmux paste-buffer -t " . g:rplugin.rconsole_pane
endif
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