首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将代码从vim发送到外部应用程序以供执行

将代码从vim发送到外部应用程序以供执行
EN

Stack Overflow用户
提问于 2011-08-12 18:18:09
回答 2查看 1.6K关注 0票数 19

我经常在工作中使用stata。我选择的文本编辑器是(g)vim。我一直在使用herehere提供的脚本将代码从vim发送到stata。这个功能非常实用,实际上是唯一阻止我完全切换到linux的原因。这些脚本是用AutoIT编写的,所以我不能在linux中使用它们。它们也基本上独立于文本编辑器的选择,编写它们的人都在使用notepad++。

本质上,这些脚本和我的vimrc中的几行代码允许我将选择或整个文件发送到一个正在运行的stata窗口(如果没有打开,则首先启动stata )。

我正在寻找在linux中实现这一点的解决方案,但我不知道从哪里开始。在linux中有两个不同的stata版本,stata用于命令行,而xstata是gui版本。我需要使用gui版本,因为不幸的是,命令行版本的功能是有限的,所以screen/tmux被排除在外。

如果这是微不足道的,我真的很抱歉错过了它,并将非常感谢解决方案。我也找不到一个现有的vim插件可以使用。如果没有,我愿意投入一些时间并弄清楚如何实现一个解决方案。然而,在正确的方向上的指针会非常有用。总的来说,我对linux和编程比较陌生,但我愿意学习。

关于工具:我不知道bash,但它是我想在某个时候研究的东西。我对python略有涉猎,所以这也没问题。如果这项任务还有什么绝对的优势,请让我知道。

任何帮助都是非常感谢的。AutoIT脚本托管在网站上,但如果需要,我可以在这里发布我的windows设置。

编辑

好了,在评论中进行了一些讨论之后,下面是我需要翻译的基本AutoIT脚本。(我更喜欢一种不会每次都覆盖系统剪贴板内容的解决方案。)

Edit2我猜这就是脚本的基本功能:它检查打开的stata窗口,选择它(或执行一个窗口),将要执行的内容粘贴到一个临时文件中,切换到stata窗口,用ctrl-1 (以及任何可能已经用ctrl-a写在那里的内容)选择命令行,然后将do "tempfile“粘贴到命令行,然后命令行执行发送的代码。至少我是这么理解的。

最终评论

前段时间我在bash中想出了一个解决方案,它发布在here上,作为对这个问题的前一个版本的回答。

代码语言:javascript
复制
; 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 Files\Stata11\StataSE.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 Files\Stata11\StataSE.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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-23 04:01:17

IronAHK是对AutoHotKey脚本语言的Linux/Mono重写,类似于AutoIt (一种图形用户界面自动化/键盘重新映射工具)。我没有用过IronAHK,但是AutoHotkey可以运行AutoIt v2脚本。

你也可以查看@Project Sikuli:"Sikuli是一种使用图像(截图)自动化和测试图形用户界面的可视化技术。Sikuli包括Sikuli Script,一种用于Jython的可视化脚本编程接口,以及Sikuli IDE,这是一种集成开发环境,用于轻松编写带有屏幕截图的可视化脚本“(来自sikuli首页)

另一个很好的选择是Linux Desktop Testing Project (LDTP),可以用Python编写脚本:

示例:

代码语言:javascript
复制
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])
    ...
票数 3
EN

Stack Overflow用户

发布于 2011-08-19 20:08:21

也许你可以使用一个类似于这个vim插件的机制,它执行类似的任务:

http://www.vim.org/scripts/script.php?script_id=1048

这个插件将R代码发送到R工具,在unix和windows下(R programming language 被广泛用于统计软件开发和数据分析)。

我不知道Stata或R语言,但似乎您可以使用R来控制Stata,如http://www.r-bloggers.com/why-use-r/中所述:

代码语言:javascript
复制
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).

一些转换为R的Stata命令:

http://www.r-bloggers.com/stata-or-r/

如果您可以通过R执行所需的任务,那么您可能可以不加更改地使用上面的vim插件。

希望这能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7038701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档