Vim 运行J脚本,就像它是逐行输入到解释器中一样

Vim 运行J脚本,就像它是逐行输入到解释器中一样,vim,j,Vim,J,有没有一种方法可以像在interpeter中键入脚本一样运行脚本?好处是我不需要到处都使用echos,所做的工作保存为一个文件,我可以使用vim进行编辑。例如: example.ijs x =. 1 x + 3 终点站 x =. 1 x + 3 4 如果没有,我将编写一个vimscript来完成上述任务,然后在这里共享它们。vimscript解决方案的优点是,我可以使用命令来运行整个文件、当前行、当前选择、当前行之前和包括当前行在内的所有内容,或者任何其他有用的内容 相关但不重复

有没有一种方法可以像在interpeter中键入脚本一样运行脚本?好处是我不需要到处都使用echos,所做的工作保存为一个文件,我可以使用vim进行编辑。例如:

example.ijs

x =. 1
x + 3
终点站

   x =. 1
   x + 3
4
如果没有,我将编写一个vimscript来完成上述任务,然后在这里共享它们。vimscript解决方案的优点是,我可以使用命令来运行整个文件、当前行、当前选择、当前行之前和包括当前行在内的所有内容,或者任何其他有用的内容


相关但不重复:

听起来您要求为jconsole接口解决这个问题。对此我没有答案,但我要指出,JHS和jQt接口都可以使用该功能。如果您不介意切换到其他界面,那么这将是一个快速而简单的解决方案。

交互式运行脚本的最简单方法是使用
labs
命令:

 load'labs/lab'

 lab'myscript.ijt'    
 1 of 2 in myscript.ijt
 x =. 1

 NB. press Ctrl+'.' to advance.
 x + 3
4

 NB. Run the whole script.
 lab 1 _
 x =. 1
 x + 3
4
更多关于实验室的信息

如果希望以非交互方式运行脚本,就像在控制台中键入脚本一样,只需将脚本馈送到j(在linux中):


这就是我提到的vimscript解决方案。就我所知,语言不可知论者也是如此

" Global variable dictates whether new terminals are opened
" horizontally ('h') or vertically ('v').
let g:terminalsplit = 'h'

" Add execution strings for each language you use.
augroup terminalcommands
    autocmd!
    autocmd Filetype j      let g:cmdstr = 'jconsole.cmd'
    autocmd Filetype python let g:cmdstr = 'python'
augroup END

" Close all terminals
nnoremap <silent> <leader>p :call CloseTerminal()<cr>
" Run file
nnoremap <leader>h :call Run(g:cmdstr, 'script')<cr>
" Run as if file were entered line-by-line into the interpreter
" Mappings for: Line, selection, file up to line, entire file
nnoremap <leader>j yy:call Run(g:cmdstr, 'interpreter')<cr>
vnoremap <leader>j ygv<esc>:call Run(g:cmdstr, 'interpreter')<cr>
nnoremap <leader>k Vggy<c-O>:call Run(g:cmdstr, 'interpreter')<cr>
nnoremap <leader>l mzggVGy'z:call Run(g:cmdstr, 'interpreter')<cr>

function! Run(cmdstr, mode)
    let filepath = expand('%:p') " Copy filepath before switch to terminal
    call CloseTerminal()
    call OpenTerminal()
    echo g:clear . " & " . a:cmdstr
    call feedkeys(g:clear . " & " . a:cmdstr) " Begin run command
    call RunCode(filepath, a:mode)
    call feedkeys("\<c-w>p") " Switch back to file window
endfunction

function! CloseTerminal()
    if has('nvim')
        let terminals = split(execute('filter/term:/ls'), '\n')
    else
        let terminals = split(execute('filter/!/ls'), '\n')
    endif
    for i in range(len(terminals))
        silent! exe "bd! " . split(terminals[i], '  ')[0]
    endfor
endfunction

function! OpenTerminal()
    if g:terminalsplit == 'h'
        terminal
    elseif g:terminalsplit == 'v'
        vertical terminal
    else
        echo 'g:terminalsplit=' . &g:terminalsplit . '. Must be "h" or "v".'
    endif
endfunction

function! RunCode(filepath, mode)
    if a:mode == 'script'
        call feedkeys(" " . a:filepath . "\<cr>")
    elseif a:mode == 'interpreter'
        call feedkeys("\<cr>")
        call feedkeys("\<c-w>\"\"")
    else
        echo 'a:mode=' . a:mode . '. Must be "script" or "interpreter".'
    endif
endfunction

" Use to clear the terminal window before running the script
if has('unix')
    let g:clear = 'clear'
else
    let g:clear = 'cls'
endif

“全局变量指示是否打开新的终端
“水平('h')或垂直('v')。
设g:terminalsplit='h'
“为您使用的每种语言添加执行字符串。
augroup终端命令
自动驾驶!
autocmd文件类型j let g:cmdstr='jconsole.cmd'
autocmd文件类型python let g:cmdstr='python'
螺旋端
“关闭所有终端
nnoremap p:callcloseterminal()
“运行文件
nnoremap h:调用运行(g:cmdstr,“脚本”)
“像文件被逐行输入解释器一样运行
映射:行、选择、文件到行、整个文件
nnoremap j yy:调用运行(g:cmdstr,“解释器”)
vnoremap j ygv:调用运行(g:cmdstr,“解释器”)
nnoremap k Vggy:调用运行(g:cmdstr,“解释器”)
nnoremap l mzggVGy'z:调用运行(g:cmdstr,“解释器”)
函数!运行(cmdstr,模式)
在切换到终端之前,让filepath=expand('%:p')“复制filepath
呼叫关闭终端()
调用OpenTerminal()
g:清楚。" & " . a:cmdstr
调用feedkeys(g:clear.&.a:cmdstr)“开始运行”命令
调用运行码(文件路径,a:mode)
调用反馈键(“\p”)“切换回文件窗口
端功能
功能!CloseTerminal()
如果有('nvim')
let terminals=split(执行('filter/term:/ls'),'\n')
其他的
let terminals=split(执行('filter/!/ls'),'\n')
恩迪夫
适用于范围内的i(len(端子))
安静!exe“bd!”。拆分(终端[i],''[0]
外循环
端功能
功能!OpenTerminal()
如果g:terminalsplit=='h'
终端
elseif g:terminalsplit=='v'
垂直终端
其他的
回显“g:terminalsplit=”&g:终端分裂。必须是“h”或“v”。'
恩迪夫
端功能
功能!运行代码(文件路径、模式)
如果a:mode=='script'
调用feedkeys(“.a:filepath.”\”)
elseif a:mode==“解释器”
调用feedkeys(“\”)
调用feedkeys(“\\”\”)
其他的
回显“a:mode=”。a:模式。必须是“脚本”或“解释器”。'
恩迪夫
端功能
“用于在运行脚本之前清除终端窗口
如果有('unix')
设g:clear='clear'
其他的
设g:clear='cls'
恩迪夫

使用
loadd
而不是
load
来运行脚本并显示行和结果

   loadd 'example.ijs'
   x =. 1
   x + 3
4

我试过jQt,但使用它感觉就像在糖浆中行走。也许最好的比喻是用Java(详细)和J(简洁)编写代码.有没有办法通过vim将jQt导入?我不知道如何将jQt导入vim,但我的经验主要是单独使用JHS和jQt接口。Eelvex关于labs的回答可能是您正在寻找的选项。labs的新版本基于sp,它本质上是同一个库,但侧重于将脚本作为输入运行。JHS本质上是一种使用JavaScript作为JJ引擎接口的Web浏览器运行J的方法。HTTP服务器是本地页面的基础,它提供了J接口。我认为这并不适合您的需求,但您可能希望以后再看它。我完成了运行文件的VimScript,我将发布。在查看了Eelvex的回复后,我发现vimscript解决方案的优点在于,据我所知,它们与语言无关;到目前为止,它们与J和Python一起工作。我发现的最大优势是,由于您已经处于web环境中,它允许您运行视频、可缩放矢量图形、音频和其他web便利无需离开界面。我创建的一些视频实验室就是这样的例子。不过,您无法通过控制台访问它们,目前它们仅在JHS和jQt上可用。感谢您的帮助。我编写了一个可工作的vimscript,稍后将与大家分享。对于实验室,看起来我必须更改文件扩展名ion,这不是理想的方法。
0!:1
方法非常好,但我的脚本只有放在J目录中才能被识别(对于我来说:
J901
)。我想知道这是否与J倾向于退出到其主目录有关。这正常吗?当
jconsole.cmd运行时,J似乎会切换到其安装目录。如果使用完整路径,
0!:1<'c:\mypath\myscript'
应该可以工作。因此,J这样切换目录是正常的吗?根据我的经验,绝对路径比相对于脚本的路径更脆弱。你应该将其发布在J编程论坛上。我相信那里的人一定很想看到你所做的事情。完美的时机!我上周刚刚订阅了。(:
" Global variable dictates whether new terminals are opened
" horizontally ('h') or vertically ('v').
let g:terminalsplit = 'h'

" Add execution strings for each language you use.
augroup terminalcommands
    autocmd!
    autocmd Filetype j      let g:cmdstr = 'jconsole.cmd'
    autocmd Filetype python let g:cmdstr = 'python'
augroup END

" Close all terminals
nnoremap <silent> <leader>p :call CloseTerminal()<cr>
" Run file
nnoremap <leader>h :call Run(g:cmdstr, 'script')<cr>
" Run as if file were entered line-by-line into the interpreter
" Mappings for: Line, selection, file up to line, entire file
nnoremap <leader>j yy:call Run(g:cmdstr, 'interpreter')<cr>
vnoremap <leader>j ygv<esc>:call Run(g:cmdstr, 'interpreter')<cr>
nnoremap <leader>k Vggy<c-O>:call Run(g:cmdstr, 'interpreter')<cr>
nnoremap <leader>l mzggVGy'z:call Run(g:cmdstr, 'interpreter')<cr>

function! Run(cmdstr, mode)
    let filepath = expand('%:p') " Copy filepath before switch to terminal
    call CloseTerminal()
    call OpenTerminal()
    echo g:clear . " & " . a:cmdstr
    call feedkeys(g:clear . " & " . a:cmdstr) " Begin run command
    call RunCode(filepath, a:mode)
    call feedkeys("\<c-w>p") " Switch back to file window
endfunction

function! CloseTerminal()
    if has('nvim')
        let terminals = split(execute('filter/term:/ls'), '\n')
    else
        let terminals = split(execute('filter/!/ls'), '\n')
    endif
    for i in range(len(terminals))
        silent! exe "bd! " . split(terminals[i], '  ')[0]
    endfor
endfunction

function! OpenTerminal()
    if g:terminalsplit == 'h'
        terminal
    elseif g:terminalsplit == 'v'
        vertical terminal
    else
        echo 'g:terminalsplit=' . &g:terminalsplit . '. Must be "h" or "v".'
    endif
endfunction

function! RunCode(filepath, mode)
    if a:mode == 'script'
        call feedkeys(" " . a:filepath . "\<cr>")
    elseif a:mode == 'interpreter'
        call feedkeys("\<cr>")
        call feedkeys("\<c-w>\"\"")
    else
        echo 'a:mode=' . a:mode . '. Must be "script" or "interpreter".'
    endif
endfunction

" Use to clear the terminal window before running the script
if has('unix')
    let g:clear = 'clear'
else
    let g:clear = 'cls'
endif

   loadd 'example.ijs'
   x =. 1
   x + 3
4