multiplayer.vim (2280B)
1 vim9script 2 3 # # Create a channel log so we can see what happens. 4 # ch_logfile('logfile', 'w') 5 6 # var shell_job: job 7 8 # # Function handling a line of text that has been typed. 9 # def TextEntered(text: string) 10 # # Send the text to a shell with Enter appended. 11 # ch_sendraw(shell_job, text .. "\n") 12 # enddef 13 14 # # Function handling output from the shell: Add it above the prompt. 15 # def GotOutput(channel: channel, msg: string) 16 # append(line("$") - 1, "- " .. msg) 17 # enddef 18 19 # # Function handling the shell exits: close the window. 20 # def JobExit(job: job, status: number) 21 # quit! 22 # enddef 23 24 # # Start a shell in the background. 25 # shell_job = job_start(["/bin/sh"], { 26 # out_cb: GotOutput, 27 # err_cb: GotOutput, 28 # exit_cb: JobExit, 29 # }) 30 31 # new 32 # set buftype=prompt 33 # var buf = bufnr('') 34 # prompt_setcallback(buf, TextEntered) 35 # prompt_setprompt(buf, "shell command: ") 36 37 # # start accepting shell commands 38 # startinsert 39 40 # function! multiplayer#Connect() 41 # let s:buf_file = expand('%:p') 42 # call system('mkfifo /tmp/vim_shared_pipe_' . getpid()) 43 # let s:shared_job = job_start('tail -f /tmp/vim_shared_pipe_' . getpid(), {"out_cb": function("s:HandlerOut"), "out_mode": "json"}) 44 # autocmd! TextChanged,TextChangedI * call <SID>TextChanged() 45 # autocmd VimLeave * call <SID>Disconnect() 46 # endfunction 47 48 # function! s:TextChanged() 49 # let content = getline(1, '$') 50 # echomsg "TextChanged: " content 51 # sleep 1 52 # echomsg "" 53 # call writefile(content, expand('%:p')) 54 # call <SID>Broadcast(join(content, "\n")) 55 # endfunction 56 57 # function! s:Broadcast(data) 58 # let all_pipes = split(globpath('/tmp', 'vim_shared_pipe_*'), '\n') 59 # for pipe in all_pipes 60 # let mtch = matchlist(pipe, '.*vim_shared_pipe_\(.*\)') 61 # if len(mtch) >= 2 && mtch[1] != getpid() 62 # echomsg "mandando: " shellescape(a:data) 63 # sleep 1 64 # echomsg "" 65 # call system('echo ' . shellescape(a:data) . ' >' . pipe) 66 # endif 67 # endfor 68 # endfunction 69 70 # function! s:HandlerOut(channel, msg) 71 # echomsg "HandlerOut: " a:msg 72 # sleep 1 73 # echomsg "" 74 # call setline(1, split(a:msg, "\n")) 75 # endfunction 76 77 # function! s:Disconnect() 78 # call job_stop(s:shared_job) 79 # call delete('/tmp/vim_shared_pipe_' . getpid()) 80 # endfunction 81 82 # command! -nargs=0 MultiplayerConnect call multiplayer#Connect()