2016-05-01 11:47:05 +08:00
|
|
|
// Prototypes for functions for syntax highlighting.
|
2005-10-04 23:11:39 +08:00
|
|
|
#ifndef FISH_HIGHLIGHT_H
|
|
|
|
#define FISH_HIGHLIGHT_H
|
|
|
|
|
2017-02-13 12:24:22 +08:00
|
|
|
#include <stddef.h>
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <stdint.h>
|
2017-02-14 12:37:27 +08:00
|
|
|
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <vector>
|
2005-10-04 23:11:39 +08:00
|
|
|
|
2016-05-01 11:47:05 +08:00
|
|
|
#include "color.h"
|
2016-04-21 14:00:54 +08:00
|
|
|
#include "common.h"
|
2011-12-27 11:18:46 +08:00
|
|
|
#include "env.h"
|
2005-10-04 23:11:39 +08:00
|
|
|
|
2019-03-04 09:34:00 +08:00
|
|
|
/// Describes the role of a span of text.
|
|
|
|
enum class highlight_role_t : uint8_t {
|
|
|
|
normal = 0, // normal text
|
|
|
|
error, // error
|
|
|
|
command, // command
|
|
|
|
statement_terminator, // process separator
|
|
|
|
param, // command parameter (argument)
|
|
|
|
comment, // comment
|
|
|
|
match, // matching parenthesis, etc.
|
|
|
|
search_match, // search match
|
|
|
|
operat, // operator
|
|
|
|
escape, // escape sequences
|
|
|
|
quote, // quoted string
|
|
|
|
redirection, // redirection
|
|
|
|
autosuggestion, // autosuggestion
|
|
|
|
selection,
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-01 11:47:05 +08:00
|
|
|
// Pager support.
|
2019-01-13 15:16:30 +08:00
|
|
|
// NOTE: pager.cpp relies on these being in this order.
|
2019-03-04 09:34:00 +08:00
|
|
|
pager_progress,
|
|
|
|
pager_background,
|
|
|
|
pager_prefix,
|
|
|
|
pager_completion,
|
|
|
|
pager_description,
|
|
|
|
pager_secondary_background,
|
|
|
|
pager_secondary_prefix,
|
|
|
|
pager_secondary_completion,
|
|
|
|
pager_secondary_description,
|
|
|
|
pager_selected_background,
|
|
|
|
pager_selected_prefix,
|
|
|
|
pager_selected_completion,
|
|
|
|
pager_selected_description,
|
2014-01-15 17:01:25 +08:00
|
|
|
};
|
|
|
|
|
2019-03-04 09:34:00 +08:00
|
|
|
/// Simply value type describing how a character should be highlighted..
|
|
|
|
struct highlight_spec_t {
|
|
|
|
highlight_role_t foreground{highlight_role_t::normal};
|
|
|
|
highlight_role_t background{highlight_role_t::normal};
|
|
|
|
bool valid_path{false};
|
|
|
|
bool force_underline{false};
|
|
|
|
|
|
|
|
highlight_spec_t() = default;
|
|
|
|
|
|
|
|
/* implicit */ highlight_spec_t(highlight_role_t fg,
|
|
|
|
highlight_role_t bg = highlight_role_t::normal)
|
|
|
|
: foreground(fg), background(bg) {}
|
|
|
|
|
|
|
|
bool operator==(const highlight_spec_t &rhs) const {
|
|
|
|
return foreground == rhs.foreground && background == rhs.background &&
|
|
|
|
valid_path == rhs.valid_path && force_underline == rhs.force_underline;
|
|
|
|
}
|
2014-01-15 17:01:25 +08:00
|
|
|
|
2019-03-04 09:34:00 +08:00
|
|
|
bool operator!=(const highlight_spec_t &rhs) const { return !(*this == rhs); }
|
|
|
|
|
|
|
|
static highlight_spec_t make_background(highlight_role_t bg_role) {
|
|
|
|
return highlight_spec_t{highlight_role_t::normal, bg_role};
|
|
|
|
}
|
|
|
|
};
|
2006-06-20 08:50:10 +08:00
|
|
|
|
2012-06-29 02:54:37 +08:00
|
|
|
class history_item_t;
|
|
|
|
|
2019-09-19 19:27:33 +08:00
|
|
|
std::string colorize(const wcstring &text, const std::vector<highlight_spec_t> &colors);
|
|
|
|
|
2016-05-01 11:47:05 +08:00
|
|
|
/// Perform syntax highlighting for the shell commands in buff. The result is stored in the color
|
|
|
|
/// array as a color_code from the HIGHLIGHT_ enum for each character in buff.
|
|
|
|
///
|
2016-06-06 09:46:04 +08:00
|
|
|
/// \param buffstr The buffer on which to perform syntax highlighting
|
2019-10-22 08:21:40 +08:00
|
|
|
/// \param color The array in which to store the color codes. The first 8 bits are used for fg
|
2016-05-01 11:47:05 +08:00
|
|
|
/// color, the next 8 bits for bg color.
|
|
|
|
/// \param pos the cursor position. Used for quote matching, etc.
|
|
|
|
/// \param error a list in which a description of each error will be inserted. May be 0, in whcich
|
|
|
|
/// case no error descriptions will be generated.
|
|
|
|
void highlight_shell(const wcstring &buffstr, std::vector<highlight_spec_t> &color, size_t pos,
|
2018-09-09 17:25:51 +08:00
|
|
|
wcstring_list_t *error, const environment_t &vars);
|
2016-05-01 11:47:05 +08:00
|
|
|
|
|
|
|
/// Perform a non-blocking shell highlighting. The function will not do any I/O that may block. As a
|
|
|
|
/// result, invalid commands may not be detected, etc.
|
|
|
|
void highlight_shell_no_io(const wcstring &buffstr, std::vector<highlight_spec_t> &color,
|
2018-09-09 17:25:51 +08:00
|
|
|
size_t pos, wcstring_list_t *error, const environment_t &vars);
|
2016-05-01 11:47:05 +08:00
|
|
|
|
2019-11-25 19:03:25 +08:00
|
|
|
/// Perform syntax highlighting for the text in buff. Matching quotes and parenthesis are
|
2016-05-01 11:47:05 +08:00
|
|
|
/// highlighted. The result is stored in the color array as a color_code from the HIGHLIGHT_ enum
|
|
|
|
/// for each character in buff.
|
|
|
|
///
|
2016-06-06 09:46:04 +08:00
|
|
|
/// \param buffstr The buffer on which to perform syntax highlighting
|
2019-10-22 08:21:40 +08:00
|
|
|
/// \param color The array in which to store the color codes. The first 8 bits are used for fg
|
2016-05-01 11:47:05 +08:00
|
|
|
/// color, the next 8 bits for bg color.
|
|
|
|
/// \param pos the cursor position. Used for quote matching, etc.
|
|
|
|
/// \param error a list in which a description of each error will be inserted. May be 0, in whcich
|
|
|
|
/// case no error descriptions will be generated.
|
|
|
|
void highlight_universal(const wcstring &buffstr, std::vector<highlight_spec_t> &color, size_t pos,
|
2018-09-09 17:25:51 +08:00
|
|
|
wcstring_list_t *error, const environment_t &vars);
|
2016-05-01 11:47:05 +08:00
|
|
|
|
2019-03-04 09:34:00 +08:00
|
|
|
/// \return an RGB color for a given highlight spec.
|
|
|
|
rgb_color_t highlight_get_color(const highlight_spec_t &highlight, bool is_background);
|
2005-10-04 23:11:39 +08:00
|
|
|
|
2016-05-01 11:47:05 +08:00
|
|
|
/// Given a command 'str' from the history, try to determine whether we ought to suggest it by
|
|
|
|
/// specially recognizing the command. Returns true if we validated the command. If so, returns by
|
|
|
|
/// reference whether the suggestion is valid or not.
|
|
|
|
bool autosuggest_validate_from_history(const history_item_t &item,
|
|
|
|
const wcstring &working_directory,
|
2018-09-09 17:25:51 +08:00
|
|
|
const environment_t &vars);
|
2016-05-01 11:47:05 +08:00
|
|
|
|
|
|
|
// Tests whether the specified string cpath is the prefix of anything we could cd to. directories is
|
|
|
|
// a list of possible parent directories (typically either the working directory, or the cdpath).
|
|
|
|
// This does I/O!
|
|
|
|
//
|
|
|
|
// This is used only internally to this file, and is exposed only for testing.
|
|
|
|
enum {
|
|
|
|
// The path must be to a directory.
|
2012-07-07 05:34:53 +08:00
|
|
|
PATH_REQUIRE_DIR = 1 << 0,
|
2016-05-01 11:47:05 +08:00
|
|
|
// Expand any leading tilde in the path.
|
2012-07-07 05:34:53 +08:00
|
|
|
PATH_EXPAND_TILDE = 1 << 1
|
|
|
|
};
|
|
|
|
typedef unsigned int path_flags_t;
|
2019-11-19 08:54:36 +08:00
|
|
|
bool is_potential_path(const wcstring &potential_path_fragment, const wcstring_list_t &directories,
|
2018-09-19 12:03:01 +08:00
|
|
|
const environment_t &vars, path_flags_t flags);
|
2012-02-19 13:56:30 +08:00
|
|
|
|
2005-10-04 23:11:39 +08:00
|
|
|
#endif
|