Do not register timers for transient input handlers

This commit is contained in:
Maxime Coste 2014-12-18 23:12:58 +00:00
parent abe3856d29
commit 1c8ee78d1a
11 changed files with 65 additions and 31 deletions

View File

@ -533,7 +533,7 @@ void Buffer::on_option_changed(const Option& option)
void Buffer::run_hook_in_own_context(const String& hook_name, StringView param)
{
InputHandler hook_handler({ *this, Selection{} });
InputHandler hook_handler({ *this, Selection{} }, Context::Flags::Transient);
hooks().run_hook(hook_name, param, hook_handler.context());
}

View File

@ -22,7 +22,7 @@ Client::Client(std::unique_ptr<UserInterface>&& ui,
EnvVarMap env_vars,
String name)
: m_ui{std::move(ui)}, m_window{std::move(window)},
m_input_handler{std::move(selections),
m_input_handler{std::move(selections), Context::Flags::None,
std::move(name)},
m_env_vars(env_vars)
{

View File

@ -1032,7 +1032,8 @@ void context_wrap(const ParametersParser& parser, Context& context, Func func)
for (auto& name : names)
{
Buffer& buffer = BufferManager::instance().get_buffer(name);
InputHandler input_handler{{ buffer, Selection{} }};
InputHandler input_handler{{ buffer, Selection{} },
Context::Flags::Transient};
Context& c = input_handler.context();
// Propagate user hooks disabled status to the temporary context
@ -1056,7 +1057,9 @@ void context_wrap(const ParametersParser& parser, Context& context, Func func)
if (parser.has_option("draft"))
{
InputHandler input_handler(real_context->selections(), real_context->name());
InputHandler input_handler(real_context->selections(),
Context::Flags::Transient,
real_context->name());
Context& c = input_handler.context();
// We do not want this draft context to commit undo groups if the real one is

View File

@ -13,9 +13,10 @@ Context::Context() = default;
Context::~Context() = default;
Context::Context(InputHandler& input_handler, SelectionList selections,
String name)
Flags flags, String name)
: m_input_handler{&input_handler},
m_selections{std::move(selections)},
m_flags(flags),
m_name(std::move(name))
{}

View File

@ -3,6 +3,7 @@
#include "selection.hh"
#include "optional.hh"
#include "flags.hh"
namespace Kakoune
{
@ -56,9 +57,15 @@ private:
class Context
{
public:
enum class Flags
{
None = 0,
Transient = 1,
};
Context();
Context(InputHandler& input_handler, SelectionList selections,
String name = "");
Flags flags, String name = "");
~Context();
Context(const Context&) = delete;
@ -120,6 +127,8 @@ public:
Disableable& history_support() { return m_history_support; }
const Disableable& history_support() const { return m_history_support; }
Flags flags() const { return m_flags; }
private:
void begin_edition();
void end_edition();
@ -127,6 +136,8 @@ private:
friend struct ScopedEdition;
Flags m_flags;
safe_ptr<InputHandler> m_input_handler;
safe_ptr<Window> m_window;
safe_ptr<Client> m_client;
@ -145,6 +156,9 @@ private:
Disableable m_history_support;
};
template<>
struct WithBitOps<Context::Flags> : std::true_type {};
struct ScopedEdition
{
ScopedEdition(Context& context)

View File

@ -30,18 +30,19 @@ void FDWatcher::close_fd()
Timer::Timer(TimePoint date, Callback callback, EventMode mode)
: m_date{date}, m_callback{std::move(callback)}, m_mode(mode)
{
if (EventManager::has_instance())
if (m_callback and EventManager::has_instance())
EventManager::instance().m_timers.push_back(this);
}
Timer::~Timer()
{
if (EventManager::has_instance())
if (m_callback and EventManager::has_instance())
unordered_erase(EventManager::instance().m_timers, this);
}
void Timer::run(EventMode mode)
{
kak_assert(m_callback);
if (mode == m_mode)
{
m_date = TimePoint::max();

View File

@ -52,17 +52,21 @@ static constexpr std::chrono::milliseconds fs_check_timeout{500};
class Normal : public InputMode
{
public:
Normal(InputHandler& input_handler)
Normal(InputHandler& input_handler, bool with_timers = true)
: InputMode(input_handler),
m_idle_timer{Clock::now() + idle_timeout, [this](Timer& timer) {
m_idle_timer{Clock::now() + idle_timeout,
context().flags() & Context::Flags::Transient ?
Timer::Callback() : Timer::Callback([this](Timer& timer) {
context().hooks().run_hook("NormalIdle", "", context());
}},
m_fs_check_timer{Clock::now() + fs_check_timeout, [this](Timer& timer) {
})},
m_fs_check_timer{Clock::now() + fs_check_timeout,
context().flags() & Context::Flags::Transient ?
Timer::Callback() : Timer::Callback([this](Timer& timer) {
if (not context().has_client())
return;
context().client().check_buffer_fs_timestamp();
timer.set_next_date(Clock::now() + fs_check_timeout);
}}
})}
{}
void on_enabled() override
@ -1034,9 +1038,10 @@ void InputMode::reset_normal_mode()
m_input_handler.reset_normal_mode();
}
InputHandler::InputHandler(SelectionList selections, String name)
: m_mode(new InputModes::Normal(*this)),
m_context(*this, std::move(selections), std::move(name))
InputHandler::InputHandler(SelectionList selections, Context::Flags flags, String name)
: m_context(*this, std::move(selections), flags, std::move(name)),
m_mode(new InputModes::Normal(*this))
{
}

View File

@ -38,7 +38,9 @@ enum class KeymapMode : int;
class InputHandler : public SafeCountable
{
public:
InputHandler(SelectionList selections, String name = "");
InputHandler(SelectionList selections,
Context::Flags flags = Context::Flags::None,
String name = "");
~InputHandler();
// switch to insert mode
@ -80,6 +82,7 @@ public:
DisplayLine mode_line() const;
void clear_mode_trash();
private:
Context m_context;

View File

@ -441,7 +441,10 @@ int run_filter(StringView keystr, memoryview<StringView> files, bool quiet)
{
try
{
InputHandler input_handler{{ buffer, Selection{{0,0}, buffer.back_coord()} }};
InputHandler input_handler{
{ buffer, {{0,0}, buffer.back_coord()} },
Context::Flags::Transient
};
for (auto& key : keys)
input_handler.handle_key(key);

View File

@ -21,9 +21,8 @@ Window::Window(Buffer& buffer)
: Scope(buffer),
m_buffer(&buffer)
{
InputHandler hook_handler{{ *m_buffer, Selection{} }};
hook_handler.context().set_window(*this);
hooks().run_hook("WinCreate", buffer.name(), hook_handler.context());
run_hook_in_own_context("WinCreate", buffer.name());
options().register_watcher(*this);
m_builtin_highlighters.add_child({"tabulations"_str, make_simple_highlighter(expand_tabulations)});
@ -36,9 +35,7 @@ Window::Window(Buffer& buffer)
Window::~Window()
{
InputHandler hook_handler{{ *m_buffer, Selection{} }};
hook_handler.context().set_window(*this);
hooks().run_hook("WinClose", buffer().name(), hook_handler.context());
run_hook_in_own_context("WinClose", buffer().name());
options().unregister_watcher(*this);
}
@ -266,10 +263,10 @@ ByteCoordAndTarget Window::offset_coord(ByteCoordAndTarget coord, LineCount offs
BufferRange range{ std::min(line, coord.line), std::max(line,coord.line)+1};
InputHandler hook_handler{{ *m_buffer, Selection{} } };
hook_handler.context().set_window(*this);
m_highlighters.highlight(hook_handler.context(), HighlightFlags::MoveOnly, display_buffer, range);
m_builtin_highlighters.highlight(hook_handler.context(), HighlightFlags::MoveOnly, display_buffer, range);
InputHandler input_handler{{ *m_buffer, Selection{} }, Context::Flags::Transient};
input_handler.context().set_window(*this);
m_highlighters.highlight(input_handler.context(), HighlightFlags::MoveOnly, display_buffer, range);
m_builtin_highlighters.highlight(input_handler.context(), HighlightFlags::MoveOnly, display_buffer, range);
CharCount column = coord.target == -1 ? find_display_column(lines[0], buffer(), coord) : coord.target;
return { find_buffer_coord(lines[1], buffer(), column), column };
@ -278,12 +275,17 @@ ByteCoordAndTarget Window::offset_coord(ByteCoordAndTarget coord, LineCount offs
void Window::on_option_changed(const Option& option)
{
String desc = option.name() + "=" + option.get_as_string();
InputHandler hook_handler{{ *m_buffer, Selection{} }};
hook_handler.context().set_window(*this);
hooks().run_hook("WinSetOption", desc, hook_handler.context());
run_hook_in_own_context("WinSetOption", desc);
// an highlighter might depend on the option, so we need to redraw
forget_timestamp();
}
void Window::run_hook_in_own_context(const String& hook_name, StringView param)
{
InputHandler hook_handler({ *m_buffer, Selection{} }, Context::Flags::Transient);
hook_handler.context().set_window(*this);
hooks().run_hook(hook_name, param, hook_handler.context());
}
}

View File

@ -48,6 +48,8 @@ private:
void on_option_changed(const Option& option) override;
void scroll_to_keep_selection_visible_ifn(const Context& context);
void run_hook_in_own_context(const String& hook_name, StringView param);
safe_ptr<Buffer> m_buffer;
CharCoord m_position;