changeset 8463:508504ca52ac v7.4.1522

commit https://github.com/vim/vim/commit/6ff02c96519946716069f05c62849986a706033b Author: Bram Moolenaar <Bram@vim.org> Date: Tue Mar 8 20:12:44 2016 +0100 patch 7.4.1522 Problem: Cannot write channel err to a buffer. Solution: Implement it.
author Christian Brabandt <cb@256bit.org>
date Tue, 08 Mar 2016 20:15:04 +0100
parents 9b1f271c9769
children accd3cfd6957
files src/channel.c src/testdir/test_channel.vim src/version.c
diffstat 3 files changed, 71 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/channel.c
+++ b/src/channel.c
@@ -871,7 +871,7 @@ channel_set_job(channel_T *channel, job_
  * Find a buffer matching "name" or create a new one.
  */
     static buf_T *
-find_buffer(char_u *name)
+find_buffer(char_u *name, int err)
 {
     buf_T *buf = NULL;
     buf_T *save_curbuf = curbuf;
@@ -890,7 +890,8 @@ find_buffer(char_u *name)
 	curbuf = buf;
 	if (curbuf->b_ml.ml_mfp == NULL)
 	    ml_open(curbuf);
-	ml_replace(1, (char_u *)"Reading from channel output...", TRUE);
+	ml_replace(1, (char_u *)(err ? "Reading from channel error..."
+				   : "Reading from channel output..."), TRUE);
 	changed_bytes(1, 0);
 	curbuf = save_curbuf;
     }
@@ -968,10 +969,27 @@ channel_set_options(channel_T *channel, 
 	if (!(opt->jo_set & JO_OUT_MODE))
 	    channel->ch_part[PART_OUT].ch_mode = MODE_NL;
 	channel->ch_part[PART_OUT].ch_buffer =
-				       find_buffer(opt->jo_io_name[PART_OUT]);
-	ch_logs(channel, "writing to buffer '%s'",
+				find_buffer(opt->jo_io_name[PART_OUT], FALSE);
+	ch_logs(channel, "writing out to buffer '%s'",
 		      (char *)channel->ch_part[PART_OUT].ch_buffer->b_ffname);
     }
+
+    if ((opt->jo_set & JO_ERR_IO) && (opt->jo_io[PART_ERR] == JIO_BUFFER
+	 || (opt->jo_io[PART_ERR] == JIO_OUT && (opt->jo_set & JO_OUT_IO)
+				       && opt->jo_io[PART_OUT] == JIO_BUFFER)))
+    {
+	/* writing err to a buffer. Default mode is NL. */
+	if (!(opt->jo_set & JO_ERR_MODE))
+	    channel->ch_part[PART_ERR].ch_mode = MODE_NL;
+	if (opt->jo_io[PART_ERR] == JIO_OUT)
+	    channel->ch_part[PART_ERR].ch_buffer =
+					 channel->ch_part[PART_OUT].ch_buffer;
+	else
+	    channel->ch_part[PART_ERR].ch_buffer =
+				 find_buffer(opt->jo_io_name[PART_ERR], TRUE);
+	ch_logs(channel, "writing err to buffer '%s'",
+		      (char *)channel->ch_part[PART_ERR].ch_buffer->b_ffname);
+    }
 }
 
 /*
--- a/src/testdir/test_channel.vim
+++ b/src/testdir/test_channel.vim
@@ -633,6 +633,53 @@ func Test_pipe_to_buffer()
   endtry
 endfunc
 
+func Test_pipe_err_to_buffer()
+  if !has('job')
+    return
+  endif
+  call ch_log('Test_pipe_err_to_buffer()')
+  let job = job_start(s:python . " test_channel_pipe.py",
+	\ {'err-io': 'buffer', 'err-name': 'pipe-err'})
+  call assert_equal("run", job_status(job))
+  try
+    let handle = job_getchannel(job)
+    call ch_sendraw(handle, "echoerr line one\n")
+    call ch_sendraw(handle, "echoerr line two\n")
+    call ch_sendraw(handle, "doubleerr this\n")
+    call ch_sendraw(handle, "quit\n")
+    sp pipe-err
+    call s:waitFor('line("$") >= 5')
+    call assert_equal(['Reading from channel error...', 'line one', 'line two', 'this', 'AND this'], getline(1, '$'))
+    bwipe!
+  finally
+    call job_stop(job)
+  endtry
+endfunc
+
+func Test_pipe_both_to_buffer()
+  if !has('job')
+    return
+  endif
+  call ch_log('Test_pipe_both_to_buffer()')
+  let job = job_start(s:python . " test_channel_pipe.py",
+	\ {'out-io': 'buffer', 'out-name': 'pipe-err', 'err-io': 'out'})
+  call assert_equal("run", job_status(job))
+  try
+    let handle = job_getchannel(job)
+    call ch_sendraw(handle, "echo line one\n")
+    call ch_sendraw(handle, "echoerr line two\n")
+    call ch_sendraw(handle, "double this\n")
+    call ch_sendraw(handle, "doubleerr that\n")
+    call ch_sendraw(handle, "quit\n")
+    sp pipe-err
+    call s:waitFor('line("$") >= 7')
+    call assert_equal(['Reading from channel output...', 'line one', 'line two', 'this', 'AND this', 'that', 'AND that', 'Goodbye!'], getline(1, '$'))
+    bwipe!
+  finally
+    call job_stop(job)
+  endtry
+endfunc
+
 func Test_pipe_from_buffer()
   if !has('job')
     return
--- a/src/version.c
+++ b/src/version.c
@@ -744,6 +744,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1522,
+/**/
     1521,
 /**/
     1520,