2016-07-21 05:30:58 +00:00
|
|
|
/// Functions related to tab-completion.
|
|
|
|
///
|
|
|
|
/// These functions are used for storing and retrieving tab-completion data, as well as for
|
|
|
|
/// performing tab-completion.
|
|
|
|
///
|
2016-05-18 22:30:21 +00:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <pwd.h>
|
2015-07-25 15:14:25 +00:00
|
|
|
#include <stddef.h>
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <wchar.h>
|
|
|
|
#include <wctype.h>
|
2017-02-14 04:37:27 +00:00
|
|
|
|
2012-04-24 02:29:44 +00:00
|
|
|
#include <algorithm>
|
2017-08-05 22:08:39 +00:00
|
|
|
#include <cstddef>
|
2017-02-11 02:47:02 +00:00
|
|
|
#include <functional>
|
2017-08-05 22:08:39 +00:00
|
|
|
#include <iterator>
|
2015-07-25 15:14:25 +00:00
|
|
|
#include <list>
|
2016-05-03 23:23:30 +00:00
|
|
|
#include <memory>
|
2015-07-25 15:14:25 +00:00
|
|
|
#include <set>
|
|
|
|
#include <string>
|
2017-02-11 02:47:02 +00:00
|
|
|
#include <type_traits>
|
2017-08-19 16:55:06 +00:00
|
|
|
#include <unordered_map>
|
2017-07-24 22:54:54 +00:00
|
|
|
#include <unordered_set>
|
2017-08-06 23:05:51 +00:00
|
|
|
#include <utility>
|
2006-02-28 13:17:16 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
#include "autoload.h"
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "builtin.h"
|
2016-05-03 23:23:30 +00:00
|
|
|
#include "common.h"
|
|
|
|
#include "complete.h"
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "exec.h"
|
|
|
|
#include "expand.h"
|
2016-05-03 23:23:30 +00:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
|
|
|
#include "function.h"
|
2013-11-30 07:44:26 +00:00
|
|
|
#include "iothread.h"
|
2015-07-25 15:14:25 +00:00
|
|
|
#include "parse_constants.h"
|
2016-05-03 23:23:30 +00:00
|
|
|
#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
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// 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?
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// Description for ~USER completion.
|
|
|
|
#define COMPLETE_USER_DESC _(L"Home for %ls")
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// Description for short variables. The value is concatenated to this description.
|
|
|
|
#define COMPLETE_VAR_DESC_VAL _(L"Variable: %ls")
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// 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).
|
2017-11-16 13:29:42 +00:00
|
|
|
#ifdef HAVE_GETTEXT
|
2016-06-02 05:03:27 +00:00
|
|
|
static const wchar_t *C_(const wcstring &s) {
|
|
|
|
return s.empty() ? L"" : wgettext(s.c_str()).c_str();
|
|
|
|
}
|
2006-11-29 14:18:22 +00:00
|
|
|
#else
|
2016-05-03 23:23:30 +00:00
|
|
|
static const wcstring &C_(const wcstring &s) { return s; }
|
2006-11-29 14:18:22 +00:00
|
|
|
#endif
|
|
|
|
|
2015-10-08 01:59:41 +00:00
|
|
|
static void complete_load(const wcstring &name, bool reload);
|
|
|
|
|
2016-08-17 01:23:10 +00:00
|
|
|
/// Testing apparatus.
|
2013-03-06 04:54:16 +00:00
|
|
|
const wcstring_list_t *s_override_variable_names = NULL;
|
2006-06-07 23:56:01 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
void complete_set_variable_names(const wcstring_list_t *names) {
|
2013-03-06 04:54:16 +00:00
|
|
|
s_override_variable_names = names;
|
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
static inline wcstring_list_t complete_get_variable_names(void) {
|
|
|
|
if (s_override_variable_names != NULL) {
|
2013-03-06 04:54:16 +00:00
|
|
|
return *s_override_variable_names;
|
|
|
|
}
|
2016-05-04 22:19:47 +00:00
|
|
|
return env_get_names(0);
|
2013-03-06 04:54:16 +00:00
|
|
|
}
|
2009-02-02 22:46:45 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// 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').
|
2016-01-16 23:46:43 +00:00
|
|
|
wcstring option;
|
2016-05-03 23:23:30 +00:00
|
|
|
// Type of the option: args-oly, short, single_long, or double_long.
|
2016-01-16 23:46:43 +00:00
|
|
|
complete_option_type_t type;
|
2016-05-03 23:23:30 +00:00
|
|
|
// Arguments to the option.
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring comp;
|
2016-05-03 23:23:30 +00:00
|
|
|
// Description of the completion.
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring desc;
|
2016-05-03 23:23:30 +00:00
|
|
|
// Condition under which to use the option.
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring condition;
|
2016-05-03 23:23:30 +00:00
|
|
|
// 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.
|
2012-11-19 00:30:30 +00:00
|
|
|
int result_mode;
|
2016-05-03 23:23:30 +00:00
|
|
|
// Completion flags.
|
2012-11-19 00:30:30 +00:00
|
|
|
complete_flags_t flags;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
const wcstring localized_desc() const { return C_(desc); }
|
|
|
|
|
|
|
|
size_t expected_dash_count() const {
|
|
|
|
switch (this->type) {
|
2016-01-16 23:46:43 +00:00
|
|
|
case option_type_args_only:
|
|
|
|
return 0;
|
|
|
|
case option_type_short:
|
|
|
|
case option_type_single_long:
|
|
|
|
return 1;
|
|
|
|
case option_type_double_long:
|
|
|
|
return 2;
|
|
|
|
}
|
2016-11-02 04:19:34 +00:00
|
|
|
DIE("unreachable");
|
2016-01-16 23:46:43 +00:00
|
|
|
}
|
2016-05-03 23:23:30 +00:00
|
|
|
|
2012-02-09 00:15:53 +00:00
|
|
|
} complete_entry_opt_t;
|
2016-08-17 01:23:10 +00:00
|
|
|
/// Last value used in the order field of completion_entry_t.
|
2012-04-12 01:25:37 +00:00
|
|
|
static unsigned int kCompleteOrder = 0;
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// Struct describing a command completion.
|
2012-02-09 00:15:53 +00:00
|
|
|
typedef std::list<complete_entry_opt_t> option_list_t;
|
2016-05-03 23:23:30 +00:00
|
|
|
class completion_entry_t {
|
|
|
|
public:
|
|
|
|
/// List of all options.
|
2012-11-19 00:30:30 +00:00
|
|
|
option_list_t options;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
public:
|
|
|
|
/// Command string.
|
2012-11-19 00:30:30 +00:00
|
|
|
const wcstring cmd;
|
2016-05-03 23:23:30 +00:00
|
|
|
/// True if command is a path.
|
2012-11-19 00:30:30 +00:00
|
|
|
const bool cmd_is_path;
|
2016-05-03 23:23:30 +00:00
|
|
|
/// Order for when this completion was created. This aids in outputting completions sorted by
|
|
|
|
/// time.
|
2012-04-12 01:25:37 +00:00
|
|
|
const unsigned int order;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// Getters for option list.
|
2012-02-26 22:32:06 +00:00
|
|
|
const option_list_t &get_options() const;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// Adds or removes an option.
|
2012-05-12 01:59:38 +00:00
|
|
|
void add_option(const complete_entry_opt_t &opt);
|
2016-01-17 06:42:14 +00:00
|
|
|
bool remove_option(const wcstring &option, complete_option_type_t type);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-12-19 17:05:18 +00:00
|
|
|
completion_entry_t(const wcstring &c, bool type)
|
|
|
|
: cmd(c), cmd_is_path(type), order(++kCompleteOrder) {}
|
2012-02-08 22:47:50 +00:00
|
|
|
};
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// Set of all completion entries.
|
2017-08-19 20:29:52 +00:00
|
|
|
namespace std {
|
2018-01-12 19:15:35 +00:00
|
|
|
template <>
|
|
|
|
struct hash<completion_entry_t> {
|
|
|
|
size_t operator()(const completion_entry_t &c) const {
|
|
|
|
std::hash<wcstring> hasher;
|
|
|
|
return hasher((wcstring)c.cmd);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <>
|
|
|
|
struct equal_to<completion_entry_t> {
|
|
|
|
bool operator()(const completion_entry_t &c1, const completion_entry_t &c2) const {
|
|
|
|
return c1.cmd == c2.cmd;
|
|
|
|
}
|
|
|
|
};
|
2017-08-19 20:29:52 +00:00
|
|
|
}
|
|
|
|
typedef std::unordered_set<completion_entry_t> completion_entry_set_t;
|
2012-04-10 03:17:06 +00:00
|
|
|
static completion_entry_set_t completion_set;
|
2012-02-26 22:32:06 +00:00
|
|
|
|
2016-08-17 01:23:10 +00:00
|
|
|
/// Comparison function to sort completions by their order field.
|
2016-05-03 23:23:30 +00:00
|
|
|
static bool compare_completions_by_order(const completion_entry_t *p1,
|
|
|
|
const completion_entry_t *p2) {
|
2012-04-12 01:25:37 +00:00
|
|
|
return p1->order < p2->order;
|
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// The lock that guards the list of completion entries.
|
2017-08-18 19:26:35 +00:00
|
|
|
static std::mutex completion_lock;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
void completion_entry_t::add_option(const complete_entry_opt_t &opt) {
|
2016-01-17 00:00:57 +00:00
|
|
|
ASSERT_IS_LOCKED(completion_lock);
|
2012-05-12 01:59:38 +00:00
|
|
|
options.push_front(opt);
|
2012-02-26 22:32:06 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
const option_list_t &completion_entry_t::get_options() const {
|
2016-01-17 00:00:57 +00:00
|
|
|
ASSERT_IS_LOCKED(completion_lock);
|
2012-02-26 22:32:06 +00:00
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
completion_t::~completion_t() {}
|
2013-04-16 22:01:24 +00:00
|
|
|
|
2016-08-17 01:23:10 +00:00
|
|
|
/// Clear the COMPLETE_AUTO_SPACE flag, and set COMPLETE_NO_SPACE appropriately depending on the
|
|
|
|
/// suffix of the string.
|
2016-05-03 23:23:30 +00:00
|
|
|
static complete_flags_t resolve_auto_space(const wcstring &comp, complete_flags_t flags) {
|
2016-06-02 05:03:27 +00:00
|
|
|
complete_flags_t new_flags = flags;
|
2016-05-03 23:23:30 +00:00
|
|
|
if (flags & COMPLETE_AUTO_SPACE) {
|
2016-06-02 05:03:27 +00:00
|
|
|
new_flags &= ~COMPLETE_AUTO_SPACE;
|
2014-01-14 22:28:06 +00:00
|
|
|
size_t len = comp.size();
|
2016-06-02 05:03:27 +00:00
|
|
|
if (len > 0 && (wcschr(L"/=@:", comp.at(len - 1)) != 0)) new_flags |= COMPLETE_NO_SPACE;
|
2012-07-18 14:18:19 +00:00
|
|
|
}
|
2016-06-02 05:03:27 +00:00
|
|
|
return new_flags;
|
2014-01-14 22:28:06 +00:00
|
|
|
}
|
2012-07-18 14:18:19 +00:00
|
|
|
|
2016-08-17 01:40:01 +00:00
|
|
|
/// completion_t functions. Note that the constructor resolves flags!
|
2016-05-03 23:23:30 +00:00
|
|
|
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)) {}
|
2012-07-17 19:47:01 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
completion_t::completion_t(const completion_t &him)
|
|
|
|
: completion(him.completion),
|
|
|
|
description(him.description),
|
|
|
|
match(him.match),
|
|
|
|
flags(him.flags) {}
|
2012-07-17 19:47:01 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
completion_t &completion_t::operator=(const completion_t &him) {
|
|
|
|
if (this != &him) {
|
2012-07-17 19:47:01 +00:00
|
|
|
this->completion = him.completion;
|
|
|
|
this->description = him.description;
|
2013-05-25 22:41:18 +00:00
|
|
|
this->match = him.match;
|
2012-07-17 19:47:01 +00:00
|
|
|
this->flags = him.flags;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
bool completion_t::is_naturally_less_than(const completion_t &a, const completion_t &b) {
|
2017-07-24 22:54:54 +00:00
|
|
|
// For this to work, stable_sort must be used because results aren't interchangeable.
|
|
|
|
if (a.flags & b.flags & COMPLETE_DONT_SORT) {
|
|
|
|
// Both completions are from a source with the --keep-order flag.
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-23 18:13:26 +00:00
|
|
|
return wcsfilecmp(a.completion.c_str(), b.completion.c_str()) < 0;
|
2013-08-31 22:01:02 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
void completion_t::prepend_token_prefix(const wcstring &prefix) {
|
|
|
|
if (this->flags & COMPLETE_REPLACES_TOKEN) {
|
2015-08-03 00:58:37 +00:00
|
|
|
this->completion.insert(0, prefix);
|
|
|
|
}
|
|
|
|
}
|
2013-08-31 22:01:02 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
static bool compare_completions_by_match_type(const completion_t &a, const completion_t &b) {
|
2016-02-06 23:06:33 +00:00
|
|
|
return a.match.type < b.match.type;
|
|
|
|
}
|
|
|
|
|
2017-07-24 22:54:54 +00:00
|
|
|
template <class Iterator, class HashFunction>
|
|
|
|
static Iterator unique_unsorted(Iterator begin, Iterator end, HashFunction hash) {
|
|
|
|
typedef typename std::iterator_traits<Iterator>::value_type T;
|
|
|
|
|
|
|
|
std::unordered_set<size_t> temp;
|
2017-08-06 23:05:51 +00:00
|
|
|
return std::remove_if(begin, end, [&](const T &val) { return !temp.insert(hash(val)).second; });
|
2017-07-24 22:54:54 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
void completions_sort_and_prioritize(std::vector<completion_t> *comps) {
|
|
|
|
// Find the best match type.
|
2016-02-06 23:06:33 +00:00
|
|
|
fuzzy_match_type_t best_type = fuzzy_match_none;
|
2016-05-03 23:23:30 +00:00
|
|
|
for (size_t i = 0; i < comps->size(); i++) {
|
2016-02-06 23:06:33 +00:00
|
|
|
best_type = std::min(best_type, comps->at(i).match.type);
|
|
|
|
}
|
2016-05-03 23:23:30 +00:00
|
|
|
// 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) {
|
2016-02-06 23:06:33 +00:00
|
|
|
best_type = fuzzy_match_prefix;
|
|
|
|
}
|
2016-05-03 23:23:30 +00:00
|
|
|
|
|
|
|
// Throw out completions whose match types are less suitable than the best.
|
2016-02-06 23:06:33 +00:00
|
|
|
size_t i = comps->size();
|
2016-05-03 23:23:30 +00:00
|
|
|
while (i--) {
|
|
|
|
if (comps->at(i).match.type > best_type) {
|
2016-02-06 23:06:33 +00:00
|
|
|
comps->erase(comps->begin() + i);
|
|
|
|
}
|
|
|
|
}
|
2016-05-03 23:23:30 +00:00
|
|
|
|
2017-07-24 22:54:54 +00:00
|
|
|
// Sort, provided COMPLETION_DONT_SORT isn't set
|
|
|
|
stable_sort(comps->begin(), comps->end(), completion_t::is_naturally_less_than);
|
|
|
|
// Deduplicate both sorted and unsorted results
|
2017-08-06 23:05:51 +00:00
|
|
|
comps->erase(
|
|
|
|
unique_unsorted(comps->begin(), comps->end(),
|
|
|
|
[](const completion_t &c) { return std::hash<wcstring>{}(c.completion); }),
|
|
|
|
comps->end());
|
2016-05-03 23:23:30 +00:00
|
|
|
|
|
|
|
// Sort the remainder by match type. They're already sorted alphabetically.
|
2016-02-06 23:06:33 +00:00
|
|
|
stable_sort(comps->begin(), comps->end(), compare_completions_by_match_type);
|
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// Class representing an attempt to compute completions.
|
|
|
|
class completer_t {
|
2013-03-06 04:54:16 +00:00
|
|
|
const completion_request_flags_t flags;
|
2012-02-26 21:27:31 +00:00
|
|
|
const wcstring initial_cmd;
|
2012-02-25 02:43:10 +00:00
|
|
|
std::vector<completion_t> completions;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// Table of completions conditions that have already been tested and the corresponding test
|
|
|
|
/// results.
|
2017-08-19 23:27:24 +00:00
|
|
|
typedef std::unordered_map<wcstring, bool> condition_cache_t;
|
2012-02-26 21:27:31 +00:00
|
|
|
condition_cache_t condition_cache;
|
2013-03-22 00:44:51 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
enum complete_type_t { COMPLETE_DEFAULT, COMPLETE_AUTOSUGGEST };
|
2013-03-22 00:44:51 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
complete_type_t type() const {
|
2016-05-04 22:19:47 +00:00
|
|
|
return flags & COMPLETION_REQUEST_AUTOSUGGESTION ? COMPLETE_AUTOSUGGEST : COMPLETE_DEFAULT;
|
2013-03-06 04:54:16 +00:00
|
|
|
}
|
2013-03-22 00:44:51 +00:00
|
|
|
|
2016-10-21 04:14:40 +00:00
|
|
|
bool wants_descriptions() const {
|
|
|
|
return static_cast<bool>(flags & COMPLETION_REQUEST_DESCRIPTIONS);
|
|
|
|
}
|
2013-03-22 00:44:51 +00:00
|
|
|
|
2016-10-21 04:14:40 +00:00
|
|
|
bool fuzzy() const { return static_cast<bool>(flags & COMPLETION_REQUEST_FUZZY_MATCH); }
|
2013-06-02 08:14:26 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
fuzzy_match_type_t max_fuzzy_match_type() const {
|
|
|
|
// If we are doing fuzzy matching, request all types; if not request only prefix matching.
|
2016-05-04 22:19:47 +00:00
|
|
|
if (flags & COMPLETION_REQUEST_FUZZY_MATCH) return fuzzy_match_none;
|
|
|
|
return fuzzy_match_prefix_case_insensitive;
|
2013-05-25 22:41:18 +00:00
|
|
|
}
|
2013-03-06 04:54:16 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
public:
|
2017-08-18 23:52:39 +00:00
|
|
|
completer_t(const wcstring &c, completion_request_flags_t f) : flags(f), initial_cmd(c) {}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
bool empty() const { return completions.empty(); }
|
|
|
|
const std::vector<completion_t> &get_completions(void) { return completions; }
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
bool try_complete_variable(const wcstring &str);
|
|
|
|
bool try_complete_user(const wcstring &str);
|
2012-02-25 02:43:10 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
bool complete_param(const wcstring &cmd_orig, const wcstring &popt, const wcstring &str,
|
2012-11-19 00:30:30 +00:00
|
|
|
bool use_switches);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
void complete_param_expand(const wcstring &str, bool do_file,
|
|
|
|
bool handle_as_special_cd = false);
|
|
|
|
|
|
|
|
void complete_cmd(const wcstring &str, bool use_function, bool use_builtin, bool use_command,
|
2014-09-20 07:26:10 +00:00
|
|
|
bool use_implicit_cd);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
void complete_from_args(const wcstring &str, const wcstring &args, const wcstring &desc,
|
2012-11-19 00:30:30 +00:00
|
|
|
complete_flags_t flags);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
void complete_cmd_desc(const wcstring &str);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-08-01 23:32:52 +00:00
|
|
|
bool complete_variable(const wcstring &str, size_t start_offset);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
bool condition_test(const wcstring &condition);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
void complete_strings(const wcstring &wc_escaped, const wchar_t *desc,
|
|
|
|
wcstring (*desc_func)(const wcstring &),
|
|
|
|
std::vector<completion_t> &possible_comp, complete_flags_t flags);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
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.
|
2012-03-10 04:16:26 +00:00
|
|
|
expand_flags_t result = 0;
|
2016-05-03 23:23:30 +00:00
|
|
|
if (this->type() == COMPLETE_AUTOSUGGEST) result |= EXPAND_SKIP_CMDSUBST;
|
2013-06-02 08:14:26 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// Allow fuzzy matching.
|
|
|
|
if (this->fuzzy()) result |= EXPAND_FUZZY_MATCH;
|
2013-06-02 08:14:26 +00:00
|
|
|
|
2012-03-10 04:16:26 +00:00
|
|
|
return result;
|
|
|
|
}
|
2012-02-25 02:43:10 +00:00
|
|
|
};
|
|
|
|
|
2017-01-30 02:56:55 +00:00
|
|
|
// Callback when an autoloaded completion is removed.
|
|
|
|
static void autoloaded_completion_removed(const wcstring &cmd) {
|
2016-01-17 06:42:14 +00:00
|
|
|
complete_remove_all(cmd, false /* not a path */);
|
2012-01-26 02:40:08 +00:00
|
|
|
}
|
|
|
|
|
2017-01-30 02:56:55 +00:00
|
|
|
// Autoloader for completions
|
|
|
|
static autoload_t completion_autoloader(L"fish_complete_path", autoloaded_completion_removed);
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// Create a new completion entry.
|
|
|
|
void append_completion(std::vector<completion_t> *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.
|
2016-08-17 01:23:10 +00:00
|
|
|
assert(completions != NULL);
|
2014-01-14 22:28:06 +00:00
|
|
|
const wcstring empty;
|
2015-07-28 01:45:47 +00:00
|
|
|
completions->push_back(completion_t(empty, empty, match, resolve_auto_space(comp, flags)));
|
|
|
|
completion_t *last = &completions->back();
|
2014-01-07 22:57:58 +00:00
|
|
|
last->completion = comp;
|
|
|
|
last->description = desc;
|
2007-02-17 11:05:55 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// 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" );
|
2012-11-19 00:30:30 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
if (this->type() == COMPLETE_AUTOSUGGEST) {
|
|
|
|
// Autosuggestion can't support conditions.
|
2012-02-25 02:43:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-02-09 00:15:53 +00:00
|
|
|
bool test_res;
|
2012-02-26 21:27:31 +00:00
|
|
|
condition_cache_t::iterator cached_entry = condition_cache.find(condition);
|
2016-05-03 23:23:30 +00:00
|
|
|
if (cached_entry == condition_cache.end()) {
|
|
|
|
// Compute new value and reinsert it.
|
2013-01-31 23:57:08 +00:00
|
|
|
test_res = (0 == exec_subshell(condition, false /* don't apply exit status */));
|
2012-02-09 00:15:53 +00:00
|
|
|
condition_cache[condition] = test_res;
|
2016-05-03 23:23:30 +00:00
|
|
|
} else {
|
|
|
|
// Use the old value.
|
2012-02-09 00:15:53 +00:00
|
|
|
test_res = cached_entry->second;
|
|
|
|
}
|
|
|
|
return test_res;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// 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) {
|
2012-02-24 20:13:35 +00:00
|
|
|
ASSERT_IS_LOCKED(completion_lock);
|
2006-10-19 11:50:23 +00:00
|
|
|
|
2017-08-19 20:29:52 +00:00
|
|
|
auto ins = completion_set.emplace(completion_entry_t(cmd, cmd_is_path));
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// 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<completion_entry_t &>(*ins.first);
|
2007-01-28 13:40:59 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
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.
|
2016-01-16 23:46:43 +00:00
|
|
|
assert(option.empty() == (option_type == option_type_args_only));
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// Lock the lock that allows us to edit the completion entry list.
|
2016-07-21 05:30:58 +00:00
|
|
|
scoped_lock lock(completion_lock);
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-03-18 22:14:16 +00:00
|
|
|
completion_entry_t &c = complete_get_exact_entry(cmd, cmd_is_path);
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// Create our new option.
|
2012-11-19 00:30:30 +00:00
|
|
|
complete_entry_opt_t opt;
|
2016-01-16 23:46:43 +00:00
|
|
|
opt.option = option;
|
|
|
|
opt.type = option_type;
|
2012-11-19 00:30:30 +00:00
|
|
|
opt.result_mode = result_mode;
|
2012-02-09 00:15:53 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
if (comp) opt.comp = comp;
|
|
|
|
if (condition) opt.condition = condition;
|
|
|
|
if (desc) opt.desc = desc;
|
|
|
|
opt.flags = flags;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-03-18 22:14:16 +00:00
|
|
|
c.add_option(opt);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// 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) {
|
2012-02-24 20:13:35 +00:00
|
|
|
ASSERT_IS_LOCKED(completion_lock);
|
2016-01-17 06:42:14 +00:00
|
|
|
option_list_t::iterator iter = this->options.begin();
|
2016-05-03 23:23:30 +00:00
|
|
|
while (iter != this->options.end()) {
|
|
|
|
if (iter->option == option && iter->type == type) {
|
2016-01-17 06:42:14 +00:00
|
|
|
iter = this->options.erase(iter);
|
2016-05-03 23:23:30 +00:00
|
|
|
} else {
|
|
|
|
// Just go to the next one.
|
2016-01-17 06:42:14 +00:00
|
|
|
++iter;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2012-05-12 01:59:38 +00:00
|
|
|
return this->options.empty();
|
2006-12-13 23:58:38 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
void complete_remove(const wcstring &cmd, bool cmd_is_path, const wcstring &option,
|
|
|
|
complete_option_type_t type) {
|
2016-07-21 05:30:58 +00:00
|
|
|
scoped_lock lock(completion_lock);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-12-19 17:05:18 +00:00
|
|
|
completion_entry_t tmp_entry(cmd, cmd_is_path);
|
2016-03-18 22:14:16 +00:00
|
|
|
completion_entry_set_t::iterator iter = completion_set.find(tmp_entry);
|
2016-05-03 23:23:30 +00:00
|
|
|
if (iter != completion_set.end()) {
|
|
|
|
// const_cast: See SET_ELEMENTS_ARE_IMMUTABLE.
|
|
|
|
completion_entry_t &entry = const_cast<completion_entry_t &>(*iter);
|
2016-03-18 22:14:16 +00:00
|
|
|
|
|
|
|
bool delete_it = entry.remove_option(option, type);
|
2016-05-03 23:23:30 +00:00
|
|
|
if (delete_it) {
|
2012-04-10 03:17:06 +00:00
|
|
|
completion_set.erase(iter);
|
2012-02-08 22:47:50 +00:00
|
|
|
}
|
2012-04-10 03:17:06 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
void complete_remove_all(const wcstring &cmd, bool cmd_is_path) {
|
2016-07-21 05:30:58 +00:00
|
|
|
scoped_lock lock(completion_lock);
|
2016-05-03 23:23:30 +00:00
|
|
|
|
2016-12-19 17:05:18 +00:00
|
|
|
completion_entry_t tmp_entry(cmd, cmd_is_path);
|
2016-03-18 22:14:16 +00:00
|
|
|
completion_set.erase(tmp_entry);
|
2016-01-17 06:42:14 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// 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.
|
2012-02-08 06:44:10 +00:00
|
|
|
path = L"";
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// Make sure the path is not included in the command.
|
2012-02-08 06:44:10 +00:00
|
|
|
size_t last_slash = str.find_last_of(L'/');
|
2016-05-03 23:23:30 +00:00
|
|
|
if (last_slash != wcstring::npos) {
|
2012-02-08 06:44:10 +00:00
|
|
|
cmd = str.substr(last_slash + 1);
|
2016-05-03 23:23:30 +00:00
|
|
|
} else {
|
2012-02-08 06:44:10 +00:00
|
|
|
cmd = str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 01:40:01 +00:00
|
|
|
/// 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.
|
2016-05-03 23:23:30 +00:00
|
|
|
///
|
2016-08-17 01:40:01 +00:00
|
|
|
/// 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.
|
2016-05-03 23:23:30 +00:00
|
|
|
///
|
2016-08-17 01:40:01 +00:00
|
|
|
/// @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
|
|
|
|
/// @param flags
|
|
|
|
/// The flags
|
2016-05-03 23:23:30 +00:00
|
|
|
void completer_t::complete_strings(const wcstring &wc_escaped, const wchar_t *desc,
|
|
|
|
wcstring (*desc_func)(const wcstring &),
|
2012-11-19 00:30:30 +00:00
|
|
|
std::vector<completion_t> &possible_comp,
|
2016-05-03 23:23:30 +00:00
|
|
|
complete_flags_t flags) {
|
2012-01-30 10:23:58 +00:00
|
|
|
wcstring tmp = wc_escaped;
|
2016-05-03 23:23:30 +00:00
|
|
|
if (!expand_one(tmp, EXPAND_SKIP_CMDSUBST | EXPAND_SKIP_WILDCARDS | this->expand_flags(), NULL))
|
2012-01-30 10:23:58 +00:00
|
|
|
return;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2015-08-19 18:35:24 +00:00
|
|
|
const wcstring wc = parse_util_unescape_wildcards(tmp);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
for (size_t i = 0; i < possible_comp.size(); i++) {
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring temp = possible_comp.at(i).completion;
|
2016-05-03 23:23:30 +00:00
|
|
|
const wchar_t *next_str = temp.empty() ? NULL : temp.c_str();
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
if (next_str) {
|
|
|
|
wildcard_complete(next_str, wc.c_str(), desc, desc_func, &this->completions,
|
|
|
|
this->expand_flags(), flags);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2007-02-17 11:05:55 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// 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) {
|
2012-02-24 20:13:35 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
const wchar_t *cmd_start;
|
|
|
|
int skip;
|
2016-05-03 23:23:30 +00:00
|
|
|
const wchar_t *const cmd = str.c_str();
|
|
|
|
cmd_start = wcsrchr(cmd, L'/');
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
if (cmd_start)
|
|
|
|
cmd_start++;
|
|
|
|
else
|
|
|
|
cmd_start = cmd;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// 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;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
if (wildcard_has(cmd_start, 0)) {
|
2012-11-19 00:30:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
skip = 1;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
for (size_t i = 0; i < this->completions.size(); i++) {
|
2012-11-19 00:30:30 +00:00
|
|
|
const completion_t &c = this->completions.at(i);
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
if (c.completion.empty() || (c.completion[c.completion.size() - 1] != L'/')) {
|
2012-11-19 00:30:30 +00:00
|
|
|
skip = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
if (skip) {
|
2012-11-19 00:30:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-02-02 20:04:04 +00:00
|
|
|
wcstring lookup_cmd(L"__fish_describe_command ");
|
|
|
|
lookup_cmd.append(escape_string(cmd_start, 1));
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2017-08-19 23:27:24 +00:00
|
|
|
std::unordered_map<wcstring, wcstring> lookup;
|
2006-02-08 15:29:09 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// 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.
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring_list_t list;
|
2016-05-03 23:23:30 +00:00
|
|
|
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++) {
|
2012-02-02 20:04:04 +00:00
|
|
|
const wcstring &elstr = list.at(i);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-02-02 20:04:04 +00:00
|
|
|
const wcstring fullkey(elstr, wcslen(cmd_start));
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-02-02 20:04:04 +00:00
|
|
|
size_t tab_idx = fullkey.find(L'\t');
|
2016-05-03 23:23:30 +00:00
|
|
|
if (tab_idx == wcstring::npos) continue;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-02-02 20:04:04 +00:00
|
|
|
const wcstring key(fullkey, 0, tab_idx);
|
|
|
|
wcstring val(fullkey, tab_idx + 1);
|
2007-01-07 14:24:30 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// 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]);
|
2012-02-02 20:04:04 +00:00
|
|
|
lookup[key] = val;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2006-01-08 02:56:56 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// 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++) {
|
2012-02-26 21:27:31 +00:00
|
|
|
completion_t &completion = this->completions.at(i);
|
2012-02-02 20:04:04 +00:00
|
|
|
const wcstring &el = completion.completion;
|
2016-05-03 23:23:30 +00:00
|
|
|
if (el.empty()) continue;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2017-08-19 16:55:06 +00:00
|
|
|
auto new_desc_iter = lookup.find(el);
|
2016-05-03 23:23:30 +00:00
|
|
|
if (new_desc_iter != lookup.end()) completion.description = new_desc_iter->second;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// Returns a description for the specified function, or an empty string if none.
|
|
|
|
static wcstring complete_function_desc(const wcstring &fn) {
|
2012-05-18 02:37:46 +00:00
|
|
|
wcstring result;
|
2012-05-18 02:46:08 +00:00
|
|
|
bool has_description = function_get_desc(fn, &result);
|
2016-05-03 23:23:30 +00:00
|
|
|
if (!has_description) {
|
2012-05-18 02:37:46 +00:00
|
|
|
function_get_definition(fn, &result);
|
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
return result;
|
2006-01-23 23:32:13 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// 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.
|
|
|
|
///
|
2016-06-06 01:46:04 +00:00
|
|
|
/// \param str_cmd the command string to find completions for
|
2016-05-03 23:23:30 +00:00
|
|
|
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;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
std::vector<completion_t> possible_comp;
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
if (use_command) {
|
2016-10-22 18:21:13 +00:00
|
|
|
expand_error_t result = expand_string(str_cmd, &this->completions,
|
|
|
|
EXPAND_SPECIAL_FOR_COMMAND | EXPAND_FOR_COMPLETIONS |
|
|
|
|
EXECUTABLES_ONLY | this->expand_flags(),
|
|
|
|
NULL);
|
|
|
|
if (result != EXPAND_ERROR && this->wants_descriptions()) {
|
|
|
|
this->complete_cmd_desc(str_cmd);
|
2016-12-04 04:12:53 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2016-10-22 18:21:13 +00:00
|
|
|
|
2016-11-02 03:42:02 +00:00
|
|
|
if (use_implicit_cd) {
|
|
|
|
// We don't really care if this succeeds or fails. If it succeeds this->completions will be
|
|
|
|
// updated with choices for the user.
|
2017-01-27 01:28:46 +00:00
|
|
|
expand_error_t ignore =
|
|
|
|
expand_string(str_cmd, &this->completions,
|
2017-01-27 04:00:43 +00:00
|
|
|
EXPAND_FOR_COMPLETIONS | DIRECTORIES_ONLY | this->expand_flags(), NULL);
|
2017-02-14 02:48:59 +00:00
|
|
|
UNUSED(ignore);
|
2014-09-20 07:26:10 +00:00
|
|
|
}
|
2016-10-22 18:21:13 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
if (str_cmd.find(L'/') == wcstring::npos && str_cmd.at(0) != L'~') {
|
|
|
|
if (use_function) {
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring_list_t names = function_get_names(str_cmd.at(0) == L'_');
|
2016-05-03 23:23:30 +00:00
|
|
|
for (size_t i = 0; i < names.size(); i++) {
|
2015-07-28 01:45:47 +00:00
|
|
|
append_completion(&possible_comp, names.at(i));
|
2012-01-14 07:44:18 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
this->complete_strings(str_cmd, 0, &complete_function_desc, possible_comp, 0);
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
possible_comp.clear();
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
if (use_builtin) {
|
2015-07-28 01:45:47 +00:00
|
|
|
builtin_get_names(&possible_comp);
|
2012-11-19 00:30:30 +00:00
|
|
|
this->complete_strings(str_cmd, 0, &builtin_get_desc, possible_comp, 0);
|
|
|
|
}
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 01:23:10 +00:00
|
|
|
/// 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
|
2016-05-03 23:23:30 +00:00
|
|
|
/// up with a case where this matters.
|
|
|
|
///
|
2016-08-17 01:23:10 +00:00
|
|
|
/// @param str
|
|
|
|
/// The string to complete.
|
|
|
|
/// @param args
|
|
|
|
/// The list of option arguments to be evaluated.
|
|
|
|
/// @param desc
|
|
|
|
/// Description of the completion
|
|
|
|
/// @param flags
|
|
|
|
/// The list into which the results will be inserted
|
|
|
|
///
|
2016-05-03 23:23:30 +00:00
|
|
|
void completer_t::complete_from_args(const wcstring &str, const wcstring &args,
|
|
|
|
const wcstring &desc, complete_flags_t flags) {
|
2013-03-06 04:54:16 +00:00
|
|
|
bool is_autosuggest = (this->type() == COMPLETE_AUTOSUGGEST);
|
2012-05-05 21:33:24 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// If type is COMPLETE_AUTOSUGGEST, it means we're on a background thread, so don't call
|
|
|
|
// proc_push_interactive.
|
|
|
|
if (!is_autosuggest) {
|
2012-02-26 02:54:49 +00:00
|
|
|
proc_push_interactive(0);
|
2016-02-28 02:25:58 +00:00
|
|
|
}
|
2012-02-26 02:54:49 +00:00
|
|
|
|
2016-02-28 02:37:59 +00:00
|
|
|
expand_flags_t eflags = 0;
|
2016-05-03 23:23:30 +00:00
|
|
|
if (is_autosuggest) {
|
2016-02-28 02:37:59 +00:00
|
|
|
eflags |= EXPAND_NO_DESCRIPTIONS | EXPAND_SKIP_CMDSUBST;
|
|
|
|
}
|
2016-05-03 23:23:30 +00:00
|
|
|
|
2015-07-28 01:45:47 +00:00
|
|
|
std::vector<completion_t> possible_comp;
|
2016-02-28 02:40:54 +00:00
|
|
|
parser_t::expand_argument_list(args, eflags, &possible_comp);
|
2012-01-29 08:41:39 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
if (!is_autosuggest) {
|
2012-02-26 02:54:49 +00:00
|
|
|
proc_pop_interactive();
|
2016-02-28 02:25:58 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2013-12-20 11:28:20 +00:00
|
|
|
this->complete_strings(escape_string(str, ESCAPE_ALL), desc.c_str(), 0, possible_comp, flags);
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
static size_t leading_dash_count(const wchar_t *str) {
|
2016-01-16 23:46:43 +00:00
|
|
|
size_t cursor = 0;
|
2016-05-03 23:23:30 +00:00
|
|
|
while (str[cursor] == L'-') {
|
2016-01-16 23:46:43 +00:00
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
return cursor;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// Match a parameter.
|
|
|
|
static bool param_match(const complete_entry_opt_t *e, const wchar_t *optstr) {
|
2016-01-16 23:46:43 +00:00
|
|
|
bool result = false;
|
2016-05-03 23:23:30 +00:00
|
|
|
if (e->type != option_type_args_only) {
|
2016-01-16 23:46:43 +00:00
|
|
|
size_t dashes = leading_dash_count(optstr);
|
|
|
|
result = (dashes == e->expected_dash_count() && e->option == &optstr[dashes]);
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2016-01-16 23:46:43 +00:00
|
|
|
return result;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// 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()) {
|
2016-01-16 23:46:43 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// Verify leading dashes.
|
2016-01-16 23:46:43 +00:00
|
|
|
size_t cursor = leading_dash_count(optstr);
|
2016-05-03 23:23:30 +00:00
|
|
|
if (cursor != e->expected_dash_count()) {
|
2016-01-16 23:46:43 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2016-05-03 23:23:30 +00:00
|
|
|
|
|
|
|
// Verify options match.
|
|
|
|
if (!string_prefixes_string(e->option, &optstr[cursor])) {
|
2016-01-16 23:46:43 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
cursor += e->option.length();
|
2016-05-03 23:23:30 +00:00
|
|
|
|
|
|
|
// 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'=') {
|
2016-01-16 23:46:43 +00:00
|
|
|
return NULL;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-01-16 23:46:43 +00:00
|
|
|
cursor += 1;
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2016-01-16 23:46:43 +00:00
|
|
|
return &optstr[cursor];
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// 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()) {
|
2016-01-16 23:46:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const wchar_t nextopt = entry->option.at(0);
|
2016-05-03 23:23:30 +00:00
|
|
|
|
|
|
|
// Empty strings are always 'OK'.
|
|
|
|
if (arg.empty()) {
|
2016-01-16 01:14:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-05-03 23:23:30 +00:00
|
|
|
|
|
|
|
// The argument must start with exactly one dash.
|
|
|
|
if (leading_dash_count(arg.c_str()) != 1) {
|
2016-01-16 01:14:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-03 23:23:30 +00:00
|
|
|
|
|
|
|
// Short option must not be already present.
|
|
|
|
if (arg.find(nextopt) != wcstring::npos) {
|
2016-01-16 01:14:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-03 23:23:30 +00:00
|
|
|
|
|
|
|
// 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.
|
2016-01-16 01:14:44 +00:00
|
|
|
bool result = true;
|
2016-05-03 23:23:30 +00:00
|
|
|
for (size_t i = 1; i < arg.size(); i++) {
|
2016-01-16 01:14:44 +00:00
|
|
|
wchar_t arg_char = arg.at(i);
|
|
|
|
const complete_entry_opt_t *match = NULL;
|
2016-05-03 23:23:30 +00:00
|
|
|
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) {
|
2016-01-16 01:14:44 +00:00
|
|
|
match = &*iter;
|
|
|
|
break;
|
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2016-05-03 23:23:30 +00:00
|
|
|
if (match == NULL || (match->result_mode & NO_COMMON)) {
|
2016-01-16 01:14:44 +00:00
|
|
|
result = false;
|
|
|
|
break;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-16 01:14:44 +00:00
|
|
|
return result;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 22:30:49 +00:00
|
|
|
/// Load command-specific completions for the specified command.
|
2016-05-03 23:23:30 +00:00
|
|
|
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.
|
2015-10-08 01:59:41 +00:00
|
|
|
function_load(name);
|
2012-11-19 00:30:30 +00:00
|
|
|
completion_autoloader.load(name, reload);
|
2006-02-08 09:20:05 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// 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 <tab> -> ("echo", "world", "")
|
|
|
|
/// echo hello world<tab> -> ("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;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-02-08 22:47:50 +00:00
|
|
|
wcstring cmd, path;
|
|
|
|
parse_cmd_string(cmd_orig, path, cmd);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
if (this->type() == COMPLETE_DEFAULT) {
|
2013-11-30 07:44:26 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2012-11-19 00:30:30 +00:00
|
|
|
complete_load(cmd, true);
|
2016-10-22 18:21:13 +00:00
|
|
|
} else if (this->type() == COMPLETE_AUTOSUGGEST &&
|
|
|
|
!completion_autoloader.has_tried_loading(cmd)) {
|
2017-01-23 18:43:34 +00:00
|
|
|
// Load this command (on the main thread)
|
2017-01-27 04:00:43 +00:00
|
|
|
iothread_perform_on_main([&]() { complete_load(cmd, false); });
|
2012-02-27 04:11:34 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// Make a list of lists of all options that we care about.
|
2016-01-16 01:14:44 +00:00
|
|
|
std::vector<option_list_t> all_options;
|
2012-05-12 01:59:38 +00:00
|
|
|
{
|
|
|
|
scoped_lock lock(completion_lock);
|
2016-05-03 23:23:30 +00:00
|
|
|
for (completion_entry_set_t::const_iterator iter = completion_set.begin();
|
|
|
|
iter != completion_set.end(); ++iter) {
|
2016-03-18 22:14:16 +00:00
|
|
|
const completion_entry_t &i = *iter;
|
|
|
|
const wcstring &match = i.cmd_is_path ? path : cmd;
|
2016-05-03 23:23:30 +00:00
|
|
|
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
|
2012-05-12 01:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// 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<option_list_t>::const_iterator iter = all_options.begin();
|
|
|
|
iter != all_options.end(); ++iter) {
|
2016-01-16 01:14:44 +00:00
|
|
|
const option_list_t &options = *iter;
|
2016-05-03 23:23:30 +00:00
|
|
|
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) {
|
2012-11-19 00:30:30 +00:00
|
|
|
const complete_entry_opt_t *o = &*oiter;
|
2016-01-16 23:46:43 +00:00
|
|
|
const wchar_t *arg = param_match2(o, str);
|
2016-05-03 23:23:30 +00:00
|
|
|
if (arg != NULL && this->condition_test(o->condition)) {
|
2012-05-12 01:59:38 +00:00
|
|
|
if (o->result_mode & NO_COMMON) use_common = false;
|
|
|
|
if (o->result_mode & NO_FILES) use_files = false;
|
2012-11-19 00:30:30 +00:00
|
|
|
complete_from_args(arg, o->comp, o->localized_desc(), o->flags);
|
|
|
|
}
|
|
|
|
}
|
2016-05-03 23:23:30 +00:00
|
|
|
} else if (popt[0] == L'-') {
|
|
|
|
// Set to true if we found a matching old-style switch.
|
2016-12-09 20:14:14 +00:00
|
|
|
// Here we are testing the previous argument,
|
|
|
|
// to see how we should complete the current argument
|
2016-01-16 23:46:43 +00:00
|
|
|
bool old_style_match = false;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// 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) {
|
2012-02-09 00:15:53 +00:00
|
|
|
const complete_entry_opt_t *o = &*oiter;
|
2016-10-22 18:21:13 +00:00
|
|
|
if (o->type == option_type_single_long && 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;
|
|
|
|
complete_from_args(str, o->comp, o->localized_desc(), o->flags);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// 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) {
|
2012-02-09 00:15:53 +00:00
|
|
|
const complete_entry_opt_t *o = &*oiter;
|
2016-05-03 23:23:30 +00:00
|
|
|
// Gnu-style options with _optional_ arguments must be specified as a single
|
|
|
|
// token, so that it can be differed from a regular argument.
|
2016-12-09 20:14:14 +00:00
|
|
|
// Here we are testing the previous argument for a GNU-style match,
|
|
|
|
// to see how we should complete the current argument
|
2016-01-16 23:46:43 +00:00
|
|
|
if (o->type == option_type_double_long && !(o->result_mode & NO_COMMON))
|
2012-11-19 00:30:30 +00:00
|
|
|
continue;
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
if (param_match(o, popt) && this->condition_test(o->condition)) {
|
2012-05-12 01:59:38 +00:00
|
|
|
if (o->result_mode & NO_COMMON) use_common = false;
|
|
|
|
if (o->result_mode & NO_FILES) use_files = false;
|
2014-01-12 21:33:35 +00:00
|
|
|
complete_from_args(str, o->comp, o->localized_desc(), o->flags);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2016-10-30 23:16:15 +00:00
|
|
|
if (!use_common) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-12-09 20:14:14 +00:00
|
|
|
// Now we try to complete an option itself
|
2016-10-30 23:16:15 +00:00
|
|
|
for (option_list_t::const_iterator oiter = options.begin(); oiter != options.end();
|
2016-12-04 04:12:53 +00:00
|
|
|
++oiter) {
|
2016-10-30 23:16:15 +00:00
|
|
|
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);
|
|
|
|
complete_from_args(str, o->comp, o->localized_desc(), o->flags);
|
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-10-30 23:16:15 +00:00
|
|
|
if (wcslen(str) == 0 || !use_switches) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-10-30 23:16:15 +00:00
|
|
|
// 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);
|
|
|
|
}
|
2006-01-30 19:53:10 +00:00
|
|
|
|
2016-10-30 23:16:15 +00:00
|
|
|
// Check if the long style option matches.
|
|
|
|
if (o->type != option_type_single_long && o->type != option_type_double_long) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int match = 0, match_no_case = 0;
|
2007-02-28 21:43:27 +00:00
|
|
|
|
2016-10-30 23:16:15 +00:00
|
|
|
wcstring whole_opt(o->expected_dash_count(), L'-');
|
|
|
|
whole_opt.append(o->option);
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2016-10-30 23:16:15 +00:00
|
|
|
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) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
offset = wcslen(str);
|
|
|
|
} else {
|
|
|
|
flags = COMPLETE_REPLACES_TOKEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
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), flags);
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2016-10-30 23:16:15 +00:00
|
|
|
|
|
|
|
append_completion(&this->completions, whole_opt.c_str() + offset, C_(o->desc), flags);
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
return use_files;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// 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) {
|
2015-08-03 23:36:10 +00:00
|
|
|
expand_flags_t flags = EXPAND_SKIP_CMDSUBST | EXPAND_FOR_COMPLETIONS | this->expand_flags();
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
if (!do_file) flags |= EXPAND_SKIP_WILDCARDS;
|
|
|
|
|
|
|
|
if (handle_as_special_cd && do_file) {
|
2018-01-08 21:36:20 +00:00
|
|
|
if (this->type() == COMPLETE_AUTOSUGGEST) {
|
|
|
|
flags |= EXPAND_SPECIAL_FOR_CD_AUTOSUGGEST;
|
|
|
|
}
|
2016-04-07 22:24:52 +00:00
|
|
|
flags |= DIRECTORIES_ONLY | EXPAND_SPECIAL_FOR_CD | EXPAND_NO_DESCRIPTIONS;
|
2016-02-06 22:39:47 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// Squelch file descriptions per issue #254.
|
|
|
|
if (this->type() == COMPLETE_AUTOSUGGEST || do_file) flags |= EXPAND_NO_DESCRIPTIONS;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// 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.
|
2015-07-27 20:43:20 +00:00
|
|
|
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);
|
2016-05-03 23:23:30 +00:00
|
|
|
|
|
|
|
if (complete_from_separator) {
|
2015-07-27 20:43:20 +00:00
|
|
|
const wcstring sep_string = wcstring(str, sep_index + 1);
|
|
|
|
std::vector<completion_t> local_completions;
|
2016-05-03 23:23:30 +00:00
|
|
|
if (expand_string(sep_string, &local_completions, flags, NULL) == EXPAND_ERROR) {
|
2015-07-27 20:43:20 +00:00
|
|
|
debug(3, L"Error while expanding string '%ls'", sep_string.c_str());
|
|
|
|
}
|
2016-05-03 23:23:30 +00:00
|
|
|
|
|
|
|
// Any COMPLETE_REPLACES_TOKEN will also stomp the separator. We need to "repair" them by
|
|
|
|
// inserting our separator and prefix.
|
2015-07-27 20:43:20 +00:00
|
|
|
const wcstring prefix_with_sep = wcstring(str, 0, sep_index + 1);
|
2016-05-03 23:23:30 +00:00
|
|
|
for (size_t i = 0; i < local_completions.size(); i++) {
|
2015-08-03 00:58:37 +00:00
|
|
|
local_completions.at(i).prepend_token_prefix(prefix_with_sep);
|
2015-07-27 20:43:20 +00:00
|
|
|
}
|
2016-05-03 23:23:30 +00:00
|
|
|
this->completions.insert(this->completions.end(), local_completions.begin(),
|
2015-07-27 20:43:20 +00:00
|
|
|
local_completions.end());
|
|
|
|
}
|
2016-05-03 23:23:30 +00:00
|
|
|
|
|
|
|
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) {
|
2015-07-27 20:43:20 +00:00
|
|
|
debug(3, L"Error while expanding string '%ls'", str.c_str());
|
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-05-05 21:21:21 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// 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();
|
2012-11-19 00:30:30 +00:00
|
|
|
const wchar_t *var = &whole_var[start_offset];
|
|
|
|
size_t varlen = wcslen(var);
|
2013-05-25 22:41:18 +00:00
|
|
|
bool res = false;
|
2013-06-02 08:14:26 +00:00
|
|
|
|
2013-03-06 04:54:16 +00:00
|
|
|
const wcstring_list_t names = complete_get_variable_names();
|
2016-05-03 23:23:30 +00:00
|
|
|
for (size_t i = 0; i < names.size(); i++) {
|
|
|
|
const wcstring &env_name = names.at(i);
|
2013-06-02 08:14:26 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
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
|
2013-05-25 22:41:18 +00:00
|
|
|
}
|
2013-06-02 08:14:26 +00:00
|
|
|
|
2013-05-25 22:41:18 +00:00
|
|
|
wcstring comp;
|
|
|
|
int flags = 0;
|
2013-06-02 08:14:26 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
if (!match_type_requires_full_replacement(match.type)) {
|
|
|
|
// Take only the suffix.
|
2013-05-25 22:41:18 +00:00
|
|
|
comp.append(env_name.c_str() + varlen);
|
2016-05-03 23:23:30 +00:00
|
|
|
} else {
|
2013-05-25 22:41:18 +00:00
|
|
|
comp.append(whole_var, start_offset);
|
|
|
|
comp.append(env_name);
|
|
|
|
flags = COMPLETE_REPLACES_TOKEN | COMPLETE_DONT_ESCAPE;
|
|
|
|
}
|
2013-06-02 08:14:26 +00:00
|
|
|
|
2013-05-25 22:41:18 +00:00
|
|
|
wcstring desc;
|
2016-05-03 23:23:30 +00:00
|
|
|
if (this->wants_descriptions()) {
|
|
|
|
// Can't use this->vars here, it could be any variable.
|
2017-08-28 07:25:41 +00:00
|
|
|
auto var = env_get(env_name);
|
|
|
|
if (!var) continue;
|
2013-06-02 08:14:26 +00:00
|
|
|
|
2017-08-28 07:25:41 +00:00
|
|
|
wcstring value = expand_escape_variable(*var);
|
2017-08-06 01:22:49 +00:00
|
|
|
if (this->type() != COMPLETE_AUTOSUGGEST) {
|
2013-05-25 22:41:18 +00:00
|
|
|
desc = format_string(COMPLETE_VAR_DESC_VAL, value.c_str());
|
2017-08-06 01:22:49 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2013-06-02 08:14:26 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
append_completion(&this->completions, comp, desc, flags, match);
|
2013-06-02 08:14:26 +00:00
|
|
|
|
2013-05-25 22:41:18 +00:00
|
|
|
res = true;
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
return res;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
bool completer_t::try_complete_variable(const wcstring &str) {
|
|
|
|
enum { e_unquoted, e_single_quoted, e_double_quoted } mode = e_unquoted;
|
2014-02-09 23:27:04 +00:00
|
|
|
const size_t len = str.size();
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// Get the position of the dollar heading a (possibly empty) run of valid variable characters.
|
|
|
|
// npos means none.
|
2016-02-28 00:28:47 +00:00
|
|
|
size_t variable_start = wcstring::npos;
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
for (size_t in_pos = 0; in_pos < len; in_pos++) {
|
2014-02-09 23:27:04 +00:00
|
|
|
wchar_t c = str.at(in_pos);
|
2017-04-20 06:43:02 +00:00
|
|
|
if (!valid_var_name_char(c)) {
|
2016-05-03 23:23:30 +00:00
|
|
|
// This character cannot be in a variable, reset the dollar.
|
2014-02-09 23:27:04 +00:00
|
|
|
variable_start = -1;
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
switch (c) {
|
2016-11-03 01:29:14 +00:00
|
|
|
case L'\\': {
|
2014-02-09 23:27:04 +00:00
|
|
|
in_pos++;
|
|
|
|
break;
|
2016-11-03 01:29:14 +00:00
|
|
|
}
|
|
|
|
case L'$': {
|
2016-05-03 23:23:30 +00:00
|
|
|
if (mode == e_unquoted || mode == e_double_quoted) {
|
2014-02-09 23:27:04 +00:00
|
|
|
variable_start = in_pos;
|
|
|
|
}
|
|
|
|
break;
|
2016-11-03 01:29:14 +00:00
|
|
|
}
|
|
|
|
case L'\'': {
|
2016-05-03 23:23:30 +00:00
|
|
|
if (mode == e_single_quoted) {
|
2014-02-09 23:27:04 +00:00
|
|
|
mode = e_unquoted;
|
2016-05-03 23:23:30 +00:00
|
|
|
} else if (mode == e_unquoted) {
|
2014-02-09 23:27:04 +00:00
|
|
|
mode = e_single_quoted;
|
|
|
|
}
|
|
|
|
break;
|
2016-11-03 01:29:14 +00:00
|
|
|
}
|
|
|
|
case L'"': {
|
2016-05-03 23:23:30 +00:00
|
|
|
if (mode == e_double_quoted) {
|
2014-02-09 23:27:04 +00:00
|
|
|
mode = e_unquoted;
|
2016-05-03 23:23:30 +00:00
|
|
|
} else if (mode == e_unquoted) {
|
2014-02-09 23:27:04 +00:00
|
|
|
mode = e_double_quoted;
|
|
|
|
}
|
|
|
|
break;
|
2016-11-03 01:29:14 +00:00
|
|
|
}
|
|
|
|
default: {
|
|
|
|
break; // all other chars ignored here
|
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// 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);
|
2016-02-28 00:28:47 +00:00
|
|
|
bool text_is_empty = (variable_start == len);
|
2014-02-09 23:27:04 +00:00
|
|
|
bool result = false;
|
2016-05-03 23:23:30 +00:00
|
|
|
if (variable_start != wcstring::npos && (allow_empty || !text_is_empty)) {
|
2014-02-09 23:27:04 +00:00
|
|
|
result = this->complete_variable(str, variable_start + 1);
|
|
|
|
}
|
|
|
|
return result;
|
2012-02-25 02:43:10 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// Try to complete the specified string as a username. This is used by ~USER type expansion.
|
|
|
|
///
|
2017-05-11 05:07:01 +00:00
|
|
|
/// \return false if unable to complete, true otherwise
|
2016-05-03 23:23:30 +00:00
|
|
|
bool completer_t::try_complete_user(const wcstring &str) {
|
2016-10-16 00:20:53 +00:00
|
|
|
#ifndef HAVE_GETPWENT
|
|
|
|
// The getpwent() function does not exist on Android. A Linux user on Android isn't
|
|
|
|
// really a user - each installed app gets an UID assigned. Listing all UID:s is not
|
|
|
|
// possible without root access, and doing a ~USER type expansion does not make sense
|
|
|
|
// since every app is sandboxed and can't access eachother.
|
|
|
|
return false;
|
|
|
|
#else
|
2012-02-26 21:27:31 +00:00
|
|
|
const wchar_t *cmd = str.c_str();
|
2016-05-03 23:23:30 +00:00
|
|
|
const wchar_t *first_char = cmd;
|
2012-11-19 00:30:30 +00:00
|
|
|
|
2017-05-11 05:07:01 +00:00
|
|
|
if (*first_char != L'~' || wcschr(first_char, L'/')) return false;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-10-30 23:16:15 +00:00
|
|
|
const wchar_t *user_name = first_char + 1;
|
|
|
|
const wchar_t *name_end = wcschr(user_name, L'~');
|
2017-05-11 05:07:01 +00:00
|
|
|
if (name_end) return false;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-10-30 23:16:15 +00:00
|
|
|
double start_time = timef();
|
|
|
|
bool result = false;
|
|
|
|
size_t name_len = wcslen(user_name);
|
|
|
|
|
2017-05-11 05:07:01 +00:00
|
|
|
// We don't bother with the thread-safe `getpwent_r()` variant because it isn't needed. This is
|
|
|
|
// only run in a completion context and thus will only be called from a single thread and there
|
|
|
|
// is no place else in fish where we call `getpwent()`.
|
|
|
|
struct passwd *pw;
|
2016-10-30 23:16:15 +00:00
|
|
|
setpwent();
|
2017-05-11 05:07:01 +00:00
|
|
|
// cppcheck-suppress getpwentCalled
|
|
|
|
while ((pw = getpwent()) != NULL) {
|
2016-10-30 23:16:15 +00:00
|
|
|
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) {
|
|
|
|
wcstring desc = format_string(COMPLETE_USER_DESC, pw_name);
|
|
|
|
append_completion(&this->completions, &pw_name[name_len], desc, COMPLETE_NO_SPACE);
|
|
|
|
result = true;
|
|
|
|
} 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);
|
2016-12-04 04:12:53 +00:00
|
|
|
append_completion(&this->completions, name, desc,
|
|
|
|
COMPLETE_REPLACES_TOKEN | COMPLETE_DONT_ESCAPE | COMPLETE_NO_SPACE);
|
2016-10-30 23:16:15 +00:00
|
|
|
result = true;
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2017-05-11 05:07:01 +00:00
|
|
|
|
|
|
|
// If we've spent too much time (more than 200 ms) doing this give up.
|
|
|
|
if (timef() - start_time > 0.2) break;
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2017-05-11 05:07:01 +00:00
|
|
|
|
2016-10-30 23:16:15 +00:00
|
|
|
endpwent();
|
|
|
|
return result;
|
2016-10-16 00:20:53 +00:00
|
|
|
#endif
|
2009-02-02 22:46:45 +00:00
|
|
|
}
|
2007-02-09 09:33:50 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
void complete(const wcstring &cmd_with_subcmds, std::vector<completion_t> *out_comps,
|
2017-08-18 23:52:39 +00:00
|
|
|
completion_request_flags_t flags) {
|
2016-05-03 23:23:30 +00:00
|
|
|
// Determine the innermost subcommand.
|
2013-10-13 01:17:03 +00:00
|
|
|
const wchar_t *cmdsubst_begin, *cmdsubst_end;
|
2016-05-03 23:23:30 +00:00
|
|
|
parse_util_cmdsubst_extent(cmd_with_subcmds.c_str(), cmd_with_subcmds.size(), &cmdsubst_begin,
|
|
|
|
&cmdsubst_end);
|
2013-10-13 01:17:03 +00:00
|
|
|
assert(cmdsubst_begin != NULL && cmdsubst_end != NULL && cmdsubst_end >= cmdsubst_begin);
|
|
|
|
const wcstring cmd = wcstring(cmdsubst_begin, cmdsubst_end - cmdsubst_begin);
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// Make our completer.
|
2017-08-18 23:52:39 +00:00
|
|
|
completer_t completer(cmd, flags);
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2012-11-18 10:23:22 +00:00
|
|
|
wcstring current_command;
|
2013-10-13 01:17:03 +00:00
|
|
|
const size_t pos = cmd.size();
|
2016-05-03 23:23:30 +00:00
|
|
|
bool done = false;
|
2013-10-13 01:17:03 +00:00
|
|
|
bool use_command = 1;
|
|
|
|
bool use_function = 1;
|
|
|
|
bool use_builtin = 1;
|
2014-09-20 07:26:10 +00:00
|
|
|
bool use_implicit_cd = 1;
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// debug( 1, L"Complete '%ls'", cmd.c_str() );
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2012-02-25 02:43:10 +00:00
|
|
|
const wchar_t *cmd_cstr = cmd.c_str();
|
2013-10-13 01:17:03 +00:00
|
|
|
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);
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// If we are completing a variable name or a tilde expansion user name, we do that and return.
|
|
|
|
// No need for any other completions.
|
2013-10-13 01:17:03 +00:00
|
|
|
const wcstring current_token = tok_begin;
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// 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);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
if (!done) {
|
2013-10-13 01:17:03 +00:00
|
|
|
parse_node_tree_t tree;
|
2018-01-12 19:15:35 +00:00
|
|
|
parse_tree_from_string(cmd, parse_flag_continue_after_error |
|
|
|
|
parse_flag_accept_incomplete_tokens |
|
|
|
|
parse_flag_include_comments,
|
2016-05-03 23:23:30 +00:00
|
|
|
&tree, NULL);
|
|
|
|
|
2016-07-22 02:36:32 +00:00
|
|
|
// Find the plain statement to operate on. The cursor may be past it (#1261), so backtrack
|
|
|
|
// until we know we're no longer in a space. But the space may actually be part of the
|
|
|
|
// argument (#2477).
|
2016-07-11 21:03:19 +00:00
|
|
|
size_t position_in_statement = pos;
|
|
|
|
while (position_in_statement > 0 && cmd.at(position_in_statement - 1) == L' ') {
|
|
|
|
position_in_statement--;
|
2014-02-22 03:55:55 +00:00
|
|
|
}
|
2018-01-12 01:19:48 +00:00
|
|
|
auto plain_statement =
|
|
|
|
tnode_t<grammar::plain_statement>::find_node_matching_source_location(
|
|
|
|
&tree, position_in_statement, nullptr);
|
2018-01-11 17:41:49 +00:00
|
|
|
if (!plain_statement) {
|
2016-05-03 23:23:30 +00:00
|
|
|
// 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).
|
2014-08-24 21:27:58 +00:00
|
|
|
bool do_file = true;
|
2016-05-03 23:23:30 +00:00
|
|
|
if (flags & COMPLETION_REQUEST_AUTOSUGGESTION) {
|
2016-07-11 21:03:19 +00:00
|
|
|
if (position_in_statement < pos) {
|
2014-08-24 21:27:58 +00:00
|
|
|
do_file = false;
|
2016-05-03 23:23:30 +00:00
|
|
|
} 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)) {
|
2014-08-24 21:27:58 +00:00
|
|
|
do_file = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
completer.complete_param_expand(current_token, do_file);
|
2016-05-03 23:23:30 +00:00
|
|
|
} else {
|
2018-01-11 17:41:49 +00:00
|
|
|
assert(plain_statement && plain_statement.has_source());
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// Get the command node.
|
2018-01-11 17:41:49 +00:00
|
|
|
tnode_t<grammar::tok_string> cmd_node = plain_statement.child<0>();
|
|
|
|
assert(cmd_node && cmd_node.has_source() && "Expected command node to be valid");
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// Get the actual command string.
|
2018-01-11 17:41:49 +00:00
|
|
|
current_command = cmd_node.get_source(cmd);
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// Check the decoration.
|
2018-01-14 00:43:12 +00:00
|
|
|
switch (get_decoration(plain_statement)) {
|
2016-05-03 23:23:30 +00:00
|
|
|
case parse_statement_decoration_none: {
|
2013-10-13 01:17:03 +00:00
|
|
|
use_command = true;
|
2013-10-13 23:46:02 +00:00
|
|
|
use_function = true;
|
|
|
|
use_builtin = true;
|
2014-09-20 07:26:10 +00:00
|
|
|
use_implicit_cd = true;
|
2013-10-13 01:17:03 +00:00
|
|
|
break;
|
2016-05-03 23:23:30 +00:00
|
|
|
}
|
2013-10-13 01:17:03 +00:00
|
|
|
case parse_statement_decoration_command:
|
2016-05-03 23:23:30 +00:00
|
|
|
case parse_statement_decoration_exec: {
|
2013-10-13 01:17:03 +00:00
|
|
|
use_command = true;
|
|
|
|
use_function = false;
|
|
|
|
use_builtin = false;
|
2014-09-20 07:26:10 +00:00
|
|
|
use_implicit_cd = false;
|
2013-10-13 01:17:03 +00:00
|
|
|
break;
|
2016-05-03 23:23:30 +00:00
|
|
|
}
|
|
|
|
case parse_statement_decoration_builtin: {
|
2013-10-13 01:17:03 +00:00
|
|
|
use_command = false;
|
|
|
|
use_function = false;
|
|
|
|
use_builtin = true;
|
2014-09-20 07:26:10 +00:00
|
|
|
use_implicit_cd = false;
|
2013-10-13 01:17:03 +00:00
|
|
|
break;
|
2016-05-03 23:23:30 +00:00
|
|
|
}
|
2013-10-13 01:17:03 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2018-01-11 17:41:49 +00:00
|
|
|
if (cmd_node.location_in_or_at_end_of_source_range(pos)) {
|
2016-05-03 23:23:30 +00:00
|
|
|
// Complete command filename.
|
|
|
|
completer.complete_cmd(current_token, use_function, use_builtin, use_command,
|
|
|
|
use_implicit_cd);
|
|
|
|
} else {
|
|
|
|
// Get all the arguments.
|
2018-01-16 06:13:37 +00:00
|
|
|
auto all_arguments = plain_statement.descendants<grammar::argument>();
|
2016-05-03 23:23:30 +00:00
|
|
|
|
|
|
|
// See whether we are in an argument. We may also be in a redirection, or nothing at
|
|
|
|
// all.
|
2013-10-13 01:17:03 +00:00
|
|
|
size_t matching_arg_index = -1;
|
2016-05-03 23:23:30 +00:00
|
|
|
for (size_t i = 0; i < all_arguments.size(); i++) {
|
2018-01-12 01:19:48 +00:00
|
|
|
tnode_t<grammar::argument> arg = all_arguments.at(i);
|
|
|
|
if (arg.location_in_or_at_end_of_source_range(position_in_statement)) {
|
2013-10-13 01:17:03 +00:00
|
|
|
matching_arg_index = i;
|
|
|
|
break;
|
2012-11-19 08:31:03 +00:00
|
|
|
}
|
2013-10-13 01:17:03 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2013-10-13 01:17:03 +00:00
|
|
|
bool had_ddash = false;
|
|
|
|
wcstring current_argument, previous_argument;
|
2016-05-03 23:23:30 +00:00
|
|
|
if (matching_arg_index != (size_t)(-1)) {
|
|
|
|
const wcstring matching_arg =
|
2018-01-12 01:19:48 +00:00
|
|
|
all_arguments.at(matching_arg_index).get_source(cmd);
|
2016-05-03 23:23:30 +00:00
|
|
|
|
|
|
|
// 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.
|
2016-07-22 02:36:32 +00:00
|
|
|
bool cursor_in_whitespace =
|
2018-01-11 17:41:49 +00:00
|
|
|
!plain_statement.location_in_or_at_end_of_source_range(pos);
|
2016-05-03 23:23:30 +00:00
|
|
|
if (cursor_in_whitespace) {
|
2014-11-20 19:12:19 +00:00
|
|
|
current_argument = L"";
|
|
|
|
previous_argument = matching_arg;
|
2016-05-03 23:23:30 +00:00
|
|
|
} else {
|
2014-11-20 19:12:19 +00:00
|
|
|
current_argument = matching_arg;
|
2016-05-03 23:23:30 +00:00
|
|
|
if (matching_arg_index > 0) {
|
|
|
|
previous_argument =
|
2018-01-12 01:19:48 +00:00
|
|
|
all_arguments.at(matching_arg_index - 1).get_source(cmd);
|
2014-11-20 19:12:19 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// Check to see if we have a preceding double-dash.
|
|
|
|
for (size_t i = 0; i < matching_arg_index; i++) {
|
2018-01-12 01:19:48 +00:00
|
|
|
if (all_arguments.at(i).get_source(cmd) == L"--") {
|
2013-10-13 01:17:03 +00:00
|
|
|
had_ddash = true;
|
|
|
|
break;
|
2012-11-19 08:31:03 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-19 08:31:03 +00:00
|
|
|
}
|
2016-05-03 23:23:30 +00:00
|
|
|
|
|
|
|
// If we are not in an argument, we may be in a redirection.
|
2014-05-02 08:22:39 +00:00
|
|
|
bool in_redirection = false;
|
2016-05-03 23:23:30 +00:00
|
|
|
if (matching_arg_index == (size_t)(-1)) {
|
2018-01-12 01:19:48 +00:00
|
|
|
if (tnode_t<grammar::redirection>::find_node_matching_source_location(
|
|
|
|
&tree, position_in_statement, plain_statement)) {
|
|
|
|
in_redirection = true;
|
|
|
|
}
|
2014-05-02 08:22:39 +00:00
|
|
|
}
|
2016-05-03 23:23:30 +00:00
|
|
|
|
2016-02-06 22:39:47 +00:00
|
|
|
bool do_file = false, handle_as_special_cd = false;
|
2016-05-03 23:23:30 +00:00
|
|
|
if (in_redirection) {
|
2014-05-02 08:22:39 +00:00
|
|
|
do_file = true;
|
2016-05-03 23:23:30 +00:00
|
|
|
} 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.
|
2014-08-16 01:14:36 +00:00
|
|
|
do_file = true;
|
2016-05-03 23:23:30 +00:00
|
|
|
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.
|
2017-01-21 23:45:38 +00:00
|
|
|
std::unique_ptr<builtin_commandline_scoped_transient_t> transient_cmd;
|
2016-05-03 23:23:30 +00:00
|
|
|
if (i == 0) {
|
2014-08-16 01:14:36 +00:00
|
|
|
assert(wrap_chain.at(i) == current_command_unescape);
|
2016-05-03 23:23:30 +00:00
|
|
|
} else if (!(flags & COMPLETION_REQUEST_AUTOSUGGESTION)) {
|
2014-08-16 01:14:36 +00:00
|
|
|
wcstring faux_cmdline = cmd;
|
2018-01-11 17:41:49 +00:00
|
|
|
faux_cmdline.replace(cmd_node.source_range()->start,
|
|
|
|
cmd_node.source_range()->length,
|
|
|
|
wrap_chain.at(i));
|
2017-01-27 04:00:43 +00:00
|
|
|
transient_cmd = make_unique<builtin_commandline_scoped_transient_t>(
|
|
|
|
faux_cmdline);
|
2014-08-16 01:14:36 +00:00
|
|
|
}
|
2016-05-03 23:23:30 +00:00
|
|
|
if (!completer.complete_param(wrap_chain.at(i),
|
|
|
|
previous_argument_unescape,
|
|
|
|
current_argument_unescape, !had_ddash)) {
|
2014-08-16 01:14:36 +00:00
|
|
|
do_file = false;
|
|
|
|
}
|
|
|
|
}
|
2014-05-02 08:22:39 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// Hack. If we're cd, handle it specially (issue #1059, others).
|
2016-02-06 22:39:47 +00:00
|
|
|
handle_as_special_cd = (current_command_unescape == L"cd");
|
2016-05-03 23:23:30 +00:00
|
|
|
|
|
|
|
// And if we're autosuggesting, and the token is empty, don't do file
|
|
|
|
// suggestions.
|
|
|
|
if ((flags & COMPLETION_REQUEST_AUTOSUGGESTION) &&
|
|
|
|
current_argument_unescape.empty()) {
|
2014-05-02 08:22:39 +00:00
|
|
|
do_file = false;
|
|
|
|
}
|
2014-02-22 03:55:55 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// This function wants the unescaped string.
|
2016-02-06 22:39:47 +00:00
|
|
|
completer.complete_param_expand(current_token, do_file, handle_as_special_cd);
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2014-01-15 09:40:40 +00:00
|
|
|
|
2016-02-07 03:17:43 +00:00
|
|
|
*out_comps = completer.get_completions();
|
2012-02-25 02:43:10 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
/// 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;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring esc = escape_string(argument, 1);
|
|
|
|
append_format(out, L" --%ls %ls", opt.c_str(), esc.c_str());
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
wcstring complete_print() {
|
2015-09-21 18:24:49 +00:00
|
|
|
wcstring out;
|
2016-07-21 05:30:58 +00:00
|
|
|
scoped_lock locker(completion_lock);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
// Get a list of all completions in a vector, then sort it by order.
|
2016-03-18 22:14:16 +00:00
|
|
|
std::vector<const completion_entry_t *> all_completions;
|
2016-05-03 23:23:30 +00:00
|
|
|
for (completion_entry_set_t::const_iterator i = completion_set.begin();
|
|
|
|
i != completion_set.end(); ++i) {
|
2016-03-18 22:14:16 +00:00
|
|
|
all_completions.push_back(&*i);
|
|
|
|
}
|
2012-04-12 01:25:37 +00:00
|
|
|
sort(all_completions.begin(), all_completions.end(), compare_completions_by_order);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
for (std::vector<const completion_entry_t *>::const_iterator iter = all_completions.begin();
|
|
|
|
iter != all_completions.end(); ++iter) {
|
2012-02-15 19:33:41 +00:00
|
|
|
const completion_entry_t *e = *iter;
|
2014-10-31 01:19:47 +00:00
|
|
|
const option_list_t &options = e->get_options();
|
2016-05-03 23:23:30 +00:00
|
|
|
for (option_list_t::const_iterator oiter = options.begin(); oiter != options.end();
|
|
|
|
++oiter) {
|
2012-02-09 00:15:53 +00:00
|
|
|
const complete_entry_opt_t *o = &*oiter;
|
2016-05-03 23:23:30 +00:00
|
|
|
const wchar_t *modestr[] = {L"", L" --no-files", L" --require-parameter",
|
|
|
|
L" --exclusive"};
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
append_format(out, L"complete%ls", modestr[o->result_mode]);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
append_switch(out, e->cmd_is_path ? L"path" : L"command",
|
2014-08-17 02:25:36 +00:00
|
|
|
escape_string(e->cmd, ESCAPE_ALL));
|
2016-05-03 23:23:30 +00:00
|
|
|
|
|
|
|
switch (o->type) {
|
|
|
|
case option_type_args_only: {
|
2016-01-16 23:46:43 +00:00
|
|
|
break;
|
2016-05-03 23:23:30 +00:00
|
|
|
}
|
|
|
|
case option_type_short: {
|
2016-10-23 20:58:12 +00:00
|
|
|
assert(!o->option.empty()); //!OCLINT(multiple unary operator)
|
2016-01-16 23:46:43 +00:00
|
|
|
append_format(out, L" --short-option '%lc'", o->option.at(0));
|
|
|
|
break;
|
2016-05-03 23:23:30 +00:00
|
|
|
}
|
2016-01-16 23:46:43 +00:00
|
|
|
case option_type_single_long:
|
2016-05-03 23:23:30 +00:00
|
|
|
case option_type_double_long: {
|
|
|
|
append_switch(
|
|
|
|
out, o->type == option_type_single_long ? L"old-option" : L"long-option",
|
|
|
|
o->option);
|
2016-01-16 23:46:43 +00:00
|
|
|
break;
|
2016-05-03 23:23:30 +00:00
|
|
|
}
|
2012-11-19 00:30:30 +00:00
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
append_switch(out, L"description", C_(o->desc));
|
|
|
|
append_switch(out, L"arguments", o->comp);
|
|
|
|
append_switch(out, L"condition", o->condition);
|
2012-11-19 00:30:30 +00:00
|
|
|
out.append(L"\n");
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
}
|
2016-05-03 23:23:30 +00:00
|
|
|
|
|
|
|
// Append wraps. This is a wonky interface where even values are the commands, and odd values
|
|
|
|
// are the targets that they wrap.
|
2014-08-16 01:14:36 +00:00
|
|
|
const wcstring_list_t wrap_pairs = complete_get_wrap_pairs();
|
|
|
|
assert(wrap_pairs.size() % 2 == 0);
|
2016-05-03 23:23:30 +00:00
|
|
|
for (size_t i = 0; i < wrap_pairs.size();) {
|
2014-08-16 01:14:36 +00:00
|
|
|
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());
|
|
|
|
}
|
2015-09-21 18:24:49 +00:00
|
|
|
return out;
|
2014-08-16 01:14:36 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 01:23:10 +00:00
|
|
|
/// Completion "wrapper" support. The map goes from wrapping-command to wrapped-command-list.
|
2017-08-18 19:26:35 +00:00
|
|
|
static std::mutex wrapper_lock;
|
2017-08-19 23:27:24 +00:00
|
|
|
typedef std::unordered_map<wcstring, wcstring_list_t> wrapper_map_t;
|
2016-05-03 23:23:30 +00:00
|
|
|
static wrapper_map_t &wrap_map() {
|
2014-08-16 01:14:36 +00:00
|
|
|
ASSERT_IS_LOCKED(wrapper_lock);
|
2016-05-03 23:23:30 +00:00
|
|
|
// A pointer is a little more efficient than an object as a static because we can elide the
|
|
|
|
// thread-safe initialization.
|
2014-08-16 01:14:36 +00:00
|
|
|
static wrapper_map_t *wrapper_map = NULL;
|
2016-05-03 23:23:30 +00:00
|
|
|
if (wrapper_map == NULL) {
|
2014-08-16 01:14:36 +00:00
|
|
|
wrapper_map = new wrapper_map_t();
|
|
|
|
}
|
|
|
|
return *wrapper_map;
|
|
|
|
}
|
|
|
|
|
2017-04-12 05:11:45 +00:00
|
|
|
/// Add a new target that wraps a command. Example: __fish_XYZ (function) wraps XYZ (target).
|
2016-05-03 23:23:30 +00:00
|
|
|
bool complete_add_wrapper(const wcstring &command, const wcstring &new_target) {
|
|
|
|
if (command.empty() || new_target.empty()) {
|
2014-08-16 01:14:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-03 23:23:30 +00:00
|
|
|
|
2014-08-16 01:14:36 +00:00
|
|
|
scoped_lock locker(wrapper_lock);
|
|
|
|
wrapper_map_t &wraps = wrap_map();
|
|
|
|
wcstring_list_t *targets = &wraps[command];
|
2016-05-03 23:23:30 +00:00
|
|
|
// If it's already present, we do nothing.
|
|
|
|
if (std::find(targets->begin(), targets->end(), new_target) == targets->end()) {
|
2014-08-16 01:14:36 +00:00
|
|
|
targets->push_back(new_target);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
bool complete_remove_wrapper(const wcstring &command, const wcstring &target_to_remove) {
|
|
|
|
if (command.empty() || target_to_remove.empty()) {
|
2014-08-16 01:14:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-03 23:23:30 +00:00
|
|
|
|
2014-08-16 01:14:36 +00:00
|
|
|
scoped_lock locker(wrapper_lock);
|
|
|
|
wrapper_map_t &wraps = wrap_map();
|
|
|
|
bool result = false;
|
|
|
|
wrapper_map_t::iterator current_targets_iter = wraps.find(command);
|
2016-05-03 23:23:30 +00:00
|
|
|
if (current_targets_iter != wraps.end()) {
|
2014-08-16 01:14:36 +00:00
|
|
|
wcstring_list_t *targets = ¤t_targets_iter->second;
|
2016-05-03 23:23:30 +00:00
|
|
|
wcstring_list_t::iterator where =
|
|
|
|
std::find(targets->begin(), targets->end(), target_to_remove);
|
|
|
|
if (where != targets->end()) {
|
2014-08-16 01:14:36 +00:00
|
|
|
targets->erase(where);
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
wcstring_list_t complete_get_wrap_chain(const wcstring &command) {
|
|
|
|
if (command.empty()) {
|
2014-08-16 01:14:36 +00:00
|
|
|
return wcstring_list_t();
|
|
|
|
}
|
2016-07-21 05:30:58 +00:00
|
|
|
scoped_lock locker(wrapper_lock);
|
2014-08-16 01:14:36 +00:00
|
|
|
const wrapper_map_t &wraps = wrap_map();
|
2016-05-03 23:23:30 +00:00
|
|
|
|
2014-08-16 01:14:36 +00:00
|
|
|
wcstring_list_t result;
|
2017-08-19 23:27:24 +00:00
|
|
|
std::unordered_set<wcstring> visited; // set of visited commands
|
2016-05-03 23:23:30 +00:00
|
|
|
wcstring_list_t to_visit(1, command); // stack of remaining-to-visit commands
|
|
|
|
|
2014-08-16 01:14:36 +00:00
|
|
|
wcstring target;
|
2016-05-03 23:23:30 +00:00
|
|
|
while (!to_visit.empty()) {
|
|
|
|
// Grab the next command to visit, put it in target.
|
2017-01-27 00:14:50 +00:00
|
|
|
target = std::move(to_visit.back());
|
2014-08-16 01:14:36 +00:00
|
|
|
to_visit.pop_back();
|
2016-05-03 23:23:30 +00:00
|
|
|
|
|
|
|
// Try inserting into visited. If it was already present, we skip it; this is how we avoid
|
|
|
|
// loops.
|
|
|
|
if (!visited.insert(target).second) {
|
2014-08-16 01:14:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-05-03 23:23:30 +00:00
|
|
|
|
|
|
|
// Insert the target in the result. Note this is the command itself, if this is the first
|
|
|
|
// iteration of the loop.
|
2014-08-16 01:14:36 +00:00
|
|
|
result.push_back(target);
|
2016-05-03 23:23:30 +00:00
|
|
|
|
|
|
|
// Enqueue its children.
|
2014-08-16 01:14:36 +00:00
|
|
|
wrapper_map_t::const_iterator target_children_iter = wraps.find(target);
|
2016-05-03 23:23:30 +00:00
|
|
|
if (target_children_iter != wraps.end()) {
|
2014-08-16 01:14:36 +00:00
|
|
|
const wcstring_list_t &children = target_children_iter->second;
|
|
|
|
to_visit.insert(to_visit.end(), children.begin(), children.end());
|
|
|
|
}
|
|
|
|
}
|
2016-05-03 23:23:30 +00:00
|
|
|
|
2014-08-16 01:14:36 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-03 23:23:30 +00:00
|
|
|
wcstring_list_t complete_get_wrap_pairs() {
|
2014-08-16 01:14:36 +00:00
|
|
|
wcstring_list_t result;
|
2016-07-21 05:30:58 +00:00
|
|
|
scoped_lock locker(wrapper_lock);
|
2014-08-16 01:14:36 +00:00
|
|
|
const wrapper_map_t &wraps = wrap_map();
|
2016-05-03 23:23:30 +00:00
|
|
|
for (wrapper_map_t::const_iterator outer = wraps.begin(); outer != wraps.end(); ++outer) {
|
2014-08-16 01:14:36 +00:00
|
|
|
const wcstring &cmd = outer->first;
|
|
|
|
const wcstring_list_t &targets = outer->second;
|
2016-05-03 23:23:30 +00:00
|
|
|
for (size_t i = 0; i < targets.size(); i++) {
|
2014-08-16 01:14:36 +00:00
|
|
|
result.push_back(cmd);
|
|
|
|
result.push_back(targets.at(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|