summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2021-03-17 13:28:05 +0100
committerBram Moolenaar <Bram@vim.org>2021-03-17 13:28:05 +0100
commit18b7d86d7fa997bbb02a069dafacb32a0f73ca1e (patch)
tree31c279c6d747651f0bcaae89d24b5ecb2ec8163f
parentf8c52e8d08de3fdf48db877d7d53d2d68c6ceb7b (diff)
downloadvim-git-8.2.2612.tar.gz
patch 8.2.2612: col('.') may get outdated column valuev8.2.2612
Problem: col('.') may get outdated column value. Solution: Add a note to the help how to make this work and add a test for it. (closes #7971)
-rw-r--r--runtime/doc/map.txt14
-rw-r--r--src/testdir/test_mapping.vim24
-rw-r--r--src/version.c2
3 files changed, 40 insertions, 0 deletions
diff --git a/runtime/doc/map.txt b/runtime/doc/map.txt
index bf391305d..873d5d6d7 100644
--- a/runtime/doc/map.txt
+++ b/runtime/doc/map.txt
@@ -263,6 +263,20 @@ input. Example: >
endfunc
nnoremap <expr> <F3> <Sid>OpenPopup()
+Also, keep in mind that the expression may be evaluated when looking for
+typeahead, before the previous command has been executed. For example: >
+ func StoreColumn()
+ let g:column = col('.')
+ return 'x'
+ endfunc
+ nnoremap <expr> x StoreColumn()
+ nmap ! f!x
+You will notice that g:column has the value from before executing "fx",
+because "z" is evaluated before "fx" is executed.
+This can be solved by inserting <Ignore> before the character that is
+expression-mapped: >
+ nmap ! f!<Ignore>x
+
Be very careful about side effects! The expression is evaluated while
obtaining characters, you may very well make the command dysfunctional.
For this reason the following is blocked:
diff --git a/src/testdir/test_mapping.vim b/src/testdir/test_mapping.vim
index 1750f39d5..f76718fca 100644
--- a/src/testdir/test_mapping.vim
+++ b/src/testdir/test_mapping.vim
@@ -485,6 +485,30 @@ func Test_list_mappings()
nmapclear
endfunc
+func Test_expr_map_gets_cursor()
+ new
+ call setline(1, ['one', 'some w!rd'])
+ func StoreColumn()
+ let g:exprLine = line('.')
+ let g:exprCol = col('.')
+ return 'x'
+ endfunc
+ nnoremap <expr> x StoreColumn()
+ 2
+ nmap ! f!<Ignore>x
+ call feedkeys("!", 'xt')
+ call assert_equal('some wrd', getline(2))
+ call assert_equal(2, g:exprLine)
+ call assert_equal(7, g:exprCol)
+
+ bwipe!
+ unlet g:exprLine
+ unlet g:exprCol
+ delfunc ExprMapped
+ nunmap x
+ nunmap !
+endfunc
+
func Test_expr_map_restore_cursor()
CheckScreendump
diff --git a/src/version.c b/src/version.c
index dad20388d..815300cbf 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 */
/**/
+ 2612,
+/**/
2611,
/**/
2610,