Encapsulate input function name and code into a single struct

Reduces the reliance in ugly parallel arrays.
This commit is contained in:
ridiculousfish 2018-01-29 19:15:16 -08:00
parent 43c839ab0e
commit d03aff8742

View file

@ -58,125 +58,82 @@ struct terminfo_mapping_t {
const char *seq; // character sequence generated on keypress const char *seq; // character sequence generated on keypress
}; };
/// Names of all the input functions supported. static constexpr size_t input_function_count = R_END_INPUT_FUNCTIONS - R_BEGIN_INPUT_FUNCTIONS;
static const wchar_t *const name_arr[] = {L"beginning-of-line",
L"end-of-line", /// Input function metadata. This list should be kept in sync with the key code list in
L"forward-char", /// input_common.h.
L"backward-char", struct input_function_metadata_t {
L"forward-word", wchar_t code;
L"backward-word", const wchar_t *name;
L"forward-bigword", };
L"backward-bigword", static const input_function_metadata_t input_function_metadata[] = {
L"history-search-backward", {R_BEGINNING_OF_LINE, L"beginning-of-line"},
L"history-search-forward", {R_END_OF_LINE, L"end-of-line"},
L"delete-char", {R_FORWARD_CHAR, L"forward-char"},
L"backward-delete-char", {R_BACKWARD_CHAR, L"backward-char"},
L"kill-line", {R_FORWARD_WORD, L"forward-word"},
L"yank", {R_BACKWARD_WORD, L"backward-word"},
L"yank-pop", {R_FORWARD_BIGWORD, L"forward-bigword"},
L"complete", {R_BACKWARD_BIGWORD, L"backward-bigword"},
L"complete-and-search", {R_HISTORY_SEARCH_BACKWARD, L"history-search-backward"},
L"beginning-of-history", {R_HISTORY_SEARCH_FORWARD, L"history-search-forward"},
L"end-of-history", {R_DELETE_CHAR, L"delete-char"},
L"backward-kill-line", {R_BACKWARD_DELETE_CHAR, L"backward-delete-char"},
L"kill-whole-line", {R_KILL_LINE, L"kill-line"},
L"kill-word", {R_YANK, L"yank"},
L"kill-bigword", {R_YANK_POP, L"yank-pop"},
L"backward-kill-word", {R_COMPLETE, L"complete"},
L"backward-kill-path-component", {R_COMPLETE_AND_SEARCH, L"complete-and-search"},
L"backward-kill-bigword", {R_BEGINNING_OF_HISTORY, L"beginning-of-history"},
L"history-token-search-backward", {R_END_OF_HISTORY, L"end-of-history"},
L"history-token-search-forward", {R_BACKWARD_KILL_LINE, L"backward-kill-line"},
L"self-insert", {R_KILL_WHOLE_LINE, L"kill-whole-line"},
L"transpose-chars", {R_KILL_WORD, L"kill-word"},
L"transpose-words", {R_KILL_BIGWORD, L"kill-bigword"},
L"upcase-word", {R_BACKWARD_KILL_WORD, L"backward-kill-word"},
L"downcase-word", {R_BACKWARD_KILL_PATH_COMPONENT, L"backward-kill-path-component"},
L"capitalize-word", {R_BACKWARD_KILL_BIGWORD, L"backward-kill-bigword"},
L"vi-arg-digit", {R_HISTORY_TOKEN_SEARCH_BACKWARD, L"history-token-search-backward"},
L"vi-delete-to", {R_HISTORY_TOKEN_SEARCH_FORWARD, L"history-token-search-forward"},
L"execute", {R_SELF_INSERT, L"self-insert"},
L"beginning-of-buffer", {R_TRANSPOSE_CHARS, L"transpose-chars"},
L"end-of-buffer", {R_TRANSPOSE_WORDS, L"transpose-words"},
L"repaint", {R_UPCASE_WORD, L"upcase-word"},
L"force-repaint", {R_DOWNCASE_WORD, L"downcase-word"},
L"up-line", {R_CAPITALIZE_WORD, L"capitalize-word"},
L"down-line", {R_VI_ARG_DIGIT, L"vi-arg-digit"},
L"suppress-autosuggestion", {R_VI_DELETE_TO, L"vi-delete-to"},
L"accept-autosuggestion", {R_EXECUTE, L"execute"},
L"begin-selection", {R_BEGINNING_OF_BUFFER, L"beginning-of-buffer"},
L"swap-selection-start-stop", {R_END_OF_BUFFER, L"end-of-buffer"},
L"end-selection", {R_REPAINT, L"repaint"},
L"kill-selection", {R_FORCE_REPAINT, L"force-repaint"},
L"forward-jump", {R_UP_LINE, L"up-line"},
L"backward-jump", {R_DOWN_LINE, L"down-line"},
L"and", {R_SUPPRESS_AUTOSUGGESTION, L"suppress-autosuggestion"},
L"cancel"}; {R_ACCEPT_AUTOSUGGESTION, L"accept-autosuggestion"},
{R_BEGIN_SELECTION, L"begin-selection"},
{R_SWAP_SELECTION_START_STOP, L"swap-selection-start-stop"},
{R_END_SELECTION, L"end-selection"},
{R_KILL_SELECTION, L"kill-selection"},
{R_FORWARD_JUMP, L"forward-jump"},
{R_BACKWARD_JUMP, L"backward-jump"},
{R_AND, L"and"},
{R_CANCEL, L"cancel"}};
static_assert(sizeof(input_function_metadata) / sizeof(input_function_metadata[0]) ==
input_function_count,
"input_function_metadata size mismatch with input_common. Did you forget to update "
"input_function_metadata?");
wcstring describe_char(wint_t c) { wcstring describe_char(wint_t c) {
wint_t initial_cmd_char = R_BEGINNING_OF_LINE; if (c >= R_BEGIN_INPUT_FUNCTIONS && c < R_END_INPUT_FUNCTIONS) {
long name_count = sizeof(name_arr) / sizeof(*name_arr); size_t idx = c - R_BEGIN_INPUT_FUNCTIONS;
if (c >= initial_cmd_char && c < initial_cmd_char + name_count) { return format_string(L"%02x (%ls)", c, input_function_metadata[idx].name);
return format_string(L"%02x (%ls)", c, name_arr[c - initial_cmd_char]);
} }
return format_string(L"%02x", c); return format_string(L"%02x", c);
} }
/// Internal code for each supported input function.
static const wchar_t code_arr[] = {R_BEGINNING_OF_LINE,
R_END_OF_LINE,
R_FORWARD_CHAR,
R_BACKWARD_CHAR,
R_FORWARD_WORD,
R_BACKWARD_WORD,
R_FORWARD_BIGWORD,
R_BACKWARD_BIGWORD,
R_HISTORY_SEARCH_BACKWARD,
R_HISTORY_SEARCH_FORWARD,
R_DELETE_CHAR,
R_BACKWARD_DELETE_CHAR,
R_KILL_LINE,
R_YANK,
R_YANK_POP,
R_COMPLETE,
R_COMPLETE_AND_SEARCH,
R_BEGINNING_OF_HISTORY,
R_END_OF_HISTORY,
R_BACKWARD_KILL_LINE,
R_KILL_WHOLE_LINE,
R_KILL_WORD,
R_KILL_BIGWORD,
R_BACKWARD_KILL_WORD,
R_BACKWARD_KILL_PATH_COMPONENT,
R_BACKWARD_KILL_BIGWORD,
R_HISTORY_TOKEN_SEARCH_BACKWARD,
R_HISTORY_TOKEN_SEARCH_FORWARD,
R_SELF_INSERT,
R_TRANSPOSE_CHARS,
R_TRANSPOSE_WORDS,
R_UPCASE_WORD,
R_DOWNCASE_WORD,
R_CAPITALIZE_WORD,
R_VI_ARG_DIGIT,
R_VI_DELETE_TO,
R_EXECUTE,
R_BEGINNING_OF_BUFFER,
R_END_OF_BUFFER,
R_REPAINT,
R_FORCE_REPAINT,
R_UP_LINE,
R_DOWN_LINE,
R_SUPPRESS_AUTOSUGGESTION,
R_ACCEPT_AUTOSUGGESTION,
R_BEGIN_SELECTION,
R_SWAP_SELECTION_START_STOP,
R_END_SELECTION,
R_KILL_SELECTION,
R_FORWARD_JUMP,
R_BACKWARD_JUMP,
R_AND,
R_CANCEL};
/// Mappings for the current input mode. /// Mappings for the current input mode.
static std::vector<input_mapping_t> mapping_list; static std::vector<input_mapping_t> mapping_list;
@ -818,15 +775,19 @@ wcstring_list_t input_terminfo_get_names(bool skip_null) {
return result; return result;
} }
wcstring_list_t input_function_get_names(void) { wcstring_list_t input_function_get_names() {
size_t count = sizeof name_arr / sizeof *name_arr; wcstring_list_t result;
return wcstring_list_t(name_arr, name_arr + count); result.reserve(input_function_count);
for (const auto &md : input_function_metadata) {
result.push_back(md.name);
}
return result;
} }
wchar_t input_function_get_code(const wcstring &name) { wchar_t input_function_get_code(const wcstring &name) {
for (size_t i = 0; i < sizeof code_arr / sizeof *code_arr; i++) { for (const auto &md : input_function_metadata) {
if (name == name_arr[i]) { if (name == md.name) {
return code_arr[i]; return md.code;
} }
} }
return INPUT_CODE_NONE; return INPUT_CODE_NONE;