@@ -85,6 +85,7 @@ static int last_recorded_len = 0; // number of last recorded chars
8585
8686#ifdef FEAT_EVAL
8787mapblock_T * last_used_map = NULL ;
88+ int last_used_sid = -1 ;
8889#endif
8990
9091static int read_readbuf (buffheader_T * buf , int advance );
@@ -837,6 +838,22 @@ start_redo(long count, int old_redo)
837838
838839 c = read_redo (FALSE, old_redo );
839840
841+ #ifdef FEAT_EVAL
842+ if (c == K_SID )
843+ {
844+ // Copy the <SID>{sid}; sequence
845+ add_char_buff (& readbuf2 , c );
846+ for (;;)
847+ {
848+ c = read_redo (FALSE, old_redo );
849+ add_char_buff (& readbuf2 , c );
850+ if (!isdigit (c ))
851+ break ;
852+ }
853+ c = read_redo (FALSE, old_redo );
854+ }
855+ #endif
856+
840857 // copy the buffer name, if present
841858 if (c == '"' )
842859 {
@@ -876,7 +893,7 @@ start_redo(long count, int old_redo)
876893 add_num_buff (& readbuf2 , count );
877894 }
878895
879- // copy from the redo buffer into the stuff buffer
896+ // copy the rest from the redo buffer into the stuff buffer
880897 add_char_buff (& readbuf2 , c );
881898 copy_redo (old_redo );
882899 return OK ;
@@ -1795,8 +1812,22 @@ vgetc(void)
17951812 // avoid it being recognized as the start of a special key.
17961813 if (c == K_CSI )
17971814 c = CSI ;
1815+ #endif
1816+ #ifdef FEAT_EVAL
1817+ if (c == K_SID )
1818+ {
1819+ int j ;
1820+
1821+ // Handle <SID>{sid}; Do up to 20 digits for safety.
1822+ last_used_sid = 0 ;
1823+ for (j = 0 ; j < 20 && isdigit (c = vgetorpeek (TRUE)); ++ j )
1824+ last_used_sid = last_used_sid * 10 + (c - '0' );
1825+ last_used_map = NULL ;
1826+ continue ;
1827+ }
17981828#endif
17991829 }
1830+
18001831 // a keypad or special function key was not mapped, use it like
18011832 // its ASCII equivalent
18021833 switch (c )
@@ -2922,6 +2953,10 @@ handle_mapping(
29222953 {
29232954 int noremap ;
29242955
2956+ #ifdef FEAT_EVAL
2957+ last_used_map = mp ;
2958+ last_used_sid = -1 ;
2959+ #endif
29252960 if (save_m_noremap != REMAP_YES )
29262961 noremap = save_m_noremap ;
29272962 else if (
@@ -2940,7 +2975,6 @@ handle_mapping(
29402975#ifdef FEAT_EVAL
29412976 if (save_m_expr )
29422977 vim_free (map_str );
2943- last_used_map = mp ;
29442978#endif
29452979 }
29462980#ifdef FEAT_EVAL
@@ -3896,17 +3930,48 @@ getcmdkeycmd(
38963930 return (char_u * )line_ga .ga_data ;
38973931}
38983932
3933+ #if defined(FEAT_EVAL ) || defined(PROTO )
3934+ /*
3935+ * If there was a mapping put info about it in the redo buffer, so that "."
3936+ * will use the same script context. We only need the SID.
3937+ */
3938+ void
3939+ may_add_last_used_map_to_redobuff (void )
3940+ {
3941+ char_u buf [3 + 20 ];
3942+
3943+ if (last_used_map == NULL || last_used_map -> m_script_ctx .sc_sid < 0 )
3944+ return ;
3945+
3946+ // <K_SID>{nr};
3947+ buf [0 ] = K_SPECIAL ;
3948+ buf [1 ] = KS_EXTRA ;
3949+ buf [2 ] = KE_SID ;
3950+ vim_snprintf ((char * )buf + 3 , 20 , "%d;" ,
3951+ last_used_map -> m_script_ctx .sc_sid );
3952+ add_buff (& redobuff , buf , -1L );
3953+ }
3954+ #endif
3955+
38993956 int
39003957do_cmdkey_command (int key UNUSED , int flags )
39013958{
39023959 int res ;
39033960#ifdef FEAT_EVAL
39043961 sctx_T save_current_sctx = {-1 , 0 , 0 , 0 };
39053962
3906- if (key == K_SCRIPT_COMMAND && last_used_map != NULL )
3963+ if (key == K_SCRIPT_COMMAND
3964+ && (last_used_map != NULL || SCRIPT_ID_VALID (last_used_sid )))
39073965 {
39083966 save_current_sctx = current_sctx ;
3909- current_sctx = last_used_map -> m_script_ctx ;
3967+ if (last_used_map != NULL )
3968+ current_sctx = last_used_map -> m_script_ctx ;
3969+ else
3970+ {
3971+ current_sctx .sc_sid = last_used_sid ;
3972+ current_sctx .sc_lnum = 0 ;
3973+ current_sctx .sc_version = SCRIPT_ITEM (last_used_sid )-> sn_version ;
3974+ }
39103975 }
39113976#endif
39123977
@@ -3925,6 +3990,9 @@ do_cmdkey_command(int key UNUSED, int flags)
39253990reset_last_used_map (mapblock_T * mp )
39263991{
39273992 if (last_used_map == mp )
3993+ {
39283994 last_used_map = NULL ;
3995+ last_used_sid = -1 ;
3996+ }
39293997}
39303998#endif
0 commit comments