Skip to content

Instantly share code, notes, and snippets.

@piec
Forked from tarruda/jobcontrol.vim
Last active August 29, 2015 14:05
Show Gist options
  • Save piec/ffa8b0707764bf6ae70d to your computer and use it in GitHub Desktop.
Save piec/ffa8b0707764bf6ae70d to your computer and use it in GitHub Desktop.

Revisions

  1. piec revised this gist Aug 26, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jobcontrol.vim
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@ set nocp
    " - The program name. Can be the full path to the program or a $PATH entry
    " - An array with the program arguments
    :let srv1_id = jobstart('netcat-server-1', 'nc', ['-l', '-p', '9991'])
    :let srv2_id = jobstart('netcat-server-2', 'nc', ['-l', '-p', '9992'])
    :let srv2_id = jobstart('netcat-server-2', 'nc', ['-l', '-p', '9991'])

    function JobEvent()
    " v:job_data[0] = the job id
  2. piec revised this gist Aug 26, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jobcontrol.vim
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,7 @@ set nocp
    " - The program name. Can be the full path to the program or a $PATH entry
    " - An array with the program arguments
    :let srv1_id = jobstart('netcat-server-1', 'nc', ['-l', '-p', '9991'])
    :let srv2_id = jobstart('netcat-server-2', 'nc', ['-l', '-p', '9991'])
    :let srv2_id = jobstart('netcat-server-2', 'nc', ['-l', '-p', '9992'])

    function JobEvent()
    " v:job_data[0] = the job id
  3. piec revised this gist Aug 26, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jobcontrol.vim
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    " To play with this, use two terminals
    "
    " On terminal 1: `nvim -S jobcontrol.vim`.
    " Use `:call jobwrite(v:srv1_id, string) to write
    " Use `:call jobwrite(srv1_id, "string") to write
    " data to the netcat client
    " On terminal 2: `nc localhost 9991`. Type lines to send data to
    " nvim
  4. piec revised this gist Aug 26, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions jobcontrol.vim
    Original file line number Diff line number Diff line change
    @@ -19,8 +19,8 @@ set nocp
    " - The job "name", this will be used mainly to filter JobActivity autocommands
    " - The program name. Can be the full path to the program or a $PATH entry
    " - An array with the program arguments
    :let srv1_id = jobstart('netcat-server-1', 'nc', ['-l', '9991'])
    :let srv2_id = jobstart('netcat-server-2', 'nc', ['-l', '9991'])
    :let srv1_id = jobstart('netcat-server-1', 'nc', ['-l', '-p', '9991'])
    :let srv2_id = jobstart('netcat-server-2', 'nc', ['-l', '-p', '9991'])

    function JobEvent()
    " v:job_data[0] = the job id
  5. @tarruda tarruda created this gist Jul 9, 2014.
    43 changes: 43 additions & 0 deletions jobcontrol.vim
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    " Demo of Neovim job control feature.
    "
    " It starts two netcat processes listening on the same TCP port,
    " the second process will exit immediately since the port will be
    " unavailable(Used to demonstrate the stderr/exit events)
    " To play with this, use two terminals
    "
    " On terminal 1: `nvim -S jobcontrol.vim`.
    " Use `:call jobwrite(v:srv1_id, string) to write
    " data to the netcat client
    " On terminal 2: `nc localhost 9991`. Type lines to send data to
    " nvim
    "
    " Use `jobstop(job_id)` to stop a running job


    set nocp
    " Arguments to jobstart:
    " - The job "name", this will be used mainly to filter JobActivity autocommands
    " - The program name. Can be the full path to the program or a $PATH entry
    " - An array with the program arguments
    :let srv1_id = jobstart('netcat-server-1', 'nc', ['-l', '9991'])
    :let srv2_id = jobstart('netcat-server-2', 'nc', ['-l', '9991'])

    function JobEvent()
    " v:job_data[0] = the job id
    " v:job_data[1] = the event type, one of "stdout", "stderr" or "exit"
    " v:job_data[2] = data read from stdout or stderr
    if v:job_data[1] == 'stdout'
    let str = 'Message from job '.v:job_data[0].': '.v:job_data[2]
    elseif v:job_data[1] == 'stderr'
    let str = 'Error message from job '.v:job_data[0].': '.v:job_data[2]
    else
    " Exit
    let str = 'Job '.v:job_data[0].' exited'
    endif

    call append(line('$'), str)
    endfunction

    " This autocommand will run whenever there's activity on jobs with names
    " starting with 'netcat-server-'
    au JobActivity netcat-server-* call JobEvent()