Move Insertion Hooks handing to the input handler

This commit is contained in:
Maxime Coste 2013-01-17 13:58:57 +01:00
parent cedb0d18a3
commit c45838cc57
6 changed files with 21 additions and 21 deletions

View File

@ -408,7 +408,6 @@ using utf8_it = utf8::utf8_iterator<BufferIterator, utf8::InvalidBytePolicy::Pas
IncrementalInserter::IncrementalInserter(Editor& editor, InsertMode mode)
: m_editor(editor), m_edition(editor), m_mode(mode)
{
m_editor.on_incremental_insertion_begin();
Buffer& buffer = *editor.m_buffer;
if (mode == InsertMode::Replace)
@ -488,8 +487,6 @@ IncrementalInserter::~IncrementalInserter()
sel.last() = utf8::previous(sel.last());
sel.avoid_eol();
}
m_editor.on_incremental_insertion_end();
}
void IncrementalInserter::insert(String content)

View File

@ -96,8 +96,6 @@ private:
void check_invariant() const;
friend class IncrementalInserter;
virtual void on_incremental_insertion_begin() {}
virtual void on_incremental_insertion_end() {}
safe_ptr<Buffer> m_buffer;
DynamicSelectionList m_selections;

View File

@ -31,6 +31,8 @@ private:
namespace InputModes
{
static constexpr std::chrono::milliseconds idle_timeout{100};
class Normal : public InputMode
{
public:
@ -513,12 +515,10 @@ private:
class Insert : public InputMode
{
public:
static constexpr std::chrono::milliseconds idle_timeout(){ return std::chrono::milliseconds{100}; }
Insert(Context& context, InsertMode mode)
: InputMode(context.input_handler()),
m_inserter(context.editor(), mode),
m_idle_timer{Clock::time_point::max(),
m_idle_timer{Clock::now() + idle_timeout,
[this, &context](Timer& timer) {
context.hooks().run_hook("InsertIdle", "", context);
m_completer.reset(context);
@ -532,7 +532,7 @@ public:
{
context.last_insert().first = mode;
context.last_insert().second.clear();
m_idle_timer.set_next_date(Clock::now() + idle_timeout());
context.hooks().run_hook("InsertBegin", "", context);
}
void on_key(const Key& key, Context& context) override
@ -546,8 +546,10 @@ public:
return;
}
bool reset_completer = true;
bool moved = false;
if (key == Key::Escape or key == Key{ Key::Modifiers::Control, 'c' })
{
context.hooks().run_hook("InsertEnd", "", context);
m_completer.reset(context);
reset_normal_mode();
}
@ -556,11 +558,20 @@ public:
else if (key == Key::Left)
m_inserter.move_cursors(-1_char);
else if (key == Key::Right)
{
m_inserter.move_cursors(1_char);
moved = true;
}
else if (key == Key::Up)
{
m_inserter.move_cursors(-1_line);
moved = true;
}
else if (key == Key::Down)
{
m_inserter.move_cursors(1_line);
moved = true;
}
else if (key.modifiers == Key::Modifiers::None)
{
m_inserter.insert(codepoint_to_str(key.key));
@ -585,8 +596,10 @@ public:
if (reset_completer)
{
// m_completer.reset(context);
m_idle_timer.set_next_date(Clock::now() + idle_timeout());
m_idle_timer.set_next_date(Clock::now() + idle_timeout);
}
if (moved)
context.hooks().run_hook("InsertMove", "", context);
}
private:
bool m_insert_reg = false;

View File

@ -22,7 +22,7 @@ hook global WinSetOption filetype=cpp %~
addfilter -group cpp-filters regex ^(\h+)([^\n]*[^([{]\h*|$) \n \n$1
addfilter -group cpp-filters regex ^(\h*)[^\n]*[([{]\h* \n '\n$1 '
addfilter -group cpp-filters cleanup_whitespaces
hook window InsertEnd .* %{ exec xs\h+(?=\n)<ret>d }
hook window InsertEnd .* %{ exec -restore-selections <a-x>s\h+$<ret>d }
~
hook global WinSetOption filetype=(?!cpp).* %{

View File

@ -176,13 +176,6 @@ String Window::status_line() const
return oss.str();
}
void Window::on_incremental_insertion_end()
{
DynamicSelectionList backup(buffer(), selections());
hooks().run_hook("InsertEnd", "", Context(*this));
select((SelectionList)backup);
}
void Window::on_option_changed(const String& name, const Option& option)
{
String desc = name + "=" + option.as_string();

View File

@ -53,7 +53,6 @@ public:
private:
Window(const Window&) = delete;
void on_incremental_insertion_end() override;
void on_option_changed(const String& name, const Option& option) override;
void scroll_to_keep_cursor_visible_ifn();