Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Integrating Stata and external text editors


From   "Jeffrey Arnold" <[email protected]>
To   Statalist <[email protected]>
Subject   st: Integrating Stata and external text editors
Date   Wed, 30 Aug 2006 01:21:40 -0400

Dear all,

I have made a few modifcations to the Friedrich Huebler scripts for
integrating Stata and external text editors
(http://huebler.info/2005/20050310_Stata_editor.html ), and I thought
I might share them with everybody.

My version has two major differences from the rundo.au3 and
rundolines.au3 scripts.
Instead of hardcoding the "do " statement, the script will pass all
the the arguments it receives (via the $CmdLineRaw variable)  to the
Stata command line.

For example, the syntax used to call the program is:

Send2Stata.exe display "Hello, world!"
Send2Stata.exe do foo.do
Send2Stata.exe run foo.do

Secondly, the location of the Stata executable and Stata window title
are specified in an .ini file (which must be in the same directory as
the script).  This means that the code in the script should not need
to be altered, although it still needs to be compiled from the source.
Some other parameters are also specified in the ini file to control
the behavior of the script.

Some other differences are:
   * If the browser or data editor windows are open when the script
is called, the script will close them.  Stata will still require a yes
or no answer if data has been changed before the script can continue.
    * If there is no open Stata window, the script will pass the
command to Stata via the command line, rather than opening Stata and
then pasting the command in the Stata command line window.
   * By default, it returns focus to the text editor after running
things in Stata. This mimics the behavior of the do-editor.

Jeff Arnold

Below are the sources for Send2Stata.au3 and Send2Stata.ini.

Send2Stata.au3
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#comments-start
; ----------------------------------------------------------------------------
AutoIt Version: 3.1.0
Script:           Send2Stata.au3
Author:         Jeffrey Arnold (jeffrey DOT arnold AT gmail DOT com)

Script Function:
   Execute text in a currently opened Stata gui session.

Usage:

Before using you will need to compile this script into an exe using
AutoIt. See http://www.autoitscript.com/autoit3/docs/intro/compiler.htm.

Edit Send2Stata.ini to include the correct path to your Stata
executable and the title of you Stata gui window.

You call this script from the command line with all the arguments
being passed to
Stata as a command. For example:
     Send2Stata.exe display "Hello, world!"
     Send2Stata.exe do foo.do

To use Send2Stata with a text editor you will need to call the script
from your text editor
of choice.  This script does not work with text editors which would require the
path to be extracted from the title of the editor window.

This is an example of a function to call Send2Stata from gVim.

   let s:StSend2stata = "C:/program\ files/vim/ftplugin/stata/Send2Stata.exe"
   let b:tempfn = tempname()

   function! Send2Stata(args)
     exe 'silent !start '.s:StSend2Stata.' '.a:args
   endfuntion

   " Do the entire do-file, or a selection of lines
   com! -buffer -range=% -nargs=0 StDo
     \ exe "<line1>,<line2>w! ".b:tmpfn  |
     \ call Send2Stata('do "'.b:tmpfn.'"')

Credits:
This script is based off of Friedrich Huebler's  rundo.au3 and
rundolines.au3 scripts posted
at http://huebler.info/2005/20050310_Stata_editor.html. It also builds
off of earlier work by Dimitriy Masterov for integrating Stata with
Vim http://www.stata.com/statalist/archive/2004-06/msg00699.html .
Ben Collerson's external.exe gave me the idea to use an ini file
(http://bur.st/~benc/?p=external).
; ----------------------------------------------------------------------------
#comments-end

;Location of the init file
$init = @ScriptDir & "\Send2Stata.ini"

;Stata Paths and Window Text
$statawin =  IniRead($init,"Stata","StataWin","Intercooled Stata 9")
$statapath = IniRead($init,"Stata","StataExe","C:\program
files\stata9\wstata.exe")

;Behavior
$shortWait = IniRead($init,"Behaviour","ShortWait",10)
$focus =  IniRead($init,"Behavior","ReturnFocus",0)
$stataloadup =  IniRead($init,"Behavior","StataLoadWait",500)
Opt("SendKeyDelay", 1)
Opt("WinWaitDelay", $shortWait)

; Parsing arguments
$command = $CmdLineRaw

; Get the handle of the currently active window, i.e. gvim
; this will be used to return focus later
Opt("WinTitleMatchMode",4)
$handle = WinGetHandle("active","")

; If more than one Stata window is open, the window
; that was most recently active will be matched
Opt("WinTitleMatchMode",2)

; Check if Stata is already open, run it if not
If WinExists($statawin) Then
   ; Closing Data browser and editor before sending stuff
   If WinExists("Data Browser") Then
       WinClose("Data Browser")
       WinWaitClose("Data Browser")
   ElseIf WinExists("Data Editor") Then
       WinClose("Data Editor")
       WinWaitClose("Data Editor")
   EndIf
   WinActivate($statawin)
   WinWaitActive($statawin)
   ; Activate Stata Command Window and select text (if any)
   Send("^4")
   ; Ctl-A was giving me problems so I used ESC instead
   Send("{ESC}")
   ; Run saved do-file
   ; Putting it in clipboard and copying is faster than sending key strokes
   ClipPut($command)
   Send("^v{Enter}")
Else
   ; if no window open
   If FileExists($statapath) Then
       ; open stata from the command line
       Run($statapath & ' ' & $command)
   Else
       MsgBox(0+16,"", $statapath & " not found")
   EndIf
EndIf

; Return focus to text editor
If $focus Then
   ; will not return focus to gvim unless paused to let Stata load
   Sleep($stataloadup)
   WinActivate($handle)
EndIf


Example Send2Stata.ini file
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;------------------------------------------------------------------------------
; Send2Stata.exe configuration file.
; Copyleft 2006 Jeffrey Arnold
;------------------------------------------------------------------------------

[Stata]
;------------------------------------------------------------------------------
; Location of Stata Executable
;------------------------------------------------------------------------------
StataExe=c:\Program Files\stata9\wsestata.exe

;------------------------------------------------------------------------------
; Title of Stata Window
;------------------------------------------------------------------------------
StataWin=Stata/SE 9.2

[Behavior]
;------------------------------------------------------------------------------
; ReturnFocus
;------------------------------------------------------------------------------
; Returns focus back to editor after executing text, set to 0 to keep focus on
; Stata after sending a command to the Stata window.
;------------------------------------------------------------------------------
; Default = 1
;------------------------------------------------------------------------------
;ReturnFocus = 0

;------------------------------------------------------------------------------
; ShortWait
;------------------------------------------------------------------------------
; Increase this value if some applications do not copy or paste text
; quickly enough and the editor starts up with no/wrong text.
;------------------------------------------------------------------------------
; Default = 10
;------------------------------------------------------------------------------
;ShortWait=10


;------------------------------------------------------------------------------
; StataLoadWait
;------------------------------------------------------------------------------
; How long to wait for Stata to Load before trying to return focus to editor
;------------------------------------------------------------------------------
; Default = 500
;------------------------------------------------------------------------------
;StataLoadWait
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index