#endif
int simplified)
{
- mapblock_T *mp = ALLOC_ONE(mapblock_T);
+ mapblock_T *mp = ALLOC_CLEAR_ONE(mapblock_T);
if (mp == NULL)
return FAIL;
}
if (mp != NULL)
{
+ int noremap;
+ int silent;
+#ifdef FEAT_EVAL
+ int expr;
+#endif
+
// Found a match:
// Insert the rest of the abbreviation in typebuf.tb_buf[].
// This goes from end to start.
// insert the last typed char
(void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
}
+
+ // copy values here, calling eval_map_expr() may make "mp" invalid!
+ noremap = mp->m_noremap;
+ silent = mp->m_silent;
#ifdef FEAT_EVAL
- if (mp->m_expr)
+ expr = mp->m_expr;
+
+ if (expr)
s = eval_map_expr(mp, c);
else
#endif
if (s != NULL)
{
// insert the to string
- (void)ins_typebuf(s, mp->m_noremap, 0, TRUE, mp->m_silent);
+ (void)ins_typebuf(s, noremap, 0, TRUE, silent);
// no abbrev. for these chars
typebuf.tb_no_abbr_cnt += (int)STRLEN(s) + j + 1;
#ifdef FEAT_EVAL
- if (mp->m_expr)
+ if (expr)
vim_free(s);
#endif
}
if (has_mbyte)
len = clen; // Delete characters instead of bytes
while (len-- > 0) // delete the from string
- (void)ins_typebuf(tb, 1, 0, TRUE, mp->m_silent);
+ (void)ins_typebuf(tb, 1, 0, TRUE, silent);
return TRUE;
}
}
/*
* Evaluate the RHS of a mapping or abbreviations and take care of escaping
* special characters.
+ * Careful: after this "mp" will be invalid if the mapping was deleted.
*/
char_u *
eval_map_expr(
mapclear
endfunc
+func GetAbbrText()
+ unabbr hola
+ return 'hello'
+endfunc
+
" Test for <expr> in abbreviation
func Test_expr_abbr()
new
call assert_equal('', getline(1))
unabbr <expr> hte
- close!
+ " evaluating the expression deletes the abbreviation
+ abbr <expr> hola GetAbbrText()
+ call assert_equal('GetAbbrText()', maparg('hola', 'i', '1'))
+ call feedkeys("ahola \<Esc>", 'xt')
+ call assert_equal('hello ', getline('.'))
+ call assert_equal('', maparg('hola', 'i', '1'))
+
+ bwipe!
endfunc
" Test for storing mappings in different modes in a vimrc file