2005-09-20 13:26:39 +00:00
|
|
|
/** \file complete.h
|
2012-11-18 10:23:22 +00:00
|
|
|
Prototypes for functions related to tab-completion.
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-18 10:23:22 +00:00
|
|
|
These functions are used for storing and retrieving tab-completion
|
|
|
|
data, as well as for performing tab-completion.
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
|
|
|
|
2005-10-04 15:11:39 +00:00
|
|
|
#ifndef FISH_COMPLETE_H
|
2006-06-17 13:07:08 +00:00
|
|
|
|
2005-10-24 15:26:25 +00:00
|
|
|
/**
|
|
|
|
Header guard
|
|
|
|
*/
|
2005-10-04 15:11:39 +00:00
|
|
|
#define FISH_COMPLETE_H
|
|
|
|
|
2012-01-16 16:56:47 +00:00
|
|
|
|
2005-10-04 15:11:39 +00:00
|
|
|
#include <wchar.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
2012-01-16 16:56:47 +00:00
|
|
|
#include "common.h"
|
2012-11-18 10:23:22 +00:00
|
|
|
/**
|
2012-11-18 12:52:21 +00:00
|
|
|
* Use all completions
|
|
|
|
*/
|
2005-09-20 13:26:39 +00:00
|
|
|
#define SHARED 0
|
2012-11-18 10:23:22 +00:00
|
|
|
/**
|
2012-11-18 12:52:21 +00:00
|
|
|
* Do not use file completion
|
|
|
|
*/
|
2005-09-20 13:26:39 +00:00
|
|
|
#define NO_FILES 1
|
2012-11-18 10:23:22 +00:00
|
|
|
/**
|
2012-11-18 12:52:21 +00:00
|
|
|
* Require a parameter after completion
|
|
|
|
*/
|
2005-09-20 13:26:39 +00:00
|
|
|
#define NO_COMMON 2
|
2012-11-18 10:23:22 +00:00
|
|
|
/**
|
2012-11-18 12:52:21 +00:00
|
|
|
* Only use the argument list specifies with completion after
|
|
|
|
* option. This is the same as (NO_FILES & NO_COMMON)
|
|
|
|
*/
|
2005-09-20 13:26:39 +00:00
|
|
|
#define EXCLUSIVE 3
|
|
|
|
|
2012-11-18 10:23:22 +00:00
|
|
|
/**
|
2012-11-18 12:52:21 +00:00
|
|
|
* Command is a path
|
|
|
|
*/
|
2005-09-20 13:26:39 +00:00
|
|
|
#define PATH 1
|
2012-11-18 10:23:22 +00:00
|
|
|
/**
|
2012-11-18 12:52:21 +00:00
|
|
|
* Command is not a path
|
|
|
|
*/
|
2005-09-20 13:26:39 +00:00
|
|
|
#define COMMAND 0
|
|
|
|
|
2012-11-18 10:23:22 +00:00
|
|
|
/**
|
2012-11-18 12:52:21 +00:00
|
|
|
* Separator between completion and description
|
|
|
|
*/
|
2005-09-20 13:26:39 +00:00
|
|
|
#define COMPLETE_SEP L'\004'
|
2006-06-17 13:07:08 +00:00
|
|
|
|
2012-11-18 10:23:22 +00:00
|
|
|
/**
|
2012-11-18 12:52:21 +00:00
|
|
|
* Separator between completion and description
|
|
|
|
*/
|
2005-09-20 13:26:39 +00:00
|
|
|
#define COMPLETE_SEP_STR L"\004"
|
|
|
|
|
2006-06-17 13:07:08 +00:00
|
|
|
/**
|
2012-11-18 12:52:21 +00:00
|
|
|
* Separator between completion items in fish_pager. This is used for
|
|
|
|
* completion grouping, e.g. when putting completions with the same
|
|
|
|
* descriptions on the same line.
|
|
|
|
*/
|
2006-02-14 11:46:31 +00:00
|
|
|
#define COMPLETE_ITEM_SEP L'\uf500'
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
2012-11-18 12:52:21 +00:00
|
|
|
* Character that separates the completion and description on
|
|
|
|
* programmable completions
|
|
|
|
*/
|
2005-09-20 13:26:39 +00:00
|
|
|
#define PROG_COMPLETE_SEP L'\t'
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
enum
|
|
|
|
{
|
2012-02-26 02:54:49 +00:00
|
|
|
/**
|
|
|
|
Do not insert space afterwards if this is the only completion. (The
|
|
|
|
default is to try insert a space)
|
|
|
|
*/
|
|
|
|
COMPLETE_NO_SPACE = 1 << 0,
|
|
|
|
|
|
|
|
/**
|
2013-01-24 11:17:13 +00:00
|
|
|
This completion is case insensitive.
|
2012-02-26 02:54:49 +00:00
|
|
|
|
|
|
|
Warning: The contents of the completion_t structure is actually
|
|
|
|
different if this flag is set! Specifically, the completion string
|
2013-02-02 22:50:22 +00:00
|
|
|
contains the _entire_ completion token, not merely its suffix.
|
2012-02-26 02:54:49 +00:00
|
|
|
*/
|
|
|
|
COMPLETE_NO_CASE = 1 << 1,
|
|
|
|
|
|
|
|
/**
|
|
|
|
This completion may or may not want a space at the end - guess by
|
|
|
|
checking the last character of the completion.
|
|
|
|
*/
|
2013-02-02 22:50:22 +00:00
|
|
|
COMPLETE_AUTO_SPACE = 1 << 2,
|
2012-02-26 02:54:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
This completion should be inserted as-is, without escaping.
|
|
|
|
*/
|
2013-02-02 22:50:22 +00:00
|
|
|
COMPLETE_DONT_ESCAPE = 1 << 3
|
2012-02-26 02:54:49 +00:00
|
|
|
};
|
|
|
|
typedef int complete_flags_t;
|
2008-01-13 19:32:21 +00:00
|
|
|
|
|
|
|
|
2012-02-02 00:27:14 +00:00
|
|
|
class completion_t
|
2007-02-09 09:33:50 +00:00
|
|
|
{
|
2007-02-18 23:25:20 +00:00
|
|
|
|
2012-02-02 00:27:14 +00:00
|
|
|
private:
|
|
|
|
/* No public default constructor */
|
2012-11-19 00:30:30 +00:00
|
|
|
completion_t();
|
2012-02-02 00:27:14 +00:00
|
|
|
public:
|
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
/**
|
|
|
|
The completion string
|
|
|
|
*/
|
|
|
|
wcstring completion;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
/**
|
|
|
|
The description for this completion
|
|
|
|
*/
|
|
|
|
wcstring description;
|
2007-02-18 23:25:20 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
/**
|
|
|
|
Flags determining the completion behaviour.
|
2007-02-18 23:25:20 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
Determines whether a space should be inserted after this
|
2013-01-24 11:17:13 +00:00
|
|
|
completion if it is the only possible completion using the
|
2012-11-19 00:30:30 +00:00
|
|
|
COMPLETE_NO_SPACE flag.
|
2007-02-18 23:25:20 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
The COMPLETE_NO_CASE can be used to signal that this completion
|
|
|
|
is case insensitive.
|
|
|
|
*/
|
|
|
|
int flags;
|
2007-02-18 23:25:20 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
bool is_case_insensitive() const
|
|
|
|
{
|
|
|
|
return !!(flags & COMPLETE_NO_CASE);
|
|
|
|
}
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-07-17 19:47:01 +00:00
|
|
|
/* Construction. Note: defining these so that they are not inlined reduces the executable size. */
|
|
|
|
completion_t(const wcstring &comp, const wcstring &desc = L"", int flags_val = 0);
|
|
|
|
completion_t(const completion_t &);
|
|
|
|
completion_t &operator=(const completion_t &);
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-07-17 19:47:01 +00:00
|
|
|
/* The following are needed for sorting and uniquing completions */
|
2012-11-19 00:30:30 +00:00
|
|
|
bool operator < (const completion_t& rhs) const;
|
|
|
|
bool operator == (const completion_t& rhs) const;
|
|
|
|
bool operator != (const completion_t& rhs) const;
|
2012-02-02 20:04:04 +00:00
|
|
|
};
|
2007-02-09 09:33:50 +00:00
|
|
|
|
2012-11-19 00:30:30 +00:00
|
|
|
enum complete_type_t
|
|
|
|
{
|
2012-02-24 20:13:35 +00:00
|
|
|
COMPLETE_DEFAULT,
|
|
|
|
COMPLETE_AUTOSUGGEST
|
|
|
|
};
|
2007-02-09 09:33:50 +00:00
|
|
|
|
2012-07-17 19:47:01 +00:00
|
|
|
/** Given a list of completions, returns a list of their completion fields */
|
2012-11-19 00:30:30 +00:00
|
|
|
wcstring_list_t completions_to_wcstring_list(const std::vector<completion_t> &completions);
|
2012-07-17 19:47:01 +00:00
|
|
|
|
|
|
|
/** Sorts a list of completions */
|
2012-11-19 00:30:30 +00:00
|
|
|
void sort_completions(std::vector<completion_t> &completions);
|
2012-07-17 19:47:01 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/**
|
|
|
|
|
2012-11-18 10:23:22 +00:00
|
|
|
Add a completion.
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-11-29 14:21:02 +00:00
|
|
|
All supplied values are copied, they should be freed by or otherwise
|
|
|
|
disposed by the caller.
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-11-18 10:23:22 +00:00
|
|
|
Examples:
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
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:
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
complete -c gcc -s o -r
|
|
|
|
|
|
|
|
The command 'grep -d' required that one of the strings 'read',
|
|
|
|
'skip' or 'recurse' is used. As such, it is suitable to specify that
|
|
|
|
a completion requires one of them. This can be done using the
|
|
|
|
following line:
|
|
|
|
|
|
|
|
complete -c grep -s d -x -a "read skip recurse"
|
|
|
|
|
|
|
|
|
|
|
|
\param cmd Command to complete.
|
|
|
|
\param cmd_type If cmd_type is PATH, cmd will be interpreted as the absolute
|
2012-11-18 12:52:21 +00:00
|
|
|
path of the program (optionally containing wildcards), otherwise it
|
|
|
|
will be interpreted as the command name.
|
|
|
|
\param short_opt The single character name of an option. (-a is a short option,
|
|
|
|
--all and -funroll are long options)
|
|
|
|
\param long_opt The multi character name of an option. (-a is a short option,
|
|
|
|
--all and -funroll are long options)
|
2012-11-18 10:23:22 +00:00
|
|
|
\param long_mode Whether to use old style, single dash long options.
|
2005-09-20 13:26:39 +00:00
|
|
|
\param result_mode Whether to search further completions when this
|
2012-11-18 12:52:21 +00:00
|
|
|
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.
|
2005-09-20 13:26:39 +00:00
|
|
|
\param comp A space separated list of completions which may contain subshells.
|
|
|
|
\param desc A description of the completion.
|
2012-11-18 12:52:21 +00:00
|
|
|
\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.
|
2008-01-13 16:47:47 +00:00
|
|
|
\param flags A set of completion flags
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
void complete_add(const wchar_t *cmd,
|
|
|
|
bool cmd_is_path,
|
|
|
|
wchar_t short_opt,
|
|
|
|
const wchar_t *long_opt,
|
|
|
|
int long_mode,
|
|
|
|
int result_mode,
|
|
|
|
const wchar_t *condition,
|
|
|
|
const wchar_t *comp,
|
|
|
|
const wchar_t *desc,
|
|
|
|
int flags);
|
2007-01-28 13:40:59 +00:00
|
|
|
/**
|
|
|
|
Sets whether the completion list for this command is complete. If
|
|
|
|
true, any options not matching one of the provided options will be
|
|
|
|
flagged as an error by syntax highlighting.
|
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
void complete_set_authoritative(const wchar_t *cmd, bool cmd_type, bool authoritative);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Remove a previously defined completion
|
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
void complete_remove(const wchar_t *cmd,
|
|
|
|
bool cmd_is_path,
|
|
|
|
wchar_t short_opt,
|
|
|
|
const wchar_t *long_opt);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-02-24 20:13:35 +00:00
|
|
|
|
2012-11-18 12:52:21 +00:00
|
|
|
/** Find all completions of the command cmd, insert them into out. If to_load is
|
|
|
|
* not NULL, append all commands that we would autoload, but did not (presumably
|
|
|
|
* because this is not the main thread)
|
|
|
|
*/
|
|
|
|
void complete(const wcstring &cmd,
|
|
|
|
std::vector<completion_t> &comp,
|
|
|
|
complete_type_t type,
|
|
|
|
wcstring_list_t *to_load = NULL);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
/**
|
2012-11-18 10:23:22 +00:00
|
|
|
Print a list of all current completions into the string.
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-03-04 06:08:34 +00:00
|
|
|
\param out The string to write completions to
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
void complete_print(wcstring &out);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Tests if the specified option is defined for the specified command
|
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
int complete_is_valid_option(const wcstring &str,
|
|
|
|
const wcstring &opt,
|
|
|
|
wcstring_list_t *inErrorsOrNull,
|
|
|
|
bool allow_autoload);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Tests if the specified argument is valid for the specified option
|
|
|
|
and command
|
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
bool complete_is_valid_argument(const wcstring &str,
|
|
|
|
const wcstring &opt,
|
|
|
|
const wcstring &arg);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Load command-specific completions for the specified command. This
|
|
|
|
is done automatically whenever completing any given command, so
|
|
|
|
there is no need to call this except in the case of completions
|
|
|
|
with internal dependencies.
|
|
|
|
|
|
|
|
\param cmd the command for which to load command-specific completions
|
2012-11-18 12:52:21 +00:00
|
|
|
\param reload should the commands completions be reloaded, even if they where
|
|
|
|
previously loaded. (This is set to true on actual completions, so that
|
|
|
|
changed completion are updated in running shells)
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2012-11-19 00:30:30 +00:00
|
|
|
void complete_load(const wcstring &cmd, bool reload);
|
2005-10-04 15:11:39 +00:00
|
|
|
|
2007-02-18 23:25:20 +00:00
|
|
|
/**
|
|
|
|
Create a new completion entry
|
|
|
|
|
2012-02-02 00:27:14 +00:00
|
|
|
\param completions The array of completions to append to
|
2008-01-13 16:47:47 +00:00
|
|
|
\param comp The completion string
|
2007-02-18 23:25:20 +00:00
|
|
|
\param desc The description of the completion
|
|
|
|
\param flags completion flags
|
2012-02-02 00:27:14 +00:00
|
|
|
|
2007-02-18 23:25:20 +00:00
|
|
|
*/
|
2012-07-17 19:47:01 +00:00
|
|
|
void append_completion(std::vector<completion_t> &completions, const wcstring &comp, const wcstring &desc = L"", int flags = 0);
|
2007-02-18 23:25:20 +00:00
|
|
|
|
|
|
|
|
2005-10-04 15:11:39 +00:00
|
|
|
#endif
|