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
|
|
|
|
2020-08-04 08:34:27 +08:00
|
|
|
#include <unordered_map>
|
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
|
2021-02-05 03:22:32 +08:00
|
|
|
keyword,
|
2019-03-04 09:34:00 +08:00
|
|
|
statement_terminator, // process separator
|
|
|
|
param, // command parameter (argument)
|
|
|
|
comment, // comment
|
|
|
|
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
|
|
|
|
2020-08-05 01:55:43 +08:00
|
|
|
namespace std {
|
2020-08-04 08:34:27 +08:00
|
|
|
template <>
|
2020-08-05 01:55:43 +08:00
|
|
|
struct hash<highlight_spec_t> {
|
2020-08-04 08:34:27 +08:00
|
|
|
std::size_t operator()(const highlight_spec_t &v) const {
|
|
|
|
size_t vals[4] = {static_cast<uint32_t>(v.foreground), static_cast<uint32_t>(v.background),
|
|
|
|
v.valid_path, v.force_underline};
|
|
|
|
return (vals[0] << 0) + (vals[1] << 6) + (vals[2] << 12) + (vals[3] << 18);
|
|
|
|
}
|
|
|
|
};
|
2020-08-05 01:55:43 +08:00
|
|
|
} // namespace std
|
2020-08-04 08:34:27 +08:00
|
|
|
|
2012-06-29 02:54:37 +08:00
|
|
|
class history_item_t;
|
2020-01-16 09:14:47 +08:00
|
|
|
class operation_context_t;
|
2012-06-29 02:54:37 +08:00
|
|
|
|
2020-08-04 08:34:27 +08:00
|
|
|
/// Given a string and list of colors of the same size, return the string with ANSI escape sequences
|
|
|
|
/// representing the colors.
|
|
|
|
std::string colorize(const wcstring &text, const std::vector<highlight_spec_t> &colors,
|
|
|
|
const environment_t &vars);
|
2019-09-19 19:27:33 +08:00
|
|
|
|
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.
|
2020-01-16 09:14:47 +08:00
|
|
|
/// \param ctx The variables and cancellation check for this operation.
|
2020-08-04 05:10:37 +08:00
|
|
|
/// \param io_ok If set, allow IO which may block. This means that e.g. invalid commands may be
|
|
|
|
/// detected.
|
2020-08-12 06:42:22 +08:00
|
|
|
void highlight_shell(const wcstring &buffstr, std::vector<highlight_spec_t> &color,
|
2020-08-04 05:10:37 +08:00
|
|
|
const operation_context_t &ctx, bool io_ok = false);
|
2016-05-01 11:47:05 +08:00
|
|
|
|
2020-08-04 08:34:27 +08:00
|
|
|
/// highlight_color_resolver_t resolves highlight specs (like "a command") to actual RGB colors.
|
|
|
|
/// It maintains a cache with no invalidation mechanism. The lifetime of these should typically be
|
|
|
|
/// one screen redraw.
|
|
|
|
struct highlight_color_resolver_t {
|
|
|
|
/// \return an RGB color for a given highlight spec.
|
|
|
|
rgb_color_t resolve_spec(const highlight_spec_t &highlight, bool is_background,
|
|
|
|
const environment_t &vars);
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unordered_map<highlight_spec_t, rgb_color_t> fg_cache_;
|
|
|
|
std::unordered_map<highlight_spec_t, rgb_color_t> bg_cache_;
|
|
|
|
rgb_color_t resolve_spec_uncached(const highlight_spec_t &highlight, bool is_background,
|
|
|
|
const environment_t &vars) const;
|
|
|
|
};
|
2005-10-04 23:11:39 +08:00
|
|
|
|
2020-11-28 05:21:21 +08:00
|
|
|
/// Given an item \p item from the history which is a proposed autosuggestion, return whether the
|
|
|
|
/// autosuggestion is valid. It may not be valid if e.g. it is attempting to cd into a directory
|
|
|
|
/// which does not exist.
|
2016-05-01 11:47:05 +08:00
|
|
|
bool autosuggest_validate_from_history(const history_item_t &item,
|
|
|
|
const wcstring &working_directory,
|
2020-01-16 09:14:47 +08:00
|
|
|
const operation_context_t &ctx);
|
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.
|
2020-04-19 13:01:25 +08:00
|
|
|
PATH_EXPAND_TILDE = 1 << 1,
|
|
|
|
// Normalize directories before resolving, as "cd".
|
|
|
|
PATH_FOR_CD = 1 << 2,
|
2012-07-07 05:34:53 +08:00
|
|
|
};
|
|
|
|
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,
|
2020-03-03 11:47:21 +08:00
|
|
|
const operation_context_t &ctx, path_flags_t flags);
|
2012-02-19 13:56:30 +08:00
|
|
|
|
2005-10-04 23:11:39 +08:00
|
|
|
#endif
|