sending code from vim to stata

2020-07-09 03:06发布

问题:

I have been using Vim to write Stata scripts in Windows for a while now at the university. I am learning R at the moment, and I want to switch completely to Linux as my OS (I've recently switched to Ubuntu on my laptop). R works fine with Vim in both Windows and Linux, however I still need to use Stata sometimes. In Windows I have been using a simple AutoIt script provided by a Stata user to send lines / the whole file to stata for evaluation. This script doesnt work in Linux.

This is what the script looks like

; AutoIt v3 script to run a Stata do-file from an external text editor
; Version 3.1, Friedrich Huebler, fhuebler@gmail.com, www.huebler.info, 30 March 2009

; Declare variables
Global $ini, $statapath, $statawin, $dofile, $winpause, $keypause, $clippause

; File locations
; Path to INI file
$ini = @ScriptDir & "\rundo.ini"
; Path to Stata executable
$statapath = IniRead($ini, "Stata", "statapath", "C:\Program Files\Stata10\wsestata.exe")
; Title of Stata window
$statawin = IniRead($ini, "Stata", "statawin", "Stata/SE 10.1")

; Path to do-file that is passed to AutoIt
; Edit line to match editor used, if necessary
$dofile = $CmdLine[1]

; Delays
; Pause after copying of Stata commands to clipboard
$clippause = IniRead($ini, "Delays", "clippause", "100")
; Pause between window-related operations
$winpause = IniRead($ini, "Delays", "winpause", "200")
; Pause between keystrokes sent to Stata
$keypause = IniRead($ini, "Delays", "keypause", "1")

; Set SendKeyDelay and WinWaitDelay to speed up or slow down script
Opt("WinWaitDelay", $winpause)
Opt("SendKeyDelay", $keypause)

; 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, start Stata if not
If WinExists($statawin) Then
  WinActivate($statawin)
  WinWaitActive($statawin)
  ; Activate Stata Command Window and select text (if any)
  Send("^4")
  Send("^a")
  ; Run saved do-file
  ; Double quotes around $dofile needed in case path contains blanks
  ClipPut("do " & '"' & $dofile & '"')
  ; Pause avoids problem with clipboard, may be AutoIt or Windows bug
  Sleep($clippause)
  Send("^v" & "{Enter}")
Else
  Run($statapath)
  WinWaitActive($statawin)
  ; Activate Stata Command Window
  Send("^4")
  ; Run saved do-file
  ; Double quotes around $dofile needed in case path contains blanks
  ClipPut("do " & '"' & $dofile & '"')
  ; Pause avoids problem with clipboard, may be AutoIt or Windows bug
  Sleep($clippause)
  Send("^v" & "{Enter}")
EndIf

; End of script

with the following in my vimrc

" STATA DO-FILE SCRIPTS

fun! RunIt()
  w
  !start "C:\Programme\Stata10\integvim\rundo3\rundo.exe" "%:p"
endfun
map <F8> :<C-U>call RunIt()<CR><CR>
imap <F8> <Esc>:<C-U>call RunIt()<CR><CR>

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)
    exec "!start C:\\Programme\\Stata10\\integvim\\rundo3\\rundo.exe " . temp
    au VimLeave * exe "!del -y" temp
endfun
map <F9> :<C-U>call RunDoLines()<CR><CR> 
imap <F9> <Esc>:<C-U>call RunDoLines()<CR><CR> 

This is really practical and virtually the only reason I'm still sticking to Windows. How would I go about getting something like that for Ubuntu? I'm new to linux, and dont really know much about programming besides statistics. Any help is greatly appreciated. (Please dont suggest emacs, emacs support for stata is faulty, and though its integration with R is much better, I would like to keep using Vim for now.)

On a possibly related topic: I'm considering learning Python, as I'll probably be working with data and doing empirical analysis for a longer time, and I think it might be useful for some tasks, e.g. to solve problems like this or parsing data from websites. Is this recommended, or should I look at another language (or forget the idea completely)?

回答1:

If you're going to switch to linux (for good) you should 1. call stata and switch your license to a linux license (which they'll do for free) and install it natively, then starting stata from vim just requires a bash script (i'm no expert on vim) 2. or, you could buy stata 11, which has one license for all supported platforms 3. you could install stata with wine (play on linux made it easier for me) but I couldn't get wine to give stata more than half a gig of ram, so i installed native stata10

I use gedit for python, stata, R, latex, et cetera, it handles anything, and gtksourceview is pretty easy to add syntax highlighting and styles et cetera..

Python is great for data analysis, please see

http://www.scipy.org/ http://openopt.org/Welcome http://www.macworld.com/appguide/app.html?id=63924&expand=false

For scraping websites theres

http://scrapy.org/ http://wwwsearch.sourceforge.net/mechanize/

and for parsing data out of text generally we have, http://pyparsing.wikispaces.com/

I have a bunch of files you might find useful (gtksoureview language definitions for stata and others, pdf copies of some books (some copyleft)) but i don't know how to attach files on this website



回答2:

in linux you can use a plugin for vim called slime.vim which will allow you to pipe code from vim to a running R process without leaving vim. I use this on the mac for mysql, php, python and ocaml and it works great. The only caveat is that it uses screen to do its magic which is not a bad thing in itself.

If I understand your question correctly I believe this will help you enormously. Good luck and hope that helps you.



回答3:

[Edit]: Oops, I noticed that your main question relates to the AutoIt script, sorry I don't know how to translate this to Linux. I don't think there is any easy way. You might want to look into into xautomation or other similar tools, but that's a totally different ball game.

In your .vimrc replace:

!start "C:\Programme\Stata10\integvim\rundo3\rundo.exe" "%:p"

With the command that does the same thing in linux. It should look similar to this:

!/usr/share/... "%:p"

As I don't have a clue about Stata, you'll have to find out the exact command from someone else.

Also I'm not sure about the "%:p": please look that up in the vim help and compare the differences between Windows and Linux.



回答4:

I rarely use stata anymore, but at some point worked out a quick solution in bash. this script, executed with a few lines in my .vimrc, works fine. you might have to adjust the delays depending on your system.

#!/bin/bash

# needs wmctrl, xte and xsel
# to get them run
# sudo apt-get install wmctrl xautomation xsel
# in debian/ubuntu linux

#copy to clipboard "do filename"
echo 'do ' '"'$1'"' | xsel -b

# get current window id
winid = $(xprop -root | awk '/_NET_ACTIVE_WINDOW\(WINDOW\)/{print $NF}')

# check for stata window, if found activate else execute
# use correct version here
if [ "$(pgrep stata)" ] 
then
    wmctrl -a 'Stata/MP 11.2'
else
    xstata &
    sleep .1 
fi

# delay depends on window manager etc
# .1 ok with xmonad in 10.04
sleep .1 

# switch to command line, ctrl-4 in stata 10, ctrl-1 in 11/12
# and select existing text via ctrl-a
xte 'keydown Control_L' 'key 1' 'key A' 'usleep 100' \
    'key V' 'keyup Control_L' 
sleep .1
xte 'key Return'
sleep .3


# go back to editor window
wmctrl -i -a $winid 

adjust this and put it in your vimrc.

"" STATA DO-FILE SCRIPTS
fun! RunIt()
  w
  "adjust this path to the bash script
  !sh "/home/username/.rundo.sh" "%:p"
endfun
au FileType stata noremap <F8> :<C-U>call RunIt()<CR><CR>
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)
          "adjust this path to the bash script
          exec "!sh /home/username/.rundo.sh " . temp
    "au VimLeave * exe "!del -y" temp
    au VimLeave * silent exe '!del /Q "'.$TEMP.'\*.tmp.do"'
endfun
au FileType stata noremap <F9> :<C-U>call RunDoLines()<CR><CR>