]> granicus.if.org Git - vim/commitdiff
patch 8.2.2612: col('.') may get outdated column value v8.2.2612
authorBram Moolenaar <Bram@vim.org>
Wed, 17 Mar 2021 12:28:05 +0000 (13:28 +0100)
committerBram Moolenaar <Bram@vim.org>
Wed, 17 Mar 2021 12:28:05 +0000 (13:28 +0100)
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)

runtime/doc/map.txt
src/testdir/test_mapping.vim
src/version.c

index bf391305dc2fd5173c55f9e3d0be2bf9bd61e2a4..873d5d6d7a006f8fd4a40116ac75b8d8b83e7fe3 100644 (file)
@@ -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:
index 1750f39d5342c2bf6f0da9fcad2185b841bd44dd..f76718fca1029d7b5744e3c72425079a0796b0e4 100644 (file)
@@ -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
 
index dad20388dfc5fb7c9a10cda113ef56b154c7e93d..815300cbfd4c5c922d9bdf8a9fe553d3518941ff 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2612,
 /**/
     2611,
 /**/