andrewheiss / SublimeStataEnhanced

Plugin that adds support for Stata 11–15 for Sublime Text 2 and 3
56 stars 22 forks source link

The sublime2stata.do file is executed by opening a new Stata instance rather than the existing instance #19

Open 2sls opened 10 years ago

2sls commented 10 years ago

I would like the executed .do file to be within an existing Stata instance, rather than a new instance. I don't know if Stata has some setting that will default to an existing instance rather than a new instance when executing a do file.

I am also curious how things are handled when multiple Stata instances are open.

aeonstone commented 10 years ago

I just found out about this plugin and I must say great work. I am having this same issue. I would love to switch over so that I am doing everything in Sublime Text but this issue is holding me back. Just thought I would chime in. Great work. Cheers. ... ST3, Stata 12.1.

jules-belanger commented 10 years ago

I second aeonstone. Great stuff but this one problem should be fixed.

andrewheiss commented 10 years ago

I completely agree that this is a serious issue, but I don't use Windows (nor do I have a Stata license for Windows), so I can't debug this directly.

I think the solution is that the temporary .vbs file that text_2_stata.py generates needs to be enhanced—instead of using CreateObject("WScript.Shell").Run to programmatically open Stata, it needs to use some sort of WMI call (like this, maybe).

But I have no experience with using VB like this, so I'm not sure how to proceed. And there's probably a better way to do it too.

emilBeBri commented 10 years ago

shame - i've been looking for a solution for this as well!

tonijung commented 10 years ago

One roadblock has to do with the 2 ways Stata treats the .do files: edit (the default option) or run (the setting this plugin asks users to change to). We clearly don't want Stata to edit the .do files.. after all that's the point of using ST. Since we're asking Stata to automatically run any .do file that is opened, the computer will automatically open a new instance of Stata to run that .do file.

Notice that due to this change in the default way that Stata deals with .do files, in line 120 of the text_2_stata.py script, the {0} is unnecessary since Windows now automatically opens by running it in a new instance of Stata. Kind of like how new computers by default open .mp3 files on Windows Media Player rather than iTunes.

The strange part is that the same change is made on OS X and it works fine on Macs. I agree with @andrewheiss that there's probably a better way to do this. I ran across the same article that he linked to and was able to get the script to recognize whether or not there was an instance of Stata already opened, but wasn't able to get the .do file to run on the same instance. That's why I believe how Stata treats .do files may be an issue.

Should have commented on this earlier, but was preoccupied with other projects.

bauchter-work commented 9 years ago

I've just come across this same issue, having tried SublimeText 3.0 (beta) and Windows StataMP 13.

If you're interested in an editor that doesn't close Stata after running and re-runs do files within the original, single Stata window, I know that the gVim, Cream package will do this after doing some modifications: http://cream.sourceforge.net/home.html First, you need to get and install the rundo and rundolines from the following URL: http://huebler.blogspot.com/2008/04/stata.html In the example below, I've placed the rundo files in a ~/.cream/stata/ folder and edited the rundo ini file to point to the correct location of StataMP for my computer.

Pressing F8 -- or -- Selecting Text and pressing F9 runs .do file contents direct into Stata.

Cream/VIM comes stock with stata syntax, but you do need to edit your ~/.cream/cream-user.vim to include the following:

" STATA DO-FILE SCRIPTS

fun! RunIt()
wa
" *** CHANGE PATH AND NAME TO REFLECT YOUR SETUP ***
!start "C:\Users\<MY_USER_FOLDER>\.cream\stata\rundo. exe" "%:p"
endfun

:map <F8> :<C-U>call RunIt() 
:imap <F8> <Esc>:<C-U>call RunIt() 

fun! RunDoLines()
let selectedLines = getbufline('%', line("'<"), line("'>"))

if col("'>") < strlen(getline(line("'>")))
let selectedLines[-1] = strpart(selectedLines[-1], 0, col("'>"))
endif
if col("'<") != 1
let selectedLines[0] = strpart(selectedLines[0], col("'<")-1)
endif

let temp = tempname() . ".do"
call writefile(selectedLines, temp)

" *** CHANGE PATH AND NAME TO REFLECT YOUR SETUP. USE \\ INSTEAD OF \ ***
exec "!start C:\\Users\\<MY_USER_FOLDER>\\.cream\\stata\\rundo. exe " . temp

" Delete the temp file after Vim closes
au VimLeave * exe "!del -y" temp
endfun

:map <F9> :<C-U>call RunDoLines() 
:imap <F9> <Esc>:<C-U>call RunDoLines()

The question then may be -- given the way vim does this -- is there a way to make a macro in SublimeText do the same thing? I'm fairly limited in my exposure to SublimeText capabilities as I just started using it.

justfortherec commented 8 years ago

I think the solution is that the temporary .vbs file that text_2_stata.py generates needs to be enhanced—instead of using CreateObject("WScript.Shell").Run to programmatically open Stata, it needs to use some sort of WMI call (like this, maybe).

Maybe a (temporary) workaround would be to make use of the rundolines.exe which @bauchter-work linked:

First, you need to get and install the rundo and rundolines from the following URL: http://huebler.blogspot.com/2008/04/stata.html

From what I read on the page, it solves the issue described here (sorry, I can't try myself since I'm not on a Windows PC atm).

Gkreindler commented 8 years ago

Just adding my 2c that without this feature this otherwise awesome package is not useful. I am a Windows user and the scripting nature of Stata means that it's essential to send code to the same instance. I am sorry, but I have no expertise to contribute to solving this issue.

andrewheiss commented 8 years ago

Does the StataEditor package keep everything in the same instance?

Yuji-Shimohira-Calvo commented 8 years ago

Hi @andrewheiss. Yes, StataEditor keeps everything in the same instance of Stata. However, in addition to StataEditor you also need to install Pywin32 and register Stata Automation (I believe this was in your "road map"). Thanks for all the effort you have put into this so far.

ywhcuhk commented 7 years ago

not sure why this issue was closed. It was not resolved.

dbraudt commented 7 years ago

I'm joining this way late, but as a Linux and Windows user only, an update to change this would be extremely useful.... Has there been any progress on this? ... @andrewheiss, if you talk to Joe Olsen, director of the the FHSS Research Support Center (I believe his office is still in the Kimble Tower), he might be able to through some resources at this. Just an option that may be close at hand for you.

andrewheiss commented 6 years ago

Sadly, there hasn't been any progress on it. I don't use Windows, so I can't debug this easily (and what's worse, I don't have an active Stata license anymore, so I definitely can't debug much right now)