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

Run J script as if it were line-by-line entered into the interpreter

有没有办法 运行 脚本就像是输入到 interpeter 中一样?好处是我不需要到处回声,完成的工作保存为文件,我可以使用 vim 进行编辑。示例:

example.ijs

x =. 1
x + 3

航站楼

   x =. 1
   x + 3
4

如果没有,我将编写一个 vim 可以执行上述操作的脚本,然后在此处共享。 vim 脚本解决方案的优点是我可以使用命令 运行 整个文件、当前行、当前选择、包括当前行在内的所有内容,或任何其他有用的内容。

相关但不重复:How to call J (ijconsole) with a script automatically

听起来你要求为 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

有关实验室的更多信息here

如果您想以非交互方式 运行 一个脚本,就像在控制台中输入一样,您可以将脚本提供给 j(在 linux 中):

j < myscript.ijs
4

Alternatively,到

see the lines on the screen just as though they had been typed from the keyboard

从脚本中,您可以使用 0!:1:

 0!:1 < 'myscript.ijs'
 x =. 1
 x + 3
4

这就是我提到的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

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

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