将代码从 Vim 发送到外部应用程序以执行
使用 Stata,我的文本编辑器是 gVim。使用此处和这里将代码从 Vim 发送到 Stata 让我无法切换到 Linux。这些脚本位于 AutoIt 中,我无法在 Linux 中使用。它们独立于文本编辑器;编写它们的人使用 Notepad++。这些脚本在我的 .vimrc 文件中包含几行,允许我发送选择或整个文件到 Stata 窗口。
我正在 Linux 中寻找这个。有命令行版本的Stata和GUI版本的xstata。我使用 GUI 版本,因此 Screen 和 Tmux 被排除。我找不到 Vim 的插件。 Bash 我想研究一下。 Python 就可以了。
我需要翻译的 AutoIt 脚本我不喜欢覆盖剪贴板的内容。它检查是否有打开的 Stata 窗口,选择或执行一个,将要执行的内容粘贴到临时文件中,切换到 Stata 窗口,使用 Ctrl + 1 选择命令行(并且任何已经用 Ctrl + A 编写的内容)然后将“tempfile”粘贴到命令行中,然后执行代码。我有 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 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
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 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
IronAHK 是 AutoHotKey 脚本语言,类似于 AutoIt(一种 GUI 自动化/键盘重新映射工具)。我没有使用过 IronAHK,但 AutoHotkey 可以运行 AutoIt v2 脚本。
您还可以查看 @Sikuli 项目:“Sikuli 是一种使用图像( Sikuli 包括 Sikuli Script(Jython 的可视化脚本 API)和 Sikuli IDE(一个用于轻松编写带有屏幕截图的可视化脚本的集成开发环境)”(来自 sikuli 首页)
另一个不错的选择是 Linux 桌面测试项目 (LDTP),可使用 Python 编写脚本:
示例:
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:
也许您可以使用类似于此 vim 插件所使用的机制,它执行类似的任务:
R.vim : 将 R 代码从 VIM 缓冲区发送到 R
此插件将 R 代码发送到 R 工具,在 unix 和 windows 下 (R 编程语言)广泛用于统计软件开发和数据分析)。
我不知道 Stata 或 R 语言,但似乎你可以使用 R 控制 Stata,如 为什么使用 R?:
一些 Stata 命令转换为 R:
Stata 或 R
如果您可以通过 R 执行所需的任务,那么您可能可以不加更改地使用上面的 Vim 插件。
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?:
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.
我使用 VI map 函数定义宏,将文件发送到 C 编译器,并检索结果。它不是很健壮(没有 if/then 编程),但实现起来非常简单,而且我使用了很多标准映射。例如,
&T
将我所在的行大写,而&t
将其小写。我使用&S
来运行我的拼写检查器 (gspell) 等。您不需要以 & 符号开始宏,但这样我就知道它不太可能是我想要的字母组合正在打字。设置地图非常简单。您可以使用 :map ex 命令、空格、用于调用地图的 单词、空格,然后是要执行的击键。如果需要插入诸如回车或转义之类的内容,请在其前面加上 Ctrl-V。
您可以使用
map!
映射可在插入或替换模式下执行的宏。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.