parent
b509cb6d50
commit
9e27520ab1
7 changed files with 167 additions and 97 deletions
@ -0,0 +1,94 @@ |
|||||||
|
#include <modes/Insert.hpp> |
||||||
|
#include <Editor.hpp> |
||||||
|
#include <iostream> |
||||||
|
|
||||||
|
namespace groove { |
||||||
|
namespace modes { |
||||||
|
|
||||||
|
void Insert::input(int c) { |
||||||
|
switch (c) { |
||||||
|
case 27: |
||||||
|
this->editor.mode_ = groove::Mode::EDIT; |
||||||
|
this->editor.x = 0; |
||||||
|
this->editor.y = 0; |
||||||
|
break; |
||||||
|
case KEY_ENTER: |
||||||
|
case 10: |
||||||
|
{ |
||||||
|
std::string appendix; |
||||||
|
appendix = ""; |
||||||
|
if (this->editor.buffer->at(this->editor.y).size() > this->editor.x) { |
||||||
|
appendix = this->editor.buffer->at(this->editor.y).substr(this->editor.x, this->editor.buffer->at(this->editor.y).size()); |
||||||
|
this->editor.buffer->at(this->editor.y) = this->editor.buffer->at(this->editor.y).substr(0, this->editor.x); |
||||||
|
} |
||||||
|
this->editor.buffer->insert(this->editor.y); |
||||||
|
this->editor.y++; |
||||||
|
this->editor.buffer->linebuffer().at(this->editor.y) += appendix; |
||||||
|
this->editor.x = 0; |
||||||
|
this->editor.scrollDown(); |
||||||
|
} |
||||||
|
break; |
||||||
|
case KEY_BACKSPACE: |
||||||
|
if (this->editor.x - 1 >= 0) { |
||||||
|
this->editor.x--; |
||||||
|
this->editor.buffer->remove(this->editor.y, this->editor.x); |
||||||
|
} |
||||||
|
else { |
||||||
|
if (this->editor.buffer->at(this->editor.y).size() > 0 && this->editor.y > 0) { |
||||||
|
int oldLength = static_cast<int>(this->editor.buffer->at(this->editor.y - 1).size()); |
||||||
|
this->editor.buffer->at(this->editor.y - 1) += this->editor.buffer->at(this->editor.y); |
||||||
|
this->editor.buffer->remove(this->editor.y); |
||||||
|
this->editor.y--; |
||||||
|
this->editor.x = oldLength; |
||||||
|
} |
||||||
|
else { |
||||||
|
if (this->editor.y - 1 >= 0) { |
||||||
|
this->editor.buffer->remove(this->editor.y); |
||||||
|
this->editor.y--; |
||||||
|
this->editor.x = static_cast<int>(this->editor.buffer->at(this->editor.y).length()); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
this->editor.scrollUp(); |
||||||
|
break; |
||||||
|
case KEY_DC: |
||||||
|
if (this->editor.buffer->at(this->editor.y).length() > this->editor.x) |
||||||
|
this->editor.buffer->deleteChar(this->editor.y, this->editor.x); |
||||||
|
else { |
||||||
|
if (this->editor.buffer->at(this->editor.y).empty() && this->editor.buffer->linebuffer().size() - 1 > this->editor.y) |
||||||
|
this->editor.buffer->remove(this->editor.y); |
||||||
|
else if (this->editor.buffer->linebuffer().size() - 1 > this->editor.y) { |
||||||
|
this->editor.buffer->at(this->editor.y) += this->editor.buffer->at(this->editor.y + 1); |
||||||
|
this->editor.buffer->remove(this->editor.y + 1); |
||||||
|
} |
||||||
|
} |
||||||
|
break; |
||||||
|
case KEY_BTAB: |
||||||
|
case KEY_CTAB: |
||||||
|
case KEY_STAB: |
||||||
|
case KEY_CATAB: |
||||||
|
case 9: |
||||||
|
this->editor.buffer->at(this->editor.y).insert(this->editor.x, 4, ' '); |
||||||
|
this->editor.x += 4; |
||||||
|
break; |
||||||
|
default: |
||||||
|
{ |
||||||
|
switch (c) { |
||||||
|
case '{': |
||||||
|
case '[': |
||||||
|
this->editor.buffer->at(this->editor.y).insert(this->editor.x, 1, c + 2); |
||||||
|
break; |
||||||
|
case '(': |
||||||
|
this->editor.buffer->at(this->editor.y).insert(this->editor.x, 1, c + 1); |
||||||
|
break; |
||||||
|
} |
||||||
|
this->editor.buffer->at(this->editor.y).insert(this->editor.x, 1, char(c)); |
||||||
|
if (this->editor.lastChar != 195 && this->editor.lastChar != 182) |
||||||
|
this->editor.x++; |
||||||
|
this->editor.lastChar = char(c); |
||||||
|
} |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,18 @@ |
|||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <modes/Mode.hpp> |
||||||
|
|
||||||
|
namespace groove { |
||||||
|
namespace modes { |
||||||
|
|
||||||
|
class Insert : public Mode { |
||||||
|
public: |
||||||
|
using Mode::Mode; |
||||||
|
std::string status() { |
||||||
|
return this->status_; |
||||||
|
} |
||||||
|
void input(int c); |
||||||
|
}; |
||||||
|
|
||||||
|
} |
||||||
|
} |
@ -0,0 +1,9 @@ |
|||||||
|
#include <modes/Mode.hpp> |
||||||
|
|
||||||
|
namespace groove { |
||||||
|
namespace modes { |
||||||
|
|
||||||
|
Mode::~Mode() {} |
||||||
|
|
||||||
|
} |
||||||
|
} |
@ -0,0 +1,22 @@ |
|||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <string> |
||||||
|
|
||||||
|
namespace groove { |
||||||
|
class Editor; |
||||||
|
|
||||||
|
namespace modes { |
||||||
|
|
||||||
|
class Mode { |
||||||
|
protected: |
||||||
|
groove::Editor& editor; |
||||||
|
std::string status_; |
||||||
|
public: |
||||||
|
Mode(groove::Editor& editor) : editor(editor) , status_("") {} |
||||||
|
virtual ~Mode() = 0; |
||||||
|
virtual std::string status() = 0; |
||||||
|
virtual void input(int c) = 0; |
||||||
|
}; |
||||||
|
|
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue