This repository was archived by the owner on Mar 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtextsender.py
330 lines (300 loc) · 12.6 KB
/
textsender.py
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import sublime
import os
import re
import subprocess
import threading
from .settings import SettingManager
class TextSender:
cb = None
thread = None
def __init__(self, view, prog=None):
self.view = view
self.sget = SettingManager(view).get
plat = sublime.platform()
if not prog:
prog = self.sget("prog")
function_str = "_dispatch_" + prog.lower().replace("-", "_")
if getattr(self, function_str + "_" + plat, None):
function_str = function_str + "_" + plat
self._send_text = eval("self." + function_str)
def is_python(self):
pt = self.view.sel()[0].begin() if len(self.view.sel()) > 0 else 0
return self.view.score_selector(pt, "source.python") > 0
def wrap_paste_magic_for_python(self, cmd):
if self.is_python():
cmd = cmd.rstrip("\n")
if len(re.findall("\n", cmd)) > 0:
cmd = "%cpaste -q\n" + cmd + "\n--"
return cmd
def send_text(self, cmd):
self._send_text(cmd)
def clean_cmd(self, cmd):
cmd = cmd.expandtabs(4)
cmd = cmd.rstrip('\n')
if self.sget("remove_line_indentation", True) and len(re.findall("\n", cmd)) == 0:
cmd = cmd.lstrip()
return cmd
@staticmethod
def escape_dquote(cmd):
cmd = cmd.replace('\\', '\\\\')
cmd = cmd.replace('"', '\\"')
return cmd
@classmethod
def set_clipboard(cls, cmd):
if not cls.thread:
cls.cb = sublime.get_clipboard()
else:
cls.thread.cancel()
cls.thread = None
sublime.set_clipboard(cmd)
@classmethod
def reset_clipboard(cls):
def _reset_clipboard():
if cls.cb is not None:
sublime.set_clipboard(cls.cb)
cls.cb = None
cls.thread = None
cls.thread = threading.Timer(0.5, _reset_clipboard)
cls.thread.start()
def _dispatch_terminal(self, cmd):
cmd = self.wrap_paste_magic_for_python(cmd)
cmd = self.clean_cmd(cmd)
cmd = self.escape_dquote(cmd)
if self.sget("bracketed_paste_mode", False):
head = '(ASCII character 27) & "[200~'
tail = '" & (ASCII character 27) & "[201~"'
cmd = head + cmd + tail
else:
cmd = '"' + cmd + '"'
args = ['osascript']
args.extend(['-e',
'tell application "Terminal" to do script ' + cmd + ' in front window'])
subprocess.check_call(args)
@staticmethod
def iterm_version():
args = ['osascript', '-e', 'tell application "iTerm" to get version']
ver = subprocess.check_output(args).decode().strip()
return tuple((int(i) for i in re.split(r"\.", ver)[0:2]))
def _dispatch_iterm(self, cmd):
cmd = self.wrap_paste_magic_for_python(cmd)
cmd = self.clean_cmd(cmd)
bpm = self.sget("bracketed_paste_mode", False)
if bpm:
cmd = self.escape_dquote(cmd)
head = '(ASCII character 27) & "[200~'
tail = '" & (ASCII character 27) & "[201~"'
cmd = head + cmd + tail
subprocess.check_call([
'osascript', '-e',
'tell application "iTerm" to tell the current window ' +
'to tell current session to write text '
])
elif self.iterm_version() >= (2, 9):
n = 1000
chunks = [cmd[i:i+n] for i in range(0, len(cmd), n)]
for chunk in chunks:
subprocess.check_call([
'osascript', '-e',
'tell application "iTerm" to tell the current window ' +
'to tell current session to write text "' +
self.escape_dquote(chunk) + '" without newline'
])
subprocess.check_call([
'osascript', '-e',
'tell application "iTerm" to tell the current window ' +
'to tell current session to write text ""'
])
else:
subprocess.check_call([
'osascript', '-e',
'tell application "iTerm" to tell the current terminal ' +
'to tell current session to write text "' +
self.escape_dquote(cmd) + '"'
])
def _dispatch_r_osx(self, cmd):
cmd = self.clean_cmd(cmd)
cmd = self.escape_dquote(cmd)
args = ['osascript']
args.extend(['-e', 'tell application "R" to cmd "' + cmd + '"'])
subprocess.check_call(args)
def _dispatch_rstudio_osx(self, cmd):
cmd = self.clean_cmd(cmd)
script = """
on run argv
tell application "RStudio"
cmd item 1 of argv
end tell
end run
"""
subprocess.check_call(['osascript', '-e', script, cmd])
def _dispatch_chrome_rstudio(self, cmd):
cmd = self.clean_cmd(cmd)
cmd = self.escape_dquote(cmd)
cmd = cmd.replace("\n", r"\n")
script = """
on run argv
tell application "Google Chrome"
set URL of front window's active tab to "javascript:{" & "
var input = document.getElementById('rstudio_console_input');
var textarea = input.getElementsByTagName('textarea')[0];
textarea.value += \\"" & item 1 of argv & "\\";
var e = document.createEvent('KeyboardEvent');
e.initKeyboardEvent('input');
textarea.dispatchEvent(e);
var e = document.createEvent('KeyboardEvent');
e.initKeyboardEvent('keydown');
Object.defineProperty(e, 'keyCode', {'value' : 13});
input.dispatchEvent(e);
" & "}"
end tell
end run
"""
subprocess.check_call(['osascript', '-e', script, cmd])
def _dispatch_safari_rstudio(self, cmd):
cmd = self.clean_cmd(cmd)
cmd = self.escape_dquote(cmd)
cmd = cmd.replace("\n", r"\n")
script = """
on run argv
tell application "Safari"
tell front window's current tab to do JavaScript "
var input = document.getElementById('rstudio_console_input');
var textarea = input.getElementsByTagName('textarea')[0];
textarea.value += \\"" & item 1 of argv & "\\";
var e = document.createEvent('KeyboardEvent');
e.initKeyboardEvent('input');
textarea.dispatchEvent(e);
var e = document.createEvent('KeyboardEvent');
e.initKeyboardEvent('keydown');
Object.defineProperty(e, 'keyCode', {'value' : 13});
input.dispatchEvent(e);
"
end tell
end run
"""
subprocess.check_call(['osascript', '-e', script, cmd])
def _dispatch_chrome_jupyter(self, cmd):
cmd = self.clean_cmd(cmd)
cmd = self.escape_dquote(cmd)
cmd = cmd.replace("\n", r"\n")
script = """
on run argv
tell application "Google Chrome"
set URL of front window's active tab to "javascript:{" & "
var mycell = IPython.notebook.get_selected_cell();
mycell.set_text(\\"" & item 1 of argv & "\\");
mycell.execute();
var nextcell = IPython.notebook.insert_cell_below();
IPython.notebook.select_next();
IPython.notebook.scroll_to_cell(IPython.notebook.find_cell_index(nextcell));
" & "}"
end tell
end run
"""
subprocess.check_call(['osascript', '-e', script, cmd])
def _dispatch_safari_jupyter(self, cmd):
cmd = self.clean_cmd(cmd)
cmd = self.escape_dquote(cmd)
cmd = cmd.replace("\n", r"\n")
script = """
on run argv
tell application "Safari"
tell front window's current tab to do JavaScript "
var mycell = IPython.notebook.get_selected_cell();
mycell.set_text(\\"" & item 1 of argv & "\\");
mycell.execute();
var nextcell = IPython.notebook.insert_cell_below();
IPython.notebook.select_next();
IPython.notebook.scroll_to_cell(IPython.notebook.find_cell_index(nextcell));
"
end tell
end run
"""
subprocess.check_call(['osascript', '-e', script, cmd])
def _dispatch_tmux(self, cmd):
cmd = self.wrap_paste_magic_for_python(cmd)
tmux = self.sget("tmux", "tmux")
cmd = self.clean_cmd(cmd) + "\n"
n = 200
chunks = [cmd[i:i+n] for i in range(0, len(cmd), n)]
for chunk in chunks:
subprocess.check_call([tmux, 'set-buffer', chunk])
subprocess.check_call([tmux, 'paste-buffer', '-d'])
def _dispatch_screen(self, cmd):
cmd = self.wrap_paste_magic_for_python(cmd)
screen = self.sget("screen", "screen")
plat = sublime.platform()
cmd = self.clean_cmd(cmd) + "\n"
n = 200
chunks = [cmd[i:i+n] for i in range(0, len(cmd), n)]
for chunk in chunks:
if plat == "linux":
chunk = chunk.replace("\\", r"\\")
chunk = chunk.replace("$", r"\$")
subprocess.check_call([screen, '-X', 'stuff', chunk])
def _dispatch_gnome_terminal(self, cmd):
cmd = self.wrap_paste_magic_for_python(cmd)
sid = subprocess.check_output(["xdotool", "getactivewindow"]).decode("utf-8").strip()
wid = subprocess.check_output(["xdotool", "search", "--onlyvisible",
"--class", "gnome-terminal"])
if wid:
wid = wid.decode("utf-8").strip().split("\n")[-1]
cmd = self.clean_cmd(cmd) + "\n"
self.set_clipboard(cmd)
subprocess.check_call(["xdotool", "windowfocus", wid])
subprocess.check_call(["xdotool", "key", "--clearmodifiers", "ctrl+shift+v"])
subprocess.check_call(["xdotool", "windowfocus", sid])
self.reset_clipboard()
def _dispatch_rstudio_linux(self, cmd):
wid = subprocess.check_output(["xdotool", "search", "--onlyvisible", "--class", "rstudio"])
if wid:
wid = wid.decode("utf-8").strip().split("\n")[-1]
cmd = self.clean_cmd(cmd)
self.set_clipboard(cmd)
subprocess.check_call(["xdotool", "key", "--window", wid,
"--clearmodifiers", "ctrl+v"])
subprocess.check_call(["xdotool", "key", "--window", wid,
"--clearmodifiers", "Return"])
self.reset_clipboard()
@staticmethod
def execute_ahk_script(script, args=[]):
ahk_path = os.path.join(sublime.packages_path(),
'User', 'SendTextPlus', 'bin', 'AutoHotkeyU32')
ahk_script_path = os.path.join(sublime.packages_path(),
'User', 'SendTextPlus', 'bin', script)
subprocess.check_call([ahk_path, ahk_script_path] + args)
def _dispatch_cygwin(self, cmd):
cmd = self.wrap_paste_magic_for_python(cmd)
cmd = self.clean_cmd(cmd) + "\n"
self.set_clipboard(cmd)
self.execute_ahk_script("Cygwin.ahk")
self.reset_clipboard()
def _dispatch_cmder(self, cmd):
cmd = self.wrap_paste_magic_for_python(cmd)
cmd = self.clean_cmd(cmd) + "\n"
self.set_clipboard(cmd)
self.execute_ahk_script("Cmder.ahk")
self.reset_clipboard()
def _dispatch_r32_windows(self, cmd):
cmd = self.clean_cmd(cmd) + "\n"
self.set_clipboard(cmd)
self.execute_ahk_script("Rgui.ahk", [self.sget("R32", "0")])
self.reset_clipboard()
def _dispatch_r64_windows(self, cmd):
cmd = self.clean_cmd(cmd) + "\n"
self.set_clipboard(cmd)
self.execute_ahk_script("Rgui.ahk", [self.sget("R64", "1")])
self.reset_clipboard()
def _dispatch_rstudio_windows(self, cmd):
cmd = self.clean_cmd(cmd)
if cmd:
self.set_clipboard(cmd)
self.execute_ahk_script("RStudio.ahk")
self.reset_clipboard()
def _dispatch_sublimerepl(self, cmd):
cmd = self.clean_cmd(cmd)
window = sublime.active_window()
view = window.active_view()
external_id = view.scope_name(0).split(" ")[0].split(".", 1)[1]
window.run_command(
"repl_send", {"external_id": external_id, "text": cmd})