patch 9.1.0511: CursorMovedC triggered wrongly with setcmdpos()

Problem:  CursorMovedC triggered wrongly with setcmdpos()
          (after v9.1.0507)
Solution: Remove the premature triggering.  Also don't trigger when
          cursor didn't move. (zeertzjq)

closes: #15064

Signed-off-by: zeertzjq <zeertzjq@outlook.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/testdir/test_autocmd.vim b/src/testdir/test_autocmd.vim
index faa0f97..371abcb 100644
--- a/src/testdir/test_autocmd.vim
+++ b/src/testdir/test_autocmd.vim
@@ -2097,20 +2097,28 @@
   au! CmdlineLeave
   let &shellslash = save_shellslash
 
-  au! CursorMovedC : let g:pos = getcmdpos()
-  let g:pos = 0
-  call feedkeys(":hello\<Left>\<ESC>", 'xt')
-  call assert_equal(5, g:pos)
-  call feedkeys(":12345678\<C-R>=setcmdpos(3)\<CR>\<ESC>", 'xt')
-  call assert_equal(3, g:pos)
+  au! CursorMovedC : let g:pos += [getcmdpos()]
+  let g:pos = []
+  call feedkeys(":hello\<Left>\<C-R>=''\<CR>\<Left>\<Right>\<Esc>", 'xt')
+  call assert_equal([5, 4, 5], g:pos)
+  let g:pos = []
+  call feedkeys(":12345678\<C-R>=setcmdpos(3)??''\<CR>\<Esc>", 'xt')
+  call assert_equal([3], g:pos)
+  let g:pos = []
+  call feedkeys(":12345678\<C-R>=setcmdpos(3)??''\<CR>\<Left>\<Esc>", 'xt')
+  call assert_equal([3, 2], g:pos)
   au! CursorMovedC
 
-  " CursorMovedC changes the cursor position.
-  au! CursorMovedC : let g:pos = getcmdpos() | call setcmdpos(getcmdpos()-1)
-  let g:pos = 0
-  call feedkeys(":hello\<Left>\<ESC>", 'xt')
-  call assert_equal(5, g:pos)
+  " setcmdpos() is no-op inside an autocommand
+  au! CursorMovedC : let g:pos += [getcmdpos()] | call setcmdpos(1)
+  let g:pos = []
+  call feedkeys(":hello\<Left>\<Left>\<Esc>", 'xt')
+  call assert_equal([5, 4], g:pos)
   au! CursorMovedC
+
+  unlet g:entered
+  unlet g:left
+  unlet g:pos
 endfunc
 
 " Test for BufWritePre autocommand that deletes or unloads the buffer.