Skip to content

Commit 169ebb0

Browse files
committed
patch 7.4.2344
Problem: The "Reading from channel output..." message can be unwanted. Appending to a buffer leaves an empty first line behind. Solution: Add the "out_msg" and "err_msg" options. Writing the first line overwrites the first, empty line.
1 parent 53f1673 commit 169ebb0

5 files changed

Lines changed: 95 additions & 26 deletions

File tree

runtime/doc/channel.txt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*channel.txt* For Vim version 7.4. Last change: 2016 Sep 01
1+
*channel.txt* For Vim version 7.4. Last change: 2016 Sep 07
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -646,6 +646,8 @@ See |job_setoptions()| and |ch_setoptions()|.
646646
"out_buf": number the number of the buffer to write to
647647
"out_modifiable": 0 when writing to a buffer, 'modifiable' will be off
648648
(see below)
649+
"out_msg": 0 when writing to a new buffer, the first line will be
650+
set to "Reading from channel output..."
649651

650652
*job-err_io* *err_name* *err_buf*
651653
"err_io": "out" stderr messages to go to stdout
@@ -657,6 +659,8 @@ See |job_setoptions()| and |ch_setoptions()|.
657659
"err_buf": number the number of the buffer to write to
658660
"err_modifiable": 0 when writing to a buffer, 'modifiable' will be off
659661
(see below)
662+
"err_msg": 0 when writing to a new buffer, the first line will be
663+
set to "Reading from channel error..."
660664

661665
"block_write": number only for testing: pretend every other write to stdin
662666
will block
@@ -686,8 +690,16 @@ buffer number.
686690

687691
For a new buffer 'buftype' is set to "nofile" and 'bufhidden' to "hide". If
688692
you prefer other settings, create the buffer first and pass the buffer number.
689-
693+
*out_modifiable* *err_modifiable*
690694
The "out_modifiable" and "err_modifiable" options can be used to set the
695+
'modifiable' option off, or write to a buffer that has 'modifiable' off. That
696+
means that lines will be appended to the buffer, but the user can't easily
697+
change the buffer.
698+
*out_msg* *err_msg*
699+
The "out_msg" option can be used to specify whether a new buffer will have the
700+
first line set to "Reading from channel output...". The default is to add the
701+
message. "err_msg" does the same for channel error.
702+
691703
'modifiable' option off, or write to a buffer that has 'modifiable' off. That
692704
means that lines will be appended to the buffer, but the user can't easily
693705
change the buffer.

src/channel.c

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,7 @@ channel_set_job(channel_T *channel, job_T *job, jobopt_T *options)
10791079
* Returns NULL if there is something very wrong (error already reported).
10801080
*/
10811081
static buf_T *
1082-
find_buffer(char_u *name, int err)
1082+
find_buffer(char_u *name, int err, int msg)
10831083
{
10841084
buf_T *buf = NULL;
10851085
buf_T *save_curbuf = curbuf;
@@ -1104,7 +1104,8 @@ find_buffer(char_u *name, int err)
11041104
#endif
11051105
if (curbuf->b_ml.ml_mfp == NULL)
11061106
ml_open(curbuf);
1107-
ml_replace(1, (char_u *)(err ? "Reading from channel error..."
1107+
if (msg)
1108+
ml_replace(1, (char_u *)(err ? "Reading from channel error..."
11081109
: "Reading from channel output..."), TRUE);
11091110
changed_bytes(1, 0);
11101111
curbuf = save_curbuf;
@@ -1196,7 +1197,11 @@ channel_set_options(channel_T *channel, jobopt_T *opt)
11961197
}
11971198
else
11981199
{
1199-
buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE);
1200+
int msg = TRUE;
1201+
1202+
if (opt->jo_set2 & JO2_OUT_MSG)
1203+
msg = opt->jo_message[PART_OUT];
1204+
buf = find_buffer(opt->jo_io_name[PART_OUT], FALSE, msg);
12001205
}
12011206
if (buf != NULL)
12021207
{
@@ -1235,7 +1240,13 @@ channel_set_options(channel_T *channel, jobopt_T *opt)
12351240
EMSGN(_(e_nobufnr), (long)opt->jo_io_buf[PART_ERR]);
12361241
}
12371242
else
1238-
buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE);
1243+
{
1244+
int msg = TRUE;
1245+
1246+
if (opt->jo_set2 & JO2_ERR_MSG)
1247+
msg = opt->jo_message[PART_ERR];
1248+
buf = find_buffer(opt->jo_io_name[PART_ERR], TRUE, msg);
1249+
}
12391250
if (buf != NULL)
12401251
{
12411252
if (opt->jo_set & JO_ERR_MODIFIABLE)
@@ -2227,6 +2238,7 @@ append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, int part)
22272238
int save_write_to = buffer->b_write_to_channel;
22282239
chanpart_T *ch_part = &channel->ch_part[part];
22292240
int save_p_ma = buffer->b_p_ma;
2241+
int empty = (buffer->b_ml.ml_flags & ML_EMPTY);
22302242

22312243
if (!buffer->b_p_ma && !ch_part->ch_nomodifiable)
22322244
{
@@ -2253,9 +2265,16 @@ append_to_buffer(buf_T *buffer, char_u *msg, channel_T *channel, int part)
22532265
curbuf = buffer;
22542266
u_sync(TRUE);
22552267
/* ignore undo failure, undo is not very useful here */
2256-
ignored = u_save(lnum, lnum + 1);
2268+
ignored = u_save(lnum, lnum + 1 + (empty ? 1 : 0));
22572269

2258-
ml_append(lnum, msg, 0, FALSE);
2270+
if (empty)
2271+
{
2272+
/* The buffer is empty, replace the first (dummy) line. */
2273+
ml_replace(lnum, msg, TRUE);
2274+
lnum = 0;
2275+
}
2276+
else
2277+
ml_append(lnum, msg, 0, FALSE);
22592278
appended_lines_mark(lnum, 1L);
22602279
curbuf = save_curbuf;
22612280
if (ch_part->ch_nomodifiable)
@@ -4083,6 +4102,16 @@ get_job_options(typval_T *tv, jobopt_T *opt, int supported)
40834102
opt->jo_set |= JO_OUT_MODIFIABLE << (part - PART_OUT);
40844103
opt->jo_modifiable[part] = get_tv_number(item);
40854104
}
4105+
else if (STRCMP(hi->hi_key, "out_msg") == 0
4106+
|| STRCMP(hi->hi_key, "err_msg") == 0)
4107+
{
4108+
part = part_from_char(*hi->hi_key);
4109+
4110+
if (!(supported & JO_OUT_IO))
4111+
break;
4112+
opt->jo_set2 |= JO2_OUT_MSG << (part - PART_OUT);
4113+
opt->jo_message[part] = get_tv_number(item);
4114+
}
40864115
else if (STRCMP(hi->hi_key, "in_top") == 0
40874116
|| STRCMP(hi->hi_key, "in_bot") == 0)
40884117
{

src/structs.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,6 +1634,10 @@ struct channel_S {
16341634
#define JO_ERR_MODIFIABLE 0x40000000 /* "err_modifiable" (JO_OUT_ << 1) */
16351635
#define JO_ALL 0x7fffffff
16361636

1637+
#define JO2_OUT_MSG 0x0001 /* "out_msg" */
1638+
#define JO2_ERR_MSG 0x0002 /* "err_msg" (JO_OUT_ << 1) */
1639+
#define JO2_ALL 0x0003
1640+
16371641
#define JO_MODE_ALL (JO_MODE + JO_IN_MODE + JO_OUT_MODE + JO_ERR_MODE)
16381642
#define JO_CB_ALL \
16391643
(JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK + JO_CLOSE_CALLBACK)
@@ -1645,6 +1649,7 @@ struct channel_S {
16451649
typedef struct
16461650
{
16471651
int jo_set; /* JO_ bits for values that were set */
1652+
int jo_set2; /* JO2_ bits for values that were set */
16481653

16491654
ch_mode_T jo_mode;
16501655
ch_mode_T jo_in_mode;
@@ -1656,6 +1661,7 @@ typedef struct
16561661
char_u *jo_io_name[4]; /* not allocated! */
16571662
int jo_io_buf[4];
16581663
int jo_modifiable[4];
1664+
int jo_message[4];
16591665
channel_T *jo_channel;
16601666

16611667
linenr_T jo_in_top;

src/testdir/test_channel.vim

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -638,21 +638,27 @@ func BufCloseCb(ch)
638638
let g:Ch_bufClosed = 'yes'
639639
endfunc
640640

641-
func Run_test_pipe_to_buffer(use_name, nomod)
641+
func Run_test_pipe_to_buffer(use_name, nomod, do_msg)
642642
if !has('job')
643643
return
644644
endif
645645
call ch_log('Test_pipe_to_buffer()')
646646
let g:Ch_bufClosed = 'no'
647647
let options = {'out_io': 'buffer', 'close_cb': 'BufCloseCb'}
648+
let expected = ['', 'line one', 'line two', 'this', 'AND this', 'Goodbye!']
648649
if a:use_name
649650
let options['out_name'] = 'pipe-output'
650-
let firstline = 'Reading from channel output...'
651+
if a:do_msg
652+
let expected[0] = 'Reading from channel output...'
653+
else
654+
let options['out_msg'] = 0
655+
call remove(expected, 0)
656+
endif
651657
else
652658
sp pipe-output
653659
let options['out_buf'] = bufnr('%')
654660
quit
655-
let firstline = ''
661+
call remove(expected, 0)
656662
endif
657663
if a:nomod
658664
let options['out_modifiable'] = 0
@@ -667,7 +673,7 @@ func Run_test_pipe_to_buffer(use_name, nomod)
667673
call ch_sendraw(handle, "quit\n")
668674
sp pipe-output
669675
call WaitFor('line("$") >= 6 && g:Ch_bufClosed == "yes"')
670-
call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this', 'Goodbye!'], getline(1, '$'))
676+
call assert_equal(expected, getline(1, '$'))
671677
if a:nomod
672678
call assert_equal(0, &modifiable)
673679
else
@@ -681,31 +687,41 @@ func Run_test_pipe_to_buffer(use_name, nomod)
681687
endfunc
682688

683689
func Test_pipe_to_buffer_name()
684-
call Run_test_pipe_to_buffer(1, 0)
690+
call Run_test_pipe_to_buffer(1, 0, 1)
685691
endfunc
686692

687693
func Test_pipe_to_buffer_nr()
688-
call Run_test_pipe_to_buffer(0, 0)
694+
call Run_test_pipe_to_buffer(0, 0, 1)
689695
endfunc
690696

691697
func Test_pipe_to_buffer_name_nomod()
692-
call Run_test_pipe_to_buffer(1, 1)
698+
call Run_test_pipe_to_buffer(1, 1, 1)
699+
endfunc
700+
701+
func Test_pipe_to_buffer_name_nomsg()
702+
call Run_test_pipe_to_buffer(1, 0, 1)
693703
endfunc
694704

695-
func Run_test_pipe_err_to_buffer(use_name, nomod)
705+
func Run_test_pipe_err_to_buffer(use_name, nomod, do_msg)
696706
if !has('job')
697707
return
698708
endif
699709
call ch_log('Test_pipe_err_to_buffer()')
700710
let options = {'err_io': 'buffer'}
711+
let expected = ['', 'line one', 'line two', 'this', 'AND this']
701712
if a:use_name
702713
let options['err_name'] = 'pipe-err'
703-
let firstline = 'Reading from channel error...'
714+
if a:do_msg
715+
let expected[0] = 'Reading from channel error...'
716+
else
717+
let options['err_msg'] = 0
718+
call remove(expected, 0)
719+
endif
704720
else
705721
sp pipe-err
706722
let options['err_buf'] = bufnr('%')
707723
quit
708-
let firstline = ''
724+
call remove(expected, 0)
709725
endif
710726
if a:nomod
711727
let options['err_modifiable'] = 0
@@ -720,7 +736,7 @@ func Run_test_pipe_err_to_buffer(use_name, nomod)
720736
call ch_sendraw(handle, "quit\n")
721737
sp pipe-err
722738
call WaitFor('line("$") >= 5')
723-
call assert_equal([firstline, 'line one', 'line two', 'this', 'AND this'], getline(1, '$'))
739+
call assert_equal(expected, getline(1, '$'))
724740
if a:nomod
725741
call assert_equal(0, &modifiable)
726742
else
@@ -733,15 +749,19 @@ func Run_test_pipe_err_to_buffer(use_name, nomod)
733749
endfunc
734750

735751
func Test_pipe_err_to_buffer_name()
736-
call Run_test_pipe_err_to_buffer(1, 0)
752+
call Run_test_pipe_err_to_buffer(1, 0, 1)
737753
endfunc
738754

739755
func Test_pipe_err_to_buffer_nr()
740-
call Run_test_pipe_err_to_buffer(0, 0)
756+
call Run_test_pipe_err_to_buffer(0, 0, 1)
741757
endfunc
742758

743759
func Test_pipe_err_to_buffer_name_nomod()
744-
call Run_test_pipe_err_to_buffer(1, 1)
760+
call Run_test_pipe_err_to_buffer(1, 1, 1)
761+
endfunc
762+
763+
func Test_pipe_err_to_buffer_name_nomsg()
764+
call Run_test_pipe_err_to_buffer(1, 0, 0)
745765
endfunc
746766

747767
func Test_pipe_both_to_buffer()
@@ -1407,7 +1427,7 @@ func Test_collapse_buffers()
14071427
1,$delete
14081428
call job_start('cat test_channel.vim', {'out_io': 'buffer', 'out_name': 'testout'})
14091429
call WaitFor('line("$") > g:linecount')
1410-
call assert_inrange(g:linecount + 1, g:linecount + 2, line('$'))
1430+
call assert_inrange(g:linecount, g:linecount + 1, line('$'))
14111431
bwipe!
14121432
endfunc
14131433

@@ -1425,9 +1445,9 @@ func Test_raw_passes_nul()
14251445
1,$delete
14261446
call job_start('cat Xtestread', {'out_io': 'buffer', 'out_name': 'testout'})
14271447
call WaitFor('line("$") > 2')
1428-
call assert_equal("asdf\nasdf", getline(2))
1429-
call assert_equal("xxx\n", getline(3))
1430-
call assert_equal("\nyyy", getline(4))
1448+
call assert_equal("asdf\nasdf", getline(1))
1449+
call assert_equal("xxx\n", getline(2))
1450+
call assert_equal("\nyyy", getline(3))
14311451

14321452
call delete('Xtestread')
14331453
bwipe!

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,8 @@ static char *(features[]) =
763763

764764
static int included_patches[] =
765765
{ /* Add new patch number below this line */
766+
/**/
767+
2344,
766768
/**/
767769
2343,
768770
/**/

0 commit comments

Comments
 (0)