comparison src/map.c @ 20522:729853a754ea v8.2.0815

patch 8.2.0815: maparg() does not provide enough information for mapset() Commit: https://github.com/vim/vim/commit/9c65253fe702ea010afec11aa971acd542c35de2 Author: Bram Moolenaar <Bram@vim.org> Date: Sun May 24 13:10:18 2020 +0200 patch 8.2.0815: maparg() does not provide enough information for mapset() Problem: maparg() does not provide enough information for mapset(). Solution: Add "lhsraw" and "lhsrawalt" items. Drop "simplified"
author Bram Moolenaar <Bram@vim.org>
date Sun, 24 May 2020 13:15:04 +0200
parents d9e3fdf26cb9
children 1f08c49d8cfe
comparison
equal deleted inserted replaced
20521:fb1d6f728a72 20522:729853a754ea
2174 2174
2175 void 2175 void
2176 get_maparg(typval_T *argvars, typval_T *rettv, int exact) 2176 get_maparg(typval_T *argvars, typval_T *rettv, int exact)
2177 { 2177 {
2178 char_u *keys; 2178 char_u *keys;
2179 char_u *keys_simplified;
2179 char_u *which; 2180 char_u *which;
2180 char_u buf[NUMBUFLEN]; 2181 char_u buf[NUMBUFLEN];
2181 char_u *keys_buf = NULL; 2182 char_u *keys_buf = NULL;
2183 char_u *alt_keys_buf = NULL;
2184 int did_simplify = FALSE;
2182 char_u *rhs; 2185 char_u *rhs;
2183 int mode; 2186 int mode;
2184 int abbr = FALSE; 2187 int abbr = FALSE;
2185 int get_dict = FALSE; 2188 int get_dict = FALSE;
2186 mapblock_T *mp; 2189 mapblock_T *mp;
2190 mapblock_T *mp_simplified;
2187 int buffer_local; 2191 int buffer_local;
2192 int flags = REPTERM_FROM_PART | REPTERM_DO_LT;
2188 2193
2189 // return empty string for failure 2194 // return empty string for failure
2190 rettv->v_type = VAR_STRING; 2195 rettv->v_type = VAR_STRING;
2191 rettv->vval.v_string = NULL; 2196 rettv->vval.v_string = NULL;
2192 2197
2209 if (which == NULL) 2214 if (which == NULL)
2210 return; 2215 return;
2211 2216
2212 mode = get_map_mode(&which, 0); 2217 mode = get_map_mode(&which, 0);
2213 2218
2214 keys = replace_termcodes(keys, &keys_buf, 2219 keys_simplified = replace_termcodes(keys, &keys_buf, flags, &did_simplify);
2215 REPTERM_FROM_PART | REPTERM_DO_LT, NULL); 2220 rhs = check_map(keys_simplified, mode, exact, FALSE, abbr,
2216 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local); 2221 &mp, &buffer_local);
2217 vim_free(keys_buf); 2222 if (did_simplify)
2223 {
2224 // When the lhs is being simplified the not-simplified keys are
2225 // preferred for priting, like in do_map().
2226 // The "rhs" and "buffer_local" values are not expected to change.
2227 mp_simplified = mp;
2228 (void)replace_termcodes(keys, &alt_keys_buf,
2229 flags | REPTERM_NO_SIMPLIFY, NULL);
2230 rhs = check_map(alt_keys_buf, mode, exact, FALSE, abbr, &mp,
2231 &buffer_local);
2232 }
2218 2233
2219 if (!get_dict) 2234 if (!get_dict)
2220 { 2235 {
2221 // Return a string. 2236 // Return a string.
2222 if (rhs != NULL) 2237 if (rhs != NULL)
2234 char_u *lhs = str2special_save(mp->m_keys, TRUE); 2249 char_u *lhs = str2special_save(mp->m_keys, TRUE);
2235 char_u *mapmode = map_mode_to_chars(mp->m_mode); 2250 char_u *mapmode = map_mode_to_chars(mp->m_mode);
2236 dict_T *dict = rettv->vval.v_dict; 2251 dict_T *dict = rettv->vval.v_dict;
2237 2252
2238 dict_add_string(dict, "lhs", lhs); 2253 dict_add_string(dict, "lhs", lhs);
2254 vim_free(lhs);
2255 dict_add_string(dict, "lhsraw", mp->m_keys);
2256 if (did_simplify)
2257 // Also add the value for the simplified entry.
2258 dict_add_string(dict, "lhsrawalt", mp_simplified->m_keys);
2239 dict_add_string(dict, "rhs", mp->m_orig_str); 2259 dict_add_string(dict, "rhs", mp->m_orig_str);
2240 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L); 2260 dict_add_number(dict, "noremap", mp->m_noremap ? 1L : 0L);
2241 dict_add_number(dict, "script", mp->m_noremap == REMAP_SCRIPT 2261 dict_add_number(dict, "script", mp->m_noremap == REMAP_SCRIPT
2242 ? 1L : 0L); 2262 ? 1L : 0L);
2243 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L); 2263 dict_add_number(dict, "expr", mp->m_expr ? 1L : 0L);
2245 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid); 2265 dict_add_number(dict, "sid", (long)mp->m_script_ctx.sc_sid);
2246 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum); 2266 dict_add_number(dict, "lnum", (long)mp->m_script_ctx.sc_lnum);
2247 dict_add_number(dict, "buffer", (long)buffer_local); 2267 dict_add_number(dict, "buffer", (long)buffer_local);
2248 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L); 2268 dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
2249 dict_add_string(dict, "mode", mapmode); 2269 dict_add_string(dict, "mode", mapmode);
2250 dict_add_number(dict, "simplified", mp->m_simplified); 2270
2251
2252 vim_free(lhs);
2253 vim_free(mapmode); 2271 vim_free(mapmode);
2254 } 2272 }
2273
2274 vim_free(keys_buf);
2275 vim_free(alt_keys_buf);
2255 } 2276 }
2256 2277
2257 /* 2278 /*
2258 * "mapset()" function 2279 * "mapset()" function
2259 */ 2280 */
2260 void 2281 void
2261 f_mapset(typval_T *argvars, typval_T *rettv UNUSED) 2282 f_mapset(typval_T *argvars, typval_T *rettv UNUSED)
2262 { 2283 {
2263 char_u *keys;
2264 char_u *keys_buf = NULL; 2284 char_u *keys_buf = NULL;
2265 char_u *which; 2285 char_u *which;
2266 int mode; 2286 int mode;
2267 char_u buf[NUMBUFLEN]; 2287 char_u buf[NUMBUFLEN];
2268 int is_abbr; 2288 int is_abbr;
2269 dict_T *d; 2289 dict_T *d;
2270 char_u *lhs; 2290 char_u *lhs;
2291 char_u *lhsraw;
2292 char_u *lhsrawalt;
2271 char_u *rhs; 2293 char_u *rhs;
2272 char_u *orig_rhs; 2294 char_u *orig_rhs;
2273 char_u *arg_buf = NULL; 2295 char_u *arg_buf = NULL;
2274 int noremap; 2296 int noremap;
2275 int expr; 2297 int expr;
2277 scid_T sid; 2299 scid_T sid;
2278 linenr_T lnum; 2300 linenr_T lnum;
2279 mapblock_T **map_table = maphash; 2301 mapblock_T **map_table = maphash;
2280 mapblock_T **abbr_table = &first_abbr; 2302 mapblock_T **abbr_table = &first_abbr;
2281 int nowait; 2303 int nowait;
2282 int simplified;
2283 char_u *arg; 2304 char_u *arg;
2284 2305
2285 which = tv_get_string_buf_chk(&argvars[0], buf); 2306 which = tv_get_string_buf_chk(&argvars[0], buf);
2286 mode = get_map_mode(&which, 0); 2307 mode = get_map_mode(&which, 0);
2287 is_abbr = (int)tv_get_number(&argvars[1]); 2308 is_abbr = (int)tv_get_number(&argvars[1]);
2293 } 2314 }
2294 d = argvars[2].vval.v_dict; 2315 d = argvars[2].vval.v_dict;
2295 2316
2296 // Get the values in the same order as above in get_maparg(). 2317 // Get the values in the same order as above in get_maparg().
2297 lhs = dict_get_string(d, (char_u *)"lhs", FALSE); 2318 lhs = dict_get_string(d, (char_u *)"lhs", FALSE);
2298 if (lhs == NULL) 2319 lhsraw = dict_get_string(d, (char_u *)"lhsraw", FALSE);
2299 { 2320 lhsrawalt = dict_get_string(d, (char_u *)"lhsrawalt", FALSE);
2300 emsg(_("E99: lhs entry missing in mapset() dict argument"));
2301 return;
2302 }
2303 rhs = dict_get_string(d, (char_u *)"rhs", FALSE); 2321 rhs = dict_get_string(d, (char_u *)"rhs", FALSE);
2304 if (rhs == NULL) 2322 if (lhs == NULL || lhsraw == NULL || rhs == NULL)
2305 { 2323 {
2306 emsg(_("E99: rhs entry missing in mapset() dict argument")); 2324 emsg(_("E460: entries missing in mapset() dict argument"));
2307 return; 2325 return;
2308 } 2326 }
2309 orig_rhs = rhs; 2327 orig_rhs = rhs;
2310 rhs = replace_termcodes(rhs, &arg_buf, 2328 rhs = replace_termcodes(rhs, &arg_buf,
2311 REPTERM_DO_LT | REPTERM_SPECIAL, NULL); 2329 REPTERM_DO_LT | REPTERM_SPECIAL, NULL);
2322 map_table = curbuf->b_maphash; 2340 map_table = curbuf->b_maphash;
2323 abbr_table = &curbuf->b_first_abbr; 2341 abbr_table = &curbuf->b_first_abbr;
2324 } 2342 }
2325 nowait = dict_get_number(d, (char_u *)"nowait") != 0; 2343 nowait = dict_get_number(d, (char_u *)"nowait") != 0;
2326 // mode from the dict is not used 2344 // mode from the dict is not used
2327 simplified = dict_get_number(d, (char_u *)"simplified") != 0;
2328 2345
2329 // Delete any existing mapping for this lhs and mode. 2346 // Delete any existing mapping for this lhs and mode.
2330 arg = vim_strsave(lhs); 2347 arg = vim_strsave(lhs);
2331 if (arg == NULL) 2348 if (arg == NULL)
2332 return; 2349 return;
2333 do_map(1, arg, mode, is_abbr); 2350 do_map(1, arg, mode, is_abbr);
2334 vim_free(arg); 2351 vim_free(arg);
2335 2352
2336 keys = replace_termcodes(lhs, &keys_buf, 2353 (void)map_add(map_table, abbr_table, lhsraw, rhs, orig_rhs, noremap,
2337 REPTERM_FROM_PART | REPTERM_DO_LT, NULL); 2354 nowait, silent, mode, is_abbr, expr, sid, lnum, 0);
2338 (void)map_add(map_table, abbr_table, keys, rhs, orig_rhs, noremap, 2355 if (lhsrawalt != NULL)
2339 nowait, silent, mode, is_abbr, expr, sid, lnum, simplified); 2356 (void)map_add(map_table, abbr_table, lhsrawalt, rhs, orig_rhs, noremap,
2357 nowait, silent, mode, is_abbr, expr, sid, lnum, 1);
2340 vim_free(keys_buf); 2358 vim_free(keys_buf);
2341 vim_free(arg_buf); 2359 vim_free(arg_buf);
2342 } 2360 }
2343 #endif 2361 #endif
2344 2362