-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
iterm.vim
141 lines (130 loc) · 4.77 KB
/
iterm.vim
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
" dispatch.vim iTerm strategy
if exists('g:autoloaded_dispatch_iterm')
finish
endif
let g:autoloaded_dispatch_iterm = 1
function! dispatch#iterm#handle(request) abort
if $TERM_PROGRAM !=# 'iTerm.app' && !((has('gui_macvim') || has('gui_vimr')) && has('gui_running'))
return 0
endif
if a:request.action ==# 'start'
return dispatch#iterm#spawn(dispatch#prepare_start(a:request), a:request, !a:request.background)
endif
endfunction
function! dispatch#iterm#is_modern_version() abort
return s:osascript(
\ 'on modernversion(version)',
\ 'set olddelimiters to AppleScript''s text item delimiters',
\ 'set AppleScript''s text item delimiters to "."',
\ 'set thearray to every text item of version',
\ 'set AppleScript''s text item delimiters to olddelimiters',
\ 'set major to item 1 of thearray',
\ 'set minor to item 2 of thearray',
\ 'set veryminor to item 3 of thearray',
\ 'if major < 2 then return false',
\ 'if major > 2 then return true',
\ 'if minor < 9 then return false',
\ 'if minor > 9 then return true',
\ 'if veryminor < 20140903 then return false',
\ 'return true',
\ 'end modernversion',
\ 'tell application "iTerm"',
\ 'if not my modernversion(version) then error',
\ 'end tell')
endfunction
function! dispatch#iterm#spawn(command, request, activate) abort
if dispatch#iterm#is_modern_version()
return dispatch#iterm#spawn3(a:command, a:request, a:activate)
else
return dispatch#iterm#spawn2(a:command, a:request, a:activate)
endif
endfunction
function! dispatch#iterm#spawn2(command, request, activate) abort
let script = dispatch#isolate(a:request, [],
\ dispatch#set_title(a:request), a:command)
return s:osascript(
\ 'if application "iTerm" is not running',
\ 'error',
\ 'end if') && s:osascript(
\ 'tell application "iTerm"',
\ 'tell the current terminal',
\ 'set oldsession to the current session',
\ 'tell (make new session)',
\ 'set name to ' . s:escape(a:request.title),
\ 'set title to ' . s:escape(a:request.expanded),
\ 'exec command ' . s:escape(script),
\ a:request.background || !has('gui_running') ? 'select oldsession' : '',
\ 'end tell',
\ 'end tell',
\ a:activate ? 'activate' : '',
\ 'end tell')
endfunction
function! dispatch#iterm#spawn3(command, request, activate) abort
let script = dispatch#isolate(a:request, [],
\ dispatch#set_title(a:request), a:command)
return s:osascript(
\ 'if application "iTerm" is not running',
\ 'error',
\ 'end if') && s:osascript(
\ 'tell application "iTerm"',
\ 'tell the current window',
\ 'set oldtab to the current tab',
\ 'set newtab to (create tab with default profile command ' . s:escape(script) . ')',
\ 'tell current session of newtab',
\ 'set name to ' . s:escape(a:request.title),
\ 'set title to ' . s:escape(a:request.expanded),
\ 'end tell',
\ a:request.background || !has('gui_running') ? 'select oldtab' : '',
\ 'end tell',
\ a:activate ? 'activate' : '',
\ 'end tell')
endfunction
function! dispatch#iterm#activate(pid) abort
if dispatch#iterm#is_modern_version()
return dispatch#iterm#activate3(a:pid)
else
return dispatch#iterm#activate2(a:pid)
endif
endfunction
function! dispatch#iterm#activate2(pid) abort
let tty = matchstr(system('ps -p '.a:pid), 'tty\S\+')
if !empty(tty)
return s:osascript(
\ 'if application "iTerm" is not running',
\ 'error',
\ 'end if') && s:osascript(
\ 'tell application "iTerm"',
\ 'activate',
\ 'tell the current terminal',
\ 'select session id "/dev/'.tty.'"',
\ 'end tell',
\ 'end tell')
endif
endfunction
function! dispatch#iterm#activate3(pid) abort
let tty = matchstr(system('ps -p '.a:pid), 'tty\S\+')
if !empty(tty)
return s:osascript(
\ 'if application "iTerm" is not running',
\ 'error',
\ 'end if') && s:osascript(
\ 'tell application "iTerm"',
\ 'activate',
\ 'tell the current window',
\ 'repeat with atab in tabs',
\ 'repeat with asession in sessions',
\ 'if (tty) = ' . tty,
\ 'select atab',
\ 'end repeat',
\ 'end repeat',
\ 'end tell',
\ 'end tell')
endif
endfunction
function! s:osascript(...) abort
call system('osascript'.join(map(copy(a:000), '" -e ".shellescape(v:val)'), ''))
return !v:shell_error
endfunction
function! s:escape(string) abort
return '"'.escape(a:string, '"\').'"'
endfunction