forked from raymondkww/R-sublime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rsublime.py
99 lines (83 loc) · 3.09 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
import sublime
import sublime_plugin
import os
import subprocess
import string
import re
########################
#### R Common CLass ####
########################
class RCommon:
settings = sublime.load_settings('Rsublime.sublime-settings')
if settings.has('Rapp'):
Rapplist = settings.get('Rapp')
if isinstance(Rapplist, unicode):
Rapplist = [Rapplist, "Terminal"]
if not Rapplist:
Rapplist = ["R64", "Terminal"]
if Rapplist != settings.get('Rapp'):
sublime.save_settings('Rsublime.sublime-settings')
def clean(self, cmd):
cmd = string.strip(cmd)
cmd = string.replace(cmd, '\\', '\\\\')
cmd = string.replace(cmd, '"', '\\"')
return cmd
def rcmd(self, cmd, which=0):
cmd = self.clean(cmd)
Rapp = self.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)
def set_Rapp(self, which, Rapp):
RCommon.Rapplist[which] = Rapp
def save_settings(self):
self.settings.set("Rapp", RCommon.Rapplist)
sublime.save_settings('Rsublime.sublime-settings')
##################################
#### change working directory ####
##################################
class ChangeDirCommand(sublime_plugin.TextCommand, RCommon):
# read settings only when the instance is created
# it is more efficient then reading settings everytime when "run" is executed
def run(self, edit):
path = os.path.dirname(self.view.file_name())
cmd = "setwd(\"" + self.clean(path) + "\")"
self.rcmd(cmd)
#########################
#### Send codes to R ####
#########################
class SendSelectCommand(sublime_plugin.TextCommand, RCommon):
def run(self, edit, which):
cmd = ''
for sel in self.view.sel():
if sel.empty():
cmd += self.view.substr(self.view.line(sel)) +'\n'
else:
cmd += self.view.substr(sel) +'\n'
self.rcmd(cmd, which)
######################
#### Source codes ####
######################
class SourceCodeCommand(sublime_plugin.TextCommand, RCommon):
def run(self, edit):
path = self.view.file_name()
cmd = "source(\"" + self.clean(path) + "\")"
self.rcmd(cmd)
# Rapp switcher
class RappSwitcher(sublime_plugin.WindowCommand, RCommon):
app_list = ["R64", "R", "Terminal"]
def run(self):
self.window.show_quick_panel(["Choose your primary Rapp"]+ self.app_list, self.on_done)
def on_done(self, action):
if action>=1:
self.set_Rapp(0, self.app_list[action-1])
self.window.show_quick_panel(["Choose your secondary Rapp"]+ self.app_list, self.on_done2)
def on_done2(self, action):
if action>=1:
self.set_Rapp(1, self.app_list[action-1])
self.save_settings()