Change autoinfo option to be a flags option, document flags options

Support the value1|value2|value3 syntax for flag options.
This commit is contained in:
Maxime Coste 2015-11-18 23:43:51 +00:00
parent 5c37f0dd5e
commit 9656f088e7
7 changed files with 60 additions and 14 deletions

View File

@ -726,8 +726,6 @@ Options are typed, their type can be
* `int`: an integer number * `int`: an integer number
* `bool`: a boolean value, `yes/true` or `no/false` * `bool`: a boolean value, `yes/true` or `no/false`
* `yesnoask`: similar to a boolean, but the additional
value `ask` is supported.
* `str`: a string, some freeform text * `str`: a string, some freeform text
* `coord`: a line,column pair (separated by comma) * `coord`: a line,column pair (separated by comma)
* `regex`: as a string but the `set` commands will complain * `regex`: as a string but the `set` commands will complain
@ -735,6 +733,9 @@ Options are typed, their type can be
* `{int,str}-list`: a list, elements are separated by a colon (:) * `{int,str}-list`: a list, elements are separated by a colon (:)
if an element needs to contain a colon, it can be escaped with a if an element needs to contain a colon, it can be escaped with a
backslash. backslash.
* `enum(value1|value2|...)`: an enum, taking on of the given values
* `flags(value1|value2|...)`: a set of flags, taking a combination
of the given values joined by `|`.
Options value can be changed using the `set` commands: Options value can be changed using the `set` commands:
@ -781,7 +782,8 @@ Some options are built in Kakoune, and can be used to control it's behaviour:
candidates exist, enable completion with common prefix. candidates exist, enable completion with common prefix.
* `incsearch` _bool_: execute search as it is typed * `incsearch` _bool_: execute search as it is typed
* `aligntab` _bool_: use tabs for alignement command * `aligntab` _bool_: use tabs for alignement command
* `autoinfo` _bool_: display automatic information box for certain commands. * `autoinfo` _flags(command|onkey|normal)_: display automatic information
box in the enabled contexts.
* `autoshowcompl` _bool_: automatically display possible completions when * `autoshowcompl` _bool_: automatically display possible completions when
editing a prompt. editing a prompt.
* `ignored_files` _regex_: filenames matching this regex wont be considered * `ignored_files` _regex_: filenames matching this regex wont be considered
@ -804,7 +806,7 @@ Some options are built in Kakoune, and can be used to control it's behaviour:
element of the list should follow the format: element of the list should follow the format:
_<line>.<column>[+<length>]@<timestamp>_ to define where the completion _<line>.<column>[+<length>]@<timestamp>_ to define where the completion
apply in the buffer, and the other strings are the candidates. apply in the buffer, and the other strings are the candidates.
* `autoreload` _yesnoask_: auto reload the buffers when an external * `autoreload` _enum(yes|no|ask)_: auto reload the buffers when an external
modification is detected. modification is detected.
* `modelinefmt` _string_: A format string used to generate the mode line, that * `modelinefmt` _string_: A format string used to generate the mode line, that
string is first expanded as a command line would be (expanding `%...{...}` string is first expanded as a command line would be (expanding `%...{...}`

View File

@ -1221,7 +1221,7 @@ void context_wrap(const ParametersParser& parser, Context& context, Func func)
{ {
// Disable these options to avoid costly code paths (and potential screen // Disable these options to avoid costly code paths (and potential screen
// redraws) That are useful only in interactive contexts. // redraws) That are useful only in interactive contexts.
DisableOption<int> disable_autoinfo(context, "autoinfo"); DisableOption<AutoInfo> disable_autoinfo(context, "autoinfo");
DisableOption<bool> disable_autoshowcompl(context, "autoshowcompl"); DisableOption<bool> disable_autoshowcompl(context, "autoshowcompl");
DisableOption<bool> disable_incsearch(context, "incsearch"); DisableOption<bool> disable_incsearch(context, "incsearch");

View File

@ -251,7 +251,8 @@ public:
{ return lhs.key < rhs; }); { return lhs.key < rhs; });
if (it != keymap.end() and it->key == key) if (it != keymap.end() and it->key == key)
{ {
if (context().options()["autoinfo"].get<int>() >= 2 and context().has_ui()) auto autoinfo = context().options()["autoinfo"].get<AutoInfo>();
if (autoinfo & AutoInfo::Normal and context().has_ui())
context().ui().info_show(key_to_str(key), it->docstring, CharCoord{}, context().ui().info_show(key_to_str(key), it->docstring, CharCoord{},
get_face("Information"), InfoStyle::Prompt); get_face("Information"), InfoStyle::Prompt);
@ -1413,13 +1414,15 @@ DisplayLine InputHandler::mode_line() const
return current_mode().mode_line(); return current_mode().mode_line();
} }
bool show_auto_info_ifn(StringView title, StringView info, AutoInfo mask, const Context& context)
bool show_auto_info_ifn(StringView title, StringView info, const Context& context)
{ {
if (context.options()["autoinfo"].get<int>() < 1 or not context.has_ui()) if ((context.options()["autoinfo"].get<AutoInfo>() & mask) or
not context.has_ui())
return false; return false;
Face face = get_face("Information"); Face face = get_face("Information");
context.ui().info_show(title, info, CharCoord{}, face, InfoStyle::Prompt); context.ui().info_show(title, info, CharCoord{}, face, InfoStyle::Prompt);
return true; return true;
} }
} }

View File

@ -103,13 +103,13 @@ private:
int m_handle_key_level = 0; int m_handle_key_level = 0;
}; };
bool show_auto_info_ifn(StringView title, StringView info, const Context& context); bool show_auto_info_ifn(StringView title, StringView info, AutoInfo mask, const Context& context);
template<typename Cmd> template<typename Cmd>
void on_next_key_with_autoinfo(const Context& context, KeymapMode keymap_mode, Cmd cmd, void on_next_key_with_autoinfo(const Context& context, KeymapMode keymap_mode, Cmd cmd,
StringView title, StringView info) StringView title, StringView info)
{ {
const bool hide = show_auto_info_ifn(title, info, context); const bool hide = show_auto_info_ifn(title, info, AutoInfo::OnKey, context);
context.input_handler().on_next_key( context.input_handler().on_next_key(
keymap_mode, [hide,cmd](Key key, Context& context) mutable { keymap_mode, [hide,cmd](Key key, Context& context) mutable {
if (hide) if (hide)

View File

@ -201,7 +201,7 @@ void register_options()
true); true);
reg.declare_option("autoinfo", reg.declare_option("autoinfo",
"automatically display contextual help", "automatically display contextual help",
1); AutoInfo::Command | AutoInfo::OnKey);
reg.declare_option("autoshowcompl", reg.declare_option("autoshowcompl",
"automatically display possible completions for prompts", "automatically display possible completions for prompts",
true); true);

View File

@ -349,7 +349,8 @@ void command(Context& context, NormalParams params)
if (context.has_ui()) if (context.has_ui())
{ {
context.ui().info_hide(); context.ui().info_hide();
if (event == PromptEvent::Change and context.options()["autoinfo"].get<int>() > 0) auto autoinfo = context.options()["autoinfo"].get<AutoInfo>();
if (event == PromptEvent::Change and autoinfo & AutoInfo::Command)
{ {
Face face = get_face("Information"); Face face = get_face("Information");
if (cmdline.length() == 1 and is_horizontal_blank(cmdline[0_byte])) if (cmdline.length() == 1 and is_horizontal_blank(cmdline[0_byte]))

View File

@ -7,6 +7,7 @@
#include "coord.hh" #include "coord.hh"
#include "array_view.hh" #include "array_view.hh"
#include "id_map.hh" #include "id_map.hh"
#include "flags.hh"
#include <tuple> #include <tuple>
#include <vector> #include <vector>
@ -222,6 +223,45 @@ inline void option_from_string(StringView str, YesNoAsk& opt)
throw runtime_error(format("invalid value '{}', expected yes, no or ask", str)); throw runtime_error(format("invalid value '{}', expected yes, no or ask", str));
} }
enum class AutoInfo
{
None = 0,
Command = 1 << 0,
OnKey = 1 << 1,
Normal = 1 << 2
};
template<>
struct WithBitOps<AutoInfo> : std::true_type {};
inline String option_to_string(AutoInfo opt)
{
String res;
if (opt & AutoInfo::Command)
res = "command";
if (opt & AutoInfo::OnKey)
res += res.empty() ? "onkey" : "|onkey";
if (opt & AutoInfo::Normal)
res += res.empty() ? "normal" : "|normal";
return res;
}
inline void option_from_string(StringView str, AutoInfo& opt)
{
opt = AutoInfo::None;
for (auto s : split(str, '|'))
{
if (s == "command")
opt |= AutoInfo::Command;
else if (s == "onkey")
opt |= AutoInfo::OnKey;
else if (s == "normal")
opt |= AutoInfo::Normal;
else
throw runtime_error(format("invalid value '{}'", s));
}
}
} }
#endif // option_types_hh_INCLUDED #endif // option_types_hh_INCLUDED