Skip to content

Commit 7f51bbe

Browse files
committed
patch 8.2.0148: mapping related function in wrong source file
Problem: Mapping related function in wrong source file. Solution: Move the function. Add a few more test cases. (Yegappan Lakshmanan, closes #5528)
1 parent 03c3bd9 commit 7f51bbe

5 files changed

Lines changed: 118 additions & 76 deletions

File tree

src/map.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,81 @@ static int expand_mapmodes = 0;
10561056
static int expand_isabbrev = 0;
10571057
static int expand_buffer = FALSE;
10581058

1059+
/*
1060+
* Translate an internal mapping/abbreviation representation into the
1061+
* corresponding external one recognized by :map/:abbrev commands.
1062+
* Respects the current B/k/< settings of 'cpoption'.
1063+
*
1064+
* This function is called when expanding mappings/abbreviations on the
1065+
* command-line.
1066+
*
1067+
* It uses a growarray to build the translation string since the latter can be
1068+
* wider than the original description. The caller has to free the string
1069+
* afterwards.
1070+
*
1071+
* Returns NULL when there is a problem.
1072+
*/
1073+
static char_u *
1074+
translate_mapping(char_u *str)
1075+
{
1076+
garray_T ga;
1077+
int c;
1078+
int modifiers;
1079+
int cpo_bslash;
1080+
int cpo_special;
1081+
1082+
ga_init(&ga);
1083+
ga.ga_itemsize = 1;
1084+
ga.ga_growsize = 40;
1085+
1086+
cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
1087+
cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
1088+
1089+
for (; *str; ++str)
1090+
{
1091+
c = *str;
1092+
if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1093+
{
1094+
modifiers = 0;
1095+
if (str[1] == KS_MODIFIER)
1096+
{
1097+
str++;
1098+
modifiers = *++str;
1099+
c = *++str;
1100+
}
1101+
if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
1102+
{
1103+
if (cpo_special)
1104+
{
1105+
ga_clear(&ga);
1106+
return NULL;
1107+
}
1108+
c = TO_SPECIAL(str[1], str[2]);
1109+
if (c == K_ZERO) // display <Nul> as ^@
1110+
c = NUL;
1111+
str += 2;
1112+
}
1113+
if (IS_SPECIAL(c) || modifiers) // special key
1114+
{
1115+
if (cpo_special)
1116+
{
1117+
ga_clear(&ga);
1118+
return NULL;
1119+
}
1120+
ga_concat(&ga, get_special_key_name(c, modifiers));
1121+
continue; // for (str)
1122+
}
1123+
}
1124+
if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
1125+
|| (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
1126+
ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
1127+
if (c)
1128+
ga_append(&ga, c);
1129+
}
1130+
ga_append(&ga, NUL);
1131+
return (char_u *)(ga.ga_data);
1132+
}
1133+
10591134
/*
10601135
* Work out what to complete when doing command line completion of mapping
10611136
* or abbreviation names.

src/proto/term.pro

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ void term_get_bg_color(char_u *r, char_u *g, char_u *b);
7272
char_u *replace_termcodes(char_u *from, char_u **bufp, int flags, int *did_simplify);
7373
void show_termcodes(void);
7474
int show_one_termcode(char_u *name, char_u *code, int printit);
75-
char_u *translate_mapping(char_u *str);
7675
void update_tcap(int attr);
7776
void swap_tcap(void);
7877
guicolor_T gui_get_color_cmn(char_u *name);

src/term.c

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5936,81 +5936,6 @@ check_for_codes_from_term(void)
59365936
}
59375937
#endif
59385938

5939-
/*
5940-
* Translate an internal mapping/abbreviation representation into the
5941-
* corresponding external one recognized by :map/:abbrev commands.
5942-
* Respects the current B/k/< settings of 'cpoption'.
5943-
*
5944-
* This function is called when expanding mappings/abbreviations on the
5945-
* command-line.
5946-
*
5947-
* It uses a growarray to build the translation string since the latter can be
5948-
* wider than the original description. The caller has to free the string
5949-
* afterwards.
5950-
*
5951-
* Returns NULL when there is a problem.
5952-
*/
5953-
char_u *
5954-
translate_mapping(char_u *str)
5955-
{
5956-
garray_T ga;
5957-
int c;
5958-
int modifiers;
5959-
int cpo_bslash;
5960-
int cpo_special;
5961-
5962-
ga_init(&ga);
5963-
ga.ga_itemsize = 1;
5964-
ga.ga_growsize = 40;
5965-
5966-
cpo_bslash = (vim_strchr(p_cpo, CPO_BSLASH) != NULL);
5967-
cpo_special = (vim_strchr(p_cpo, CPO_SPECI) != NULL);
5968-
5969-
for (; *str; ++str)
5970-
{
5971-
c = *str;
5972-
if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
5973-
{
5974-
modifiers = 0;
5975-
if (str[1] == KS_MODIFIER)
5976-
{
5977-
str++;
5978-
modifiers = *++str;
5979-
c = *++str;
5980-
}
5981-
if (c == K_SPECIAL && str[1] != NUL && str[2] != NUL)
5982-
{
5983-
if (cpo_special)
5984-
{
5985-
ga_clear(&ga);
5986-
return NULL;
5987-
}
5988-
c = TO_SPECIAL(str[1], str[2]);
5989-
if (c == K_ZERO) // display <Nul> as ^@
5990-
c = NUL;
5991-
str += 2;
5992-
}
5993-
if (IS_SPECIAL(c) || modifiers) // special key
5994-
{
5995-
if (cpo_special)
5996-
{
5997-
ga_clear(&ga);
5998-
return NULL;
5999-
}
6000-
ga_concat(&ga, get_special_key_name(c, modifiers));
6001-
continue; // for (str)
6002-
}
6003-
}
6004-
if (c == ' ' || c == '\t' || c == Ctrl_J || c == Ctrl_V
6005-
|| (c == '<' && !cpo_special) || (c == '\\' && !cpo_bslash))
6006-
ga_append(&ga, cpo_bslash ? Ctrl_V : '\\');
6007-
if (c)
6008-
ga_append(&ga, c);
6009-
}
6010-
ga_append(&ga, NUL);
6011-
return (char_u *)(ga.ga_data);
6012-
}
6013-
60145939
#if (defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
60155940
static char ksme_str[20];
60165941
static char ksmr_str[20];

src/testdir/test_mapping.vim

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,15 @@ func Test_list_mappings()
476476
call assert_equal(['n ,n <Nop>'],
477477
\ execute('nmap ,n')->trim()->split("\n"))
478478

479+
" verbose map
480+
call assert_match("\tLast set from .*/test_mapping.vim line \\d\\+$",
481+
\ execute('verbose map ,n')->trim()->split("\n")[1])
482+
483+
" map to CTRL-V
484+
exe "nmap ,k \<C-V>"
485+
call assert_equal(['n ,k <Nop>'],
486+
\ execute('nmap ,k')->trim()->split("\n"))
487+
479488
nmapclear
480489
endfunc
481490

@@ -812,4 +821,36 @@ func Test_abbr_remove()
812821
call assert_equal({}, maparg('foo', 'i', 1, 1))
813822
endfunc
814823

824+
" Trigger an abbreviation using a special key
825+
func Test_abbr_trigger_special()
826+
new
827+
iabbr teh the
828+
call feedkeys("iteh\<F2>\<Esc>", 'xt')
829+
call assert_equal('the<F2>', getline(1))
830+
iunab teh
831+
close!
832+
endfunc
833+
834+
" Test for '<' in 'cpoptions'
835+
func Test_map_cpo_special_keycode()
836+
set cpo-=<
837+
imap x<Bslash>k Test
838+
let d = maparg('x<Bslash>k', 'i', 0, 1)
839+
call assert_equal(['x\k', 'Test', 'i'], [d.lhs, d.rhs, d.mode])
840+
call feedkeys(":imap x\<C-A>\<C-B>\"\<CR>", 'tx')
841+
call assert_equal('"imap x\k', @:)
842+
iunmap x<Bslash>k
843+
set cpo+=<
844+
imap x<Bslash>k Test
845+
let d = maparg('x<Bslash>k', 'i', 0, 1)
846+
call assert_equal(['x<Bslash>k', 'Test', 'i'], [d.lhs, d.rhs, d.mode])
847+
call feedkeys(":imap x\<C-A>\<C-B>\"\<CR>", 'tx')
848+
call assert_equal('"imap x<Bslash>k', @:)
849+
iunmap x<Bslash>k
850+
set cpo-=<
851+
" Modifying 'cpo' above adds some default mappings, remove them
852+
mapclear
853+
mapclear!
854+
endfunc
855+
815856
" vim: shiftwidth=2 sts=2 expandtab

src/version.c

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

743743
static int included_patches[] =
744744
{ /* Add new patch number below this line */
745+
/**/
746+
148,
745747
/**/
746748
147,
747749
/**/

0 commit comments

Comments
 (0)