get_keymap API (#6236)

* Add api function get keymap

nvim_get_keymap(mode)
nvim_buf_get_keymap(buffer, mode)
This commit is contained in:
TJ DeVries
2017-05-25 05:41:53 -05:00
committed by Björn Linse
parent f4fddbfb77
commit 45626de63f
8 changed files with 502 additions and 23 deletions

View File

@@ -453,6 +453,26 @@ Integer nvim_buf_get_changedtick(Buffer buffer, Error *err)
return buf->b_changedtick;
}
/// Get a list of dictionaries describing buffer-local mappings
/// Note that the buffer key in the dictionary will represent the buffer
/// handle where the mapping is present
///
/// @param mode The abbreviation for the mode
/// @param buffer_id Buffer handle
/// @param[out] err Error details, if any
/// @returns An array of maparg() like dictionaries describing mappings
ArrayOf(Dictionary) nvim_buf_get_keymap(Buffer buffer, String mode, Error *err)
FUNC_API_SINCE(3)
{
buf_T *buf = find_buffer_by_handle(buffer, err);
if (!buf) {
return (Array)ARRAY_DICT_INIT;
}
return keymap_array(mode, buf);
}
/// Sets a buffer-scoped (b:) variable
///
/// @param buffer Buffer handle

View File

@@ -24,6 +24,7 @@
#include "nvim/option_defs.h"
#include "nvim/version.h"
#include "nvim/lib/kvec.h"
#include "nvim/getchar.h"
/// Helper structure for vim_to_object
typedef struct {
@@ -1034,3 +1035,41 @@ void api_set_error(Error *err, ErrorType errType, const char *format, ...)
err->type = errType;
}
/// Get an array containing dictionaries describing mappings
/// based on mode and buffer id
///
/// @param mode The abbreviation for the mode
/// @param buf The buffer to get the mapping array. NULL for global
/// @returns An array of maparg() like dictionaries describing mappings
ArrayOf(Dictionary) keymap_array(String mode, buf_T *buf)
{
Array mappings = ARRAY_DICT_INIT;
dict_T *const dict = tv_dict_alloc();
// Convert the string mode to the integer mode
// that is stored within each mapblock
char_u *p = (char_u *)mode.data;
int int_mode = get_map_mode(&p, 0);
// Determine the desired buffer value
long buffer_value = (buf == NULL) ? 0 : buf->handle;
for (int i = 0; i < MAX_MAPHASH; i++) {
for (const mapblock_T *current_maphash = get_maphash(i, buf);
current_maphash;
current_maphash = current_maphash->m_next) {
// Check for correct mode
if (int_mode & current_maphash->m_mode) {
mapblock_fill_dict(dict, current_maphash, buffer_value, false);
ADD(mappings, vim_to_object(
(typval_T[]) { { .v_type = VAR_DICT, .vval.v_dict = dict } }));
tv_dict_clear(dict);
}
}
}
tv_dict_free(dict);
return mappings;
}

View File

@@ -742,6 +742,17 @@ Dictionary nvim_get_mode(void)
return rv;
}
/// Get a list of dictionaries describing global (i.e. non-buffer) mappings
/// Note that the "buffer" key will be 0 to represent false.
///
/// @param mode The abbreviation for the mode
/// @returns An array of maparg() like dictionaries describing mappings
ArrayOf(Dictionary) nvim_get_keymap(String mode)
FUNC_API_SINCE(3)
{
return keymap_array(mode, NULL);
}
Array nvim_get_api_info(uint64_t channel_id)
FUNC_API_SINCE(1) FUNC_API_ASYNC FUNC_API_REMOTE_ONLY
{