LibLine: Allow the user to override (or add) keybinds in the config file

This commit is contained in:
AnotherTest 2020-08-17 21:08:52 +04:30 committed by Andreas Kling
parent 32839d40e3
commit 5b9d563b4b
Notes: sideshowbarker 2024-07-19 03:27:48 +09:00
2 changed files with 84 additions and 32 deletions

View file

@ -25,6 +25,7 @@
*/
#include "Editor.h"
#include <AK/GenericLexer.h>
#include <AK/JsonObject.h>
#include <AK/StringBuilder.h>
#include <AK/Utf32View.h>
@ -71,6 +72,52 @@ Configuration Configuration::from_config(const StringView& libname)
else
configuration.set(Configuration::OperationMode::Unset);
// Read keybinds.
for (auto& binding_key : config_file->keys("keybinds")) {
GenericLexer key_lexer(binding_key);
auto has_ctrl = false;
auto alt = false;
unsigned key = 0;
while (!key && !key_lexer.is_eof()) {
if (key_lexer.next_is("alt+")) {
alt = key_lexer.consume_specific("alt+");
continue;
}
if (key_lexer.next_is("^[")) {
alt = key_lexer.consume_specific("^[");
continue;
}
if (key_lexer.next_is("^")) {
has_ctrl = key_lexer.consume_specific("^");
continue;
}
if (key_lexer.next_is("ctrl+")) {
has_ctrl = key_lexer.consume_specific("ctrl+");
continue;
}
// FIXME: Support utf?
key = key_lexer.consume();
}
if (has_ctrl)
key = ctrl(key);
auto value = config_file->read_entry("keybinds", binding_key);
if (value.starts_with("internal:")) {
configuration.set(KeyBinding {
Key { key, alt ? Key::Alt : Key::None },
KeyBinding::Kind::InternalFunction,
value.substring(9, value.length() - 9) });
} else {
configuration.set(KeyBinding {
Key { key, alt ? Key::Alt : Key::None },
KeyBinding::Kind::Insertion,
value });
}
}
return configuration;
}
@ -118,6 +165,9 @@ Editor::Editor(Configuration configuration)
m_suggestion_display = make<XtermSuggestionDisplay>(m_num_lines, m_num_columns);
set_default_keybinds();
for (auto& keybind : m_configuration.keybindings)
register_key_input_callback(keybind);
}
Editor::~Editor()

View file

@ -53,38 +53,6 @@
namespace Line {
struct Configuration {
enum RefreshBehaviour {
Lazy,
Eager,
};
enum OperationMode {
Unset,
Full,
NoEscapeSequences,
NonInteractive,
};
Configuration()
{
}
template<typename Arg, typename... Rest>
Configuration(Arg arg, Rest... rest)
: Configuration(rest...)
{
set(arg);
}
void set(RefreshBehaviour refresh) { refresh_behaviour = refresh; }
void set(OperationMode mode) { operation_mode = mode; }
static Configuration from_config(const StringView& libname = "line");
RefreshBehaviour refresh_behaviour { RefreshBehaviour::Lazy };
OperationMode operation_mode { OperationMode::Unset };
};
struct Key {
enum Modifier : int {
None = 0,
@ -119,6 +87,40 @@ struct KeyBinding {
String binding;
};
struct Configuration {
enum RefreshBehaviour {
Lazy,
Eager,
};
enum OperationMode {
Unset,
Full,
NoEscapeSequences,
NonInteractive,
};
Configuration()
{
}
template<typename Arg, typename... Rest>
Configuration(Arg arg, Rest... rest)
: Configuration(rest...)
{
set(arg);
}
void set(RefreshBehaviour refresh) { refresh_behaviour = refresh; }
void set(OperationMode mode) { operation_mode = mode; }
void set(const KeyBinding& binding) { keybindings.append(binding); }
static Configuration from_config(const StringView& libname = "line");
RefreshBehaviour refresh_behaviour { RefreshBehaviour::Lazy };
OperationMode operation_mode { OperationMode::Unset };
Vector<KeyBinding> keybindings;
};
#define ENUMERATE_EDITOR_INTERNAL_FUNCTIONS(M) \
M(clear_screen) \
M(cursor_left_character) \