forked from raymondkww/R-sublime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rsublime.py
112 lines (95 loc) · 4.05 KB
/
Rsublime.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
import sublime
import sublime_plugin
import os
import subprocess
import re
# A Common Class
class RCommon:
Rapplist = None
def clean(self, cmd):
cmd = cmd.strip()
cmd = cmd.replace('\\', '\\\\')
cmd = cmd.replace('"', '\\"')
return cmd
def rcmd(self, cmd, which=0):
if RCommon.Rapplist == None: self.get_Rapp()
cmd = self.clean(cmd)
Rapp = RCommon.Rapplist[which]
if re.match('R', Rapp):
args = ['osascript']
args.extend(['-e', 'tell app "' + Rapp + '" to cmd "' + cmd + '"'])
subprocess.Popen(args)
elif Rapp == 'Terminal':
args = ['osascript']
args.extend(['-e', 'tell app "Terminal" to do script "' + cmd + '" in front window\n'])
subprocess.Popen(args)
elif Rapp == 'iTerm':
args = ['osascript']
apple_script = ('tell application "' + Rapp + '"\n'
'tell the first terminal\n'
'tell current session\n'
'write text "' + cmd + '"\n'
'end tell\n'
'end tell\n'
'end tell\n')
args.extend(['-e', apple_script])
subprocess.Popen(args)
def set_Rapp(self, which, Rapp):
if RCommon.Rapplist == None: self.get_Rapp()
RCommon.Rapplist[which] = Rapp
def get_Rapp(self):
settings = sublime.load_settings('Rsublime.sublime-settings')
RCommon.Rapplist = settings.get('Rapp')
if RCommon.Rapplist == None:
RCommon.Rapplist = ["R", "Terminal"]
def save_settings(self):
settings = sublime.load_settings('Rsublime.sublime-settings')
settings.set("Rapp", RCommon.Rapplist)
sublime.save_settings('Rsublime.sublime-settings')
class ChangeDirCommand(sublime_plugin.TextCommand, RCommon):
def run(self, edit):
path = os.path.dirname(self.view.file_name())
cmd = "setwd(\"" + self.clean(path) + "\")"
self.rcmd(cmd)
class SendSelectCommand(sublime_plugin.TextCommand, RCommon):
def expand_sel(self, sel):
esel = self.view.find(r"""^.*(\{(?:(["\'])(?:[^\\\\]|\\\\.|\n)*?\\2|#.*$|[^\{\}]|\n|(?1))*\})"""
, self.view.line(sel).begin())
if self.view.line(sel).begin() == esel.begin():
return esel
def run(self, edit, which):
cmd = ''
for sel in self.view.sel():
if sel.empty():
thiscmd = self.view.substr(self.view.line(sel))
if re.match(r".*\{\s*$", thiscmd):
esel = self.expand_sel(sel)
if esel:
thiscmd = self.view.substr(esel)
else:
thiscmd = self.view.substr(sel)
if self.view.score_selector(sel.end()-1, "meta.function.r") and \
not self.view.score_selector(sel.end(), "meta.function.r"):
esel = self.expand_sel(sel)
if esel:
thiscmd = self.view.substr(esel)
cmd += thiscmd +'\n'
self.rcmd(cmd, which)
class SourceCodeCommand(sublime_plugin.TextCommand, RCommon):
def run(self, edit):
path = self.view.file_name()
cmd = "source(\"" + self.clean(path) + "\")"
self.rcmd(cmd)
class RappSwitcher(sublime_plugin.WindowCommand, RCommon):
app_list = ["R", "R64", "Terminal", "iTerm"]
msg = ["Choose your Primary Rapp", "Choose your Secondary Rapp"]
pop_string = ["R", "R64 (for R 2.x.x)", "Terminal", "iTerm 2"]
def show_quick_panel(self, options, done):
sublime.set_timeout(lambda: self.window.show_quick_panel(options, done), 10)
def run(self, which):
self.which = which
self.show_quick_panel([self.msg[which]]+ self.pop_string, self.on_done)
def on_done(self, action):
if action>=1:
self.set_Rapp(self.which, self.app_list[action-1])
self.save_settings()