2016-05-04 07:23:30 +08:00
|
|
|
/// Prototypes for functions related to tab-completion.
|
|
|
|
///
|
|
|
|
/// These functions are used for storing and retrieving tab-completion data, as well as for
|
|
|
|
/// performing tab-completion.
|
2005-10-04 23:11:39 +08:00
|
|
|
#ifndef FISH_COMPLETE_H
|
|
|
|
#define FISH_COMPLETE_H
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
#include <stdint.h>
|
2017-02-14 12:37:27 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
#include <vector>
|
2005-10-04 23:11:39 +08:00
|
|
|
|
2012-01-17 00:56:47 +08:00
|
|
|
#include "common.h"
|
2016-04-21 14:00:54 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Use all completions.
|
2005-09-20 21:26:39 +08:00
|
|
|
#define SHARED 0
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Do not use file completion.
|
2005-09-20 21:26:39 +08:00
|
|
|
#define NO_FILES 1
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Require a parameter after completion.
|
2005-09-20 21:26:39 +08:00
|
|
|
#define NO_COMMON 2
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Only use the argument list specifies with completion after option. This is the same as (NO_FILES
|
|
|
|
/// | NO_COMMON).
|
2005-09-20 21:26:39 +08:00
|
|
|
#define EXCLUSIVE 3
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Command is a path.
|
2005-09-20 21:26:39 +08:00
|
|
|
#define PATH 1
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Command is not a path.
|
2005-09-20 21:26:39 +08:00
|
|
|
#define COMMAND 0
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Separator between completion and description.
|
2005-09-20 21:26:39 +08:00
|
|
|
#define COMPLETE_SEP L'\004'
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Character that separates the completion and description on programmable completions.
|
2005-09-20 21:26:39 +08:00
|
|
|
#define PROG_COMPLETE_SEP L'\t'
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
enum {
|
|
|
|
/// Do not insert space afterwards if this is the only completion. (The default is to try insert
|
|
|
|
/// a space).
|
2012-02-26 10:54:49 +08:00
|
|
|
COMPLETE_NO_SPACE = 1 << 0,
|
2016-05-04 07:23:30 +08:00
|
|
|
/// This is not the suffix of a token, but replaces it entirely.
|
2013-03-06 12:54:16 +08:00
|
|
|
COMPLETE_REPLACES_TOKEN = 1 << 2,
|
2016-05-04 07:23:30 +08:00
|
|
|
/// This completion may or may not want a space at the end - guess by checking the last
|
|
|
|
/// character of the completion.
|
2013-03-06 12:54:16 +08:00
|
|
|
COMPLETE_AUTO_SPACE = 1 << 3,
|
2016-05-04 07:23:30 +08:00
|
|
|
/// This completion should be inserted as-is, without escaping.
|
2013-04-08 14:54:43 +08:00
|
|
|
COMPLETE_DONT_ESCAPE = 1 << 4,
|
2016-05-04 07:23:30 +08:00
|
|
|
/// If you do escape, don't escape tildes.
|
2017-07-25 06:54:54 +08:00
|
|
|
COMPLETE_DONT_ESCAPE_TILDES = 1 << 5,
|
|
|
|
/// Do not sort supplied completions
|
|
|
|
COMPLETE_DONT_SORT = 1 << 6
|
2012-02-26 10:54:49 +08:00
|
|
|
};
|
|
|
|
typedef int complete_flags_t;
|
2008-01-14 03:32:21 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
class completion_t {
|
|
|
|
private:
|
|
|
|
// No public default constructor.
|
2012-11-19 08:30:30 +08:00
|
|
|
completion_t();
|
2012-02-02 08:27:14 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
public:
|
|
|
|
// Destructor. Not inlining it saves code size.
|
2013-04-17 06:01:24 +08:00
|
|
|
~completion_t();
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// The completion string.
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring completion;
|
2016-05-04 07:23:30 +08:00
|
|
|
/// The description for this completion.
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring description;
|
2016-05-04 07:23:30 +08:00
|
|
|
/// The type of fuzzy match.
|
2013-05-26 06:41:18 +08:00
|
|
|
string_fuzzy_match_t match;
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Flags determining the completion behaviour.
|
|
|
|
///
|
|
|
|
/// Determines whether a space should be inserted after this completion if it is the only
|
|
|
|
/// possible completion using the COMPLETE_NO_SPACE flag. The COMPLETE_NO_CASE can be used to
|
|
|
|
/// signal that this completion is case insensitive.
|
2014-01-15 06:28:06 +08:00
|
|
|
complete_flags_t flags;
|
2013-06-02 16:14:26 +08:00
|
|
|
|
2018-02-17 13:26:15 +08:00
|
|
|
// Construction.
|
|
|
|
explicit completion_t(wcstring comp, wcstring desc = wcstring(),
|
2016-05-04 07:23:30 +08:00
|
|
|
string_fuzzy_match_t match = string_fuzzy_match_t(fuzzy_match_exact),
|
|
|
|
complete_flags_t flags_val = 0);
|
2012-07-18 03:47:01 +08:00
|
|
|
completion_t(const completion_t &);
|
|
|
|
completion_t &operator=(const completion_t &);
|
2013-10-27 06:27:39 +08:00
|
|
|
|
2018-02-17 13:26:15 +08:00
|
|
|
completion_t(completion_t &&);
|
|
|
|
completion_t &operator=(completion_t &&);
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
// Compare two completions. No operating overlaoding to make this always explicit (there's
|
|
|
|
// potentially multiple ways to compare completions).
|
|
|
|
//
|
|
|
|
// "Naturally less than" means in a natural ordering, where digits are treated as numbers. For
|
|
|
|
// example, foo10 is naturally greater than foo2 (but alphabetically less than it).
|
2015-03-24 02:13:26 +08:00
|
|
|
static bool is_naturally_less_than(const completion_t &a, const completion_t &b);
|
2017-07-25 06:54:54 +08:00
|
|
|
|
|
|
|
// Deduplicate a potentially-unsorted vector, preserving the order
|
|
|
|
template <class Iterator, class HashFunction>
|
|
|
|
static Iterator unique_unsorted(Iterator begin, Iterator end, HashFunction hash);
|
2016-05-04 07:23:30 +08:00
|
|
|
|
|
|
|
// If this completion replaces the entire token, prepend a prefix. Otherwise do nothing.
|
2015-08-03 08:58:37 +08:00
|
|
|
void prepend_token_prefix(const wcstring &prefix);
|
2012-02-03 04:04:04 +08:00
|
|
|
};
|
2007-02-09 17:33:50 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Sorts and remove any duplicate completions in the completion list, then puts them in priority
|
|
|
|
/// order.
|
2016-02-07 07:06:33 +08:00
|
|
|
void completions_sort_and_prioritize(std::vector<completion_t> *comps);
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
enum {
|
2013-03-06 12:54:16 +08:00
|
|
|
COMPLETION_REQUEST_DEFAULT = 0,
|
2016-05-04 07:23:30 +08:00
|
|
|
COMPLETION_REQUEST_AUTOSUGGESTION = 1
|
|
|
|
<< 0, // indicates the completion is for an autosuggestion
|
|
|
|
COMPLETION_REQUEST_DESCRIPTIONS = 1 << 1, // indicates that we want descriptions
|
|
|
|
COMPLETION_REQUEST_FUZZY_MATCH = 1 << 2 // indicates that we don't require a prefix match
|
2012-02-25 04:13:35 +08:00
|
|
|
};
|
2013-03-06 12:54:16 +08:00
|
|
|
typedef uint32_t completion_request_flags_t;
|
2007-02-09 17:33:50 +08:00
|
|
|
|
2016-06-06 09:46:04 +08:00
|
|
|
enum complete_option_type_t {
|
|
|
|
option_type_args_only, // no option
|
|
|
|
option_type_short, // -x
|
|
|
|
option_type_single_long, // -foo
|
|
|
|
option_type_double_long // --foo
|
|
|
|
};
|
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Add a completion.
|
|
|
|
///
|
|
|
|
/// All supplied values are copied, they should be freed by or otherwise disposed by the caller.
|
|
|
|
///
|
|
|
|
/// Examples:
|
|
|
|
///
|
|
|
|
/// The command 'gcc -o' requires that a file follows it, so the NO_COMMON option is suitable. This
|
|
|
|
/// can be done using the following line:
|
|
|
|
///
|
|
|
|
/// complete -c gcc -s o -r
|
|
|
|
///
|
|
|
|
/// The command 'grep -d' required that one of the strings 'read', 'skip' or 'recurse' is used. As
|
|
|
|
/// such, it is suitable to specify that a completion requires one of them. This can be done using
|
|
|
|
/// the following line:
|
|
|
|
///
|
|
|
|
/// complete -c grep -s d -x -a "read skip recurse"
|
|
|
|
///
|
|
|
|
/// \param cmd Command to complete.
|
2016-06-06 09:46:04 +08:00
|
|
|
/// \param cmd_is_path If cmd_is_path is true, cmd will be interpreted as the absolute
|
2016-05-04 07:23:30 +08:00
|
|
|
/// path of the program (optionally containing wildcards), otherwise it
|
|
|
|
/// will be interpreted as the command name.
|
2016-06-06 09:46:04 +08:00
|
|
|
/// \param option The name of an option.
|
|
|
|
/// \param option_type The type of option: can be option_type_short (-x),
|
|
|
|
/// option_type_single_long (-foo), option_type_double_long (--bar).
|
2016-05-04 07:23:30 +08:00
|
|
|
/// \param result_mode Whether to search further completions when this completion has been
|
|
|
|
/// succesfully matched. If result_mode is SHARED, any other completions may also be used. If
|
|
|
|
/// result_mode is NO_FILES, file completion should not be used, but other completions may be used.
|
|
|
|
/// If result_mode is NO_COMMON, on option may follow it - only a parameter. If result_mode is
|
|
|
|
/// EXCLUSIVE, no option may follow it, and file completion is not performed.
|
|
|
|
/// \param comp A space separated list of completions which may contain subshells.
|
|
|
|
/// \param desc A description of the completion.
|
|
|
|
/// \param condition a command to be run to check it this completion should be used. If \c condition
|
|
|
|
/// is empty, the completion is always used.
|
|
|
|
/// \param flags A set of completion flags
|
|
|
|
void complete_add(const wchar_t *cmd, bool cmd_is_path, const wcstring &option,
|
|
|
|
complete_option_type_t option_type, int result_mode, const wchar_t *condition,
|
|
|
|
const wchar_t *comp, const wchar_t *desc, int flags);
|
|
|
|
|
|
|
|
/// Remove a previously defined completion.
|
|
|
|
void complete_remove(const wcstring &cmd, bool cmd_is_path, const wcstring &option,
|
2016-01-17 14:42:14 +08:00
|
|
|
complete_option_type_t type);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Removes all completions for a given command.
|
2016-01-17 14:42:14 +08:00
|
|
|
void complete_remove_all(const wcstring &cmd, bool cmd_is_path);
|
2012-02-25 04:13:35 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Find all completions of the command cmd, insert them into out.
|
|
|
|
void complete(const wcstring &cmd, std::vector<completion_t> *out_comps,
|
2017-08-19 07:52:39 +08:00
|
|
|
completion_request_flags_t flags);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Return a list of all current completions.
|
2015-09-22 02:24:49 +08:00
|
|
|
wcstring complete_print();
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Tests if the specified option is defined for the specified command.
|
|
|
|
int complete_is_valid_option(const wcstring &str, const wcstring &opt,
|
|
|
|
wcstring_list_t *inErrorsOrNull, bool allow_autoload);
|
2007-02-19 07:25:20 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Tests if the specified argument is valid for the specified option and command.
|
|
|
|
bool complete_is_valid_argument(const wcstring &str, const wcstring &opt, const wcstring &arg);
|
2012-02-02 08:27:14 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Create a new completion entry.
|
|
|
|
///
|
|
|
|
/// \param completions The array of completions to append to
|
|
|
|
/// \param comp The completion string
|
|
|
|
/// \param desc The description of the completion
|
|
|
|
/// \param flags completion flags
|
2018-02-17 13:26:15 +08:00
|
|
|
void append_completion(std::vector<completion_t> *completions, wcstring comp,
|
|
|
|
wcstring desc = wcstring(), int flags = 0,
|
2016-05-04 07:23:30 +08:00
|
|
|
string_fuzzy_match_t match = string_fuzzy_match_t(fuzzy_match_exact));
|
2007-02-19 07:25:20 +08:00
|
|
|
|
2016-05-04 07:23:30 +08:00
|
|
|
/// Function used for testing.
|
2013-03-06 12:54:16 +08:00
|
|
|
void complete_set_variable_names(const wcstring_list_t *names);
|
2007-02-19 07:25:20 +08:00
|
|
|
|
2018-02-27 11:21:46 +08:00
|
|
|
/// Support for "wrap targets." A wrap target is a command that completes like another command.
|
2014-08-16 09:14:36 +08:00
|
|
|
bool complete_add_wrapper(const wcstring &command, const wcstring &wrap_target);
|
|
|
|
bool complete_remove_wrapper(const wcstring &command, const wcstring &wrap_target);
|
2018-02-27 11:21:46 +08:00
|
|
|
|
|
|
|
/// Returns a list of wrap targets for a given command.
|
|
|
|
wcstring_list_t complete_get_wrap_targets(const wcstring &command);
|
2014-08-16 09:14:36 +08:00
|
|
|
|
2018-05-12 13:11:40 +08:00
|
|
|
tuple_list<wcstring, wcstring> complete_get_wrap_pairs();
|
2014-08-16 09:14:36 +08:00
|
|
|
|
2018-02-16 13:58:02 +08:00
|
|
|
// Observes that fish_complete_path has changed.
|
|
|
|
void complete_invalidate_path();
|
|
|
|
|
2005-10-04 23:11:39 +08:00
|
|
|
#endif
|