From 45c6ac02088873d2b151d8a550129209f7f13da8 Mon Sep 17 00:00:00 2001 From: Kurtis Rader Date: Sat, 30 Apr 2016 21:31:25 -0700 Subject: restyle input module to match project style Reduces lint errors from 69 to 48 (-30%). Line count from 1270 to 1044 (-18%). Another step in resolving issue #2902. --- src/input.cpp | 951 +++++++++++++++++++++++----------------------------------- src/input.h | 127 +++----- 2 files changed, 426 insertions(+), 652 deletions(-) (limited to 'src') diff --git a/src/input.cpp b/src/input.cpp index c45939bc..d37b963c 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -1,7 +1,4 @@ -/** \file input.c - - Functions for reading a character of input from stdin. -*/ +// Functions for reading a character of input from stdin. #include "config.h" #include @@ -21,288 +18,243 @@ #include #endif #include -#include #include #include #include +#include -#include "fallback.h" // IWYU pragma: keep -#include "wutil.h" // IWYU pragma: keep -#include "reader.h" -#include "proc.h" #include "common.h" -#include "input_common.h" -#include "input.h" -#include "parser.h" #include "env.h" #include "event.h" -#include "signal.h" // IWYU pragma: keep +#include "fallback.h" // IWYU pragma: keep +#include "input.h" +#include "input_common.h" #include "io.h" #include "output.h" +#include "parser.h" +#include "proc.h" +#include "reader.h" +#include "signal.h" // IWYU pragma: keep +#include "wutil.h" // IWYU pragma: keep #define DEFAULT_TERM L"ansi" #define MAX_INPUT_FUNCTION_ARGS 20 -/** - Struct representing a keybinding. Returned by input_get_mappings. - */ - -struct input_mapping_t -{ - wcstring seq; /**< Character sequence which generates this event */ - wcstring_list_t commands; /**< commands that should be evaluated by this mapping */ - - /* We wish to preserve the user-specified order. This is just an incrementing value. */ +/// Struct representing a keybinding. Returned by input_get_mappings. +struct input_mapping_t { + /// Character sequence which generates this event. + wcstring seq; + /// Commands that should be evaluated by this mapping. + wcstring_list_t commands; + /// We wish to preserve the user-specified order. This is just an incrementing value. unsigned int specification_order; - - wcstring mode; /**< mode in which this command should be evaluated */ - wcstring sets_mode; /** new mode that should be switched to after command evaluation */ + /// Mode in which this command should be evaluated. + wcstring mode; + /// New mode that should be switched to after command evaluation. + wcstring sets_mode; input_mapping_t(const wcstring &s, const std::vector &c, - const wcstring &m = DEFAULT_BIND_MODE, - const wcstring &sm = DEFAULT_BIND_MODE) : seq(s), commands(c), mode(m), sets_mode(sm) - { + const wcstring &m = DEFAULT_BIND_MODE, const wcstring &sm = DEFAULT_BIND_MODE) + : seq(s), commands(c), mode(m), sets_mode(sm) { static unsigned int s_last_input_mapping_specification_order = 0; specification_order = ++s_last_input_mapping_specification_order; - } }; -/** - A struct representing the mapping from a terminfo key name to a terminfo character sequence - */ -struct terminfo_mapping_t -{ - const wchar_t *name; /**< Name of key */ - const char *seq; /**< Character sequence generated on keypress. Constant string. */ +/// A struct representing the mapping from a terminfo key name to a terminfo character sequence. +struct terminfo_mapping_t { + const wchar_t *name; // name of key + const char *seq; // character sequence generated on keypress }; - -/** - Names of all the input functions supported -*/ -static const wchar_t * const name_arr[] = -{ - L"beginning-of-line", - L"end-of-line", - L"forward-char", - L"backward-char", - L"forward-word", - L"backward-word", - L"forward-bigword", - L"backward-bigword", - L"history-search-backward", - L"history-search-forward", - L"delete-char", - L"backward-delete-char", - L"kill-line", - L"yank", - L"yank-pop", - L"complete", - L"complete-and-search", - L"beginning-of-history", - L"end-of-history", - L"backward-kill-line", - L"kill-whole-line", - L"kill-word", - L"kill-bigword", - L"backward-kill-word", - L"backward-kill-path-component", - L"backward-kill-bigword", - L"history-token-search-backward", - L"history-token-search-forward", - L"self-insert", - L"transpose-chars", - L"transpose-words", - L"upcase-word", - L"downcase-word", - L"capitalize-word", - L"vi-arg-digit", - L"vi-delete-to", - L"execute", - L"beginning-of-buffer", - L"end-of-buffer", - L"repaint", - L"force-repaint", - L"up-line", - L"down-line", - L"suppress-autosuggestion", - L"accept-autosuggestion", - L"begin-selection", - L"swap-selection-start-stop", - L"end-selection", - L"kill-selection", - L"forward-jump", - L"backward-jump", - L"and", - L"cancel" -}; - -wcstring describe_char(wint_t c) -{ +/// Names of all the input functions supported. +static const wchar_t *const name_arr[] = {L"beginning-of-line", + L"end-of-line", + L"forward-char", + L"backward-char", + L"forward-word", + L"backward-word", + L"forward-bigword", + L"backward-bigword", + L"history-search-backward", + L"history-search-forward", + L"delete-char", + L"backward-delete-char", + L"kill-line", + L"yank", + L"yank-pop", + L"complete", + L"complete-and-search", + L"beginning-of-history", + L"end-of-history", + L"backward-kill-line", + L"kill-whole-line", + L"kill-word", + L"kill-bigword", + L"backward-kill-word", + L"backward-kill-path-component", + L"backward-kill-bigword", + L"history-token-search-backward", + L"history-token-search-forward", + L"self-insert", + L"transpose-chars", + L"transpose-words", + L"upcase-word", + L"downcase-word", + L"capitalize-word", + L"vi-arg-digit", + L"vi-delete-to", + L"execute", + L"beginning-of-buffer", + L"end-of-buffer", + L"repaint", + L"force-repaint", + L"up-line", + L"down-line", + L"suppress-autosuggestion", + L"accept-autosuggestion", + L"begin-selection", + L"swap-selection-start-stop", + L"end-selection", + L"kill-selection", + L"forward-jump", + L"backward-jump", + L"and", + L"cancel"}; + +wcstring describe_char(wint_t c) { wint_t initial_cmd_char = R_BEGINNING_OF_LINE; size_t name_count = sizeof name_arr / sizeof *name_arr; - if (c >= initial_cmd_char && c < initial_cmd_char + name_count) - { + if (c >= initial_cmd_char && c < initial_cmd_char + name_count) { return format_string(L"%02x (%ls)", c, name_arr[c - initial_cmd_char]); } return format_string(L"%02x", c); } -/** - Description of each supported input function -*/ -/* -static const wchar_t *desc_arr[] = -{ - L"Move to beginning of line", - L"Move to end of line", - L"Move forward one character", - L"Move backward one character", - L"Move forward one word", - L"Move backward one word", - L"Search backward through list of previous commands", - L"Search forward through list of previous commands", - L"Delete one character forward", - L"Delete one character backward", - L"Move contents from cursor to end of line to killring", - L"Paste contents of killring", - L"Rotate to previous killring entry", - L"Guess the rest of the next input token", - L"Move to first item of history", - L"Move to last item of history", - L"Clear current line", - L"Move contents from beginning of line to cursor to killring", - L"Move entire line to killring", - L"Move next word to killring", - L"Move previous word to killring", - L"Write out key bindings", - L"Clear entire screen", - L"Quit the running program", - L"Search backward through list of previous commands for matching token", - L"Search forward through list of previous commands for matching token", - L"Insert the pressed key", - L"Do nothing", - L"End of file", - L"Repeat command" -} - ; -*/ - -/** - 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 */ +/// Description of each supported input function. +static const wchar_t *desc_arr[] = { + L"Move to beginning of line", + L"Move to end of line", + L"Move forward one character", + L"Move backward one character", + L"Move forward one word", + L"Move backward one word", + L"Search backward through list of previous commands", + L"Search forward through list of previous commands", + L"Delete one character forward", + L"Delete one character backward", + L"Move contents from cursor to end of line to killring", + L"Paste contents of killring", + L"Rotate to previous killring entry", + L"Guess the rest of the next input token", + L"Move to first item of history", + L"Move to last item of history", + L"Clear current line", + L"Move contents from beginning of line to cursor to killring", + L"Move entire line to killring", + L"Move next word to killring", + L"Move previous word to killring", + L"Write out key bindings", + L"Clear entire screen", + L"Quit the running program", + L"Search backward through list of previous commands for matching token", + L"Search forward through list of previous commands for matching token", + L"Insert the pressed key", + L"Do nothing", + L"End of file", + L"Repeat command"}; + +/// 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. static std::vector mapping_list; -/* Terminfo map list */ +/// Terminfo map list. static std::vector terminfo_mappings; -#define TERMINFO_ADD(key) { (L ## #key) + 4, key } +#define TERMINFO_ADD(key) \ + { (L## #key) + 4, key } - -/** - List of all terminfo mappings - */ +/// List of all terminfo mappings. static std::vector mappings; - -/** - Set to one when the input subsytem has been initialized. -*/ +/// Set to one when the input subsytem has been initialized. static bool is_init = false; -/** - Initialize terminfo. - */ +/// Initialize terminfo. static void input_terminfo_init(); static wchar_t input_function_args[MAX_INPUT_FUNCTION_ARGS]; static bool input_function_status; static int input_function_args_index = 0; -/** - Return the current bind mode -*/ -wcstring input_get_bind_mode() -{ +/// Return the current bind mode. +wcstring input_get_bind_mode() { env_var_t mode = env_get_string(FISH_BIND_MODE_VAR); return mode.missing() ? DEFAULT_BIND_MODE : mode; } -/** - Set the current bind mode -*/ -void input_set_bind_mode(const wcstring &bm) -{ +/// Set the current bind mode. +void input_set_bind_mode(const wcstring &bm) { env_set(FISH_BIND_MODE_VAR, bm.c_str(), ENV_GLOBAL); } - -/** - Returns the arity of a given input function -*/ -int input_function_arity(int function) -{ - switch (function) - { +/// Returns the arity of a given input function. +int input_function_arity(int function) { + switch (function) { case R_FORWARD_JUMP: case R_BACKWARD_JUMP: return 1; @@ -311,153 +263,115 @@ int input_function_arity(int function) } } -/** - Sets the return status of the most recently executed input function -*/ -void input_function_set_status(bool status) -{ - input_function_status = status; -} +/// Sets the return status of the most recently executed input function. +void input_function_set_status(bool status) { input_function_status = status; } -/* Helper function to compare the lengths of sequences */ -static bool length_is_greater_than(const input_mapping_t &m1, const input_mapping_t &m2) -{ +/// Helper function to compare the lengths of sequences. +static bool length_is_greater_than(const input_mapping_t &m1, const input_mapping_t &m2) { return m1.seq.size() > m2.seq.size(); } -static bool specification_order_is_less_than(const input_mapping_t &m1, const input_mapping_t &m2) -{ +static bool specification_order_is_less_than(const input_mapping_t &m1, const input_mapping_t &m2) { return m1.specification_order < m2.specification_order; } -/* Inserts an input mapping at the correct position. We sort them in descending order by length, so that we test longer sequences first. */ -static void input_mapping_insert_sorted(const input_mapping_t &new_mapping) -{ - std::vector::iterator loc = std::lower_bound(mapping_list.begin(), mapping_list.end(), new_mapping, length_is_greater_than); +/// Inserts an input mapping at the correct position. We sort them in descending order by length, so +/// that we test longer sequences first. +static void input_mapping_insert_sorted(const input_mapping_t &new_mapping) { + std::vector::iterator loc = std::lower_bound( + mapping_list.begin(), mapping_list.end(), new_mapping, length_is_greater_than); mapping_list.insert(loc, new_mapping); } -/* Adds an input mapping */ -void input_mapping_add(const wchar_t *sequence, const wchar_t * const *commands, size_t commands_len, - const wchar_t *mode, const wchar_t *sets_mode) -{ - CHECK(sequence,); - CHECK(commands,); - CHECK(mode,); - CHECK(sets_mode,); +/// Adds an input mapping. +void input_mapping_add(const wchar_t *sequence, const wchar_t *const *commands, size_t commands_len, + const wchar_t *mode, const wchar_t *sets_mode) { + CHECK(sequence, ); + CHECK(commands, ); + CHECK(mode, ); + CHECK(sets_mode, ); - // debug( 0, L"Add mapping from %ls to %ls in mode %ls", escape(sequence, ESCAPE_ALL).c_str(), escape(command, ESCAPE_ALL).c_str(), mode); + // debug( 0, L"Add mapping from %ls to %ls in mode %ls", escape(sequence, ESCAPE_ALL).c_str(), + // escape(command, ESCAPE_ALL).c_str(), mode); - // remove existing mappings with this sequence + // Remove existing mappings with this sequence. const wcstring_list_t commands_vector(commands, commands + commands_len); - for (size_t i=0; i(fish_term256); - } - else - { + } else { env_var_t term = env_get_string(L"TERM"); - if (term.missing()) - { + if (term.missing()) { support_term256 = false; - } - else if (term.find(L"256color") != wcstring::npos) - { - /* Explicitly supported */ + } else if (term.find(L"256color") != wcstring::npos) { + // Explicitly supported. support_term256 = true; - } - else if (term.find(L"xterm") != wcstring::npos) - { - // assume that all xterms are 256, except for OS X SnowLeopard + } else if (term.find(L"xterm") != wcstring::npos) { + // Assume that all xterms are 256, except for OS X SnowLeopard. env_var_t prog = env_get_string(L"TERM_PROGRAM"); support_term256 = (prog != L"Apple_Terminal"); - } - else - { - // Don't know, default to false + } else { + // Don't know, default to false. support_term256 = false; } } env_var_t fish_term24bit = env_get_string(L"fish_term24bit"); bool support_term24bit; - if (! fish_term24bit.missing_or_empty()) - { + if (!fish_term24bit.missing_or_empty()) { support_term24bit = from_string(fish_term24bit); - } - else - { - /* We don't attempt to infer term24 bit support yet. */ + } else { + // We don't attempt to infer term24 bit support yet. support_term24bit = false; } - color_support_t support = (support_term256 ? color_support_term256 : 0) | (support_term24bit ? color_support_term24bit : 0); + color_support_t support = (support_term256 ? color_support_term256 : 0) | + (support_term24bit ? color_support_term24bit : 0); output_set_color_support(support); } -int input_init() -{ - if (is_init) - return 1; +int input_init() { + if (is_init) return 1; is_init = true; @@ -465,37 +379,32 @@ int input_init() const env_var_t term = env_get_string(L"TERM"); int errret; - if (setupterm(const_cast(wcs2string(term).c_str()), STDOUT_FILENO, &errret) == ERR) - { + if (setupterm(const_cast(wcs2string(term).c_str()), STDOUT_FILENO, &errret) == ERR) { debug(0, _(L"Could not set up terminal")); - if (errret == 0) - { + if (errret == 0) { debug(0, _(L"Check that your terminal type, '%ls', is supported on this system"), term.c_str()); debug(0, _(L"Attempting to use '%ls' instead"), DEFAULT_TERM); env_set(L"TERM", DEFAULT_TERM, ENV_GLOBAL | ENV_EXPORT); const std::string default_term = wcs2string(DEFAULT_TERM); - if (setupterm(const_cast(default_term.c_str()), STDOUT_FILENO, &errret) == ERR) - { + if (setupterm(const_cast(default_term.c_str()), STDOUT_FILENO, &errret) == + ERR) { debug(0, _(L"Could not set up terminal")); exit_without_destructors(1); } - } - else - { + } else { exit_without_destructors(1); } } - assert(! term.missing()); + assert(!term.missing()); output_set_term(term); input_terminfo_init(); update_fish_color_support(); - /* If we have no keybindings, add a few simple defaults */ - if (mapping_list.empty()) - { + // If we have no keybindings, add a few simple defaults. + if (mapping_list.empty()) { input_mapping_add(L"", L"self-insert"); input_mapping_add(L"\n", L"execute"); input_mapping_add(L"\r", L"execute"); @@ -508,340 +417,260 @@ int input_init() return 1; } -void input_destroy() -{ - if (!is_init) - return; - - +void input_destroy() { + if (!is_init) return; is_init = false; - input_common_destroy(); - if (del_curterm(cur_term) == ERR) - { + if (del_curterm(cur_term) == ERR) { debug(0, _(L"Error while closing terminfo")); } } -void input_function_push_arg(wchar_t arg) -{ +void input_function_push_arg(wchar_t arg) { input_function_args[input_function_args_index++] = arg; } -wchar_t input_function_pop_arg() -{ - return input_function_args[--input_function_args_index]; -} +wchar_t input_function_pop_arg() { return input_function_args[--input_function_args_index]; } -void input_function_push_args(int code) -{ +void input_function_push_args(int code) { int arity = input_function_arity(code); std::vector skipped; - for (int i = 0; i < arity; i++) - { + for (int i = 0; i < arity; i++) { wchar_t arg; - // skip and queue up any function codes - // See #2357 - while(((arg = input_common_readch(0)) >= R_MIN) && (arg <= R_MAX)) - { + // Skip and queue up any function codes. See issue #2357. + while (((arg = input_common_readch(0)) >= R_MIN) && (arg <= R_MAX)) { skipped.push_back(arg); } input_function_push_arg(arg); } - // push the function codes back into the input stream + // Push the function codes back into the input stream. size_t idx = skipped.size(); - while (idx--) - { + while (idx--) { input_common_next_ch(skipped.at(idx)); } } -/** - Perform the action of the specified binding - allow_commands controls whether fish commands should be executed, or should - be deferred until later. -*/ -static void input_mapping_execute(const input_mapping_t &m, bool allow_commands) -{ - /* has_functions: there are functions that need to be put on the input - queue - has_commands: there are shell commands that need to be evaluated */ +/// Perform the action of the specified binding. allow_commands controls whether fish commands +/// should be executed, or should be deferred until later. +static void input_mapping_execute(const input_mapping_t &m, bool allow_commands) { + // has_functions: there are functions that need to be put on the input queue + // has_commands: there are shell commands that need to be evaluated bool has_commands = false, has_functions = false; - for (wcstring_list_t::const_iterator it = m.commands.begin(), end = m.commands.end(); it != end; ++it) - { + for (wcstring_list_t::const_iterator it = m.commands.begin(), end = m.commands.end(); it != end; + ++it) { if (input_function_get_code(*it) != INPUT_CODE_NONE) has_functions = true; else has_commands = true; } - /* !has_functions && !has_commands: only set bind mode */ - if (!has_commands && !has_functions) - { + // !has_functions && !has_commands: only set bind mode + if (!has_commands && !has_functions) { input_set_bind_mode(m.sets_mode); return; } - if (has_commands && !allow_commands) - { - /* We don't want to run commands yet. Put the characters back and return - R_NULL */ - for (wcstring::const_reverse_iterator it = m.seq.rbegin(), end = m.seq.rend(); it != end; ++it) - { + if (has_commands && !allow_commands) { + // We don't want to run commands yet. Put the characters back and return R_NULL. + for (wcstring::const_reverse_iterator it = m.seq.rbegin(), end = m.seq.rend(); it != end; + ++it) { input_common_next_ch(*it); } input_common_next_ch(R_NULL); - return; /* skip the input_set_bind_mode */ - } - else if (has_functions && !has_commands) - { - /* functions are added at the head of the input queue */ - for (wcstring_list_t::const_reverse_iterator it = m.commands.rbegin(), end = m.commands.rend(); it != end; ++it) - { + return; // skip the input_set_bind_mode + } else if (has_functions && !has_commands) { + // Functions are added at the head of the input queue. + for (wcstring_list_t::const_reverse_iterator it = m.commands.rbegin(), + end = m.commands.rend(); + it != end; ++it) { wchar_t code = input_function_get_code(*it); input_function_push_args(code); input_common_next_ch(code); } - } - else if (has_commands && !has_functions) - { - /* Execute all commands. - - FIXME(snnw): if commands add stuff to input queue (e.g. commandline - -f execute), we won't see that until all other commands have also - been run. */ + } else if (has_commands && !has_functions) { + // Execute all commands. + // + // FIXME(snnw): if commands add stuff to input queue (e.g. commandline -f execute), we won't + // see that until all other commands have also been run. int last_status = proc_get_last_status(); - for (wcstring_list_t::const_iterator it = m.commands.begin(), end = m.commands.end(); it != end; ++it) - { + for (wcstring_list_t::const_iterator it = m.commands.begin(), end = m.commands.end(); + it != end; ++it) { parser_t::principal_parser().eval(it->c_str(), io_chain_t(), TOP); } proc_set_last_status(last_status); input_common_next_ch(R_NULL); - } - else - { - /* invalid binding, mixed commands and functions. We would need to - execute these one by one. */ + } else { + // Invalid binding, mixed commands and functions. We would need to execute these one by + // one. input_common_next_ch(R_NULL); } input_set_bind_mode(m.sets_mode); } - - -/** - Try reading the specified function mapping -*/ -static bool input_mapping_is_match(const input_mapping_t &m) -{ +/// Try reading the specified function mapping. +static bool input_mapping_is_match(const input_mapping_t &m) { wint_t c = 0; int j; - //debug(0, L"trying mapping %ls\n", escape(m.seq.c_str(), ESCAPE_ALL).c_str()); + // debug(0, L"trying mapping %ls\n", escape(m.seq.c_str(), ESCAPE_ALL).c_str()); const wchar_t *str = m.seq.c_str(); - for (j=0; str[j] != L'\0'; j++) - { + for (j = 0; str[j] != L'\0'; j++) { bool timed = (j > 0 && iswcntrl(str[0])); c = input_common_readch(timed); - if (str[j] != c) - { + if (str[j] != c) { break; } } - if (str[j] == L'\0') - { - //debug(0, L"matched mapping %ls (%ls)\n", escape(m.seq.c_str(), ESCAPE_ALL).c_str(), m.command.c_str()); - /* We matched the entire sequence */ + if (str[j] == L'\0') { + // debug(0, L"matched mapping %ls (%ls)\n", escape(m.seq.c_str(), ESCAPE_ALL).c_str(), + // m.command.c_str()); + // We matched the entire sequence. return true; - } - else - { + } else { int k; - /* - Return the read characters - */ + // Return the read characters. input_common_next_ch(c); - for (k=j-1; k>=0; k--) - { + for (k = j - 1; k >= 0; k--) { input_common_next_ch(m.seq[k]); } } return false; - } -void input_queue_ch(wint_t ch) -{ - input_common_queue_ch(ch); -} +void input_queue_ch(wint_t ch) { input_common_queue_ch(ch); } -static void input_mapping_execute_matching_or_generic(bool allow_commands) -{ +static void input_mapping_execute_matching_or_generic(bool allow_commands) { const input_mapping_t *generic = NULL; const wcstring bind_mode = input_get_bind_mode(); - for (int i = 0; i < mapping_list.size(); i++) - { + for (int i = 0; i < mapping_list.size(); i++) { const input_mapping_t &m = mapping_list.at(i); - //debug(0, L"trying mapping (%ls,%ls,%ls)\n", escape(m.seq.c_str(), ESCAPE_ALL).c_str(), + // debug(0, L"trying mapping (%ls,%ls,%ls)\n", escape(m.seq.c_str(), ESCAPE_ALL).c_str(), // m.mode.c_str(), m.sets_mode.c_str()); - if (m.mode != bind_mode) - { - //debug(0, L"skipping mapping because mode %ls != %ls\n", m.mode.c_str(), input_get_bind_mode().c_str()); + if (m.mode != bind_mode) { + // debug(0, L"skipping mapping because mode %ls != %ls\n", m.mode.c_str(), + // input_get_bind_mode().c_str()); continue; } - if (m.seq.length() == 0) - { + if (m.seq.length() == 0) { generic = &m; - } - else if (input_mapping_is_match(m)) - { + } else if (input_mapping_is_match(m)) { input_mapping_execute(m, allow_commands); return; } } - if (generic) - { + if (generic) { input_mapping_execute(*generic, allow_commands); - } - else - { - //debug(0, L"no generic found, ignoring..."); + } else { + // debug(0, L"no generic found, ignoring..."); wchar_t c = input_common_readch(0); - if (c == R_EOF) - input_common_next_ch(c); + if (c == R_EOF) input_common_next_ch(c); } } -/* Helper function. Picks through the queue of incoming characters until we get to one that's not a readline function. */ -static wchar_t input_read_characters_only() -{ +/// Helper function. Picks through the queue of incoming characters until we get to one that's not a +/// readline function. +static wchar_t input_read_characters_only() { std::vector functions_to_put_back; wchar_t char_to_return; - for (;;) - { + for (;;) { char_to_return = input_common_readch(0); bool is_readline_function = (char_to_return >= R_MIN && char_to_return <= R_MAX); - // R_NULL and R_EOF are more control characters than readline functions, so check specially for those - if (!is_readline_function || char_to_return == R_NULL || char_to_return == R_EOF) - { + // R_NULL and R_EOF are more control characters than readline functions, so check specially + // for those. + if (!is_readline_function || char_to_return == R_NULL || char_to_return == R_EOF) { break; } - // This is a readline function; save it off for later re-enqueing and try again + // This is a readline function; save it off for later re-enqueing and try again. functions_to_put_back.push_back(char_to_return); } - // Restore any readline functions, in reverse to preserve their original order + // Restore any readline functions, in reverse to preserve their original order. size_t idx = functions_to_put_back.size(); - while (idx--) - { + while (idx--) { input_common_next_ch(functions_to_put_back.at(idx)); } return char_to_return; } -wint_t input_readch(bool allow_commands) -{ +wint_t input_readch(bool allow_commands) { CHECK_BLOCK(R_NULL); - /* - Clear the interrupted flag - */ + // Clear the interrupted flag. reader_reset_interrupted(); - - /* - Search for sequence in mapping tables - */ - - while (1) - { + // Search for sequence in mapping tables. + while (1) { wchar_t c = input_common_readch(0); - if (c >= R_MIN && c <= R_MAX) - { - switch (c) - { - case R_EOF: /* If it's closed, then just return */ + if (c >= R_MIN && c <= R_MAX) { + switch (c) { + case R_EOF: // if it's closed, then just return { return R_EOF; } - case R_SELF_INSERT: - { - /* #1595: ensure we only insert characters, not readline functions. The common case is that this will be empty. */ + case R_SELF_INSERT: { + // Issue #1595: ensure we only insert characters, not readline functions. The + // common case is that this will be empty. return input_read_characters_only(); } - case R_AND: - { - if (input_function_status) - { + case R_AND: { + if (input_function_status) { return input_readch(); - } - else - { - while ((c = input_common_readch(0)) && c >= R_MIN && c <= R_MAX); + } else { + while ((c = input_common_readch(0)) && c >= R_MIN && c <= R_MAX) + ; input_common_next_ch(c); return input_readch(); } } - default: - { - return c; - } + default: { return c; } } - } - else - { + } else { input_common_next_ch(c); input_mapping_execute_matching_or_generic(allow_commands); - // regarding allow_commands, we're in a loop, but if a fish command + // Regarding allow_commands, we're in a loop, but if a fish command // is executed, R_NULL is unread, so the next pass through the loop // we'll break out and return it. } } } -std::vector input_mapping_get_names() -{ - // Sort the mappings by the user specification order, so we can return them in the same order that the user specified them in +std::vector input_mapping_get_names() { + // Sort the mappings by the user specification order, so we can return them in the same order + // that the user specified them in. std::vector local_list = mapping_list; std::sort(local_list.begin(), local_list.end(), specification_order_is_less_than); std::vector result; result.reserve(local_list.size()); - for (size_t i=0; i::iterator it = mapping_list.begin(), end = mapping_list.end(); - it != end; - ++it) - { - if (sequence == it->seq && mode == it->mode) - { + it != end; ++it) { + if (sequence == it->seq && mode == it->mode) { mapping_list.erase(it); result = true; break; @@ -850,15 +679,13 @@ bool input_mapping_erase(const wcstring &sequence, const wcstring &mode) return result; } -bool input_mapping_get(const wcstring &sequence, const wcstring &mode, wcstring_list_t *out_cmds, wcstring *out_sets_mode) -{ +bool input_mapping_get(const wcstring &sequence, const wcstring &mode, wcstring_list_t *out_cmds, + wcstring *out_sets_mode) { bool result = false; - for (std::vector::const_iterator it = mapping_list.begin(), end = mapping_list.end(); - it != end; - ++it) - { - if (sequence == it->seq && mode == it->mode) - { + for (std::vector::const_iterator it = mapping_list.begin(), + end = mapping_list.end(); + it != end; ++it) { + if (sequence == it->seq && mode == it->mode) { *out_cmds = it->commands; *out_sets_mode = it->sets_mode; result = true; @@ -868,13 +695,9 @@ bool input_mapping_get(const wcstring &sequence, const wcstring &mode, wcstring_ return result; } -/** - Add all terminfo mappings - */ -static void input_terminfo_init() -{ - const terminfo_mapping_t tinfos[] = - { +/// Add all terminfo mappings. +static void input_terminfo_init() { + const terminfo_mapping_t tinfos[] = { TERMINFO_ADD(key_a1), TERMINFO_ADD(key_a3), TERMINFO_ADD(key_b2), @@ -921,13 +744,10 @@ static void input_terminfo_init() TERMINFO_ADD(key_f18), TERMINFO_ADD(key_f19), TERMINFO_ADD(key_f20), - /* - I know of no keyboard with more than 20 function keys, so - adding the rest here makes very little sense, since it will - take up a lot of room in any listings (like tab completions), - but with no benefit. - */ - /* +#if 0 + // I know of no keyboard with more than 20 function keys, so adding the rest here makes very + // little sense, since it will take up a lot of room in any listings (like tab completions), + // but with no benefit. TERMINFO_ADD(key_f21), TERMINFO_ADD(key_f22), TERMINFO_ADD(key_f23), @@ -970,7 +790,8 @@ static void input_terminfo_init() TERMINFO_ADD(key_f60), TERMINFO_ADD(key_f61), TERMINFO_ADD(key_f62), - TERMINFO_ADD(key_f63),*/ + TERMINFO_ADD(key_f63), +#endif TERMINFO_ADD(key_find), TERMINFO_ADD(key_help), TERMINFO_ADD(key_home), @@ -1037,8 +858,7 @@ static void input_terminfo_init() terminfo_mappings.insert(terminfo_mappings.end(), tinfos, tinfos + count); } -bool input_terminfo_get_sequence(const wchar_t *name, wcstring *out_seq) -{ +bool input_terminfo_get_sequence(const wchar_t *name, wcstring *out_seq) { ASSERT_IS_MAIN_THREAD(); const char *res = 0; @@ -1047,44 +867,36 @@ bool input_terminfo_get_sequence(const wchar_t *name, wcstring *out_seq) CHECK(name, 0); input_init(); - for (size_t i=0; iassign(m.name); return true; } @@ -1093,19 +905,16 @@ bool input_terminfo_get_name(const wcstring &seq, wcstring *out_name) return false; } -wcstring_list_t input_terminfo_get_names(bool skip_null) -{ +wcstring_list_t input_terminfo_get_names(bool skip_null) { wcstring_list_t result; result.reserve(terminfo_mappings.size()); input_init(); - for (size_t i=0; i #include #include -#include #include "common.h" #include "env.h" @@ -18,121 +15,93 @@ inputrc information for key bindings. wcstring describe_char(wint_t c); -/** - Initialize the terminal by calling setupterm, and set up arrays - used by readch to detect escape sequences for special keys. - - Before calling input_init, terminfo is not initialized and MUST not be used -*/ +/// Initialize the terminal by calling setupterm, and set up arrays used by readch to detect escape +/// sequences for special keys. +/// +/// Before calling input_init, terminfo is not initialized and MUST not be used. int input_init(); -/** - free up memory used by terminal functions. -*/ +/// free up memory used by terminal functions. void input_destroy(); -/** - Read a character from fd 0. Try to convert some escape sequences - into character constants, but do not permanently block the escape - character. - - This is performed in the same way vim does it, i.e. if an escape - character is read, wait for more input for a short time (a few - milliseconds). If more input is avaialable, it is assumed to be an - escape sequence for a special character (such as an arrow key), and - readch attempts to parse it. If no more input follows after the - escape key, it is assumed to be an actual escape key press, and is - returned as such. - - The argument determines whether fish commands are allowed to be run - as bindings. If false, when a character is encountered that would - invoke a fish command, it is unread and R_NULL is returned. -*/ +/// Read a character from fd 0. Try to convert some escape sequences into character constants, but +/// do not permanently block the escape character. +/// +/// This is performed in the same way vim does it, i.e. if an escape character is read, wait for +/// more input for a short time (a few milliseconds). If more input is avaialable, it is assumed to +/// be an escape sequence for a special character (such as an arrow key), and readch attempts to +/// parse it. If no more input follows after the escape key, it is assumed to be an actual escape +/// key press, and is returned as such. +/// +/// The argument determines whether fish commands are allowed to be run as bindings. If false, when +/// a character is encountered that would invoke a fish command, it is unread and R_NULL is +/// returned. wint_t input_readch(bool allow_commands = true); -/** - Enqueue a character or a readline function to the queue of unread - characters that input_readch will return before actually reading from fd - 0. - */ +/// Enqueue a character or a readline function to the queue of unread characters that input_readch +/// will return before actually reading from fd 0. void input_queue_ch(wint_t ch); - -/** - Add a key mapping from the specified sequence to the specified command - - \param sequence the sequence to bind - \param command an input function that will be run whenever the key sequence occurs -*/ +/// Add a key mapping from the specified sequence to the specified command. +/// +/// \param sequence the sequence to bind +/// \param command an input function that will be run whenever the key sequence occurs void input_mapping_add(const wchar_t *sequence, const wchar_t *command, const wchar_t *mode = DEFAULT_BIND_MODE, const wchar_t *new_mode = DEFAULT_BIND_MODE); -void input_mapping_add(const wchar_t *sequence, const wchar_t * const *commands, size_t commands_len, - const wchar_t *mode = DEFAULT_BIND_MODE, const wchar_t *new_mode = DEFAULT_BIND_MODE); +void input_mapping_add(const wchar_t *sequence, const wchar_t *const *commands, size_t commands_len, + const wchar_t *mode = DEFAULT_BIND_MODE, + const wchar_t *new_mode = DEFAULT_BIND_MODE); struct input_mapping_name_t { wcstring seq; wcstring mode; }; -/** - Returns all mapping names and modes - */ +/// Returns all mapping names and modes. std::vector input_mapping_get_names(); -/** - Erase binding for specified key sequence - */ +/// Erase binding for specified key sequence. bool input_mapping_erase(const wcstring &sequence, const wcstring &mode = DEFAULT_BIND_MODE); -/** - Gets the command bound to the specified key sequence in the specified mode. Returns true if it exists, false if not. - */ -bool input_mapping_get(const wcstring &sequence, const wcstring &mode, wcstring_list_t *out_cmds, wcstring *out_new_mode); +/// Gets the command bound to the specified key sequence in the specified mode. Returns true if it +/// exists, false if not. +bool input_mapping_get(const wcstring &sequence, const wcstring &mode, wcstring_list_t *out_cmds, + wcstring *out_new_mode); -/** - Return the current bind mode -*/ +/// Return the current bind mode. wcstring input_get_bind_mode(); -/** - Set the current bind mode -*/ +/// Set the current bind mode. void input_set_bind_mode(const wcstring &bind_mode); - wchar_t input_function_pop_arg(); - -/** - Sets the return status of the most recently executed input function -*/ +/// Sets the return status of the most recently executed input function. void input_function_set_status(bool status); -/** - Return the sequence for the terminfo variable of the specified name. - - If no terminfo variable of the specified name could be found, return false and set errno to ENOENT. - If the terminfo variable does not have a value, return false and set errno to EILSEQ. - */ +/// Return the sequence for the terminfo variable of the specified name. +/// +/// If no terminfo variable of the specified name could be found, return false and set errno to +/// ENOENT. If the terminfo variable does not have a value, return false and set errno to EILSEQ. bool input_terminfo_get_sequence(const wchar_t *name, wcstring *out_seq); -/** Return the name of the terminfo variable with the specified sequence, in out_name. Returns true if found, false if not found. */ +/// Return the name of the terminfo variable with the specified sequence, in out_name. Returns true +/// if found, false if not found. bool input_terminfo_get_name(const wcstring &seq, wcstring *out_name); -/** Return a list of all known terminfo names */ +/// Return a list of all known terminfo names. wcstring_list_t input_terminfo_get_names(bool skip_null); -/** Returns the input function code for the given input function name. */ +/// Returns the input function code for the given input function name. #define INPUT_CODE_NONE (wchar_t(-1)) wchar_t input_function_get_code(const wcstring &name); -/** Returns a list of all existing input function names */ +/// Returns a list of all existing input function names. wcstring_list_t input_function_get_names(void); -/** Updates our idea of whether we support term256 and term24bit */ +/// Updates our idea of whether we support term256 and term24bit. void update_fish_color_support(); - #endif -- cgit v1.2.3