Skip to content

Commit f3e738a

Browse files
committed
updated for version 7.3.196
Problem: Can't intercept a character that is going to be inserted. Solution: Add the InsertCharPre autocommand event. (Jakson A. Aquino)
1 parent 091f96e commit f3e738a

File tree

8 files changed

+57
-8
lines changed

8 files changed

+57
-8
lines changed

runtime/doc/autocmd.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ Name triggered by ~
299299
|InsertEnter| starting Insert mode
300300
|InsertChange| when typing <Insert> while in Insert or Replace mode
301301
|InsertLeave| when leaving Insert mode
302+
|InsertCharPre| when a character was typed in Insert mode, before
303+
inserting it
302304

303305
|ColorScheme| after loading a color scheme
304306

@@ -657,6 +659,17 @@ InsertChange When typing <Insert> while in Insert or
657659
indicates the new mode.
658660
Be careful not to move the cursor or do
659661
anything else that the user does not expect.
662+
*InsertCharPre*
663+
InsertCharPre When a character is typed in Insert mode,
664+
before inserting the char.
665+
The |v:char| variable indicates the char typed
666+
and can be changed during the event to insert
667+
a different character. When |v:char| is set
668+
to more than one character this text is
669+
inserted literally.
670+
It is not allowed to change the text |textlock|.
671+
The event is not triggered when 'paste' is
672+
set.
660673
*InsertEnter*
661674
InsertEnter Just before starting Insert mode. Also for
662675
Replace mode and Virtual Replace mode. The

runtime/doc/eval.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,7 @@ v:beval_winnr The number of the window, over which the mouse pointer is. Only
12931293
*v:char* *char-variable*
12941294
v:char Argument for evaluating 'formatexpr' and used for the typed
12951295
character when using <expr> in an abbreviation |:map-<expr>|.
1296+
It is also used by the |InsertPreChar| event.
12961297

12971298
*v:charconvert_from* *charconvert_from-variable*
12981299
v:charconvert_from

runtime/doc/map.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ text before the cursor and start omni completion when some condition is met.
226226

227227
For abbreviations |v:char| is set to the character that was typed to trigger
228228
the abbreviation. You can use this to decide how to expand the {lhs}. You
229-
can't change v:char and you should not insert it.
229+
you should not either insert or change the v:char.
230230

231231
Be very careful about side effects! The expression is evaluated while
232232
obtaining characters, you may very well make the command dysfunctional.

src/edit.c

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,10 +1381,45 @@ edit(cmdchar, startln, count)
13811381
goto do_intr;
13821382
#endif
13831383

1384+
normalchar:
13841385
/*
13851386
* Insert a nomal character.
13861387
*/
1387-
normalchar:
1388+
#ifdef FEAT_AUTOCMD
1389+
if (!p_paste)
1390+
{
1391+
/* Trigger the InsertCharPre event. Lock the text to avoid
1392+
* weird things from happening. */
1393+
set_vim_var_char(c);
1394+
++textlock;
1395+
if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL,
1396+
FALSE, curbuf))
1397+
{
1398+
/* Get the new value of v:char. If it is more than one
1399+
* character insert it literally. */
1400+
char_u *s = get_vim_var_str(VV_CHAR);
1401+
if (MB_CHARLEN(s) > 1)
1402+
{
1403+
if (stop_arrow() != FAIL)
1404+
{
1405+
ins_str(s);
1406+
AppendToRedobuffLit(s, -1);
1407+
}
1408+
c = NUL;
1409+
}
1410+
else
1411+
c = PTR2CHAR(s);
1412+
}
1413+
1414+
set_vim_var_string(VV_CHAR, NULL, -1);
1415+
--textlock;
1416+
1417+
/* If the new value is an empty string then don't insert a
1418+
* char. */
1419+
if (c == NUL)
1420+
break;
1421+
}
1422+
#endif
13881423
#ifdef FEAT_SMARTINDENT
13891424
/* Try to perform smart-indenting. */
13901425
ins_try_si(c);
@@ -3491,11 +3526,7 @@ ins_compl_addfrommatch()
34913526
return;
34923527
}
34933528
p += len;
3494-
#ifdef FEAT_MBYTE
3495-
c = mb_ptr2char(p);
3496-
#else
3497-
c = *p;
3498-
#endif
3529+
c = PTR2CHAR(p);
34993530
ins_compl_addleader(c);
35003531
}
35013532

src/eval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ static struct vimvar
352352
{VV_NAME("swapname", VAR_STRING), VV_RO},
353353
{VV_NAME("swapchoice", VAR_STRING), 0},
354354
{VV_NAME("swapcommand", VAR_STRING), VV_RO},
355-
{VV_NAME("char", VAR_STRING), VV_RO},
355+
{VV_NAME("char", VAR_STRING), 0},
356356
{VV_NAME("mouse_win", VAR_NUMBER), 0},
357357
{VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358358
{VV_NAME("mouse_col", VAR_NUMBER), 0},

src/fileio.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7662,6 +7662,7 @@ static struct event_name
76627662
{"InsertChange", EVENT_INSERTCHANGE},
76637663
{"InsertEnter", EVENT_INSERTENTER},
76647664
{"InsertLeave", EVENT_INSERTLEAVE},
7665+
{"InsertCharPre", EVENT_INSERTCHARPRE},
76657666
{"MenuPopup", EVENT_MENUPOPUP},
76667667
{"QuickFixCmdPost", EVENT_QUICKFIXCMDPOST},
76677668
{"QuickFixCmdPre", EVENT_QUICKFIXCMDPRE},

src/version.c

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

710710
static int included_patches[] =
711711
{ /* Add new patch number below this line */
712+
/**/
713+
196,
712714
/**/
713715
195,
714716
/**/

src/vim.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,7 @@ enum auto_event
12741274
EVENT_WINENTER, /* after entering a window */
12751275
EVENT_WINLEAVE, /* before leaving a window */
12761276
EVENT_ENCODINGCHANGED, /* after changing the 'encoding' option */
1277+
EVENT_INSERTCHARPRE, /* before inserting a char */
12771278
EVENT_CURSORHOLD, /* cursor in same position for a while */
12781279
EVENT_CURSORHOLDI, /* idem, in Insert mode */
12791280
EVENT_FUNCUNDEFINED, /* if calling a function which doesn't exist */

0 commit comments

Comments
 (0)