Send code from Vim to an external application for execution

Question:

Using Stata my text editor is gVim. Using scripts from here and here to send code from Vim to Stata keeps me from switching to Linux. The scripts are in AutoIt I cannot use in Linux. They are independent of text editor; people who wrote them use Notepad++. These scripts with a few lines in my .vimrc file allow me to send selections or the whole file to a Stata window.

I am looking for this in Linux. There are Stata for command line and xstata is the GUI version. I use the GUI version so Screen and Tmux are ruled out. I wasn’t able to find a plugin for Vim. Bash I want to look into. Python would be OK.

AutoIt script I need to translate I prefer doesn’t overwrite content of clipboard. It checks for an open Stata window, selects or executes one, pastes contents to be executed into temporary file, switches to Stata window, selects command line with Ctrl + 1 (and anything already be written with Ctrl + A) then pastes "tempfile" into command line, which then executes the code. I have a solution in Bash.

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

; File locations
; Path to INI file
$ini = @ScriptDir & "rundo.ini"
;; contents of ini file are the following
    ;[Stata]
    ;; Path to Stata executable
    ;statapath = "C:Program FilesStata11StataSE.exe"
    ;; Title of Stata window
    ;statawin = "Stata/SE 11.2"
    ;; Keyboard shortcut for Stata command window
    ;statacmd = "^1"
    ;[Delays]
    ;; Pause after copying of Stata commands to clipboard, in milliseconds
    ;; Use higher number if script fails (default: 100, recommended range: 0 - 200)
    ;clippause = 100
    ;; Pause between window-related operations, in milliseconds
    ;; Use lower number to speed up script, higher number if script fails (default: 200)
    ;winpause = 200
    ;; Pause between key strokes sent to Stata, in milliseconds
    ;; Use lower number to speed up script, higher number if script fails (default: 1)
    ;keypause = 1


; Path to Stata executable
$statapath = IniRead($ini, "Stata", "statapath", "C:Program FilesStata11StataSE.exe")
; Title of Stata window
$statawin = IniRead($ini, "Stata", "statawin", "Stata/SE 11.2")

; Keyboard shortcut for Stata command window
$statacmd = IniRead($ini, "Stata", "statacmd", "^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 WinWaitDelay and SendKeyDelay 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($statacmd)
  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($statacmd)
  ; 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
Asked By: ilprincipe

||

Answers:

I’ve used the VI map function to define macros to send my file to a C compiler, and retrieve the results. It’s not very robust (no if/then programming), but it’s pretty simple to implement, and I have a lot of standard mappings I use. For example &T uppercases the line I’m on while &t lowercases it. I use &S to run my spell checker (gspell), etc. You don’t need to begin your macros with an ampersand, but this way I know it’s an unlikely combination of letters I’d be typing.

Setting up a Map is pretty easy. You use the :map ex command, space, a word used to invoke the map, a space, and then the keystrokes you want to execute. If you need to insert something like a return or escape, prefix it with a Ctrl-V.

You can use map! to map a macro that can be executed while in Insert or Replace mode.

Answered By: David W.

Maybe you could use a mechanism similar to used by this vim plugin, which perform a similar task:

R.vim : Send R code from a VIM buffer to R

This plugin sends R code to a R tool, under unix and windows (R programming language) is widely used for statistical software development and data analysis).

I don’t know about Stata or R language, but it seems that you could control Stata using R, as stated in Why use R?:

You can easily use it anywhere. It’s platform-independent, so you can use it
on any operating system. And it’s free, so you can use it at any employer
without having to persuade your boss to purchase a license.
:
:
R allows you to integrate with other languages (C/C++, Java, Python) and
enables you to interact with many data sources: ODBC-compliant databases
(Excel, Access) and other statistical packages (SAS, Stata, SPSS,
Minitab).

Some Stata commands translated to R:

Stata or R

If you could perform the desired task through R then probably you could use the Vim plugin above unchanged.

Answered By: mMontu

IronAHK is a Linux/Mono rewrite of the AutoHotKey scripting language, which is similar to AutoIt (a GUI automation / keyboard remapping tool). I haven’t used IronAHK, but AutoHotkey can run AutoIt v2 scripts.

You can also look @Project Sikuli: “Sikuli is a visual technology to automate and test graphical user interfaces (GUI) using images (screenshots). Sikuli includes Sikuli Script, a visual scripting API for Jython, and Sikuli IDE, an integrated development environment for writing visual scripts with screenshots easily” (from the sikuli front page)

Another good option is Linux Desktop Testing Project (LDTP), scriptable with Python:

example:

from ldtp import *
from ldtputils import *

try:
    launchapp("gedit")
    if waittillguiexist("*.gedit")==0:
        raise LdtpExecutionError("Gedit window does not exist")

    selectmenuitem("*-gedit", "mnuFile;mnuOpen")
    selectrow("dkgOpenFiles...", "tblFiles", fileName[0])
    ...
Answered By: PabloG
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.