diff --git a/src/autoload.cpp b/src/autoload.cpp index 6f7c39058..31f6e6df3 100644 --- a/src/autoload.cpp +++ b/src/autoload.cpp @@ -1,18 +1,18 @@ // The classes responsible for autoloading functions and completions. #include #include +#include +#include +#include #include +#include #include #include #include +#include #include #include #include -#include -#include -#include -#include -#include #include "autoload.h" #include "common.h" diff --git a/src/autoload.h b/src/autoload.h index 5b1d5dfe4..1d6d4a618 100644 --- a/src/autoload.h +++ b/src/autoload.h @@ -3,10 +3,10 @@ #define FISH_AUTOLOAD_H #include +#include #include #include #include -#include #include "common.h" #include "lru.h" diff --git a/src/builtin.cpp b/src/builtin.cpp index 70b379b1f..8ed2858ba 100644 --- a/src/builtin.cpp +++ b/src/builtin.cpp @@ -1,4 +1,3 @@ -// // Functions for executing builtin functions. // // How to add a new builtin function: @@ -1895,17 +1894,18 @@ static int builtin_read(parser_t &parser, io_streams_t &streams, wchar_t **argv) nchars = fish_wcstoi(w.woptarg, &end, 10); if (errno || *end != 0) { switch (errno) { - case ERANGE: + case ERANGE: { streams.err.append_format(_(L"%ls: Argument '%ls' is out of range\n"), argv[0], w.woptarg); builtin_print_help(parser, streams, argv[0], streams.err); return STATUS_BUILTIN_ERROR; - - default: + } + default: { streams.err.append_format( _(L"%ls: Argument '%ls' must be an integer\n"), argv[0], w.woptarg); builtin_print_help(parser, streams, argv[0], streams.err); return STATUS_BUILTIN_ERROR; + } } } break; diff --git a/src/builtin_printf.cpp b/src/builtin_printf.cpp index a3df00d39..a755d27ab 100644 --- a/src/builtin_printf.cpp +++ b/src/builtin_printf.cpp @@ -596,21 +596,24 @@ int builtin_printf_state_t::print_formatted(const wchar_t *format, int argc, wch for (;; f++, direc_length++) { switch (*f) { case L'I': - case L'\'': + case L'\'': { modify_allowed_format_specifiers(ok, "aAceEosxX", false); break; + } case '-': case '+': - case ' ': + case ' ': { break; - case L'#': + } + case L'#': { modify_allowed_format_specifiers(ok, "cdisu", false); break; - case '0': + } + case '0': { modify_allowed_format_specifiers(ok, "cs", false); break; - default: - goto no_more_flag_characters; + } + default: { goto no_more_flag_characters; } } } no_more_flag_characters:; diff --git a/src/builtin_set.cpp b/src/builtin_set.cpp index 2bc60d8b8..f4f49baf9 100644 --- a/src/builtin_set.cpp +++ b/src/builtin_set.cpp @@ -412,12 +412,11 @@ int builtin_set(parser_t &parser, io_streams_t &streams, wchar_t **argv) { builtin_print_help(parser, streams, argv[0], streams.out); return 0; } - case '?': + case '?': { builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); return 1; - - default: - break; + } + default: { break; } } } diff --git a/src/color.cpp b/src/color.cpp index 006518052..6b920d15d 100644 --- a/src/color.cpp +++ b/src/color.cpp @@ -311,20 +311,26 @@ rgb_color_t::rgb_color_t(const std::string &str) : type(), flags() { wcstring rgb_color_t::description() const { switch (type) { - case type_none: + case type_none: { return L"none"; - case type_named: + } + case type_named: { return format_string(L"named(%d: %ls)", (int)data.name_idx, name_for_color_idx(data.name_idx)); - case type_rgb: + } + case type_rgb: { return format_string(L"rgb(0x%02x%02x%02x)", data.color.rgb[0], data.color.rgb[1], data.color.rgb[2]); - case type_reset: + } + case type_reset: { return L"reset"; - case type_normal: + } + case type_normal: { return L"normal"; - default: + } + default: { abort(); return L""; + } } } diff --git a/src/complete.cpp b/src/complete.cpp index db63e3487..198ad2e31 100644 --- a/src/complete.cpp +++ b/src/complete.cpp @@ -1,143 +1,114 @@ /** \file complete.c Functions related to tab-completion. - These functions are used for storing and retrieving tab-completion data, as well as for performing tab-completion. + These functions are used for storing and retrieving tab-completion data, as well as for performing + tab-completion. */ #include +#include +#include +#include #include #include #include -#include -#include #include #include #include +#include #include #include #include -#include -#include -#include "fallback.h" // IWYU pragma: keep -#include "util.h" -#include "wildcard.h" -#include "proc.h" -#include "parser.h" -#include "function.h" -#include "complete.h" +#include "autoload.h" #include "builtin.h" +#include "common.h" +#include "complete.h" #include "env.h" #include "exec.h" #include "expand.h" -#include "common.h" -#include "parse_util.h" -#include "wutil.h" // IWYU pragma: keep -#include "path.h" -#include "parse_tree.h" +#include "fallback.h" // IWYU pragma: keep +#include "function.h" #include "iothread.h" -#include "autoload.h" #include "parse_constants.h" +#include "parse_tree.h" +#include "parse_util.h" +#include "parser.h" +#include "path.h" +#include "proc.h" +#include "util.h" +#include "wildcard.h" +#include "wutil.h" // IWYU pragma: keep -/* - Completion description strings, mostly for different types of files, such as sockets, block devices, etc. +// Completion description strings, mostly for different types of files, such as sockets, block +// devices, etc. +// +// There are a few more completion description strings defined in expand.c. Maybe all completion +// description strings should be defined in the same file? - There are a few more completion description strings defined in - expand.c. Maybe all completion description strings should be defined - in the same file? -*/ +/// Description for ~USER completion. +#define COMPLETE_USER_DESC _(L"Home for %ls") -/** - Description for ~USER completion -*/ -#define COMPLETE_USER_DESC _( L"Home for %ls" ) +/// Description for short variables. The value is concatenated to this description. +#define COMPLETE_VAR_DESC_VAL _(L"Variable: %ls") -/** - Description for short variables. The value is concatenated to this description -*/ -#define COMPLETE_VAR_DESC_VAL _( L"Variable: %ls" ) - -/** - The special cased translation macro for completions. The empty - string needs to be special cased, since it can occur, and should - not be translated. (Gettext returns the version information as the - response) -*/ +/// The special cased translation macro for completions. The empty string needs to be special cased, +/// since it can occur, and should not be translated. (Gettext returns the version information as +/// the response). #ifdef USE_GETTEXT -static const wchar_t *C_(const wcstring &s) -{ - return s.empty() ? L"" : wgettext(s.c_str()); -} +static const wchar_t *C_(const wcstring &s) { return s.empty() ? L"" : wgettext(s.c_str()); } #else -static const wcstring &C_(const wcstring &s) -{ - return s; -} +static const wcstring &C_(const wcstring &s) { return s; } #endif static void complete_load(const wcstring &name, bool reload); -/* Testing apparatus */ +// Testing apparatus. const wcstring_list_t *s_override_variable_names = NULL; -void complete_set_variable_names(const wcstring_list_t *names) -{ +void complete_set_variable_names(const wcstring_list_t *names) { s_override_variable_names = names; } -static inline wcstring_list_t complete_get_variable_names(void) -{ - if (s_override_variable_names != NULL) - { +static inline wcstring_list_t complete_get_variable_names(void) { + if (s_override_variable_names != NULL) { return *s_override_variable_names; - } - else - { + } else { return env_get_names(0); } } -/** - Struct describing a completion option entry. - - If option is empty, the comp field must not be - empty and contains a list of arguments to the command. - - The type field determines how the option is to be interpreted: - either empty (args_only) or short, single-long ("old") or double-long ("GNU"). - An invariant is that the option is empty if and only if the type is args_only. - - If option is non-empty, it specifies a switch - for the command. If \c comp is also not empty, it contains a list - of non-switch arguments that may only follow directly after the - specified switch. -*/ -typedef struct complete_entry_opt -{ - /* Text of the option (like 'foo') */ +/// Struct describing a completion option entry. +/// +/// If option is empty, the comp field must not be empty and contains a list of arguments to the +/// command. +/// +/// The type field determines how the option is to be interpreted: either empty (args_only) or +/// short, single-long ("old") or double-long ("GNU"). An invariant is that the option is empty if +/// and only if the type is args_only. +/// +/// If option is non-empty, it specifies a switch for the command. If \c comp is also not empty, it +/// contains a list of non-switch arguments that may only follow directly after the specified +/// switch. +typedef struct complete_entry_opt { + // Text of the option (like 'foo'). wcstring option; - /* Type of the option: args-oly, short, single_long, or double_long */ + // Type of the option: args-oly, short, single_long, or double_long. complete_option_type_t type; - /** Arguments to the option */ + // Arguments to the option. wcstring comp; - /** Description of the completion */ + // Description of the completion. wcstring desc; - /** Condition under which to use the option */ + // Condition under which to use the option. wcstring condition; - /** Must be one of the values SHARED, NO_FILES, NO_COMMON, - EXCLUSIVE, and determines how completions should be performed - on the argument after the switch. */ + // Must be one of the values SHARED, NO_FILES, NO_COMMON, EXCLUSIVE, and determines how + // completions should be performed on the argument after the switch. int result_mode; - /** Completion flags */ + // Completion flags. complete_flags_t flags; - const wcstring localized_desc() const - { - return C_(desc); - } - - size_t expected_dash_count() const - { - switch (this->type) - { + const wcstring localized_desc() const { return C_(desc); } + + size_t expected_dash_count() const { + switch (this->type) { case option_type_args_only: return 0; case option_type_short: @@ -148,65 +119,49 @@ typedef struct complete_entry_opt } assert(0 && "Unreachable"); } - + } complete_entry_opt_t; -/* Last value used in the order field of completion_entry_t */ +// Last value used in the order field of completion_entry_t. static unsigned int kCompleteOrder = 0; -/** - Struct describing a command completion -*/ +/// Struct describing a command completion. typedef std::list option_list_t; -class completion_entry_t -{ -public: - /** List of all options */ +class completion_entry_t { + public: + /// List of all options. option_list_t options; -public: - - /** Command string */ + public: + /// Command string. const wcstring cmd; - - /** True if command is a path */ + /// True if command is a path. const bool cmd_is_path; - - /** True if no other options than the ones supplied are possible */ + /// True if no other options than the ones supplied are possible. bool authoritative; - - /** Order for when this completion was created. This aids in outputting completions sorted by time. */ + /// Order for when this completion was created. This aids in outputting completions sorted by + /// time. const unsigned int order; - /** Getters for option list. */ + /// Getters for option list. const option_list_t &get_options() const; - /** Adds or removes an option. */ + /// Adds or removes an option. void add_option(const complete_entry_opt_t &opt); bool remove_option(const wcstring &option, complete_option_type_t type); - completion_entry_t(const wcstring &c, bool type, bool author) : - cmd(c), - cmd_is_path(type), - authoritative(author), - order(++kCompleteOrder) - { - } + completion_entry_t(const wcstring &c, bool type, bool author) + : cmd(c), cmd_is_path(type), authoritative(author), order(++kCompleteOrder) {} }; -/** Set of all completion entries */ -struct completion_entry_set_comparer -{ +/// Set of all completion entries. +struct completion_entry_set_comparer { /** Comparison for std::set */ - bool operator()(const completion_entry_t &p1, const completion_entry_t &p2) const - { - /* Paths always come last for no particular reason */ - if (p1.cmd_is_path != p2.cmd_is_path) - { + bool operator()(const completion_entry_t &p1, const completion_entry_t &p2) const { + // Paths always come last for no particular reason. + if (p1.cmd_is_path != p2.cmd_is_path) { return p1.cmd_is_path < p2.cmd_is_path; - } - else - { + } else { return p1.cmd < p2.cmd; } } @@ -214,62 +169,51 @@ struct completion_entry_set_comparer typedef std::set completion_entry_set_t; static completion_entry_set_t completion_set; -// Comparison function to sort completions by their order field -static bool compare_completions_by_order(const completion_entry_t *p1, const completion_entry_t *p2) -{ +// Comparison function to sort completions by their order field. +static bool compare_completions_by_order(const completion_entry_t *p1, + const completion_entry_t *p2) { return p1->order < p2->order; } -/** The lock that guards the list of completion entries */ +/// The lock that guards the list of completion entries. static pthread_mutex_t completion_lock = PTHREAD_MUTEX_INITIALIZER; - -void completion_entry_t::add_option(const complete_entry_opt_t &opt) -{ +void completion_entry_t::add_option(const complete_entry_opt_t &opt) { ASSERT_IS_LOCKED(completion_lock); options.push_front(opt); } -const option_list_t &completion_entry_t::get_options() const -{ +const option_list_t &completion_entry_t::get_options() const { ASSERT_IS_LOCKED(completion_lock); return options; } -completion_t::~completion_t() -{ -} +completion_t::~completion_t() {} -/* Clear the COMPLETE_AUTO_SPACE flag, and set COMPLETE_NO_SPACE appropriately depending on the suffix of the string */ -static complete_flags_t resolve_auto_space(const wcstring &comp, complete_flags_t flags) -{ - if (flags & COMPLETE_AUTO_SPACE) - { +// Clear the COMPLETE_AUTO_SPACE flag, and set COMPLETE_NO_SPACE appropriately depending on the +// suffix of the string. +static complete_flags_t resolve_auto_space(const wcstring &comp, complete_flags_t flags) { + if (flags & COMPLETE_AUTO_SPACE) { flags = flags & ~COMPLETE_AUTO_SPACE; size_t len = comp.size(); - if (len > 0 && (wcschr(L"/=@:", comp.at(len-1)) != 0)) - flags |= COMPLETE_NO_SPACE; + if (len > 0 && (wcschr(L"/=@:", comp.at(len - 1)) != 0)) flags |= COMPLETE_NO_SPACE; } return flags; } -/* completion_t functions. Note that the constructor resolves flags! */ -completion_t::completion_t(const wcstring &comp, const wcstring &desc, string_fuzzy_match_t mat, complete_flags_t flags_val) : - completion(comp), - description(desc), - match(mat), - flags(resolve_auto_space(comp, flags_val)) -{ -} +// completion_t functions. Note that the constructor resolves flags! +completion_t::completion_t(const wcstring &comp, const wcstring &desc, string_fuzzy_match_t mat, + complete_flags_t flags_val) + : completion(comp), description(desc), match(mat), flags(resolve_auto_space(comp, flags_val)) {} -completion_t::completion_t(const completion_t &him) : completion(him.completion), description(him.description), match(him.match), flags(him.flags) -{ -} +completion_t::completion_t(const completion_t &him) + : completion(him.completion), + description(him.description), + match(him.match), + flags(him.flags) {} -completion_t &completion_t::operator=(const completion_t &him) -{ - if (this != &him) - { +completion_t &completion_t::operator=(const completion_t &him) { + if (this != &him) { this->completion = him.completion; this->description = him.description; this->match = him.match; @@ -278,140 +222,105 @@ completion_t &completion_t::operator=(const completion_t &him) return *this; } -bool completion_t::is_naturally_less_than(const completion_t &a, const completion_t &b) -{ +bool completion_t::is_naturally_less_than(const completion_t &a, const completion_t &b) { return wcsfilecmp(a.completion.c_str(), b.completion.c_str()) < 0; } -bool completion_t::is_alphabetically_equal_to(const completion_t &a, const completion_t &b) -{ +bool completion_t::is_alphabetically_equal_to(const completion_t &a, const completion_t &b) { return a.completion == b.completion; } -void completion_t::prepend_token_prefix(const wcstring &prefix) -{ - if (this->flags & COMPLETE_REPLACES_TOKEN) - { +void completion_t::prepend_token_prefix(const wcstring &prefix) { + if (this->flags & COMPLETE_REPLACES_TOKEN) { this->completion.insert(0, prefix); } } - -static bool compare_completions_by_match_type(const completion_t &a, const completion_t &b) -{ +static bool compare_completions_by_match_type(const completion_t &a, const completion_t &b) { return a.match.type < b.match.type; } -void completions_sort_and_prioritize(std::vector *comps) -{ - /* Find the best match type */ +void completions_sort_and_prioritize(std::vector *comps) { + // Find the best match type. fuzzy_match_type_t best_type = fuzzy_match_none; - for (size_t i=0; i < comps->size(); i++) - { + for (size_t i = 0; i < comps->size(); i++) { best_type = std::min(best_type, comps->at(i).match.type); } - /* If the best type is an exact match, reduce it to prefix match. Otherwise a tab completion will only show one match if it matches a file exactly. (see issue #959) */ - if (best_type == fuzzy_match_exact) - { + // If the best type is an exact match, reduce it to prefix match. Otherwise a tab completion + // will only show one match if it matches a file exactly. (see issue #959). + if (best_type == fuzzy_match_exact) { best_type = fuzzy_match_prefix; } - - /* Throw out completions whose match types are less suitable than the best. */ + + // Throw out completions whose match types are less suitable than the best. size_t i = comps->size(); - while (i--) - { - if (comps->at(i).match.type > best_type) - { + while (i--) { + if (comps->at(i).match.type > best_type) { comps->erase(comps->begin() + i); } } - - /* Remove duplicates */ + + // Remove duplicates. sort(comps->begin(), comps->end(), completion_t::is_naturally_less_than); - comps->erase(std::unique(comps->begin(), comps->end(), completion_t::is_alphabetically_equal_to), comps->end()); - - /* Sort the remainder by match type. They're already sorted alphabetically */ + comps->erase( + std::unique(comps->begin(), comps->end(), completion_t::is_alphabetically_equal_to), + comps->end()); + + // Sort the remainder by match type. They're already sorted alphabetically. stable_sort(comps->begin(), comps->end(), compare_completions_by_match_type); } -/** Class representing an attempt to compute completions */ -class completer_t -{ +/// Class representing an attempt to compute completions. +class completer_t { const completion_request_flags_t flags; const wcstring initial_cmd; std::vector completions; - const env_vars_snapshot_t &vars; //transient, stack-allocated + const env_vars_snapshot_t &vars; // transient, stack-allocated - /** Table of completions conditions that have already been tested and the corresponding test results */ + /// Table of completions conditions that have already been tested and the corresponding test + /// results. typedef std::map condition_cache_t; condition_cache_t condition_cache; - enum complete_type_t - { - COMPLETE_DEFAULT, - COMPLETE_AUTOSUGGEST - }; + enum complete_type_t { COMPLETE_DEFAULT, COMPLETE_AUTOSUGGEST }; - complete_type_t type() const - { - return (flags & COMPLETION_REQUEST_AUTOSUGGESTION) ? COMPLETE_AUTOSUGGEST : COMPLETE_DEFAULT; + complete_type_t type() const { + return (flags & COMPLETION_REQUEST_AUTOSUGGESTION) ? COMPLETE_AUTOSUGGEST + : COMPLETE_DEFAULT; } - bool wants_descriptions() const - { - return !!(flags & COMPLETION_REQUEST_DESCRIPTIONS); + bool wants_descriptions() const { return !!(flags & COMPLETION_REQUEST_DESCRIPTIONS); } + + bool fuzzy() const { return !!(flags & COMPLETION_REQUEST_FUZZY_MATCH); } + + fuzzy_match_type_t max_fuzzy_match_type() const { + // If we are doing fuzzy matching, request all types; if not request only prefix matching. + return (flags & COMPLETION_REQUEST_FUZZY_MATCH) ? fuzzy_match_none + : fuzzy_match_prefix_case_insensitive; } - bool fuzzy() const - { - return !!(flags & COMPLETION_REQUEST_FUZZY_MATCH); - } + public: + completer_t(const wcstring &c, completion_request_flags_t f, const env_vars_snapshot_t &evs) + : flags(f), initial_cmd(c), vars(evs) {} - fuzzy_match_type_t max_fuzzy_match_type() const - { - /* If we are doing fuzzy matching, request all types; if not request only prefix matching */ - return (flags & COMPLETION_REQUEST_FUZZY_MATCH) ? fuzzy_match_none : fuzzy_match_prefix_case_insensitive; - } - - -public: - completer_t(const wcstring &c, completion_request_flags_t f, const env_vars_snapshot_t &evs) : - flags(f), - initial_cmd(c), - vars(evs) - { - } - - bool empty() const - { - return completions.empty(); - } - const std::vector &get_completions(void) - { - return completions; - } + bool empty() const { return completions.empty(); } + const std::vector &get_completions(void) { return completions; } bool try_complete_variable(const wcstring &str); bool try_complete_user(const wcstring &str); - bool complete_param(const wcstring &cmd_orig, - const wcstring &popt, - const wcstring &str, + bool complete_param(const wcstring &cmd_orig, const wcstring &popt, const wcstring &str, bool use_switches); - void complete_param_expand(const wcstring &str, bool do_file, bool handle_as_special_cd = false); - + void complete_param_expand(const wcstring &str, bool do_file, + bool handle_as_special_cd = false); + void complete_special_cd(const wcstring &str); - void complete_cmd(const wcstring &str, - bool use_function, - bool use_builtin, - bool use_command, + void complete_cmd(const wcstring &str, bool use_function, bool use_builtin, bool use_command, bool use_implicit_cd); - void complete_from_args(const wcstring &str, - const wcstring &args, - const wcstring &desc, + void complete_from_args(const wcstring &str, const wcstring &args, const wcstring &desc, complete_flags_t flags); void complete_cmd_desc(const wcstring &str); @@ -420,56 +329,51 @@ public: bool condition_test(const wcstring &condition); - void complete_strings(const wcstring &wc_escaped, - const wchar_t *desc, - wcstring(*desc_func)(const wcstring &), - std::vector &possible_comp, - complete_flags_t flags); + void complete_strings(const wcstring &wc_escaped, const wchar_t *desc, + wcstring (*desc_func)(const wcstring &), + std::vector &possible_comp, complete_flags_t flags); - expand_flags_t expand_flags() const - { - /* Never do command substitution in autosuggestions. Sadly, we also can't yet do job expansion because it's not thread safe. */ + expand_flags_t expand_flags() const { + // Never do command substitution in autosuggestions. Sadly, we also can't yet do job + // expansion because it's not thread safe. expand_flags_t result = 0; - if (this->type() == COMPLETE_AUTOSUGGEST) - result |= EXPAND_SKIP_CMDSUBST; + if (this->type() == COMPLETE_AUTOSUGGEST) result |= EXPAND_SKIP_CMDSUBST; - /* Allow fuzzy matching */ - if (this->fuzzy()) - result |= EXPAND_FUZZY_MATCH; + // Allow fuzzy matching. + if (this->fuzzy()) result |= EXPAND_FUZZY_MATCH; return result; } }; -/* Autoloader for completions */ -class completion_autoload_t : public autoload_t -{ -public: +/// Autoloader for completions. +class completion_autoload_t : public autoload_t { + public: completion_autoload_t(); virtual void command_removed(const wcstring &cmd); }; static completion_autoload_t completion_autoloader; -/** Constructor */ -completion_autoload_t::completion_autoload_t() : autoload_t(L"fish_complete_path", NULL, 0) -{ -} +// Constructor +completion_autoload_t::completion_autoload_t() : autoload_t(L"fish_complete_path", NULL, 0) {} -/** Callback when an autoloaded completion is removed */ -void completion_autoload_t::command_removed(const wcstring &cmd) -{ +/// Callback when an autoloaded completion is removed. +void completion_autoload_t::command_removed(const wcstring &cmd) { complete_remove_all(cmd, false /* not a path */); } - -/** Create a new completion entry. */ -void append_completion(std::vector *completions, const wcstring &comp, const wcstring &desc, complete_flags_t flags, string_fuzzy_match_t match) -{ - /* If we just constructed the completion and used push_back, we would get two string copies. Try to avoid that by making a stubby completion in the vector first, and then copying our string in. Note that completion_t's constructor will munge 'flags' so it's important that we pass those to the constructor. - - Nasty hack for #1241 - since the constructor needs the completion string to resolve AUTO_SPACE, and we aren't providing it with the completion, we have to do the resolution ourselves. We should get this resolving out of the constructor. - */ +/// Create a new completion entry. +void append_completion(std::vector *completions, const wcstring &comp, + const wcstring &desc, complete_flags_t flags, string_fuzzy_match_t match) { + // If we just constructed the completion and used push_back, we would get two string copies. Try + // to avoid that by making a stubby completion in the vector first, and then copying our string + // in. Note that completion_t's constructor will munge 'flags' so it's important that we pass + // those to the constructor. + // + // Nasty hack for #1241 - since the constructor needs the completion string to resolve + // AUTO_SPACE, and we aren't providing it with the completion, we have to do the resolution + // ourselves. We should get this resolving out of the constructor. assert(completions != NULL); const wcstring empty; completions->push_back(completion_t(empty, empty, match, resolve_auto_space(comp, flags))); @@ -478,23 +382,17 @@ void append_completion(std::vector *completions, const wcstring &c last->description = desc; } -/** - Test if the specified script returns zero. The result is cached, so - that if multiple completions use the same condition, it needs only - be evaluated once. condition_cache_clear must be called after a - completion run to make sure that there are no stale completions. -*/ -bool completer_t::condition_test(const wcstring &condition) -{ - if (condition.empty()) - { -// fwprintf( stderr, L"No condition specified\n" ); +/// Test if the specified script returns zero. The result is cached, so that if multiple completions +/// use the same condition, it needs only be evaluated once. condition_cache_clear must be called +/// after a completion run to make sure that there are no stale completions. +bool completer_t::condition_test(const wcstring &condition) { + if (condition.empty()) { + // fwprintf( stderr, L"No condition specified\n" ); return 1; } - if (this->type() == COMPLETE_AUTOSUGGEST) - { - /* Autosuggestion can't support conditions */ + if (this->type() == COMPLETE_AUTOSUGGEST) { + // Autosuggestion can't support conditions. return 0; } @@ -502,66 +400,51 @@ bool completer_t::condition_test(const wcstring &condition) bool test_res; condition_cache_t::iterator cached_entry = condition_cache.find(condition); - if (cached_entry == condition_cache.end()) - { - /* Compute new value and reinsert it */ + if (cached_entry == condition_cache.end()) { + // Compute new value and reinsert it. test_res = (0 == exec_subshell(condition, false /* don't apply exit status */)); condition_cache[condition] = test_res; - } - else - { - /* Use the old value */ + } else { + // Use the old value. test_res = cached_entry->second; } return test_res; } - -/** Locate the specified entry. Create it if it doesn't exist. Must be called while locked. */ -static completion_entry_t &complete_get_exact_entry(const wcstring &cmd, bool cmd_is_path) -{ +/// Locate the specified entry. Create it if it doesn't exist. Must be called while locked. +static completion_entry_t &complete_get_exact_entry(const wcstring &cmd, bool cmd_is_path) { ASSERT_IS_LOCKED(completion_lock); std::pair ins = completion_set.insert(completion_entry_t(cmd, cmd_is_path, false)); - // NOTE SET_ELEMENTS_ARE_IMMUTABLE: Exposing mutable access here is only - // okay as long as callers do not change any field that matters to ordering - // - affecting order without telling std::set invalidates its internal state - return const_cast(*ins.first); + // NOTE SET_ELEMENTS_ARE_IMMUTABLE: Exposing mutable access here is only okay as long as callers + // do not change any field that matters to ordering - affecting order without telling std::set + // invalidates its internal state. + return const_cast(*ins.first); } - -void complete_set_authoritative(const wchar_t *cmd, bool cmd_is_path, bool authoritative) -{ - CHECK(cmd,); +void complete_set_authoritative(const wchar_t *cmd, bool cmd_is_path, bool authoritative) { + CHECK(cmd, ); scoped_lock lock(completion_lock); completion_entry_t &c = complete_get_exact_entry(cmd, cmd_is_path); c.authoritative = authoritative; } - -void complete_add(const wchar_t *cmd, - bool cmd_is_path, - const wcstring &option, - complete_option_type_t option_type, - int result_mode, - const wchar_t *condition, - const wchar_t *comp, - const wchar_t *desc, - complete_flags_t flags) -{ - CHECK(cmd,); - // option should be empty iff the option type is arguments only +void complete_add(const wchar_t *cmd, bool cmd_is_path, const wcstring &option, + complete_option_type_t option_type, int result_mode, const wchar_t *condition, + const wchar_t *comp, const wchar_t *desc, complete_flags_t flags) { + CHECK(cmd, ); + // option should be empty iff the option type is arguments only. assert(option.empty() == (option_type == option_type_args_only)); - /* Lock the lock that allows us to edit the completion entry list */ + // Lock the lock that allows us to edit the completion entry list. scoped_lock lock(completion_lock); completion_entry_t &c = complete_get_exact_entry(cmd, cmd_is_path); - /* Create our new option */ + // Create our new option. complete_entry_opt_t opt; opt.option = option; opt.type = option_type; @@ -575,302 +458,227 @@ void complete_add(const wchar_t *cmd, c.add_option(opt); } -/** - Remove all completion options in the specified entry that match the - specified short / long option strings. Returns true if it is now - empty and should be deleted, false if it's not empty. Must be called while locked. -*/ -bool completion_entry_t::remove_option(const wcstring &option, complete_option_type_t type) -{ +/// Remove all completion options in the specified entry that match the specified short / long +/// option strings. Returns true if it is now empty and should be deleted, false if it's not empty. +/// Must be called while locked. +bool completion_entry_t::remove_option(const wcstring &option, complete_option_type_t type) { ASSERT_IS_LOCKED(completion_lock); option_list_t::iterator iter = this->options.begin(); - while (iter != this->options.end()) - { - if (iter->option == option && iter->type == type) - { + while (iter != this->options.end()) { + if (iter->option == option && iter->type == type) { iter = this->options.erase(iter); - } - else - { - /* Just go to the next one */ + } else { + // Just go to the next one. ++iter; } } return this->options.empty(); } - -void complete_remove(const wcstring &cmd, bool cmd_is_path, const wcstring &option, complete_option_type_t type) -{ +void complete_remove(const wcstring &cmd, bool cmd_is_path, const wcstring &option, + complete_option_type_t type) { scoped_lock lock(completion_lock); completion_entry_t tmp_entry(cmd, cmd_is_path, false); completion_entry_set_t::iterator iter = completion_set.find(tmp_entry); - if (iter != completion_set.end()) - { - // const_cast: See SET_ELEMENTS_ARE_IMMUTABLE - completion_entry_t &entry = const_cast(*iter); + if (iter != completion_set.end()) { + // const_cast: See SET_ELEMENTS_ARE_IMMUTABLE. + completion_entry_t &entry = const_cast(*iter); bool delete_it = entry.remove_option(option, type); - if (delete_it) - { - /* Delete this entry */ + if (delete_it) { + // Delete this entry. completion_set.erase(iter); } } } -void complete_remove_all(const wcstring &cmd, bool cmd_is_path) -{ +void complete_remove_all(const wcstring &cmd, bool cmd_is_path) { scoped_lock lock(completion_lock); - + completion_entry_t tmp_entry(cmd, cmd_is_path, false); completion_set.erase(tmp_entry); } - -/** - Find the full path and commandname from a command string 'str'. -*/ -static void parse_cmd_string(const wcstring &str, wcstring &path, wcstring &cmd) -{ - if (! path_get_path(str, &path)) - { - /** Use the empty string as the 'path' for commands that can not be found. */ +/// Find the full path and commandname from a command string 'str'. +static void parse_cmd_string(const wcstring &str, wcstring &path, wcstring &cmd) { + if (!path_get_path(str, &path)) { + /// Use the empty string as the 'path' for commands that can not be found. path = L""; } - /* Make sure the path is not included in the command */ + // Make sure the path is not included in the command. size_t last_slash = str.find_last_of(L'/'); - if (last_slash != wcstring::npos) - { + if (last_slash != wcstring::npos) { cmd = str.substr(last_slash + 1); - } - else - { + } else { cmd = str; } } -/** - Copy any strings in possible_comp which have the specified prefix - to the completer's completion array. The prefix may contain wildcards. - The output will consist of completion_t structs. - - There are three ways to specify descriptions for each - completion. Firstly, if a description has already been added to the - completion, it is _not_ replaced. Secondly, if the desc_func - function is specified, use it to determine a dynamic - completion. Thirdly, if none of the above are available, the desc - string is used as a description. - - \param wc_escaped the prefix, possibly containing wildcards. The wildcard should not have been unescaped, i.e. '*' should be used for any string, not the ANY_STRING character. - \param desc the default description, used for completions with no embedded description. The description _may_ contain a COMPLETE_SEP character, if not, one will be prefixed to it - \param desc_func the function that generates a description for those completions witout an embedded description - \param possible_comp the list of possible completions to iterate over -*/ - -void completer_t::complete_strings(const wcstring &wc_escaped, - const wchar_t *desc, - wcstring(*desc_func)(const wcstring &), +/// Copy any strings in possible_comp which have the specified prefix to the completer's completion +/// array. The prefix may contain wildcards. The output will consist of completion_t structs. +/// +/// There are three ways to specify descriptions for each completion. Firstly, if a description has +/// already been added to the completion, it is _not_ replaced. Secondly, if the desc_func function +/// is specified, use it to determine a dynamic completion. Thirdly, if none of the above are +/// available, the desc string is used as a description. +/// +/// \param wc_escaped the prefix, possibly containing wildcards. The wildcard should not have been +/// unescaped, i.e. '*' should be used for any string, not the ANY_STRING character. +/// \param desc the default description, used for completions with no embedded description. The +/// description _may_ contain a COMPLETE_SEP character, if not, one will be prefixed to it +/// \param desc_func the function that generates a description for those completions witout an +/// embedded description +/// \param possible_comp the list of possible completions to iterate over +void completer_t::complete_strings(const wcstring &wc_escaped, const wchar_t *desc, + wcstring (*desc_func)(const wcstring &), std::vector &possible_comp, - complete_flags_t flags) -{ + complete_flags_t flags) { wcstring tmp = wc_escaped; - if (! expand_one(tmp, EXPAND_SKIP_CMDSUBST | EXPAND_SKIP_WILDCARDS | this->expand_flags(), NULL)) + if (!expand_one(tmp, EXPAND_SKIP_CMDSUBST | EXPAND_SKIP_WILDCARDS | this->expand_flags(), NULL)) return; const wcstring wc = parse_util_unescape_wildcards(tmp); - for (size_t i=0; i< possible_comp.size(); i++) - { + for (size_t i = 0; i < possible_comp.size(); i++) { wcstring temp = possible_comp.at(i).completion; - const wchar_t *next_str = temp.empty()?NULL:temp.c_str(); + const wchar_t *next_str = temp.empty() ? NULL : temp.c_str(); - if (next_str) - { - wildcard_complete(next_str, wc.c_str(), desc, desc_func, &this->completions, this->expand_flags(), flags); + if (next_str) { + wildcard_complete(next_str, wc.c_str(), desc, desc_func, &this->completions, + this->expand_flags(), flags); } } } -/** - If command to complete is short enough, substitute - the description with the whatis information for the executable. -*/ -void completer_t::complete_cmd_desc(const wcstring &str) -{ +/// If command to complete is short enough, substitute the description with the whatis information +/// for the executable. +void completer_t::complete_cmd_desc(const wcstring &str) { ASSERT_IS_MAIN_THREAD(); const wchar_t *cmd_start; int skip; - - const wchar_t * const cmd = str.c_str(); - cmd_start=wcsrchr(cmd, L'/'); + const wchar_t *const cmd = str.c_str(); + cmd_start = wcsrchr(cmd, L'/'); if (cmd_start) cmd_start++; else cmd_start = cmd; - /* - Using apropos with a single-character search term produces far - to many results - require at least two characters if we don't - know the location of the whatis-database. - */ - if (wcslen(cmd_start) < 2) - return; + // Using apropos with a single-character search term produces far to many results - require at + // least two characters if we don't know the location of the whatis-database. + if (wcslen(cmd_start) < 2) return; - if (wildcard_has(cmd_start, 0)) - { + if (wildcard_has(cmd_start, 0)) { return; } skip = 1; - for (size_t i=0; i< this->completions.size(); i++) - { + for (size_t i = 0; i < this->completions.size(); i++) { const completion_t &c = this->completions.at(i); - if (c.completion.empty() || (c.completion[c.completion.size()-1] != L'/')) - { + if (c.completion.empty() || (c.completion[c.completion.size() - 1] != L'/')) { skip = 0; break; } - } - if (skip) - { + if (skip) { return; } - wcstring lookup_cmd(L"__fish_describe_command "); lookup_cmd.append(escape_string(cmd_start, 1)); std::map lookup; - /* - First locate a list of possible descriptions using a single - call to apropos or a direct search if we know the location - of the whatis database. This can take some time on slower - systems with a large set of manuals, but it should be ok - since apropos is only called once. - */ + // First locate a list of possible descriptions using a single call to apropos or a direct + // search if we know the location of the whatis database. This can take some time on slower + // systems with a large set of manuals, but it should be ok since apropos is only called once. wcstring_list_t list; - if (exec_subshell(lookup_cmd, list, false /* don't apply exit status */) != -1) - { - - /* - Then discard anything that is not a possible completion and put - the result into a hashtable with the completion as key and the - description as value. - - Should be reasonably fast, since no memory allocations are needed. - */ - for (size_t i=0; i < list.size(); i++) - { + if (exec_subshell(lookup_cmd, list, false /* don't apply exit status */) != -1) { + // Then discard anything that is not a possible completion and put the result into a + // hashtable with the completion as key and the description as value. + // + // Should be reasonably fast, since no memory allocations are needed. + for (size_t i = 0; i < list.size(); i++) { const wcstring &elstr = list.at(i); const wcstring fullkey(elstr, wcslen(cmd_start)); size_t tab_idx = fullkey.find(L'\t'); - if (tab_idx == wcstring::npos) - continue; + if (tab_idx == wcstring::npos) continue; const wcstring key(fullkey, 0, tab_idx); wcstring val(fullkey, tab_idx + 1); - /* - And once again I make sure the first character is uppercased - because I like it that way, and I get to decide these - things. - */ - if (! val.empty()) - val[0]=towupper(val[0]); - + // And once again I make sure the first character is uppercased because I like it that + // way, and I get to decide these things. + if (!val.empty()) val[0] = towupper(val[0]); lookup[key] = val; } - /* - Then do a lookup on every completion and if a match is found, - change to the new description. - - This needs to do a reallocation for every description added, but - there shouldn't be that many completions, so it should be ok. - */ - for (size_t i=0; icompletions.size(); i++) - { + // Then do a lookup on every completion and if a match is found, change to the new + // description. + // + // This needs to do a reallocation for every description added, but there shouldn't be that + // many completions, so it should be ok. + for (size_t i = 0; i < this->completions.size(); i++) { completion_t &completion = this->completions.at(i); const wcstring &el = completion.completion; - if (el.empty()) - continue; + if (el.empty()) continue; std::map::iterator new_desc_iter = lookup.find(el); - if (new_desc_iter != lookup.end()) - completion.description = new_desc_iter->second; + if (new_desc_iter != lookup.end()) completion.description = new_desc_iter->second; } } - } -/** - Returns a description for the specified function, or an empty string if none -*/ -static wcstring complete_function_desc(const wcstring &fn) -{ +/// Returns a description for the specified function, or an empty string if none. +static wcstring complete_function_desc(const wcstring &fn) { wcstring result; bool has_description = function_get_desc(fn, &result); - if (! has_description) - { + if (!has_description) { function_get_definition(fn, &result); } return result; } - -/** - Complete the specified command name. Search for executables in the - path, executables defined using an absolute path, functions, - builtins and directories for implicit cd commands. - - \param cmd the command string to find completions for - - \param comp the list to add all completions to -*/ -void completer_t::complete_cmd(const wcstring &str_cmd, bool use_function, bool use_builtin, bool use_command, bool use_implicit_cd) -{ - /* Paranoia */ - if (str_cmd.empty()) - return; +/// Complete the specified command name. Search for executables in the path, executables defined +/// using an absolute path, functions, builtins and directories for implicit cd commands. +/// +/// \param cmd the command string to find completions for +/// \param comp the list to add all completions to +void completer_t::complete_cmd(const wcstring &str_cmd, bool use_function, bool use_builtin, + bool use_command, bool use_implicit_cd) { + if (str_cmd.empty()) return; std::vector possible_comp; - if (use_command) - { - if (expand_string(str_cmd, &this->completions, EXPAND_SPECIAL_FOR_COMMAND | EXPAND_FOR_COMPLETIONS | EXECUTABLES_ONLY | this->expand_flags(), NULL) != EXPAND_ERROR) - { - if (this->wants_descriptions()) - { + if (use_command) { + if (expand_string(str_cmd, &this->completions, + EXPAND_SPECIAL_FOR_COMMAND | EXPAND_FOR_COMPLETIONS | EXECUTABLES_ONLY | + this->expand_flags(), + NULL) != EXPAND_ERROR) { + if (this->wants_descriptions()) { this->complete_cmd_desc(str_cmd); } } } - if (use_implicit_cd) - { - if (!expand_string(str_cmd, &this->completions, EXPAND_FOR_COMPLETIONS | DIRECTORIES_ONLY | this->expand_flags(), NULL)) - { + if (use_implicit_cd) { + if (!expand_string(str_cmd, &this->completions, + EXPAND_FOR_COMPLETIONS | DIRECTORIES_ONLY | this->expand_flags(), + NULL)) { // Not valid as implicit cd. } } - if (str_cmd.find(L'/') == wcstring::npos && str_cmd.at(0) != L'~') - { - if (use_function) - { + if (str_cmd.find(L'/') == wcstring::npos && str_cmd.at(0) != L'~') { + if (use_function) { wcstring_list_t names = function_get_names(str_cmd.at(0) == L'_'); - for (size_t i=0; i < names.size(); i++) - { + for (size_t i = 0; i < names.size(); i++) { append_completion(&possible_comp, names.at(i)); } @@ -879,113 +687,88 @@ void completer_t::complete_cmd(const wcstring &str_cmd, bool use_function, bool possible_comp.clear(); - if (use_builtin) - { + if (use_builtin) { builtin_get_names(&possible_comp); this->complete_strings(str_cmd, 0, &builtin_get_desc, possible_comp, 0); } - } } - -/** - Evaluate the argument list (as supplied by complete -a) and insert - any return matching completions. Matching is done using \c - copy_strings_with_prefix, meaning the completion may contain - wildcards. Logically, this is not always the right thing to do, but - I have yet to come up with a case where this matters. - - \param str The string to complete. - \param args The list of option arguments to be evaluated. - \param desc Description of the completion - \param comp_out The list into which the results will be inserted -*/ -void completer_t::complete_from_args(const wcstring &str, - const wcstring &args, - const wcstring &desc, - complete_flags_t flags) -{ +/// Evaluate the argument list (as supplied by complete -a) and insert any return matching +/// completions. Matching is done using \c copy_strings_with_prefix, meaning the completion may +/// contain wildcards. Logically, this is not always the right thing to do, but I have yet to come +/// up with a case where this matters. +/// +/// \param str The string to complete. +/// \param args The list of option arguments to be evaluated. +/// \param desc Description of the completion +/// \param comp_out The list into which the results will be inserted +void completer_t::complete_from_args(const wcstring &str, const wcstring &args, + const wcstring &desc, complete_flags_t flags) { bool is_autosuggest = (this->type() == COMPLETE_AUTOSUGGEST); - /* If type is COMPLETE_AUTOSUGGEST, it means we're on a background thread, so don't call proc_push_interactive */ - if (! is_autosuggest) - { + // If type is COMPLETE_AUTOSUGGEST, it means we're on a background thread, so don't call + // proc_push_interactive. + if (!is_autosuggest) { proc_push_interactive(0); } expand_flags_t eflags = 0; - if (is_autosuggest) - { + if (is_autosuggest) { eflags |= EXPAND_NO_DESCRIPTIONS | EXPAND_SKIP_CMDSUBST; } - + std::vector possible_comp; parser_t::expand_argument_list(args, eflags, &possible_comp); - if (! is_autosuggest) - { + if (!is_autosuggest) { proc_pop_interactive(); } this->complete_strings(escape_string(str, ESCAPE_ALL), desc.c_str(), 0, possible_comp, flags); } - -static size_t leading_dash_count(const wchar_t *str) -{ +static size_t leading_dash_count(const wchar_t *str) { size_t cursor = 0; - while (str[cursor] == L'-') - { + while (str[cursor] == L'-') { cursor++; } return cursor; } -/** - Match a parameter -*/ -static bool param_match(const complete_entry_opt_t *e, const wchar_t *optstr) -{ +/// Match a parameter. +static bool param_match(const complete_entry_opt_t *e, const wchar_t *optstr) { bool result = false; - if (e->type != option_type_args_only) - { + if (e->type != option_type_args_only) { size_t dashes = leading_dash_count(optstr); result = (dashes == e->expected_dash_count() && e->option == &optstr[dashes]); } return result; } -/** - Test if a string is an option with an argument, like --color=auto or -I/usr/include -*/ -static const wchar_t *param_match2(const complete_entry_opt_t *e, const wchar_t *optstr) -{ - // We may get a complete_entry_opt_t with no options if it's just arguments - if (e->option.empty()) - { +/// Test if a string is an option with an argument, like --color=auto or -I/usr/include. +static const wchar_t *param_match2(const complete_entry_opt_t *e, const wchar_t *optstr) { + // We may get a complete_entry_opt_t with no options if it's just arguments. + if (e->option.empty()) { return NULL; } - /* Verify leading dashes */ + // Verify leading dashes. size_t cursor = leading_dash_count(optstr); - if (cursor != e->expected_dash_count()) - { + if (cursor != e->expected_dash_count()) { return NULL; } - - /* Verify options match */ - if (! string_prefixes_string(e->option, &optstr[cursor])) - { + + // Verify options match. + if (!string_prefixes_string(e->option, &optstr[cursor])) { return NULL; } cursor += e->option.length(); - - /* short options are like -DNDEBUG. Long options are like --color=auto. So check for an equal sign for long options. */ - if (e->type != option_type_short) - { - if (optstr[cursor] != L'=') - { + + // Short options are like -DNDEBUG. Long options are like --color=auto. So check for an equal + // sign for long options. + if (e->type != option_type_short) { + if (optstr[cursor] != L'=') { return NULL; } cursor += 1; @@ -993,54 +776,44 @@ static const wchar_t *param_match2(const complete_entry_opt_t *e, const wchar_t return &optstr[cursor]; } -/** - Tests whether a short option is a viable completion. - arg_str will be like '-xzv', nextopt will be a character like 'f' - options will be the list of all options, used to validate the argument. -*/ -static bool short_ok(const wcstring &arg, const complete_entry_opt_t *entry, const option_list_t &options) -{ - /* Ensure it's a short option */ - if (entry->type != option_type_short || entry->option.empty()) - { +/// Tests whether a short option is a viable completion. arg_str will be like '-xzv', nextopt will +/// be a character like 'f' options will be the list of all options, used to validate the argument. +static bool short_ok(const wcstring &arg, const complete_entry_opt_t *entry, + const option_list_t &options) { + // Ensure it's a short option. + if (entry->type != option_type_short || entry->option.empty()) { return false; } const wchar_t nextopt = entry->option.at(0); - - /* Empty strings are always 'OK' */ - if (arg.empty()) - { + + // Empty strings are always 'OK'. + if (arg.empty()) { return true; } - - /* The argument must start with exactly one dash */ - if (leading_dash_count(arg.c_str()) != 1) - { + + // The argument must start with exactly one dash. + if (leading_dash_count(arg.c_str()) != 1) { return false; } - - /* Short option must not be already present */ - if (arg.find(nextopt) != wcstring::npos) - { + + // Short option must not be already present. + if (arg.find(nextopt) != wcstring::npos) { return false; } - - /* Verify that all characters in our combined short option list are present as short options in the options list. If we get a short option that can't be combined (NO_COMMON), then we stop. */ + + // Verify that all characters in our combined short option list are present as short options in + // the options list. If we get a short option that can't be combined (NO_COMMON), then we stop. bool result = true; - for (size_t i=1; i < arg.size(); i++) - { + for (size_t i = 1; i < arg.size(); i++) { wchar_t arg_char = arg.at(i); const complete_entry_opt_t *match = NULL; - for (option_list_t::const_iterator iter = options.begin(); iter != options.end(); ++iter) - { - if (iter->type == option_type_short && iter->option.at(0) == arg_char) - { + for (option_list_t::const_iterator iter = options.begin(); iter != options.end(); ++iter) { + if (iter->type == option_type_short && iter->option.at(0) == arg_char) { match = &*iter; break; } } - if (match == NULL || (match->result_mode & NO_COMMON)) - { + if (match == NULL || (match->result_mode & NO_COMMON)) { result = false; break; } @@ -1048,119 +821,97 @@ static bool short_ok(const wcstring &arg, const complete_entry_opt_t *entry, con return result; } - -/* Load command-specific completions for the specified command. */ -static void complete_load(const wcstring &name, bool reload) -{ - // we have to load this as a function, since it may define a --wraps or signature - // see #2466 +// Load command-specific completions for the specified command. +static void complete_load(const wcstring &name, bool reload) { + // We have to load this as a function, since it may define a --wraps or signature. + // See issue #2466. function_load(name); completion_autoloader.load(name, reload); } // Performed on main thread, from background thread. Return type is ignored. -static int complete_load_no_reload(wcstring *name) -{ +static int complete_load_no_reload(wcstring *name) { assert(name != NULL); ASSERT_IS_MAIN_THREAD(); complete_load(*name, false); return 0; } -/** - complete_param: Given a command, find completions for the argument str of command cmd_orig with previous option popt. - - Examples in format (cmd, popt, str): - - echo hello world -> ("echo", "world", "") - echo hello world -> ("echo", "hello", "world") - - Insert results into comp_out. Return true to perform file completion, false to disable it. -*/ -bool completer_t::complete_param(const wcstring &scmd_orig, const wcstring &spopt, const wcstring &sstr, bool use_switches) -{ - const wchar_t * const cmd_orig = scmd_orig.c_str(); - const wchar_t * const popt = spopt.c_str(); - const wchar_t * const str = sstr.c_str(); +/// complete_param: Given a command, find completions for the argument str of command cmd_orig with +/// previous option popt. +/// +/// Examples in format (cmd, popt, str): +/// +/// echo hello world -> ("echo", "world", "") +/// echo hello world -> ("echo", "hello", "world") +/// +/// Insert results into comp_out. Return true to perform file completion, false to disable it. +bool completer_t::complete_param(const wcstring &scmd_orig, const wcstring &spopt, + const wcstring &sstr, bool use_switches) { + const wchar_t *const cmd_orig = scmd_orig.c_str(); + const wchar_t *const popt = spopt.c_str(); + const wchar_t *const str = sstr.c_str(); - bool use_common=1, use_files=1; + bool use_common = 1, use_files = 1; wcstring cmd, path; parse_cmd_string(cmd_orig, path, cmd); - if (this->type() == COMPLETE_DEFAULT) - { + if (this->type() == COMPLETE_DEFAULT) { ASSERT_IS_MAIN_THREAD(); complete_load(cmd, true); - } - else if (this->type() == COMPLETE_AUTOSUGGEST) - { - /* Maybe load this command (on the main thread) */ - if (! completion_autoloader.has_tried_loading(cmd)) - { + } else if (this->type() == COMPLETE_AUTOSUGGEST) { + // Maybe load this command (on the main thread). + if (!completion_autoloader.has_tried_loading(cmd)) { iothread_perform_on_main(complete_load_no_reload, &cmd); } } - /* Make a list of lists of all options that we care about */ + // Make a list of lists of all options that we care about. std::vector all_options; { scoped_lock lock(completion_lock); - for (completion_entry_set_t::const_iterator iter = completion_set.begin(); iter != completion_set.end(); ++iter) - { + for (completion_entry_set_t::const_iterator iter = completion_set.begin(); + iter != completion_set.end(); ++iter) { const completion_entry_t &i = *iter; const wcstring &match = i.cmd_is_path ? path : cmd; - if (wildcard_match(match, i.cmd)) - { - /* Copy all of their options into our list */ - all_options.push_back(i.get_options()); //Oof, this is a lot of copying + if (wildcard_match(match, i.cmd)) { + // Copy all of their options into our list. + all_options.push_back(i.get_options()); // Oof, this is a lot of copying } } } - /* Now release the lock and test each option that we captured above. - We have to do this outside the lock because callouts (like the condition) may add or remove completions. - See https://github.com/ridiculousfish/fishfish/issues/2 */ - for (std::vector::const_iterator iter = all_options.begin(); iter != all_options.end(); ++iter) - { + // Now release the lock and test each option that we captured above. We have to do this outside + // the lock because callouts (like the condition) may add or remove completions. See issue 2. + for (std::vector::const_iterator iter = all_options.begin(); + iter != all_options.end(); ++iter) { const option_list_t &options = *iter; - use_common=1; - if (use_switches) - { - - if (str[0] == L'-') - { - /* Check if we are entering a combined option and argument - (like --color=auto or -I/usr/include) */ - for (option_list_t::const_iterator oiter = options.begin(); oiter != options.end(); ++oiter) - { + use_common = 1; + if (use_switches) { + if (str[0] == L'-') { + // Check if we are entering a combined option and argument (like --color=auto or + // -I/usr/include). + for (option_list_t::const_iterator oiter = options.begin(); oiter != options.end(); + ++oiter) { const complete_entry_opt_t *o = &*oiter; const wchar_t *arg = param_match2(o, str); - if (arg != NULL && this->condition_test(o->condition)) - { + if (arg != NULL && this->condition_test(o->condition)) { if (o->result_mode & NO_COMMON) use_common = false; if (o->result_mode & NO_FILES) use_files = false; complete_from_args(arg, o->comp, o->localized_desc(), o->flags); } - } - } - else if (popt[0] == L'-') - { - /* Set to true if we found a matching old-style switch */ + } else if (popt[0] == L'-') { + // Set to true if we found a matching old-style switch. bool old_style_match = false; - /* - If we are using old style long options, check for them - first - */ - for (option_list_t::const_iterator oiter = options.begin(); oiter != options.end(); ++oiter) - { + // If we are using old style long options, check for them first. + for (option_list_t::const_iterator oiter = options.begin(); oiter != options.end(); + ++oiter) { const complete_entry_opt_t *o = &*oiter; - if (o->type == option_type_single_long) - { - if (param_match(o, popt) && this->condition_test(o->condition)) - { + if (o->type == option_type_single_long) { + if (param_match(o, popt) && this->condition_test(o->condition)) { old_style_match = true; if (o->result_mode & NO_COMMON) use_common = false; if (o->result_mode & NO_FILES) use_files = false; @@ -1169,129 +920,88 @@ bool completer_t::complete_param(const wcstring &scmd_orig, const wcstring &spop } } - /* - No old style option matched, or we are not using old - style options. We check if any short (or gnu style - options do. - */ - if (!old_style_match) - { - for (option_list_t::const_iterator oiter = options.begin(); oiter != options.end(); ++oiter) - { + // No old style option matched, or we are not using old style options. We check if + // any short (or gnu style options do. + if (!old_style_match) { + for (option_list_t::const_iterator oiter = options.begin(); + oiter != options.end(); ++oiter) { const complete_entry_opt_t *o = &*oiter; - /* - Gnu-style options with _optional_ arguments must - be specified as a single token, so that it can - be differed from a regular argument. - */ + // Gnu-style options with _optional_ arguments must be specified as a single + // token, so that it can be differed from a regular argument. if (o->type == option_type_double_long && !(o->result_mode & NO_COMMON)) continue; - if (param_match(o, popt) && this->condition_test(o->condition)) - { + if (param_match(o, popt) && this->condition_test(o->condition)) { if (o->result_mode & NO_COMMON) use_common = false; if (o->result_mode & NO_FILES) use_files = false; complete_from_args(str, o->comp, o->localized_desc(), o->flags); - } } } } } - if (use_common) - { - - for (option_list_t::const_iterator oiter = options.begin(); oiter != options.end(); ++oiter) - { + if (use_common) { + for (option_list_t::const_iterator oiter = options.begin(); oiter != options.end(); + ++oiter) { const complete_entry_opt_t *o = &*oiter; - /* - If this entry is for the base command, - check if any of the arguments match - */ - - if (!this->condition_test(o->condition)) - continue; - if (o->option.empty()) - { - use_files = use_files && ((o->result_mode & NO_FILES)==0); + // If this entry is for the base command, check if any of the arguments match. + if (!this->condition_test(o->condition)) continue; + if (o->option.empty()) { + use_files = use_files && ((o->result_mode & NO_FILES) == 0); complete_from_args(str, o->comp, o->localized_desc(), o->flags); } - if (wcslen(str) > 0 && use_switches) - { - /* - Check if the short style option matches - */ - if (short_ok(str, o, options)) - { - // It's a match + if (wcslen(str) > 0 && use_switches) { + // Check if the short style option matches. + if (short_ok(str, o, options)) { + // It's a match. const wcstring desc = o->localized_desc(); append_completion(&this->completions, o->option, desc, 0); - } - /* - Check if the long style option matches - */ - if (o->type == option_type_single_long || o->type == option_type_double_long) - { - int match=0, match_no_case=0; + // Check if the long style option matches. + if (o->type == option_type_single_long || o->type == option_type_double_long) { + int match = 0, match_no_case = 0; wcstring whole_opt(o->expected_dash_count(), L'-'); whole_opt.append(o->option); match = string_prefixes_string(str, whole_opt); - if (!match) - { - match_no_case = wcsncasecmp(str, whole_opt.c_str(), wcslen(str))==0; + if (!match) { + match_no_case = wcsncasecmp(str, whole_opt.c_str(), wcslen(str)) == 0; } - if (match || match_no_case) - { - int has_arg=0; /* Does this switch have any known arguments */ - int req_arg=0; /* Does this switch _require_ an argument */ + if (match || match_no_case) { + int has_arg = 0; // does this switch have any known arguments + int req_arg = 0; // does this switch _require_ an argument size_t offset = 0; complete_flags_t flags = 0; - if (match) - { + if (match) { offset = wcslen(str); - } - else - { + } else { flags = COMPLETE_REPLACES_TOKEN; } - has_arg = ! o->comp.empty(); + has_arg = !o->comp.empty(); req_arg = (o->result_mode & NO_COMMON); - if (o->type == option_type_double_long && (has_arg && !req_arg)) - { - - /* - Optional arguments to a switch can - only be handled using the '=', so we - add it as a completion. By default - we avoid using '=' and instead rely - on '--switch switch-arg', since it - is more commonly supported by - homebrew getopt-like functions. - */ - wcstring completion = format_string(L"%ls=", whole_opt.c_str()+offset); - append_completion(&this->completions, - completion, - C_(o->desc), + if (o->type == option_type_double_long && (has_arg && !req_arg)) { + // Optional arguments to a switch can only be handled using the '=', + // so we add it as a completion. By default we avoid using '=' and + // instead rely on '--switch switch-arg', since it is more commonly + // supported by homebrew getopt-like functions. + wcstring completion = + format_string(L"%ls=", whole_opt.c_str() + offset); + append_completion(&this->completions, completion, C_(o->desc), flags); - } - append_completion(&this->completions, - whole_opt.c_str() + offset, - C_(o->desc), - flags); + append_completion(&this->completions, whole_opt.c_str() + offset, + C_(o->desc), flags); } } } @@ -1302,127 +1012,100 @@ bool completer_t::complete_param(const wcstring &scmd_orig, const wcstring &spop return use_files; } -/** - Perform generic (not command-specific) expansions on the specified string -*/ -void completer_t::complete_param_expand(const wcstring &str, bool do_file, bool handle_as_special_cd) -{ +/// Perform generic (not command-specific) expansions on the specified string. +void completer_t::complete_param_expand(const wcstring &str, bool do_file, + bool handle_as_special_cd) { expand_flags_t flags = EXPAND_SKIP_CMDSUBST | EXPAND_FOR_COMPLETIONS | this->expand_flags(); - if (! do_file) - flags |= EXPAND_SKIP_WILDCARDS; - - if (handle_as_special_cd && do_file) - { + if (!do_file) flags |= EXPAND_SKIP_WILDCARDS; + + if (handle_as_special_cd && do_file) { flags |= DIRECTORIES_ONLY | EXPAND_SPECIAL_FOR_CD | EXPAND_NO_DESCRIPTIONS; } - /* Squelch file descriptions per issue 254 */ - if (this->type() == COMPLETE_AUTOSUGGEST || do_file) - flags |= EXPAND_NO_DESCRIPTIONS; + // Squelch file descriptions per issue #254. + if (this->type() == COMPLETE_AUTOSUGGEST || do_file) flags |= EXPAND_NO_DESCRIPTIONS; - /* We have the following cases: - - --foo=bar => expand just bar - -foo=bar => expand just bar - foo=bar => expand the whole thing, and also just bar - - We also support colon separator (#2178). If there's more than one, prefer the last one. - */ - + // We have the following cases: + // + // --foo=bar => expand just bar + // -foo=bar => expand just bar + // foo=bar => expand the whole thing, and also just bar + // + // We also support colon separator (#2178). If there's more than one, prefer the last one. size_t sep_index = str.find_last_of(L"=:"); bool complete_from_separator = (sep_index != wcstring::npos); bool complete_from_start = !complete_from_separator || !string_prefixes_string(L"-", str); - - if (complete_from_separator) - { + + if (complete_from_separator) { const wcstring sep_string = wcstring(str, sep_index + 1); std::vector local_completions; - if (expand_string(sep_string, - &local_completions, - flags, - NULL) == EXPAND_ERROR) - { + if (expand_string(sep_string, &local_completions, flags, NULL) == EXPAND_ERROR) { debug(3, L"Error while expanding string '%ls'", sep_string.c_str()); } - - /* Any COMPLETE_REPLACES_TOKEN will also stomp the separator. We need to "repair" them by inserting our separator and prefix. */ + + // Any COMPLETE_REPLACES_TOKEN will also stomp the separator. We need to "repair" them by + // inserting our separator and prefix. const wcstring prefix_with_sep = wcstring(str, 0, sep_index + 1); - for (size_t i=0; i < local_completions.size(); i++) - { + for (size_t i = 0; i < local_completions.size(); i++) { local_completions.at(i).prepend_token_prefix(prefix_with_sep); } - this->completions.insert(this->completions.end(), - local_completions.begin(), + this->completions.insert(this->completions.end(), local_completions.begin(), local_completions.end()); } - - if (complete_from_start) - { - /* Don't do fuzzy matching for files if the string begins with a dash (#568). We could consider relaxing this if there was a preceding double-dash argument */ - if (string_prefixes_string(L"-", str)) - flags &= ~EXPAND_FUZZY_MATCH; - - if (expand_string(str, - &this->completions, - flags, NULL) == EXPAND_ERROR) - { + + if (complete_from_start) { + // Don't do fuzzy matching for files if the string begins with a dash (issue #568). We could + // consider relaxing this if there was a preceding double-dash argument. + if (string_prefixes_string(L"-", str)) flags &= ~EXPAND_FUZZY_MATCH; + + if (expand_string(str, &this->completions, flags, NULL) == EXPAND_ERROR) { debug(3, L"Error while expanding string '%ls'", str.c_str()); } } } -/** - Complete the specified string as an environment variable -*/ -bool completer_t::complete_variable(const wcstring &str, size_t start_offset) -{ - const wchar_t * const whole_var = str.c_str(); +/// Complete the specified string as an environment variable. +bool completer_t::complete_variable(const wcstring &str, size_t start_offset) { + const wchar_t *const whole_var = str.c_str(); const wchar_t *var = &whole_var[start_offset]; size_t varlen = wcslen(var); bool res = false; const wcstring_list_t names = complete_get_variable_names(); - for (size_t i=0; imax_fuzzy_match_type()); - if (match.type == fuzzy_match_none) - { - // No match - continue; + string_fuzzy_match_t match = + string_fuzzy_match_string(var, env_name, this->max_fuzzy_match_type()); + if (match.type == fuzzy_match_none) { + continue; // no match } wcstring comp; int flags = 0; - if (! match_type_requires_full_replacement(match.type)) - { - // Take only the suffix + if (!match_type_requires_full_replacement(match.type)) { + // Take only the suffix. comp.append(env_name.c_str() + varlen); - } - else - { + } else { comp.append(whole_var, start_offset); comp.append(env_name); flags = COMPLETE_REPLACES_TOKEN | COMPLETE_DONT_ESCAPE; } wcstring desc; - if (this->wants_descriptions()) - { - // Can't use this->vars here, it could be any variable + if (this->wants_descriptions()) { + // Can't use this->vars here, it could be any variable. env_var_t value_unescaped = env_get_string(env_name); - if (value_unescaped.missing()) - continue; + if (value_unescaped.missing()) continue; wcstring value = expand_escape_variable(value_unescaped); if (this->type() != COMPLETE_AUTOSUGGEST) desc = format_string(COMPLETE_VAR_DESC_VAL, value.c_str()); } - append_completion(&this->completions, comp, desc, flags, match); + append_completion(&this->completions, comp, desc, flags, match); res = true; } @@ -1430,128 +1113,103 @@ bool completer_t::complete_variable(const wcstring &str, size_t start_offset) return res; } -bool completer_t::try_complete_variable(const wcstring &str) -{ - enum {e_unquoted, e_single_quoted, e_double_quoted} mode = e_unquoted; +bool completer_t::try_complete_variable(const wcstring &str) { + enum { e_unquoted, e_single_quoted, e_double_quoted } mode = e_unquoted; const size_t len = str.size(); - /* Get the position of the dollar heading a (possibly empty) run of valid variable characters. npos means none. */ + // Get the position of the dollar heading a (possibly empty) run of valid variable characters. + // npos means none. size_t variable_start = wcstring::npos; - for (size_t in_pos=0; in_posflags & COMPLETION_REQUEST_AUTOSUGGESTION); + // Now complete if we have a variable start. Note the variable text may be empty; in that case + // don't generate an autosuggestion, but do allow tab completion. + bool allow_empty = !(this->flags & COMPLETION_REQUEST_AUTOSUGGESTION); bool text_is_empty = (variable_start == len); bool result = false; - if (variable_start != wcstring::npos && (allow_empty || ! text_is_empty)) - { + if (variable_start != wcstring::npos && (allow_empty || !text_is_empty)) { result = this->complete_variable(str, variable_start + 1); } return result; } -/** - Try to complete the specified string as a username. This is used by - ~USER type expansion. - - \return 0 if unable to complete, 1 otherwise -*/ -bool completer_t::try_complete_user(const wcstring &str) -{ +/// Try to complete the specified string as a username. This is used by ~USER type expansion. +/// +/// \return 0 if unable to complete, 1 otherwise +bool completer_t::try_complete_user(const wcstring &str) { const wchar_t *cmd = str.c_str(); - const wchar_t *first_char=cmd; - int res=0; + const wchar_t *first_char = cmd; + int res = 0; double start_time = timef(); - if (*first_char ==L'~' && !wcschr(first_char, L'/')) - { - const wchar_t *user_name = first_char+1; + if (*first_char == L'~' && !wcschr(first_char, L'/')) { + const wchar_t *user_name = first_char + 1; const wchar_t *name_end = wcschr(user_name, L'~'); - if (name_end == 0) - { + if (name_end == 0) { struct passwd *pw; size_t name_len = wcslen(user_name); setpwent(); - while ((pw=getpwent()) != 0) - { + while ((pw = getpwent()) != 0) { double current_time = timef(); - if (current_time - start_time > 0.2) - { + if (current_time - start_time > 0.2) { return 1; } - if (pw->pw_name) - { + if (pw->pw_name) { const wcstring pw_name_str = str2wcstring(pw->pw_name); const wchar_t *pw_name = pw_name_str.c_str(); - if (wcsncmp(user_name, pw_name, name_len)==0) - { + if (wcsncmp(user_name, pw_name, name_len) == 0) { wcstring desc = format_string(COMPLETE_USER_DESC, pw_name); - append_completion(&this->completions, - &pw_name[name_len], - desc, + append_completion(&this->completions, &pw_name[name_len], desc, COMPLETE_NO_SPACE); - res=1; - } - else if (wcsncasecmp(user_name, pw_name, name_len)==0) - { + res = 1; + } else if (wcsncasecmp(user_name, pw_name, name_len) == 0) { wcstring name = format_string(L"~%ls", pw_name); wcstring desc = format_string(COMPLETE_USER_DESC, pw_name); - append_completion(&this->completions, - name, - desc, - COMPLETE_REPLACES_TOKEN | COMPLETE_DONT_ESCAPE | COMPLETE_NO_SPACE); - res=1; + append_completion(&this->completions, name, desc, COMPLETE_REPLACES_TOKEN | + COMPLETE_DONT_ESCAPE | + COMPLETE_NO_SPACE); + res = 1; } } } @@ -1562,76 +1220,83 @@ bool completer_t::try_complete_user(const wcstring &str) return res; } -void complete(const wcstring &cmd_with_subcmds, std::vector *out_comps, completion_request_flags_t flags, const env_vars_snapshot_t &vars) -{ - /* Determine the innermost subcommand */ +void complete(const wcstring &cmd_with_subcmds, std::vector *out_comps, + completion_request_flags_t flags, const env_vars_snapshot_t &vars) { + // Determine the innermost subcommand. const wchar_t *cmdsubst_begin, *cmdsubst_end; - parse_util_cmdsubst_extent(cmd_with_subcmds.c_str(), cmd_with_subcmds.size(), &cmdsubst_begin, &cmdsubst_end); + parse_util_cmdsubst_extent(cmd_with_subcmds.c_str(), cmd_with_subcmds.size(), &cmdsubst_begin, + &cmdsubst_end); assert(cmdsubst_begin != NULL && cmdsubst_end != NULL && cmdsubst_end >= cmdsubst_begin); const wcstring cmd = wcstring(cmdsubst_begin, cmdsubst_end - cmdsubst_begin); - /* Make our completer */ + // Make our completer. completer_t completer(cmd, flags, vars); wcstring current_command; const size_t pos = cmd.size(); - bool done=false; + bool done = false; bool use_command = 1; bool use_function = 1; bool use_builtin = 1; bool use_implicit_cd = 1; - //debug( 1, L"Complete '%ls'", cmd.c_str() ); + // debug( 1, L"Complete '%ls'", cmd.c_str() ); const wchar_t *cmd_cstr = cmd.c_str(); const wchar_t *tok_begin = NULL, *prev_begin = NULL, *prev_end = NULL; parse_util_token_extent(cmd_cstr, cmd.size(), &tok_begin, NULL, &prev_begin, &prev_end); - /** - If we are completing a variable name or a tilde expansion user - name, we do that and return. No need for any other completions. - */ + // If we are completing a variable name or a tilde expansion user name, we do that and return. + // No need for any other completions. const wcstring current_token = tok_begin; - /* Unconditionally complete variables and processes. This is a little weird since we will happily complete variables even in e.g. command position, despite the fact that they are invalid there. */ - if (!done) - { - done = completer.try_complete_variable(current_token) || completer.try_complete_user(current_token); + // Unconditionally complete variables and processes. This is a little weird since we will + // happily complete variables even in e.g. command position, despite the fact that they are + // invalid there. */ + if (!done) { + done = completer.try_complete_variable(current_token) || + completer.try_complete_user(current_token); } - if (!done) - { + if (!done) { parse_node_tree_t tree; - parse_tree_from_string(cmd, parse_flag_continue_after_error | parse_flag_accept_incomplete_tokens | parse_flag_include_comments, &tree, NULL); + parse_tree_from_string(cmd, parse_flag_continue_after_error | + parse_flag_accept_incomplete_tokens | + parse_flag_include_comments, + &tree, NULL); - /* Find any plain statement that contains the position. We have to backtrack past spaces (#1261). So this will be at either the last space character, or after the end of the string */ + // Find any plain statement that contains the position. We have to backtrack past spaces + // (issue #1261). So this will be at either the last space character, or after the end of + // the string. size_t adjusted_pos = pos; - while (adjusted_pos > 0 && cmd.at(adjusted_pos - 1) == L' ') - { + while (adjusted_pos > 0 && cmd.at(adjusted_pos - 1) == L' ') { adjusted_pos--; } - const parse_node_t *plain_statement = tree.find_node_matching_source_location(symbol_plain_statement, adjusted_pos, NULL); - if (plain_statement == NULL) - { - /* Not part of a plain statement. This could be e.g. a for loop header, case expression, etc. Do generic file completions (#1309). If we had to backtrack, it means there was whitespace; don't do an autosuggestion in that case. Also don't do it if we are just after a pipe, semicolon, or & (#1631), or in a comment. - - Overall this logic is a total mess. A better approach would be to return the "possible next token" from the parse tree directly (this data is available as the first of the sequence of nodes without source locations at the very end of the parse tree). */ + const parse_node_t *plain_statement = + tree.find_node_matching_source_location(symbol_plain_statement, adjusted_pos, NULL); + if (plain_statement == NULL) { + // Not part of a plain statement. This could be e.g. a for loop header, case expression, + // etc. Do generic file completions (issue #1309). If we had to backtrack, it means + // there was whitespace; don't do an autosuggestion in that case. Also don't do it if we + // are just after a pipe, semicolon, or & (issue #1631), or in a comment. + // + // Overall this logic is a total mess. A better approach would be to return the + // "possible next token" from the parse tree directly (this data is available as the + // first of the sequence of nodes without source locations at the very end of the parse + // tree). bool do_file = true; - if (flags & COMPLETION_REQUEST_AUTOSUGGESTION) - { - if (adjusted_pos < pos) - { + if (flags & COMPLETION_REQUEST_AUTOSUGGESTION) { + if (adjusted_pos < pos) { do_file = false; - } - else if (pos > 0) - { - // If the previous character is in one of these types, we don't do file suggestions - parse_token_type_t bad_types[] = {parse_token_type_pipe, parse_token_type_end, parse_token_type_background, parse_special_type_comment}; - for (size_t i=0; i < sizeof bad_types / sizeof *bad_types; i++) - { - if (tree.find_node_matching_source_location(bad_types[i], pos - 1, NULL)) - { + } else if (pos > 0) { + // If the previous character is in one of these types, we don't do file + // suggestions. + parse_token_type_t bad_types[] = {parse_token_type_pipe, parse_token_type_end, + parse_token_type_background, + parse_special_type_comment}; + for (size_t i = 0; i < sizeof bad_types / sizeof *bad_types; i++) { + if (tree.find_node_matching_source_location(bad_types[i], pos - 1, NULL)) { do_file = false; break; } @@ -1639,61 +1304,58 @@ void complete(const wcstring &cmd_with_subcmds, std::vector *out_c } } completer.complete_param_expand(current_token, do_file); - } - else - { - assert(plain_statement->has_source() && plain_statement->type == symbol_plain_statement); + } else { + assert(plain_statement->has_source() && + plain_statement->type == symbol_plain_statement); - /* Get the command node */ - const parse_node_t *cmd_node = tree.get_child(*plain_statement, 0, parse_token_type_string); + // Get the command node. + const parse_node_t *cmd_node = + tree.get_child(*plain_statement, 0, parse_token_type_string); - /* Get the actual command string */ - if (cmd_node != NULL) - current_command = cmd_node->get_source(cmd); + // Get the actual command string. + if (cmd_node != NULL) current_command = cmd_node->get_source(cmd); - /* Check the decoration */ - switch (tree.decoration_for_plain_statement(*plain_statement)) - { - case parse_statement_decoration_none: + // Check the decoration. + switch (tree.decoration_for_plain_statement(*plain_statement)) { + case parse_statement_decoration_none: { use_command = true; use_function = true; use_builtin = true; use_implicit_cd = true; break; - + } case parse_statement_decoration_command: - case parse_statement_decoration_exec: + case parse_statement_decoration_exec: { use_command = true; use_function = false; use_builtin = false; use_implicit_cd = false; break; - - case parse_statement_decoration_builtin: + } + case parse_statement_decoration_builtin: { use_command = false; use_function = false; use_builtin = true; use_implicit_cd = false; break; + } } - if (cmd_node && cmd_node->location_in_or_at_end_of_source_range(pos)) - { - /* Complete command filename */ - completer.complete_cmd(current_token, use_function, use_builtin, use_command, use_implicit_cd); - } - else - { - /* Get all the arguments */ - const parse_node_tree_t::parse_node_list_t all_arguments = tree.find_nodes(*plain_statement, symbol_argument); + if (cmd_node && cmd_node->location_in_or_at_end_of_source_range(pos)) { + // Complete command filename. + completer.complete_cmd(current_token, use_function, use_builtin, use_command, + use_implicit_cd); + } else { + // Get all the arguments. + const parse_node_tree_t::parse_node_list_t all_arguments = + tree.find_nodes(*plain_statement, symbol_argument); - /* See whether we are in an argument. We may also be in a redirection, or nothing at all. */ + // See whether we are in an argument. We may also be in a redirection, or nothing at + // all. size_t matching_arg_index = -1; - for (size_t i=0; i < all_arguments.size(); i++) - { + for (size_t i = 0; i < all_arguments.size(); i++) { const parse_node_t *node = all_arguments.at(i); - if (node->location_in_or_at_end_of_source_range(adjusted_pos)) - { + if (node->location_in_or_at_end_of_source_range(adjusted_pos)) { matching_arg_index = i; break; } @@ -1701,103 +1363,103 @@ void complete(const wcstring &cmd_with_subcmds, std::vector *out_c bool had_ddash = false; wcstring current_argument, previous_argument; - if (matching_arg_index != (size_t)(-1)) - { - const wcstring matching_arg = all_arguments.at(matching_arg_index)->get_source(cmd); - - /* If the cursor is in whitespace, then the "current" argument is empty and the previous argument is the matching one. But if the cursor was in or at the end of the argument, then the current argument is the matching one, and the previous argument is the one before it. */ + if (matching_arg_index != (size_t)(-1)) { + const wcstring matching_arg = + all_arguments.at(matching_arg_index)->get_source(cmd); + + // If the cursor is in whitespace, then the "current" argument is empty and the + // previous argument is the matching one. But if the cursor was in or at the end + // of the argument, then the current argument is the matching one, and the + // previous argument is the one before it. bool cursor_in_whitespace = (adjusted_pos < pos); - if (cursor_in_whitespace) - { + if (cursor_in_whitespace) { current_argument = L""; previous_argument = matching_arg; - } - else - { + } else { current_argument = matching_arg; - if (matching_arg_index > 0) - { - previous_argument = all_arguments.at(matching_arg_index - 1)->get_source(cmd); + if (matching_arg_index > 0) { + previous_argument = + all_arguments.at(matching_arg_index - 1)->get_source(cmd); } } - /* Check to see if we have a preceding double-dash */ - for (size_t i=0; i < matching_arg_index; i++) - { - if (all_arguments.at(i)->get_source(cmd) == L"--") - { + // Check to see if we have a preceding double-dash. + for (size_t i = 0; i < matching_arg_index; i++) { + if (all_arguments.at(i)->get_source(cmd) == L"--") { had_ddash = true; break; } } } - - /* If we are not in an argument, we may be in a redirection */ + + // If we are not in an argument, we may be in a redirection. bool in_redirection = false; - if (matching_arg_index == (size_t)(-1)) - { - const parse_node_t *redirection = tree.find_node_matching_source_location(symbol_redirection, adjusted_pos, plain_statement); + if (matching_arg_index == (size_t)(-1)) { + const parse_node_t *redirection = tree.find_node_matching_source_location( + symbol_redirection, adjusted_pos, plain_statement); in_redirection = (redirection != NULL); } - + bool do_file = false, handle_as_special_cd = false; - if (in_redirection) - { + if (in_redirection) { do_file = true; - } - else - { - /* Try completing as an argument */ - wcstring current_command_unescape, previous_argument_unescape, current_argument_unescape; - if (unescape_string(current_command, ¤t_command_unescape, UNESCAPE_DEFAULT) && - unescape_string(previous_argument, &previous_argument_unescape, UNESCAPE_DEFAULT) && - unescape_string(current_argument, ¤t_argument_unescape, UNESCAPE_INCOMPLETE)) - { - // Have to walk over the command and its entire wrap chain - // If any command disables do_file, then they all do + } else { + // Try completing as an argument. + wcstring current_command_unescape, previous_argument_unescape, + current_argument_unescape; + if (unescape_string(current_command, ¤t_command_unescape, + UNESCAPE_DEFAULT) && + unescape_string(previous_argument, &previous_argument_unescape, + UNESCAPE_DEFAULT) && + unescape_string(current_argument, ¤t_argument_unescape, + UNESCAPE_INCOMPLETE)) { + // Have to walk over the command and its entire wrap chain. If any command + // disables do_file, then they all do. do_file = true; - const wcstring_list_t wrap_chain = complete_get_wrap_chain(current_command_unescape); - for (size_t i=0; i < wrap_chain.size(); i++) - { - // Hackish, this. The first command in the chain is always the given command. For every command past the first, we need to create a transient commandline for builtin_commandline. But not for COMPLETION_REQUEST_AUTOSUGGESTION, which may occur on background threads. + const wcstring_list_t wrap_chain = + complete_get_wrap_chain(current_command_unescape); + for (size_t i = 0; i < wrap_chain.size(); i++) { + // Hackish, this. The first command in the chain is always the given + // command. For every command past the first, we need to create a + // transient commandline for builtin_commandline. But not for + // COMPLETION_REQUEST_AUTOSUGGESTION, which may occur on background + // threads. builtin_commandline_scoped_transient_t *transient_cmd = NULL; - if (i == 0) - { + if (i == 0) { assert(wrap_chain.at(i) == current_command_unescape); - } - else if (! (flags & COMPLETION_REQUEST_AUTOSUGGESTION)) - { + } else if (!(flags & COMPLETION_REQUEST_AUTOSUGGESTION)) { assert(cmd_node != NULL); wcstring faux_cmdline = cmd; - faux_cmdline.replace(cmd_node->source_start, cmd_node->source_length, wrap_chain.at(i)); - transient_cmd = new builtin_commandline_scoped_transient_t(faux_cmdline); + faux_cmdline.replace(cmd_node->source_start, + cmd_node->source_length, wrap_chain.at(i)); + transient_cmd = + new builtin_commandline_scoped_transient_t(faux_cmdline); } - if (! completer.complete_param(wrap_chain.at(i), - previous_argument_unescape, - current_argument_unescape, - !had_ddash)) - { + if (!completer.complete_param(wrap_chain.at(i), + previous_argument_unescape, + current_argument_unescape, !had_ddash)) { do_file = false; } - delete transient_cmd; //may be null + delete transient_cmd; // may be null } } - /* If we have found no command specific completions at all, fall back to using file completions. */ - if (completer.empty()) - do_file = true; - - /* Hack. If we're cd, handle it specially (#1059, others) */ + // If we have found no command specific completions at all, fall back to using + // file completions. + if (completer.empty()) do_file = true; + + // Hack. If we're cd, handle it specially (issue #1059, others). handle_as_special_cd = (current_command_unescape == L"cd"); - - /* And if we're autosuggesting, and the token is empty, don't do file suggestions */ - if ((flags & COMPLETION_REQUEST_AUTOSUGGESTION) && current_argument_unescape.empty()) - { + + // And if we're autosuggesting, and the token is empty, don't do file + // suggestions. + if ((flags & COMPLETION_REQUEST_AUTOSUGGESTION) && + current_argument_unescape.empty()) { do_file = false; } } - /* This function wants the unescaped string */ + // This function wants the unescaped string. completer.complete_param_expand(current_token, do_file, handle_as_special_cd); } } @@ -1806,100 +1468,72 @@ void complete(const wcstring &cmd_with_subcmds, std::vector *out_c *out_comps = completer.get_completions(); } - - -/** - Print the GNU longopt style switch \c opt, and the argument \c - argument to the specified stringbuffer, but only if arguemnt is - non-null and longer than 0 characters. -*/ -static void append_switch(wcstring &out, - const wcstring &opt, - const wcstring &argument) -{ - if (argument.empty()) - return; +/// Print the GNU longopt style switch \c opt, and the argument \c argument to the specified +/// stringbuffer, but only if arguemnt is non-null and longer than 0 characters. +static void append_switch(wcstring &out, const wcstring &opt, const wcstring &argument) { + if (argument.empty()) return; wcstring esc = escape_string(argument, 1); append_format(out, L" --%ls %ls", opt.c_str(), esc.c_str()); } -wcstring complete_print() -{ +wcstring complete_print() { wcstring out; scoped_lock locker(completion_lock); - // Get a list of all completions in a vector, then sort it by order + // Get a list of all completions in a vector, then sort it by order. std::vector all_completions; - for (completion_entry_set_t::const_iterator i = completion_set.begin(); i != completion_set.end(); ++i) - { + for (completion_entry_set_t::const_iterator i = completion_set.begin(); + i != completion_set.end(); ++i) { all_completions.push_back(&*i); } sort(all_completions.begin(), all_completions.end(), compare_completions_by_order); - for (std::vector::const_iterator iter = all_completions.begin(); iter != all_completions.end(); ++iter) - { + for (std::vector::const_iterator iter = all_completions.begin(); + iter != all_completions.end(); ++iter) { const completion_entry_t *e = *iter; const option_list_t &options = e->get_options(); - for (option_list_t::const_iterator oiter = options.begin(); oiter != options.end(); ++oiter) - { + for (option_list_t::const_iterator oiter = options.begin(); oiter != options.end(); + ++oiter) { const complete_entry_opt_t *o = &*oiter; - const wchar_t *modestr[] = - { - L"", - L" --no-files", - L" --require-parameter", - L" --exclusive" - } - ; + const wchar_t *modestr[] = {L"", L" --no-files", L" --require-parameter", + L" --exclusive"}; - append_format(out, - L"complete%ls", - modestr[o->result_mode]); + append_format(out, L"complete%ls", modestr[o->result_mode]); - append_switch(out, - e->cmd_is_path ? L"path" : L"command", + append_switch(out, e->cmd_is_path ? L"path" : L"command", escape_string(e->cmd, ESCAPE_ALL)); - - switch (o->type) - { - case option_type_args_only: + + switch (o->type) { + case option_type_args_only: { break; - - case option_type_short: - assert(! o->option.empty()); + } + case option_type_short: { + assert(!o->option.empty()); append_format(out, L" --short-option '%lc'", o->option.at(0)); break; - + } case option_type_single_long: - case option_type_double_long: - append_switch(out, - o->type == option_type_single_long ? L"old-option" : L"long-option", - o->option); + case option_type_double_long: { + append_switch( + out, o->type == option_type_single_long ? L"old-option" : L"long-option", + o->option); break; + } } - append_switch(out, - L"description", - C_(o->desc)); - - append_switch(out, - L"arguments", - o->comp); - - append_switch(out, - L"condition", - o->condition); - + append_switch(out, L"description", C_(o->desc)); + append_switch(out, L"arguments", o->comp); + append_switch(out, L"condition", o->condition); out.append(L"\n"); } } - - /* Append wraps. This is a wonky interface where even values are the commands, and odd values are the targets that they wrap. */ + + // Append wraps. This is a wonky interface where even values are the commands, and odd values + // are the targets that they wrap. const wcstring_list_t wrap_pairs = complete_get_wrap_pairs(); assert(wrap_pairs.size() % 2 == 0); - for (size_t i=0; i < wrap_pairs.size(); ) - { + for (size_t i = 0; i < wrap_pairs.size();) { const wcstring &cmd = wrap_pairs.at(i++); const wcstring &target = wrap_pairs.at(i++); append_format(out, L"complete --command %ls --wraps %ls\n", cmd.c_str(), target.c_str()); @@ -1907,58 +1541,50 @@ wcstring complete_print() return out; } - -/* Completion "wrapper" support. The map goes from wrapping-command to wrapped-command-list */ +// Completion "wrapper" support. The map goes from wrapping-command to wrapped-command-list. static pthread_mutex_t wrapper_lock = PTHREAD_MUTEX_INITIALIZER; typedef std::map wrapper_map_t; -static wrapper_map_t &wrap_map() -{ +static wrapper_map_t &wrap_map() { ASSERT_IS_LOCKED(wrapper_lock); - // A pointer is a little more efficient than an object as a static because we can elide the thread-safe initialization + // A pointer is a little more efficient than an object as a static because we can elide the + // thread-safe initialization. static wrapper_map_t *wrapper_map = NULL; - if (wrapper_map == NULL) - { + if (wrapper_map == NULL) { wrapper_map = new wrapper_map_t(); } return *wrapper_map; } -/* Add a new target that is wrapped by command. Example: __fish_sgrep (command) wraps grep (target). */ -bool complete_add_wrapper(const wcstring &command, const wcstring &new_target) -{ - if (command.empty() || new_target.empty()) - { +// Add a new target that is wrapped by command. Example: __fish_sgrep (command) wraps grep (target). +bool complete_add_wrapper(const wcstring &command, const wcstring &new_target) { + if (command.empty() || new_target.empty()) { return false; } - + scoped_lock locker(wrapper_lock); wrapper_map_t &wraps = wrap_map(); wcstring_list_t *targets = &wraps[command]; - // If it's already present, we do nothing - if (std::find(targets->begin(), targets->end(), new_target) == targets->end()) - { + // If it's already present, we do nothing. + if (std::find(targets->begin(), targets->end(), new_target) == targets->end()) { targets->push_back(new_target); } return true; } -bool complete_remove_wrapper(const wcstring &command, const wcstring &target_to_remove) -{ - if (command.empty() || target_to_remove.empty()) - { +bool complete_remove_wrapper(const wcstring &command, const wcstring &target_to_remove) { + if (command.empty() || target_to_remove.empty()) { return false; } - + scoped_lock locker(wrapper_lock); wrapper_map_t &wraps = wrap_map(); bool result = false; wrapper_map_t::iterator current_targets_iter = wraps.find(command); - if (current_targets_iter != wraps.end()) - { + if (current_targets_iter != wraps.end()) { wcstring_list_t *targets = ¤t_targets_iter->second; - wcstring_list_t::iterator where = std::find(targets->begin(), targets->end(), target_to_remove); - if (where != targets->end()) - { + wcstring_list_t::iterator where = + std::find(targets->begin(), targets->end(), target_to_remove); + if (where != targets->end()) { targets->erase(where); result = true; } @@ -1966,58 +1592,52 @@ bool complete_remove_wrapper(const wcstring &command, const wcstring &target_to_ return result; } -wcstring_list_t complete_get_wrap_chain(const wcstring &command) -{ - if (command.empty()) - { +wcstring_list_t complete_get_wrap_chain(const wcstring &command) { + if (command.empty()) { return wcstring_list_t(); } scoped_lock locker(wrapper_lock); const wrapper_map_t &wraps = wrap_map(); - + wcstring_list_t result; - std::set visited; // set of visited commands - wcstring_list_t to_visit(1, command); // stack of remaining-to-visit commands - + std::set visited; // set of visited commands + wcstring_list_t to_visit(1, command); // stack of remaining-to-visit commands + wcstring target; - while (! to_visit.empty()) - { - // Grab the next command to visit, put it in target + while (!to_visit.empty()) { + // Grab the next command to visit, put it in target. target.swap(to_visit.back()); to_visit.pop_back(); - - // Try inserting into visited. If it was already present, we skip it; this is how we avoid loops. - if (! visited.insert(target).second) - { + + // Try inserting into visited. If it was already present, we skip it; this is how we avoid + // loops. + if (!visited.insert(target).second) { continue; } - - // Insert the target in the result. Note this is the command itself, if this is the first iteration of the loop. + + // Insert the target in the result. Note this is the command itself, if this is the first + // iteration of the loop. result.push_back(target); - - // Enqueue its children + + // Enqueue its children. wrapper_map_t::const_iterator target_children_iter = wraps.find(target); - if (target_children_iter != wraps.end()) - { + if (target_children_iter != wraps.end()) { const wcstring_list_t &children = target_children_iter->second; to_visit.insert(to_visit.end(), children.begin(), children.end()); } } - + return result; } -wcstring_list_t complete_get_wrap_pairs() -{ +wcstring_list_t complete_get_wrap_pairs() { wcstring_list_t result; scoped_lock locker(wrapper_lock); const wrapper_map_t &wraps = wrap_map(); - for (wrapper_map_t::const_iterator outer = wraps.begin(); outer != wraps.end(); ++outer) - { + for (wrapper_map_t::const_iterator outer = wraps.begin(); outer != wraps.end(); ++outer) { const wcstring &cmd = outer->first; const wcstring_list_t &targets = outer->second; - for (size_t i=0; i < targets.size(); i++) - { + for (size_t i = 0; i < targets.size(); i++) { result.push_back(cmd); result.push_back(targets.at(i)); } diff --git a/src/complete.h b/src/complete.h index 1e2c1a185..57c743c02 100644 --- a/src/complete.h +++ b/src/complete.h @@ -1,269 +1,197 @@ -/** \file complete.h - Prototypes for functions related to tab-completion. - - These functions are used for storing and retrieving tab-completion - data, as well as for performing tab-completion. -*/ - +/// Prototypes for functions related to tab-completion. +/// +/// These functions are used for storing and retrieving tab-completion data, as well as for +/// performing tab-completion. #ifndef FISH_COMPLETE_H #define FISH_COMPLETE_H -#include -#include #include +#include +#include #include "common.h" -/** - * Use all completions - */ +/// Use all completions. #define SHARED 0 -/** - * Do not use file completion - */ +/// Do not use file completion. #define NO_FILES 1 -/** - * Require a parameter after completion - */ +/// Require a parameter after completion. #define NO_COMMON 2 -/** - * Only use the argument list specifies with completion after - * option. This is the same as (NO_FILES | NO_COMMON) - */ +/// Only use the argument list specifies with completion after option. This is the same as (NO_FILES +/// | NO_COMMON). #define EXCLUSIVE 3 - -/** - * Command is a path - */ +/// Command is a path. #define PATH 1 -/** - * Command is not a path - */ +/// Command is not a path. #define COMMAND 0 - -/** - * Separator between completion and description - */ +/// Separator between completion and description. #define COMPLETE_SEP L'\004' - -/** - * Character that separates the completion and description on - * programmable completions - */ +/// Character that separates the completion and description on programmable completions. #define PROG_COMPLETE_SEP L'\t' -enum -{ - /** - Do not insert space afterwards if this is the only completion. (The - default is to try insert a space) - */ +enum { + /// Do not insert space afterwards if this is the only completion. (The default is to try insert + /// a space). COMPLETE_NO_SPACE = 1 << 0, - - /** This is not the suffix of a token, but replaces it entirely */ + /// This is not the suffix of a token, but replaces it entirely. COMPLETE_REPLACES_TOKEN = 1 << 2, - - /** This completion may or may not want a space at the end - guess by - checking the last character of the completion. */ + /// This completion may or may not want a space at the end - guess by checking the last + /// character of the completion. COMPLETE_AUTO_SPACE = 1 << 3, - - /** This completion should be inserted as-is, without escaping. */ + /// This completion should be inserted as-is, without escaping. COMPLETE_DONT_ESCAPE = 1 << 4, - - /** If you do escape, don't escape tildes */ + /// If you do escape, don't escape tildes. COMPLETE_DONT_ESCAPE_TILDES = 1 << 5 }; typedef int complete_flags_t; - -class completion_t -{ - -private: - /* No public default constructor */ +class completion_t { + private: + // No public default constructor. completion_t(); -public: - /* Destructor. Not inlining it saves code size. */ + public: + // Destructor. Not inlining it saves code size. ~completion_t(); - /** The completion string */ + /// The completion string. wcstring completion; - - /** The description for this completion */ + /// The description for this completion. wcstring description; - - /** The type of fuzzy match */ + /// The type of fuzzy match. string_fuzzy_match_t match; - - /** - Flags determining the completion behaviour. - - Determines whether a space should be inserted after this - completion if it is the only possible completion using the - COMPLETE_NO_SPACE flag. - - The COMPLETE_NO_CASE can be used to signal that this completion - is case insensitive. - */ + /// Flags determining the completion behaviour. + /// + /// Determines whether a space should be inserted after this completion if it is the only + /// possible completion using the COMPLETE_NO_SPACE flag. The COMPLETE_NO_CASE can be used to + /// signal that this completion is case insensitive. complete_flags_t flags; - /* Construction. Note: defining these so that they are not inlined reduces the executable size. */ - explicit completion_t(const wcstring &comp, const wcstring &desc = wcstring(), string_fuzzy_match_t match = string_fuzzy_match_t(fuzzy_match_exact), complete_flags_t flags_val = 0); + // Construction. Note: defining these so that they are not inlined reduces the executable size. + explicit completion_t(const wcstring &comp, const wcstring &desc = wcstring(), + string_fuzzy_match_t match = string_fuzzy_match_t(fuzzy_match_exact), + complete_flags_t flags_val = 0); completion_t(const completion_t &); completion_t &operator=(const completion_t &); - /* Compare two completions. No operating overlaoding to make this always explicit (there's potentially multiple ways to compare completions). */ - - /* "Naturally less than" means in a natural ordering, where digits are treated as numbers. For example, foo10 is naturally greater than foo2 (but alphabetically less than it) */ + // Compare two completions. No operating overlaoding to make this always explicit (there's + // potentially multiple ways to compare completions). + // + // "Naturally less than" means in a natural ordering, where digits are treated as numbers. For + // example, foo10 is naturally greater than foo2 (but alphabetically less than it). static bool is_naturally_less_than(const completion_t &a, const completion_t &b); static bool is_alphabetically_equal_to(const completion_t &a, const completion_t &b); - - /* If this completion replaces the entire token, prepend a prefix. Otherwise do nothing. */ + + // If this completion replaces the entire token, prepend a prefix. Otherwise do nothing. void prepend_token_prefix(const wcstring &prefix); }; -/** Sorts and remove any duplicate completions in the completion list, then puts them in priority order. */ +/// Sorts and remove any duplicate completions in the completion list, then puts them in priority +/// order. void completions_sort_and_prioritize(std::vector *comps); -enum -{ +enum { COMPLETION_REQUEST_DEFAULT = 0, - COMPLETION_REQUEST_AUTOSUGGESTION = 1 << 0, // indicates the completion is for an autosuggestion - COMPLETION_REQUEST_DESCRIPTIONS = 1 << 1, // indicates that we want descriptions - COMPLETION_REQUEST_FUZZY_MATCH = 1 << 2 // indicates that we don't require a prefix match + COMPLETION_REQUEST_AUTOSUGGESTION = 1 + << 0, // indicates the completion is for an autosuggestion + COMPLETION_REQUEST_DESCRIPTIONS = 1 << 1, // indicates that we want descriptions + COMPLETION_REQUEST_FUZZY_MATCH = 1 << 2 // indicates that we don't require a prefix match }; typedef uint32_t completion_request_flags_t; -/** - - Add a completion. - - All supplied values are copied, they should be freed by or otherwise - disposed by the caller. - - Examples: - - The command 'gcc -o' requires that a file follows it, so the - NO_COMMON option is suitable. This can be done using the following - line: - - complete -c gcc -s o -r - - The command 'grep -d' required that one of the strings 'read', - 'skip' or 'recurse' is used. As such, it is suitable to specify that - a completion requires one of them. This can be done using the - following line: - - complete -c grep -s d -x -a "read skip recurse" - - - \param cmd Command to complete. - \param cmd_type If cmd_type is PATH, cmd will be interpreted as the absolute - path of the program (optionally containing wildcards), otherwise it - will be interpreted as the command name. - \param short_opt The single character name of an option. (-a is a short option, - --all and -funroll are long options) - \param long_opt The multi character name of an option. (-a is a short option, - --all and -funroll are long options) - \param long_mode Whether to use old style, single dash long options. - \param result_mode Whether to search further completions when this - completion has been succesfully matched. If result_mode is SHARED, - any other completions may also be used. If result_mode is NO_FILES, - file completion should not be used, but other completions may be - used. If result_mode is NO_COMMON, on option may follow it - only a - parameter. If result_mode is EXCLUSIVE, no option may follow it, and - file completion is not performed. - \param comp A space separated list of completions which may contain subshells. - \param desc A description of the completion. - \param condition a command to be run to check it this completion should be used. - If \c condition is empty, the completion is always used. - \param flags A set of completion flags -*/ -enum complete_option_type_t -{ - option_type_args_only, // no option - option_type_short, // -x - option_type_single_long, // -foo - option_type_double_long // --foo +/// Add a completion. +/// +/// All supplied values are copied, they should be freed by or otherwise disposed by the caller. +/// +/// Examples: +/// +/// The command 'gcc -o' requires that a file follows it, so the NO_COMMON option is suitable. This +/// can be done using the following line: +/// +/// complete -c gcc -s o -r +/// +/// The command 'grep -d' required that one of the strings 'read', 'skip' or 'recurse' is used. As +/// such, it is suitable to specify that a completion requires one of them. This can be done using +/// the following line: +/// +/// complete -c grep -s d -x -a "read skip recurse" +/// +/// \param cmd Command to complete. +/// \param cmd_type If cmd_type is PATH, cmd will be interpreted as the absolute +/// path of the program (optionally containing wildcards), otherwise it +/// will be interpreted as the command name. +/// \param short_opt The single character name of an option. (-a is a short option, +/// --all and -funroll are long options) +/// \param long_opt The multi character name of an option. (-a is a short option, --all and +/// -funroll are long options) +/// \param long_mode Whether to use old style, single dash long options. +/// \param result_mode Whether to search further completions when this completion has been +/// succesfully matched. If result_mode is SHARED, any other completions may also be used. If +/// result_mode is NO_FILES, file completion should not be used, but other completions may be used. +/// If result_mode is NO_COMMON, on option may follow it - only a parameter. If result_mode is +/// EXCLUSIVE, no option may follow it, and file completion is not performed. +/// \param comp A space separated list of completions which may contain subshells. +/// \param desc A description of the completion. +/// \param condition a command to be run to check it this completion should be used. If \c condition +/// is empty, the completion is always used. +/// \param flags A set of completion flags +enum complete_option_type_t { + option_type_args_only, // no option + option_type_short, // -x + option_type_single_long, // -foo + option_type_double_long // --foo }; -void complete_add(const wchar_t *cmd, - bool cmd_is_path, - const wcstring &option, - complete_option_type_t option_type, - int result_mode, - const wchar_t *condition, - const wchar_t *comp, - const wchar_t *desc, - int flags); -/** - Sets whether the completion list for this command is complete. If - true, any options not matching one of the provided options will be - flagged as an error by syntax highlighting. -*/ +void complete_add(const wchar_t *cmd, bool cmd_is_path, const wcstring &option, + complete_option_type_t option_type, int result_mode, const wchar_t *condition, + const wchar_t *comp, const wchar_t *desc, int flags); + +/// Sets whether the completion list for this command is complete. If true, any options not matching +/// one of the provided options will be flagged as an error by syntax highlighting. void complete_set_authoritative(const wchar_t *cmd, bool cmd_type, bool authoritative); -/** - Remove a previously defined completion -*/ -void complete_remove(const wcstring &cmd, - bool cmd_is_path, - const wcstring &option, +/// Remove a previously defined completion. +void complete_remove(const wcstring &cmd, bool cmd_is_path, const wcstring &option, complete_option_type_t type); -/** Removes all completions for a given command */ +/// Removes all completions for a given command. void complete_remove_all(const wcstring &cmd, bool cmd_is_path); -/** Find all completions of the command cmd, insert them into out. - */ +/// Find all completions of the command cmd, insert them into out. class env_vars_snapshot_t; -void complete(const wcstring &cmd, - std::vector *out_comps, - completion_request_flags_t flags, - const env_vars_snapshot_t &vars); +void complete(const wcstring &cmd, std::vector *out_comps, + completion_request_flags_t flags, const env_vars_snapshot_t &vars); -/** - Return a list of all current completions. -*/ +/// Return a list of all current completions. wcstring complete_print(); -/** - Tests if the specified option is defined for the specified command -*/ -int complete_is_valid_option(const wcstring &str, - const wcstring &opt, - wcstring_list_t *inErrorsOrNull, - bool allow_autoload); +/// Tests if the specified option is defined for the specified command. +int complete_is_valid_option(const wcstring &str, const wcstring &opt, + wcstring_list_t *inErrorsOrNull, bool allow_autoload); -/** - Tests if the specified argument is valid for the specified option - and command -*/ -bool complete_is_valid_argument(const wcstring &str, - const wcstring &opt, - const wcstring &arg); +/// Tests if the specified argument is valid for the specified option and command. +bool complete_is_valid_argument(const wcstring &str, const wcstring &opt, const wcstring &arg); +/// Create a new completion entry. +/// +/// \param completions The array of completions to append to +/// \param comp The completion string +/// \param desc The description of the completion +/// \param flags completion flags +void append_completion(std::vector *completions, const wcstring &comp, + const wcstring &desc = wcstring(), int flags = 0, + string_fuzzy_match_t match = string_fuzzy_match_t(fuzzy_match_exact)); -/** - Create a new completion entry - - \param completions The array of completions to append to - \param comp The completion string - \param desc The description of the completion - \param flags completion flags - -*/ -void append_completion(std::vector *completions, const wcstring &comp, const wcstring &desc = wcstring(), int flags = 0, string_fuzzy_match_t match = string_fuzzy_match_t(fuzzy_match_exact)); - -/* Function used for testing */ +/// Function used for testing. void complete_set_variable_names(const wcstring_list_t *names); -/* Support for "wrap targets." A wrap target is a command that completes liek another command. The target chain is the sequence of wraps (A wraps B wraps C...). Any loops in the chain are silently ignored. */ +/// Support for "wrap targets." A wrap target is a command that completes liek another command. The +/// target chain is the sequence of wraps (A wraps B wraps C...). Any loops in the chain are +/// silently ignored. bool complete_add_wrapper(const wcstring &command, const wcstring &wrap_target); bool complete_remove_wrapper(const wcstring &command, const wcstring &wrap_target); wcstring_list_t complete_get_wrap_chain(const wcstring &command); -/* Wonky interface: returns all wraps. Even-values are the commands, odd values are the targets. */ +// Wonky interface: returns all wraps. Even-values are the commands, odd values are the targets. wcstring_list_t complete_get_wrap_pairs(); #endif diff --git a/src/exec.cpp b/src/exec.cpp index b7735d62e..b55e7a26e 100644 --- a/src/exec.cpp +++ b/src/exec.cpp @@ -257,23 +257,16 @@ static bool io_transmogrify(const io_chain_t &in_chain, io_chain_t *out_chain, shared_ptr out; // gets allocated via new switch (in->io_mode) { - default: - // Unknown type, should never happen. - fprintf(stderr, "Unknown io_mode %ld\n", (long)in->io_mode); - abort(); - break; - - // These redirections don't need transmogrification. They can be passed through. case IO_PIPE: case IO_FD: case IO_BUFFER: case IO_CLOSE: { + // These redirections don't need transmogrification. They can be passed through. out = in; break; } - - // Transmogrify file redirections. case IO_FILE: { + // Transmogrify file redirections. int fd; CAST_INIT(io_file_t *, in_file, in.get()); if ((fd = open(in_file->filename_cstr, in_file->flags, OPEN_MASK)) == -1) { @@ -288,6 +281,12 @@ static bool io_transmogrify(const io_chain_t &in_chain, io_chain_t *out_chain, out.reset(new io_fd_t(in->fd, fd, false)); break; } + default: { + // Unknown type, should never happen. + fprintf(stderr, "Unknown io_mode %ld\n", (long)in->io_mode); + abort(); + break; + } } if (out.get() != NULL) result_chain.push_back(out); diff --git a/src/history.cpp b/src/history.cpp index dfb3608c1..f7f3affc6 100644 --- a/src/history.cpp +++ b/src/history.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -15,7 +16,6 @@ #include #include #include -#include #include "common.h" #include "env.h" @@ -473,9 +473,9 @@ static size_t offset_of_next_item(const char *begin, size_t mmap_length, offset_of_next_item_fish_1_x(begin, mmap_length, inout_cursor, cutoff_timestamp); break; } - default: - case history_type_unknown: { - // Oh well + case history_type_unknown: + default: { + // Oh well. result = (size_t)(-1); break; } @@ -804,12 +804,13 @@ done: history_item_t history_t::decode_item(const char *base, size_t len, history_file_type_t type) { switch (type) { - case history_type_fish_1_x: + case history_type_fish_1_x: { return history_t::decode_item_fish_1_x(base, len); - case history_type_fish_2_0: + } + case history_type_fish_2_0: { return history_t::decode_item_fish_2_0(base, len); - default: - return history_item_t(L""); + } + default: { return history_item_t(L""); } } } diff --git a/src/history.h b/src/history.h index a861f9784..0aa09b7c1 100644 --- a/src/history.h +++ b/src/history.h @@ -4,16 +4,16 @@ // IWYU pragma: no_include #include +#include #include #include #include #include +#include #include #include #include #include -#include -#include #include "common.h" #include "wutil.h" // IWYU pragma: keep diff --git a/src/input.cpp b/src/input.cpp index d37b963c0..0740a113e 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -256,10 +256,10 @@ void input_set_bind_mode(const wcstring &bm) { int input_function_arity(int function) { switch (function) { case R_FORWARD_JUMP: - case R_BACKWARD_JUMP: + case R_BACKWARD_JUMP: { return 1; - default: - return 0; + } + default: { return 0; } } } diff --git a/src/key_reader.cpp b/src/key_reader.cpp index bb4a0075b..881730771 100644 --- a/src/key_reader.cpp +++ b/src/key_reader.cpp @@ -5,32 +5,28 @@ Type ^C to exit the program. */ -#include -#include -#include -#include #include +#include +#include +#include #include +#include #include "common.h" #include "fallback.h" // IWYU pragma: keep #include "input_common.h" -int writestr(char *str) -{ +int writestr(char *str) { write_ignore(1, str, strlen(str)); return 0; } -int main(int argc, char **argv) -{ +int main(int argc, char **argv) { set_main_thread(); setup_fork_guards(); setlocale(LC_ALL, ""); - - if (argc == 2) - { + if (argc == 2) { static char term_buffer[2048]; char *termtype = getenv("TERM"); char *tbuff = new char[9999]; @@ -38,45 +34,35 @@ int main(int argc, char **argv) tgetent(term_buffer, termtype); res = tgetstr(argv[1], &tbuff); - if (res != 0) - { - while (*res != 0) - { + if (res != 0) { + while (*res != 0) { printf("%d ", *res); - res++; } printf("\n"); - } - else - { + } else { printf("Undefined sequence\n"); } - } - else - { + } else { char scratch[1024]; unsigned int c; - struct termios modes, /* so we can change the modes */ - savemodes; /* so we can reset the modes when we're done */ + struct termios modes, /* so we can change the modes */ + savemodes; /* so we can reset the modes when we're done */ input_common_init(0); + tcgetattr(0, &modes); /* get the current terminal modes */ + savemodes = modes; /* save a copy so we can reset them */ - tcgetattr(0,&modes); /* get the current terminal modes */ - savemodes = modes; /* save a copy so we can reset them */ - - modes.c_lflag &= ~ICANON; /* turn off canonical mode */ + modes.c_lflag &= ~ICANON; /* turn off canonical mode */ modes.c_lflag &= ~ECHO; /* turn off echo mode */ - modes.c_cc[VMIN]=1; - modes.c_cc[VTIME]=0; - tcsetattr(0,TCSANOW,&modes); /* set the new modes */ - while (1) - { - if ((c=input_common_readch(0)) == EOF) - break; + modes.c_cc[VMIN] = 1; + modes.c_cc[VTIME] = 0; + tcsetattr(0, TCSANOW, &modes); /* set the new modes */ + while (1) { + if ((c = input_common_readch(0)) == EOF) break; if ((c > 31) && (c != 127)) sprintf(scratch, "dec: %u hex: %x char: %c\n", c, c, c); else @@ -84,7 +70,7 @@ int main(int argc, char **argv) writestr(scratch); } /* reset the terminal to the saved mode */ - tcsetattr(0,TCSANOW,&savemodes); + tcsetattr(0, TCSANOW, &savemodes); input_common_destroy(); } diff --git a/src/pager.cpp b/src/pager.cpp index 446e0704e..dff8b1a58 100644 --- a/src/pager.cpp +++ b/src/pager.cpp @@ -584,26 +584,27 @@ bool pager_t::select_next_completion_in_direction(selection_direction_t directio // Handle the case of nothing selected yet. if (selected_completion_idx == PAGER_SELECTION_NONE) { switch (direction) { - // These directions do something sane. case direction_south: case direction_page_south: case direction_next: - case direction_prev: + case direction_prev: { + // These directions do something sane. if (direction == direction_prev) { selected_completion_idx = completion_infos.size() - 1; } else { selected_completion_idx = 0; } return true; - - // These do nothing. + } case direction_north: case direction_page_north: case direction_east: case direction_west: case direction_deselect: - default: + default: { + // These do nothing. return false; + } } } @@ -651,7 +652,6 @@ bool pager_t::select_next_completion_in_direction(selection_direction_t directio } break; } - case direction_page_south: { if (current_row + page_height < rendering.rows) { current_row += page_height; @@ -675,7 +675,6 @@ bool pager_t::select_next_completion_in_direction(selection_direction_t directio } break; } - case direction_east: { // Go east, wrapping to the next row. There is no "row memory," so if we run off the // end, wrap. @@ -688,7 +687,6 @@ bool pager_t::select_next_completion_in_direction(selection_direction_t directio } break; } - case direction_west: { // Go west, wrapping to the previous row. if (current_col > 0) { @@ -699,10 +697,10 @@ bool pager_t::select_next_completion_in_direction(selection_direction_t directio } break; } - - default: + default: { assert(0 && "Unknown cardinal direction"); break; + } } // Compute the new index based on the changed row. diff --git a/src/parse_util.cpp b/src/parse_util.cpp index 85016244b..821669ed8 100644 --- a/src/parse_util.cpp +++ b/src/parse_util.cpp @@ -540,13 +540,15 @@ wcstring parse_util_escape_string_with_quote(const wcstring &cmd, wchar_t quote) case L'\n': case L'\t': case L'\b': - case L'\r': + case L'\r': { unescapable = true; break; - default: + } + default: { if (c == quote) result.push_back(L'\\'); result.push_back(c); break; + } } } @@ -815,21 +817,25 @@ static wcstring truncate_string(const wcstring &str) { /// For example, if wc is @, then the variable name was $@ and we suggest $argv. static const wchar_t *error_format_for_character(wchar_t wc) { switch (wc) { - case L'?': + case L'?': { return ERROR_NOT_STATUS; - case L'#': + } + case L'#': { return ERROR_NOT_ARGV_COUNT; - case L'@': + } + case L'@': { return ERROR_NOT_ARGV_AT; - case L'*': + } + case L'*': { return ERROR_NOT_ARGV_STAR; + } case L'$': case VARIABLE_EXPAND: case VARIABLE_EXPAND_SINGLE: - case VARIABLE_EXPAND_EMPTY: + case VARIABLE_EXPAND_EMPTY: { return ERROR_NOT_PID; - default: - return ERROR_BAD_VAR_CHAR1; + } + default: { return ERROR_BAD_VAR_CHAR1; } } } @@ -1156,7 +1162,6 @@ parser_test_error_bits_t parse_util_detect_errors(const wcstring &buff_src, BACKGROUND_IN_CONDITIONAL_ERROR_MSG); break; } - case symbol_job_list: { // This isn't very complete, e.g. we don't catch 'foo & ; not and bar'. assert(node_tree.get_child(*job_parent, 0) == &node); @@ -1195,9 +1200,7 @@ parser_test_error_bits_t parse_util_detect_errors(const wcstring &buff_src, } break; } - - default: - break; + default: { break; } } } } else if (node.type == symbol_plain_statement) { @@ -1268,23 +1271,24 @@ parser_test_error_bits_t parse_util_detect_errors(const wcstring &buff_src, if (loop_or_function_header != NULL) { switch (loop_or_function_header->type) { case symbol_while_header: - case symbol_for_header: + case symbol_for_header: { // This is a loop header, so we can break or continue. found_loop = true; end_search = true; break; - - case symbol_function_header: + } + case symbol_function_header: { // This is a function header, so we cannot break or // continue. We stop our search here. found_loop = false; end_search = true; break; - - default: + } + default: { // Most likely begin / end style block, which makes no // difference. break; + } } } ancestor = node_tree.get_parent(*ancestor); diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp index 37263fe23..746807eb7 100644 --- a/src/tokenizer.cpp +++ b/src/tokenizer.cpp @@ -686,8 +686,7 @@ bool move_word_state_machine_t::consume_char_path_components(wchar_t c) { case s_separator: { if (!iswspace(c) && !is_path_component_character(c)) { consumed = true; // consumed separator - } - else { + } else { state = s_end; } break; diff --git a/src/wgetopt.cpp b/src/wgetopt.cpp index d85b5e748..41b7896a1 100644 --- a/src/wgetopt.cpp +++ b/src/wgetopt.cpp @@ -45,7 +45,7 @@ // Don't include stdlib.h for non-GNU C libraries because some of them contain conflicting // prototypes for getopt. #include -#endif // GNU C library. +#endif // GNU C library. // This version of `getopt' appears to the caller like standard Unix `getopt' but it behaves // differently for the user, since it allows the user to intersperse the options with the other @@ -105,10 +105,10 @@ static wchar_t *my_index(const wchar_t *str, int chr) { // gcc with -traditional declares the built-in strlen to return int, and has done so at least since // version 2.4.5. -- rms. extern int wcslen(const wchar_t *); -#endif // not __STDC__ -#endif // __GNUC__ +#endif // not __STDC__ +#endif // __GNUC__ -#endif // not __GNU_LIBRARY__ +#endif // not __GNU_LIBRARY__ // Exchange two adjacent subsequences of ARGV. One subsequence is elements // [first_nonopt,last_nonopt) which contains all the non-options that have been skipped so far. The @@ -303,10 +303,12 @@ int wgetopter_t::_wgetopt_internal(int argc, wchar_t **argv, const wchar_t *opts const struct woption *pfound = NULL; int exact = 0; int ambig = 0; - int indfound = 0; // set to zero by Anton + int indfound = 0; // set to zero by Anton int option_index; - for (nameend = nextchar; *nameend && *nameend != '='; nameend++) /* Do nothing. */; + for (nameend = nextchar; *nameend && *nameend != '='; nameend++) { + // Do nothing. + } // Test all long options for either exact match or abbreviated matches. for (p = longopts, option_index = 0; p->name; p++, option_index++) @@ -344,7 +346,7 @@ int wgetopter_t::_wgetopt_internal(int argc, wchar_t **argv, const wchar_t *opts woptarg = nameend + 1; else { if (wopterr) { - if (argv[woptind - 1][1] == '-') // --option + if (argv[woptind - 1][1] == '-') // --option fwprintf(stderr, _(L"%ls: Option '--%ls' doesn't allow an argument\n"), argv[0], pfound->name); else @@ -380,7 +382,7 @@ int wgetopter_t::_wgetopt_internal(int argc, wchar_t **argv, const wchar_t *opts // short option. if (!long_only || argv[woptind][1] == '-' || my_index(optstring, *nextchar) == NULL) { if (wopterr) { - if (argv[woptind][1] == '-') // --option + if (argv[woptind][1] == '-') // --option fwprintf(stderr, _(L"%ls: Unrecognized option '--%ls'\n"), argv[0], nextchar); else // +option or -option diff --git a/src/wgetopt.h b/src/wgetopt.h index 95e1462d9..5cf60ea0c 100644 --- a/src/wgetopt.h +++ b/src/wgetopt.h @@ -68,7 +68,7 @@ class wgetopter_t { int woptind; // The next char to be scanned in the option-element in which the last option character we - // returned was found. This allows us to pick up the scan where we left off. + // returned was found. This allows us to pick up the scan where we left off. // // If this is zero, or a null string, it means resume the scan by advancing to the next // ARGV-element. diff --git a/src/wildcard.cpp b/src/wildcard.cpp index 1a09d63e9..93242570e 100644 --- a/src/wildcard.cpp +++ b/src/wildcard.cpp @@ -94,7 +94,7 @@ static enum fuzzy_match_type_t wildcard_match_internal(const wchar_t *str, const bool leading_dots_fail_to_match, bool is_first) { if (*str == 0 && *wc == 0) { - return fuzzy_match_exact; // we're done + return fuzzy_match_exact; // we're done } // Hackish fix for issue #270. Prevent wildcards from matching . or .., but we must still allow @@ -261,7 +261,6 @@ static bool wildcard_complete_internal(const wchar_t *str, const wchar_t *wc, } break; } - case ANY_STRING: { // Hackish. If this is the last character of the wildcard, then just complete with // the empty string. This fixes cases like "f*" -> "f*o". @@ -289,14 +288,14 @@ static bool wildcard_complete_internal(const wchar_t *str, const wchar_t *wc, } return has_match; } - - case ANY_STRING_RECURSIVE: + case ANY_STRING_RECURSIVE: { // We don't even try with this one. return false; - - default: + } + default: { assert(0 && "Unreachable code reached"); return false; + } } } assert(0 && "Unreachable code reached"); @@ -535,9 +534,9 @@ class wildcard_expander_t { wcstring child_entry; while (wreaddir_resolving(dir, abs_unique_hierarchy, child_entry, &child_is_dir)) { if (child_entry.empty() || child_entry.at(0) == L'.') { - continue; // either hidden, or . and .. entries -- skip them + continue; // either hidden, or . and .. entries -- skip them } else if (child_is_dir && unique_entry.empty()) { - unique_entry = child_entry; // first candidate + unique_entry = child_entry; // first candidate } else { // We either have two or more candidates, or the child is not a directory. We're // done.