diff options
author | Jan Edmund Lazo <jan.lazo@mail.utoronto.ca> | 2021-06-10 22:41:01 -0400 |
---|---|---|
committer | Jan Edmund Lazo <jan.lazo@mail.utoronto.ca> | 2021-06-10 22:58:15 -0400 |
commit | 8ebbeee1d0fe8c1fa67a95324ba98fe6a9794519 (patch) | |
tree | d4550a33f8065cd6be13ddc1f8269b4ff19e12ec /src | |
parent | d662dfde363b80ce8fc446ab7d8299cbd626f074 (diff) | |
download | rneovim-8ebbeee1d0fe8c1fa67a95324ba98fe6a9794519.tar.gz rneovim-8ebbeee1d0fe8c1fa67a95324ba98fe6a9794519.tar.bz2 rneovim-8ebbeee1d0fe8c1fa67a95324ba98fe6a9794519.zip |
vim-patch:8.2.2971: cannot yank a block without trailing spaces
Problem: Cannot yank a block without trailing spaces.
Solution: Add the "zy" command. (Christian Brabandt, closes vim/vim#8292)
https://github.com/vim/vim/commit/544a38e44db0f25ec4fa7a2a4666cf28a2336f33
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/normal.c | 4 | ||||
-rw-r--r-- | src/nvim/normal.h | 2 | ||||
-rw-r--r-- | src/nvim/ops.c | 21 | ||||
-rw-r--r-- | src/nvim/testdir/test_visual.vim | 55 |
4 files changed, 79 insertions, 3 deletions
diff --git a/src/nvim/normal.c b/src/nvim/normal.c index a674560f08..e8ab8da744 100644 --- a/src/nvim/normal.c +++ b/src/nvim/normal.c @@ -1870,6 +1870,7 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank) } } else { curwin->w_p_lbr = lbr_saved; + oap->excl_tr_ws = cap->cmdchar == 'z'; (void)op_yank(oap, !gui_yank, false); } check_cursor_col(); @@ -4393,6 +4394,9 @@ dozet: case 'p': nv_put(cap); break; + // "zy" Yank without trailing spaces + case 'y': nv_operator(cap); + break; /* "zF": create fold command */ /* "zf": create fold operator */ diff --git a/src/nvim/normal.h b/src/nvim/normal.h index 51170105ed..8e15e909d4 100644 --- a/src/nvim/normal.h +++ b/src/nvim/normal.h @@ -48,6 +48,8 @@ typedef struct oparg_S { colnr_T end_vcol; // end col for block mode operator long prev_opcount; // ca.opcount saved for K_EVENT long prev_count0; // ca.count0 saved for K_EVENT + bool excl_tr_ws; // exclude trailing whitespace for yank of a + // block } oparg_T; /* diff --git a/src/nvim/ops.c b/src/nvim/ops.c index 2c8c7f0567..f2f6803665 100644 --- a/src/nvim/ops.c +++ b/src/nvim/ops.c @@ -156,6 +156,9 @@ int get_op_type(int char1, int char2) // subtract return OP_NR_SUB; } + if (char1 == 'z' && char2 == 'y') { // OP_YANK + return OP_YANK; + } for (i = 0;; i++) { if (opchars[i][0] == char1 && opchars[i][1] == char2) { break; @@ -2563,7 +2566,7 @@ static void op_yank_reg(oparg_T *oap, bool message, yankreg_T *reg, bool append) switch (reg->y_type) { case kMTBlockWise: block_prep(oap, &bd, lnum, false); - yank_copy_line(reg, &bd, y_idx); + yank_copy_line(reg, &bd, y_idx, oap->excl_tr_ws); break; case kMTLineWise: @@ -2627,7 +2630,7 @@ static void op_yank_reg(oparg_T *oap, bool message, yankreg_T *reg, bool append) bd.textlen = endcol - startcol + oap->inclusive; } bd.textstart = p + startcol; - yank_copy_line(reg, &bd, y_idx); + yank_copy_line(reg, &bd, y_idx, false); break; } // NOTREACHED @@ -2714,7 +2717,11 @@ static void op_yank_reg(oparg_T *oap, bool message, yankreg_T *reg, bool append) return; } -static void yank_copy_line(yankreg_T *reg, struct block_def *bd, size_t y_idx) +// Copy a block range into a register. +// If "exclude_trailing_space" is set, do not copy trailing whitespaces. +static void yank_copy_line(yankreg_T *reg, const struct block_def *bd, + size_t y_idx, bool exclude_trailing_space) + FUNC_ATTR_NONNULL_ALL { int size = bd->startspaces + bd->endspaces + bd->textlen; assert(size >= 0); @@ -2726,6 +2733,14 @@ static void yank_copy_line(yankreg_T *reg, struct block_def *bd, size_t y_idx) pnew += bd->textlen; memset(pnew, ' ', (size_t)bd->endspaces); pnew += bd->endspaces; + if (exclude_trailing_space) { + int s = bd->textlen + bd->endspaces; + + while (ascii_iswhite(*(bd->textstart + s - 1)) && s > 0) { + s = s - utf_head_off(bd->textstart, bd->textstart + s - 1) - 1; + pnew--; + } + } *pnew = NUL; } diff --git a/src/nvim/testdir/test_visual.vim b/src/nvim/testdir/test_visual.vim index 21fd57b791..a40b0236e0 100644 --- a/src/nvim/testdir/test_visual.vim +++ b/src/nvim/testdir/test_visual.vim @@ -1006,4 +1006,59 @@ func Test_visual_put_in_block_using_zp() bwipe! endfunc +func Test_visual_put_in_block_using_zy_and_zp() + new + + " Test 1) Paste using zp - after the cursor without trailing spaces + call setline(1, ['/path;text', '/path;text', '/path;text', '', + \ 'texttext /subdir columntext', + \ 'texttext /longsubdir columntext', + \ 'texttext /longlongsubdir columntext']) + exe "normal! 5G0f/\<c-v>2jezy" + norm! 1G0f;hzp + call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3)) + + " Test 2) Paste using zP - in front of the cursor without trailing spaces + %d + call setline(1, ['/path;text', '/path;text', '/path;text', '', + \ 'texttext /subdir columntext', + \ 'texttext /longsubdir columntext', + \ 'texttext /longlongsubdir columntext']) + exe "normal! 5G0f/\<c-v>2jezy" + norm! 1G0f;zP + call assert_equal(['/path/subdir;text', '/path/longsubdir;text', '/path/longlongsubdir;text'], getline(1, 3)) + + " Test 3) Paste using p - with trailing spaces + %d + call setline(1, ['/path;text', '/path;text', '/path;text', '', + \ 'texttext /subdir columntext', + \ 'texttext /longsubdir columntext', + \ 'texttext /longlongsubdir columntext']) + exe "normal! 5G0f/\<c-v>2jezy" + norm! 1G0f;hp + call assert_equal(['/path/subdir ;text', '/path/longsubdir ;text', '/path/longlongsubdir;text'], getline(1, 3)) + + " Test 4) Paste using P - with trailing spaces + %d + call setline(1, ['/path;text', '/path;text', '/path;text', '', + \ 'texttext /subdir columntext', + \ 'texttext /longsubdir columntext', + \ 'texttext /longlongsubdir columntext']) + exe "normal! 5G0f/\<c-v>2jezy" + norm! 1G0f;P + call assert_equal(['/path/subdir ;text', '/path/longsubdir ;text', '/path/longlongsubdir;text'], getline(1, 3)) + + " Test 5) Yank with spaces inside the block + %d + call setline(1, ['/path;text', '/path;text', '/path;text', '', + \ 'texttext /sub dir/ columntext', + \ 'texttext /lon gsubdir/ columntext', + \ 'texttext /lon glongsubdir/ columntext']) + exe "normal! 5G0f/\<c-v>2jf/zy" + norm! 1G0f;zP + call assert_equal(['/path/sub dir/;text', '/path/lon gsubdir/;text', '/path/lon glongsubdir/;text'], getline(1, 3)) + bwipe! +endfunc + + " vim: shiftwidth=2 sts=2 expandtab |