-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathmacos.vim
294 lines (262 loc) · 9.64 KB
/
macos.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
"======================================================================
"
" macos.vim -
"
" Created by skywind on 2021/12/30
" Last Modified: 2021/12/30 15:52:58
"
"======================================================================
" vim: set ts=4 sw=4 tw=78 noet :
"----------------------------------------------------------------------
" script name
"----------------------------------------------------------------------
function! asyncrun#macos#script_name(name)
let tmpname = fnamemodify(tempname(), ':h') . '/' . a:name
return tmpname
endfunc
"----------------------------------------------------------------------
" write script
"----------------------------------------------------------------------
function! asyncrun#macos#script_write(name, content)
let tmpname = fnamemodify(tempname(), ':h') . '/' . a:name
call writefile(a:content, tmpname)
silent! call setfperm(tmpname, 'rwxrwxrws')
return tmpname
endfunc
"----------------------------------------------------------------------
" return pause script
"----------------------------------------------------------------------
function! asyncrun#macos#pause_script()
let lines = []
if executable('bash')
let pause = 'read -n1 -rsp "press any key to continue ..."'
let lines += ['bash -c ''' . pause . '''']
else
let lines += ['echo "press enter to continue ..."']
let lines += ['sh -c "read _tmp_"']
endif
return lines
endfunc
"----------------------------------------------------------------------
" write a scpt file
"----------------------------------------------------------------------
function! asyncrun#macos#osascript(content, wait)
let content = ['#! /usr/bin/osascript', '']
let content += a:content
let tmpname = asyncrun#macos#script_write('runner1.scpt', content)
let cmd = '/usr/bin/osascript ' . shellescape(tmpname)
call system(cmd . ((a:wait)? '' : ' &'))
endfunc
"----------------------------------------------------------------------
" open system terminal
"----------------------------------------------------------------------
function! asyncrun#macos#open_system(title, script, profile)
let content = ['#! /bin/sh']
let content = ['clear']
let content += [asyncrun#utils#set_title(a:title, 0)]
let content += a:script
let tmpname = asyncrun#macos#script_write('runner1.sh', content)
let cmd = 'open -a Terminal ' . shellescape(tmpname)
call system(cmd . ' &')
endfunc
"----------------------------------------------------------------------
" open terminal
"----------------------------------------------------------------------
function! asyncrun#macos#open_terminal(title, script, profile, active)
let content = ['#! /bin/sh']
let content += ['clear']
let content += [asyncrun#utils#set_title(a:title, 0)]
let content += a:script
let tmpname = asyncrun#macos#script_write('runner2.sh', content)
let osascript = []
let osascript += ['tell application "Terminal"']
let osascript += [' if it is running then']
let osascript += [' do script "' . tmpname . '; exit"']
let osascript += [' else']
let osascript += [' do script "' . tmpname . '; exit" in window 1']
let osascript += [' end if']
let x = ' set current settings of selected tab of '
let x = x . 'window 1 to settings set "' . a:profile . '"'
if a:profile != ''
let osascript += [x]
endif
if a:active
let osascript += [' activate']
endif
let osascript += ['end tell']
call asyncrun#macos#osascript(osascript, 1)
return 1
endfunc
"----------------------------------------------------------------------
" utils
"----------------------------------------------------------------------
function! s:osascript(...) abort
call system('osascript'.join(map(copy(a:000), '" -e ".shellescape(v:val)'), ''))
return !v:shell_error
endfunc
function! s:escape(string) abort
return '"'.escape(a:string, '"\').'"'
endfunc
function! s:iterm_new_version() abort
if !exists('s:iterm_is_new')
let s:iterm_is_new = asyncrun#macos#iterm_new_version()
endif
return s:iterm_is_new
endfunc
"----------------------------------------------------------------------
" check version
"----------------------------------------------------------------------
function! asyncrun#macos#iterm_new_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
"----------------------------------------------------------------------
" spawn2
"----------------------------------------------------------------------
function! asyncrun#macos#iterm_spawn2(script, opts, activate) abort
let script = asyncrun#utils#isolate(a:opts, [],
\ asyncrun#utils#set_title(a:opts.title, a:opts.expanded), a:script)
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:opts.title),
\ 'set title to ' . s:escape(a:opts.expanded),
\ 'exec command ' . s:escape(script),
\ a:opts.background ? 'select oldsession' : '',
\ 'end tell',
\ 'end tell',
\ a:activate ? 'activate' : '',
\ 'end tell')
endfunc
"----------------------------------------------------------------------
" spawn3
"----------------------------------------------------------------------
function! asyncrun#macos#iterm_spawn3(script, opts, activate) abort
let script = asyncrun#utils#isolate(a:opts, [],
\ asyncrun#utils#set_title(a:opts.title, a:opts.expanded), a:script)
" echom 'name: '. script
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:opts.title),
\ 'set title to ' . s:escape(a:opts.expanded),
\ 'end tell',
\ a:opts.background ? 'select oldtab' : '',
\ 'end tell',
\ a:activate ? 'activate' : '',
\ 'end tell')
endfunc
"----------------------------------------------------------------------
" spawn new iterm
"----------------------------------------------------------------------
function! asyncrun#macos#open_iterm(script, opts)
let opts = {}
let opts.title = get(a:opts, 'title', 'AsyncRun')
let opts.expanded = get(a:opts, 'expanded', 1)
let opts.background = get(a:opts, 'background', 0)
let opts.file = asyncrun#macos#script_name(expand('%:t'))
let active = get(a:opts, 'active', 1)
let script = deepcopy(a:script)
if s:iterm_new_version()
return asyncrun#macos#iterm_spawn3(script, opts, active)
else
return asyncrun#macos#iterm_spawn2(script, opts, active)
endif
endfunc
function! asyncrun#macos#iterm_activate(pid)
if s:iterm_new_version()
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
else
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
endif
endfunction
"----------------------------------------------------------------------
" start_command
"----------------------------------------------------------------------
function! asyncrun#macos#start_command(runner, opts)
let script = []
let script += ['cd ' . shellescape(getcwd())]
let script += [a:opts.cmd]
let op = {}
let op.title = a:opts.cmd
let op.active = get(a:opts, 'focus', 1)
let op.background = get(a:opts, 'focus', 1)? 0 : 1
if get(a:opts, 'close', 0) == 0
let script += ['echo ""']
let script += asyncrun#macos#pause_script()
endif
if a:runner == 'terminal'
let p = get(a:opts, 'option', '')
return asyncrun#macos#open_terminal(op.title, script, p, op.active)
elseif a:runner == 'iterm' || a:runner == 'iterm2'
return asyncrun#macos#open_iterm(script, op)
endif
endfunc
"----------------------------------------------------------------------
" check environ
"----------------------------------------------------------------------
function! asyncrun#macos#check()
if has('mac') || has('macunix') || has('osx') || has('osxdarwin')
return 1
elseif has('gui_macvim') || has('macvim')
return 1
endif
return 0
endfunc