Permalink
Browse files

consolidate R and terminal into primary and seconary application

  • Loading branch information...
1 parent 0293c7a commit 0d516cb3f77254c3122406f7257b9990d1d97837 @shingx1 shingx1 committed Nov 22, 2012
Showing with 49 additions and 34 deletions.
  1. +8 −7 Default (OSX).sublime-keymap
  2. +8 −7 README.md
  3. +32 −19 Rsublime.py
  4. +1 −1 Rsublime.sublime-settings
@@ -2,22 +2,23 @@
{ "keys": ["super+enter"], "command": "send_select", "context":
[
{ "key": "selector", "operator": "equal", "operand": "source.r" }
- ]
+ ],
+ "args": {"which": 0}
},
- { "keys": ["super+\\"], "command": "change_dir", "context":
+ { "keys": ["super+alt+enter"], "command": "send_select", "context":
[
{ "key": "selector", "operator": "equal", "operand": "source.r" }
- ]
+ ],
+ "args": {"which": 1}
},
- { "keys": ["super+."], "command": "source_code", "context":
+ { "keys": ["super+\\"], "command": "change_dir", "context":
[
{ "key": "selector", "operator": "equal", "operand": "source.r" }
]
},
- { "keys": ["super+alt+enter"], "command": "send_select", "context":
+ { "keys": ["super+."], "command": "source_code", "context":
[
{ "key": "selector", "operator": "equal", "operand": "source.r" }
- ],
- "args": {"Rapp": "Terminal"}
+ ]
}
]
View
@@ -21,27 +21,27 @@ The correct indentation rule now indents like
Useful Commands
---------------
-### Sending Code to R ###
+### Sending Code to primary application (default: R)###
**Keybinding:** `Cmd-Enter`
* Send the selection to R
* If no syntax is selected, it sends the whole line where the cursor stays at.
* See settings to choose default R application to send to.
-### Sending Code to Terminal ###
+### Sending Code to secondary application (default: Terminal)###
**Keybinding:** `Cmd-Alt-Enter`
* Send the selection to R session in terminal
* An R session needs to be running on terminal.
* If no syntax is selected, it sends the whole line where the cursor stays at.
* Useful for people working on server
-### Changing Working Directory ###
+### Changing Working Directory (Primary)###
**Keybinding:** `Cmd-\`
* Change working directory to where the current working script stays
-### Sourcing file ###
+### Sourcing file (Primary)###
**Keybinding:** `Cmd-.`
* Tell R to source the working script
@@ -51,8 +51,9 @@ Kepmaps are changable by editing `Default (OSX).sublime-keymap`.
Settings
---------
### Rapp ###
-**Default application:** `R64`
+**Default primary application:** `R64`
-* Type ``Rapp Switcher`` in Command Palette to switcher between `R`, `R64` and `Terminal`.<br>
-It maps `Cmd-Enter` to the corresponding application.
+**Default secondary application:** `Terminal`
+
+* Type ``Rapp Switcher`` in Command Palette to switch between `R`, `R64` and `Terminal`.<br>
* Or edit Rsublime.sublime-settings to change settings
View
@@ -11,17 +11,24 @@
class RCommon:
settings = sublime.load_settings('Rsublime.sublime-settings')
- Rapp = settings.get('Rapp')
+ 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, str):
- str = string.rstrip(str)
- str = string.replace(str, '\\', '\\\\')
- str = string.replace(str, '"', '\\"')
- return str
+ def clean(self, cmd):
+ cmd = string.strip(cmd)
+ cmd = string.replace(cmd, '\\', '\\\\')
+ cmd = string.replace(cmd, '"', '\\"')
+ return cmd
- def rcmd(self, cmd, Rapp=None):
+ def rcmd(self, cmd, which=0):
cmd = self.clean(cmd)
- if not Rapp: Rapp = self.Rapp
+ Rapp = self.Rapplist[which]
if re.match('R', Rapp):
args = ['osascript']
args.extend(['-e', 'tell app "' + Rapp + '" to cmd "' + cmd + '"'])
@@ -31,9 +38,11 @@ def rcmd(self, cmd, Rapp=None):
args.extend(['-e', 'tell app "Terminal" to do script "' + cmd + '" in front window\n'])
subprocess.Popen(args)
- def set_Rapp(self, Rapp):
- RCommon.Rapp = Rapp
- RCommon.settings.set("Rapp", RCommon.Rapp)
+ 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')
##################################
@@ -53,17 +62,14 @@ def run(self, edit):
#########################
class SendSelectCommand(sublime_plugin.TextCommand, RCommon):
- def run(self, edit, **kwargs):
+ 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'
- if kwargs.has_key('Rapp'):
- self.rcmd(cmd, kwargs['Rapp'])
- else:
- self.rcmd(cmd)
+ self.rcmd(cmd, which)
######################
#### Source codes ####
@@ -76,11 +82,18 @@ def run(self, edit):
self.rcmd(cmd)
# Rapp switcher
+
class RappSwitcher(sublime_plugin.WindowCommand, RCommon):
app_list = ["R64", "R", "Terminal"]
def run(self):
- self.window.show_quick_panel(self.app_list, self.on_done)
+ self.window.show_quick_panel(["Choose your primary Rapp"]+ self.app_list, self.on_done)
def on_done(self, action):
- if action>=0:
- self.set_Rapp(self.app_list[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()
@@ -1,3 +1,3 @@
{
- "Rapp" : "R64" // "R", "R32", "R64" or "Terminal"
+ "Rapp" : ["R64", "Terminal"] // "R", "R32", "R64" or "Terminal"
}

0 comments on commit 0d516cb

Please sign in to comment.