Add support for the erase key in prompt and insert mode

Fixes #18
This commit is contained in:
Maxime Coste 2014-01-04 18:18:59 +00:00
parent 39bc83c8c1
commit feaf197cae
3 changed files with 26 additions and 13 deletions

View File

@ -128,6 +128,12 @@ public:
--m_cursor_pos;
}
}
else if (key == Key::Erase)
{
if (m_cursor_pos != m_line.char_length())
m_line = m_line.substr(0, m_cursor_pos)
+ m_line.substr(m_cursor_pos+1);
}
else
{
m_line = m_line.substr(0, m_cursor_pos) + codepoint_to_str(key.key)
@ -904,6 +910,7 @@ public:
void on_key(Key key) override
{
auto& buffer = context().buffer();
last_insert().second.push_back(key);
if (m_mode == Mode::InsertReg)
{
@ -935,7 +942,23 @@ public:
reset_normal_mode();
}
else if (key == Key::Backspace)
erase();
{
for (auto& sel : context().selections())
{
if (sel.last() == BufferCoord{0,0})
continue;
auto pos = buffer.iterator_at(sel.last());
buffer.erase(utf8::previous(pos), pos);
}
}
else if (key == Key::Erase)
{
for (auto& sel : context().selections())
{
auto pos = buffer.iterator_at(sel.last());
buffer.erase(pos, utf8::next(pos));
}
}
else if (key == Key::Left)
{
move(-1_char);
@ -995,18 +1018,6 @@ public:
KeymapMode keymap_mode() const override { return KeymapMode::Insert; }
private:
void erase() const
{
auto& buffer = context().buffer();
for (auto& sel : context().selections())
{
if (sel.last() == BufferCoord{0,0})
continue;
auto pos = buffer.iterator_at(sel.last());
buffer.erase(utf8::previous(pos), pos);
}
}
template<typename Type>
void move(Type offset)
{

View File

@ -22,6 +22,7 @@ struct Key
{
// use UTF-16 surrogate pairs range
Backspace = 0xD800,
Erase,
Escape,
Up,
Down,

View File

@ -343,6 +343,7 @@ Key NCursesUI::get_key()
else switch (c)
{
case KEY_BACKSPACE: case 127: return Key::Backspace;
case KEY_DC: return Key::Erase;
case KEY_UP: return Key::Up;
case KEY_DOWN: return Key::Down;
case KEY_LEFT: return Key::Left;