Skip to content

Commit 18b7d86

Browse files
committed
patch 8.2.2612: col('.') may get outdated column value
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)
1 parent f8c52e8 commit 18b7d86

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

runtime/doc/map.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,20 @@ input. Example: >
263263
endfunc
264264
nnoremap <expr> <F3> <Sid>OpenPopup()
265265
266+
Also, keep in mind that the expression may be evaluated when looking for
267+
typeahead, before the previous command has been executed. For example: >
268+
func StoreColumn()
269+
let g:column = col('.')
270+
return 'x'
271+
endfunc
272+
nnoremap <expr> x StoreColumn()
273+
nmap ! f!x
274+
You will notice that g:column has the value from before executing "fx",
275+
because "z" is evaluated before "fx" is executed.
276+
This can be solved by inserting <Ignore> before the character that is
277+
expression-mapped: >
278+
nmap ! f!<Ignore>x
279+
266280
Be very careful about side effects! The expression is evaluated while
267281
obtaining characters, you may very well make the command dysfunctional.
268282
For this reason the following is blocked:

src/testdir/test_mapping.vim

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,30 @@ func Test_list_mappings()
485485
nmapclear
486486
endfunc
487487

488+
func Test_expr_map_gets_cursor()
489+
new
490+
call setline(1, ['one', 'some w!rd'])
491+
func StoreColumn()
492+
let g:exprLine = line('.')
493+
let g:exprCol = col('.')
494+
return 'x'
495+
endfunc
496+
nnoremap <expr> x StoreColumn()
497+
2
498+
nmap ! f!<Ignore>x
499+
call feedkeys("!", 'xt')
500+
call assert_equal('some wrd', getline(2))
501+
call assert_equal(2, g:exprLine)
502+
call assert_equal(7, g:exprCol)
503+
504+
bwipe!
505+
unlet g:exprLine
506+
unlet g:exprCol
507+
delfunc ExprMapped
508+
nunmap x
509+
nunmap !
510+
endfunc
511+
488512
func Test_expr_map_restore_cursor()
489513
CheckScreendump
490514

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ static char *(features[]) =
750750

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2612,
753755
/**/
754756
2611,
755757
/**/

0 commit comments

Comments
 (0)