@@ -2274,6 +2274,42 @@ check_map(
22742274 return NULL ;
22752275}
22762276
2277+ /*
2278+ * Fill in the empty dictionary with items as defined by maparg builtin.
2279+ */
2280+ static void
2281+ mapblock2dict (
2282+ mapblock_T * mp ,
2283+ dict_T * dict ,
2284+ char_u * lhsrawalt , // may be NULL
2285+ int buffer_local ) // false if not buffer local mapping
2286+ {
2287+ char_u * lhs = str2special_save (mp -> m_keys , TRUE);
2288+ char_u * mapmode = map_mode_to_chars (mp -> m_mode );
2289+
2290+ dict_add_string (dict , "lhs" , lhs );
2291+ vim_free (lhs );
2292+ dict_add_string (dict , "lhsraw" , mp -> m_keys );
2293+ if (lhsrawalt )
2294+ // Also add the value for the simplified entry.
2295+ dict_add_string (dict , "lhsrawalt" , lhsrawalt );
2296+ dict_add_string (dict , "rhs" , mp -> m_orig_str );
2297+ dict_add_number (dict , "noremap" , mp -> m_noremap ? 1L : 0L );
2298+ dict_add_number (dict , "script" , mp -> m_noremap == REMAP_SCRIPT
2299+ ? 1L : 0L );
2300+ dict_add_number (dict , "expr" , mp -> m_expr ? 1L : 0L );
2301+ dict_add_number (dict , "silent" , mp -> m_silent ? 1L : 0L );
2302+ dict_add_number (dict , "sid" , (long )mp -> m_script_ctx .sc_sid );
2303+ dict_add_number (dict , "scriptversion" ,
2304+ (long )mp -> m_script_ctx .sc_version );
2305+ dict_add_number (dict , "lnum" , (long )mp -> m_script_ctx .sc_lnum );
2306+ dict_add_number (dict , "buffer" , (long )buffer_local );
2307+ dict_add_number (dict , "nowait" , mp -> m_nowait ? 1L : 0L );
2308+ dict_add_string (dict , "mode" , mapmode );
2309+
2310+ vim_free (mapmode );
2311+ }
2312+
22772313 static void
22782314get_maparg (typval_T * argvars , typval_T * rettv , int exact )
22792315{
@@ -2346,39 +2382,66 @@ get_maparg(typval_T *argvars, typval_T *rettv, int exact)
23462382
23472383 }
23482384 else if (rettv_dict_alloc (rettv ) != FAIL && rhs != NULL )
2349- {
2350- // Return a dictionary.
2351- char_u * lhs = str2special_save (mp -> m_keys , TRUE);
2352- char_u * mapmode = map_mode_to_chars (mp -> m_mode );
2353- dict_T * dict = rettv -> vval .v_dict ;
2354-
2355- dict_add_string (dict , "lhs" , lhs );
2356- vim_free (lhs );
2357- dict_add_string (dict , "lhsraw" , mp -> m_keys );
2358- if (did_simplify )
2359- // Also add the value for the simplified entry.
2360- dict_add_string (dict , "lhsrawalt" , mp_simplified -> m_keys );
2361- dict_add_string (dict , "rhs" , mp -> m_orig_str );
2362- dict_add_number (dict , "noremap" , mp -> m_noremap ? 1L : 0L );
2363- dict_add_number (dict , "script" , mp -> m_noremap == REMAP_SCRIPT
2364- ? 1L : 0L );
2365- dict_add_number (dict , "expr" , mp -> m_expr ? 1L : 0L );
2366- dict_add_number (dict , "silent" , mp -> m_silent ? 1L : 0L );
2367- dict_add_number (dict , "sid" , (long )mp -> m_script_ctx .sc_sid );
2368- dict_add_number (dict , "scriptversion" ,
2369- (long )mp -> m_script_ctx .sc_version );
2370- dict_add_number (dict , "lnum" , (long )mp -> m_script_ctx .sc_lnum );
2371- dict_add_number (dict , "buffer" , (long )buffer_local );
2372- dict_add_number (dict , "nowait" , mp -> m_nowait ? 1L : 0L );
2373- dict_add_string (dict , "mode" , mapmode );
2374-
2375- vim_free (mapmode );
2376- }
2385+ mapblock2dict (mp , rettv -> vval .v_dict ,
2386+ did_simplify ? mp_simplified -> m_keys : NULL , buffer_local );
23772387
23782388 vim_free (keys_buf );
23792389 vim_free (alt_keys_buf );
23802390}
23812391
2392+ /*
2393+ * "getmappings()" function
2394+ */
2395+ void
2396+ f_getmappings (typval_T * argvars UNUSED , typval_T * rettv )
2397+ {
2398+ dict_T * d ;
2399+ mapblock_T * mp ;
2400+ int buffer_local ;
2401+ char_u * keys_buf ;
2402+ int did_simplify ;
2403+ int hash ;
2404+ char_u * lhs ;
2405+ const int flags = REPTERM_FROM_PART | REPTERM_DO_LT ;
2406+
2407+ if (rettv_list_alloc (rettv ) != OK )
2408+ return ;
2409+
2410+ validate_maphash ();
2411+
2412+ // Do it twice: once for global maps and once for local maps.
2413+ for (buffer_local = 0 ; buffer_local <= 1 ; ++ buffer_local )
2414+ {
2415+ for (hash = 0 ; hash < 256 ; ++ hash )
2416+ {
2417+ if (buffer_local )
2418+ mp = curbuf -> b_maphash [hash ];
2419+ else
2420+ mp = maphash [hash ];
2421+ for (; mp ; mp = mp -> m_next )
2422+ {
2423+ if (mp -> m_simplified )
2424+ continue ;
2425+ if ((d = dict_alloc ()) == NULL )
2426+ return ;
2427+ if (list_append_dict (rettv -> vval .v_list , d ) == FAIL )
2428+ return ;
2429+
2430+ keys_buf = NULL ;
2431+ did_simplify = FALSE;
2432+
2433+ lhs = str2special_save (mp -> m_keys , TRUE);
2434+ (void )replace_termcodes (lhs , & keys_buf , flags , & did_simplify );
2435+ vim_free (lhs );
2436+
2437+ mapblock2dict (mp , d ,
2438+ did_simplify ? keys_buf : NULL , buffer_local );
2439+ vim_free (keys_buf );
2440+ }
2441+ }
2442+ }
2443+ }
2444+
23822445/*
23832446 * "maparg()" function
23842447 */
0 commit comments