I have a function that is called via a mapping when I am in insert mode:
inoremap <buffer> <M-i> <ESC>:call TheFunc()<CR>
The function is supposed to "calculate" a string (l:text
) and to insert it at the current cursor position:
fu! TheFunc()
if g:FOO = 1
let l:text = XYZ()
elseif g:FOO = g:BAR
let l:text = ABC()
... etc etc
endif
execute 'normal a' . l:text
execute 'normal l'
startinsert
endfu
I use execute 'normal l'
so that after calling the function, further typed text is inserted after the text inserted by the function.
This works fine, except when the cursor is on the end of a line. In this case, the normal l
cannot move the cursor one to the right, and the subsequent startinsert
inserts one character to early.
I can circumenvent this problem by replacing the startinsert
command with this construct
if col('.') + 1 == col('$')
startinsert!
else
execute 'normal l'
startinsert
end
This works as I need it.
Yet, I feel there should be a more elegant way to achieve what I want. Is there?