]> granicus.if.org Git - vim/commitdiff
patch 9.0.0756: no autocmd event for changing text in a terminal window v9.0.0756
authorShougo Matsushita <Shougo.Matsu@gmail.com>
Sat, 15 Oct 2022 10:48:00 +0000 (11:48 +0100)
committerBram Moolenaar <Bram@vim.org>
Sat, 15 Oct 2022 10:48:00 +0000 (11:48 +0100)
Problem:    No autocmd event for changing text in a terminal window.
Solution:   Add TextChangedT. (Shougo Matsushita, closes #11366)

runtime/doc/autocmd.txt
src/autocmd.c
src/terminal.c
src/testdir/test_terminal.vim
src/version.c
src/vim.h

index 61db274bb4775489cdaed8ff1b8c76c5b5e1c41a..5051cc3dc09accb8f631a18568fd193fd520f252 100644 (file)
@@ -409,6 +409,7 @@ Name                        triggered by ~
                        when popup menu is not visible
 |TextChangedP|         after a change was made to the text in Insert mode
                        when popup menu visible
+|TextChangedT|         after a change was made to the text in Terminal mode
 |TextYankPost|         after text has been yanked or deleted
 
 |SafeState|            nothing pending, going to wait for the user to type a
@@ -1237,6 +1238,10 @@ TextChangedP                     After a change was made to the text in the
                                current buffer in Insert mode, only when the
                                popup menu is visible.  Otherwise the same as
                                TextChanged.
+                                                       *TextChangedT*
+TextChangedT                   After a change was made to the text in the
+                               current buffer in Terminal mode.
+                               Otherwise the same as TextChanged.
                                                        *TextYankPost*
 TextYankPost                   After text has been yanked or deleted in the
                                current buffer.  The following values of
index 93da0bbbb199aa97a765495f0dc53a5eab963aa9..cf0f0946a8116c6c17d6c81176eb3a2f962126d4 100644 (file)
@@ -182,6 +182,7 @@ static struct event_name
     {"TextChanged",    EVENT_TEXTCHANGED},
     {"TextChangedI",   EVENT_TEXTCHANGEDI},
     {"TextChangedP",   EVENT_TEXTCHANGEDP},
+    {"TextChangedT",   EVENT_TEXTCHANGEDT},
     {"User",           EVENT_USER},
     {"VimEnter",       EVENT_VIMENTER},
     {"VimLeave",       EVENT_VIMLEAVE},
index ae532dc7219f9c3436c3b8a59d065a789bceefb9..d9f1e0f82162ce54e9549432c20328c5d3685fdc 100644 (file)
@@ -1222,6 +1222,8 @@ update_cursor(term_T *term, int redraw)
        setcursor();
     if (redraw)
     {
+       aco_save_T      aco;
+
        if (term->tl_buffer == curbuf && term->tl_cursor_visible)
            cursor_on();
        out_flush();
@@ -1232,6 +1234,16 @@ update_cursor(term_T *term, int redraw)
            gui_mch_flush();
        }
 #endif
+        // Make sure an invoked autocmd doesn't delete the buffer (and the
+        // terminal) under our fingers.
+       ++term->tl_buffer->b_locked;
+
+       // save and restore curwin and curbuf, in case the autocmd changes them
+       aucmd_prepbuf(&aco, curbuf);
+       apply_autocmds(EVENT_TEXTCHANGEDT, NULL, NULL, FALSE, term->tl_buffer);
+       aucmd_restbuf(&aco);
+
+       --term->tl_buffer->b_locked;
     }
 }
 
index 69faa3dfc48de9000e80fa9a64e39aba0d6a8071..d0ffe3bdbbbdccbfc29da263a445fc706740f2da 100644 (file)
@@ -2321,5 +2321,54 @@ func Test_term_wait_in_close_cb()
   bwipe!
 endfunc
 
+func Test_term_TextChangedT()
+  augroup TermTest
+    autocmd TextChangedT * ++once
+          \ execute expand('<abuf>') . 'buffer' |
+          \ let b:called = 1 |
+          \ split |
+          \ enew
+  augroup END
+
+  terminal
+
+  let term_buf = bufnr()
+
+  let b:called = 0
+
+  call term_sendkeys(term_buf, "aaabbc\r")
+  call TermWait(term_buf)
+
+  call assert_equal(1, getbufvar(term_buf, 'called'))
+
+  " Current buffer will be restored
+  call assert_equal(bufnr(), term_buf)
+
+  bwipe!
+  augroup TermTest
+    au!
+  augroup END
+endfunc
+
+func Test_term_TextChangedT_close()
+  augroup TermTest
+    autocmd TextChangedT * ++once split | enew | 1close!
+  augroup END
+
+  terminal
+
+  let term_buf = bufnr()
+
+  call term_sendkeys(term_buf, "aaabbc\r")
+  call TermWait(term_buf)
+
+  " Current buffer will be restored
+  call assert_equal(bufnr(), term_buf)
+
+  bwipe!
+  augroup TermTest
+    au!
+  augroup END
+endfunc
 
 " vim: shiftwidth=2 sts=2 expandtab
index a917dfdf2de04b8b8eb67e803c2f9bf699e965be..d987d915fc4e30f10f136d87d962c046bda43213 100644 (file)
@@ -695,6 +695,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    756,
 /**/
     755,
 /**/
index 57368f3b402b8e440e4c6e42e52aed58ec131205..5d8c73848da3b31eb5fe90420680666ffbe78138 100644 (file)
--- a/src/vim.h
+++ b/src/vim.h
@@ -1394,6 +1394,7 @@ enum auto_event
     EVENT_TEXTCHANGED,         // text was modified not in Insert mode
     EVENT_TEXTCHANGEDI,                // text was modified in Insert mode
     EVENT_TEXTCHANGEDP,                // TextChangedI with popup menu visible
+    EVENT_TEXTCHANGEDT,                // text was modified in Terminal mode
     EVENT_TEXTYANKPOST,                // after some text was yanked
     EVENT_USER,                        // user defined autocommand
     EVENT_VIMENTER,            // after starting Vim