Hi, I just wrote a small Mac OS plugin that takes the selected code and sends it to R, Stata or iTerm. Any suggestions are welcome. SendSelectionCommand calls R or stata depending on the extension of the file and SendSelectionIterm always sends stuff to iterm (I am using this for cloud computing).
I will work with this for a while and put it on the plugin list when there are no bugs…
R, and iTerm work flawlessly. Stata is a little annoying because you first have to save the code in a file and then open the .do file with Stata. Doing this, however, changes the stata working directory. Currently, I call a second file which just runs “cd ‘$job’” to change to the working dir to the global $job. So if you define such a global, stata always changes back. Suggestions for alternative implementations are welcome! You might also have to change ‘StataMP’ to ‘StataSE’ depending on the version of stata you have.
Key bindings
{ "keys": "super+e"], "command": "send_selection" },
{ "keys": "super+shift+e"], "command": "send_selection_iterm" }
Send-Selection.py (copy to /Packages/User)
[code]import sublime, sublime_plugin
import os, subprocess, string
#https://sites.google.com/site/josefmontag/programs
to test strings:
#self.view.insert(edit, 0, cmd)
class SendSelectionCommand(sublime_plugin.TextCommand):
def run(self, edit):
# get selection
selection = “”
for region in self.view.sel():
selection+=self.view.substr(region) + “\n”
# only proceed if selection is not empty
if(selection!="\n"):
extension = os.path.splitext(self.view.file_name())[1]
# R file
if(extension.lower()==".r"):
# define osascript command
cmd = """osascript -e 'tell app "R" to activate' """
cmd+= """-e 'tell app "R" to cmd \"""" + string.replace(selection,"\"","\\\"") + """\"' """
# run and reactivate Sublime Text 2
os.system(cmd)
subprocess.Popen("""osascript -e 'tell app "Sublime Text 2" to activate' """, shell=True)
# Stata file
if(extension.lower()==".do"):
# define location of do file
file=sublime.packages_path() + "/User/sublime2stata.do"
file_restore=sublime.packages_path() + "/User/sublime2stata_restore.do"
# copy selection into file
os.system("echo '" + selection + "' > '" + file + "'")
os.system("""echo 'qui cd \"$job\"' > '""" + file_restore + "'")
# define osascript command and run
cmd_run = """osascript -e 'tell app "StataMP" to activate' """
cmd_run+= """-e 'tell app "StataMP" to open POSIX file \"""" + file + """\"' """
cmd_run+= """-e 'tell app "StataMP" to open POSIX file \"""" + file_restore + """\"' """
os.system(cmd_run)
os.system(“rm '” + file + “’”)
os.system(“rm '” + file_restore + “’”)
# restore stata working directory from global $job
subprocess.Popen("""osascript -e 'tell app "Sublime Text 2" to activate' """, shell=True)
class SendSelectionItermCommand(sublime_plugin.TextCommand):
def run(self, edit):
selection = “”
for region in self.view.sel():
selection+=self.view.substr(region) + “\n”
if(selection!="\n"):
# define location of shell script with selection as argument
script="sh '" + sublime.packages_path() + "/User/send-selection-iterm.sh' \"" + string.replace(selection,"\"","\\\"") + "\""
# run shell script and reactivate Sublime Text 2
os.system(script)
subprocess.Popen("""osascript -e 'tell app "Sublime Text 2" to activate' """, shell=True)[/code]
shell script for iTerm: send-selection-iterm.sh (copy to /Packages/User)
[code]#!/bin/sh
PASTE=$(echo “$@” | sed 's/////g’ | sed ‘s/"/\"/g’ | sed ‘s/ //g’ | sed -n '1h
1!{
# if the sought-after regex is not found, append the pattern space to hold space
//*.*// !H
# copy hold space into pattern space
g
# if the regex is found, then…
//*.*// {
# the regular expression
s//*.*// /g
# print
p
# read the next line into the pattern space
n
# copy the pattern space into the hold space
h
}
# copy pattern buffer into hold buffer
h
}
if the last line then print
$p
')
osascript << END
tell application “iTerm”
activate
tell current terminal
tell current session
write text “$PASTE”
end tell
end tell
end tell
#tell application “Sublime Text 2”
activate
#end tell
#END[/code]