summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2020-11-26 20:34:00 +0100
committerBram Moolenaar <Bram@vim.org>2020-11-26 20:34:00 +0100
commitfccbf068f8c85474db8d8dead1530321d1f3e5b8 (patch)
tree53f940fd2f9ce11dcf6fb2b688fd1eea604f3320
parentce7be3a0e6f19bc85990bb8fcfe5e208944777b4 (diff)
patch 8.2.2057: getting the selection may trigger TextYankPost autocmdv8.2.2057
Problem: Getting the selection may trigger TextYankPost autocmd. Solution: Only trigger the autocommand when yanking in Vim, not for getting the selection. (closes #7367)
-rw-r--r--src/clipboard.c5
-rw-r--r--src/normal.c32
-rw-r--r--src/register.c3
-rw-r--r--src/testdir/test_autocmd.vim22
-rw-r--r--src/version.c2
5 files changed, 62 insertions, 2 deletions
diff --git a/src/clipboard.c b/src/clipboard.c
index 5db13a0d21..3084ce1871 100644
--- a/src/clipboard.c
+++ b/src/clipboard.c
@@ -2025,6 +2025,9 @@ clip_get_selection(Clipboard_T *cbd)
&& get_y_register(STAR_REGISTER)->y_array != NULL))
return;
+ // Avoid triggering autocmds such as TextYankPost.
+ block_autocmds();
+
// Get the text between clip_star.start & clip_star.end
old_y_previous = get_y_previous();
old_y_current = get_y_current();
@@ -2054,6 +2057,8 @@ clip_get_selection(Clipboard_T *cbd)
curbuf->b_op_end = old_op_end;
VIsual = old_visual;
VIsual_mode = old_visual_mode;
+
+ unblock_autocmds();
}
else if (!is_clipboard_needs_update())
{
diff --git a/src/normal.c b/src/normal.c
index 87f1956fa9..d90326feb2 100644
--- a/src/normal.c
+++ b/src/normal.c
@@ -1325,6 +1325,26 @@ check_visual_highlight(void)
}
}
+#if defined(FEAT_CLIPBOARD) && defined(FEAT_EVAL)
+/*
+ * Call yank_do_autocmd() for "regname".
+ */
+ static void
+call_yank_do_autocmd(int regname)
+{
+ oparg_T oa;
+ yankreg_T *reg;
+
+ clear_oparg(&oa);
+ oa.regname = regname;
+ oa.op_type = OP_YANK;
+ oa.is_VIsual = TRUE;
+ reg = get_register(regname, TRUE);
+ yank_do_autocmd(&oa, reg);
+ free_register(reg);
+}
+#endif
+
/*
* End Visual mode.
* This function should ALWAYS be called to end Visual mode, except from
@@ -1342,6 +1362,18 @@ end_visual_mode(void)
*/
if (clip_star.available && clip_star.owned)
clip_auto_select();
+
+# if defined(FEAT_EVAL)
+ // Emit a TextYankPost for the automatic copy of the selection into the
+ // star and/or plus register.
+ if (has_textyankpost())
+ {
+ if (clip_isautosel_star())
+ call_yank_do_autocmd('*');
+ if (clip_isautosel_plus())
+ call_yank_do_autocmd('+');
+ }
+# endif
#endif
VIsual_active = FALSE;
diff --git a/src/register.c b/src/register.c
index 625150db22..6574432bdb 100644
--- a/src/register.c
+++ b/src/register.c
@@ -322,8 +322,7 @@ put_register(int name, void *reg)
#endif
}
-#if (defined(FEAT_CLIPBOARD) && defined(FEAT_X11) && defined(USE_SYSTEM)) \
- || defined(PROTO)
+#if defined(FEAT_CLIPBOARD) || defined(PROTO)
void
free_register(void *reg)
{
diff --git a/src/testdir/test_autocmd.vim b/src/testdir/test_autocmd.vim
index 890d28ff24..c51f37f0e7 100644
--- a/src/testdir/test_autocmd.vim
+++ b/src/testdir/test_autocmd.vim
@@ -1760,6 +1760,28 @@ func Test_TextYankPost()
call assert_equal({}, v:event)
+ if has('clipboard_working') && !has('gui_running')
+ " Test that when the visual selection is automatically copied to clipboard
+ " register a TextYankPost is emitted
+ call setline(1, ['foobar'])
+
+ let @* = ''
+ set clipboard=autoselect
+ exe "norm! ggviw\<Esc>"
+ call assert_equal(
+ \{'regcontents': ['foobar'], 'regname': '*', 'operator': 'y', 'regtype': 'v', 'visual': v:true},
+ \g:event)
+
+ let @+ = ''
+ set clipboard=autoselectplus
+ exe "norm! ggviw\<Esc>"
+ call assert_equal(
+ \{'regcontents': ['foobar'], 'regname': '+', 'operator': 'y', 'regtype': 'v', 'visual': v:true},
+ \g:event)
+
+ set clipboard&vim
+ endif
+
au! TextYankPost
unlet g:event
bwipe!
diff --git a/src/version.c b/src/version.c
index 0308c91209..cebba1555d 100644
--- a/src/version.c
+++ b/src/version.c
@@ -751,6 +751,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 2057,
+/**/
2056,
/**/
2055,