Andrew Stewart wrote:

> I have been trying to run a job asynchronously and capture the output,
> but despite reading the docs many times I can't get it to work.
> 
> Ideally I want all the diff output in one go, rather than line by
> line, so I first tried using only an on_exit callback:
> 
>     let cmd = 'diff A B'
>     let job = job_start(cmd, {'exit_cb': 'MyExitHandler'})
> 
> And then:
> 
>     function! MyExitHandler(job, exit_status)
>       let data = ch_read(a:job)
>       echom data
>     endfunction
> 
> I would expect the handler to be called just about immediately but
> nothing happens.  After 30s of waiting I get bored and move the cursor
> (if I'm in normal mode) / press <Escape> (if I'm in insert mode), at
> which point "E906: not an open channel" is echoed.

The exit callback is only invoked once Vim detects the job is gone.  By
then it's too late to read the output.  This is documented.  


There is also the close_cb for the channel, which gets called much
sooner than the exit_cb.  Actually, it's called before the out_cb
function, should probably change that.

Perhaps, when there is a close_cb, the DETACH message should not be
added.  That simplifies how it works.

> From this I deduce that the handler isn't called when the job exits,
> >but instead some time later as and when vim checks up on the job.
> 
> So I tried using an on_stdout callback as well:
> 
>     let job = job_start(cmd, {'on_stdout': 'MyOutHandler', 'exit_cb':
>     'MyExitHandler'})
> 
> And also:
> 
>     function! MyOutHandler(channel, message)
>       echom a:message
>     endfunction
> 
> This handler gets called just about immediately, as expected, but
> a:message is always 'DETACH', not the diff output.  And then a few
> seconds later the exit handler echoes its E906 error.

This works.  But the script should use "out_cb":

        func! OutHandler(channel, msg)
          echomsg a:msg
        endfunc
        let cmd = 'diff -u /tmp/a /tmp/b'
        let job = job_start(cmd, {'out_cb': 'OutHandler'})

The echo statements overwrite each other, "DETACH" is the last one.  Use
":messages" to see what happened before this.  Obviously you need to do
something else than echoing.

> Please could somebody tell me what I'm doing wrong?
> 
> I'm using Vim v7.4.1770.

-- 
Two cows are standing together in a field.  One asks the other:
"So what do you think about this Mad Cow Disease?"
The other replies: "That doesn't concern me. I'm a helicopter."

 /// Bram Moolenaar -- b...@moolenaar.net -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to vim_dev+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Raspunde prin e-mail lui