summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-01-09 14:16:28 +0100
committertomsmeding <tom.smeding@gmail.com>2017-01-09 14:16:28 +0100
commit99142b0661ba8f54e1ff8dda035c67be54cac6a6 (patch)
treefb74342482890cf33b644c6cde62b465d83ef0de
parentde013a7b1e16f021b3bc97818a5d7007ea0a92e9 (diff)
Backspace/delete and end-of-line cursor fix
-rw-r--r--buffer.cpp34
-rw-r--r--config.cpp2
2 files changed, 34 insertions, 2 deletions
diff --git a/buffer.cpp b/buffer.cpp
index f213e2f..cc93e00 100644
--- a/buffer.cpp
+++ b/buffer.cpp
@@ -16,6 +16,8 @@ Buffer::Buffer(Manager *manager)
:manager(manager){}
void Buffer::receive(const Command &cmd){
+ //TODO: optimise the renewLayout's in this function
+
if(cmd[0]=="open_file"){
const string &fname=cmd[1];
filename=fname;
@@ -33,17 +35,37 @@ void Buffer::receive(const Command &cmd){
tb.insert(cursor.line,cursor.x,cmd[1][0]);
cursor.x++;
}
- //TODO: optimise this
renewLayout(screen[0].line,screen[0].part,lastWidth,lastHeight);
} else if(cmd[0]=="insert_newline"){
tb.insert(cursor.line,cursor.x,'\n');
cursor.x=0;
cursor.line++;
- //TODO: optimise this
renewLayout(screen[0].line,screen[0].part,lastWidth,lastHeight);
+ } else if(cmd[0]=="delete_backward"){
+ if(cursor.x==0&&cursor.line==0){
+ bel();
+ } else {
+ if(cursor.x>0){
+ cursor.x--;
+ tb.erase(cursor.line,cursor.x);
+ } else {
+ cursor.line--;
+ cursor.x=tb.lineLen(cursor.line);
+ tb.erase(cursor.line,cursor.x);
+ }
+ renewLayout(screen[0].line,screen[0].part,lastWidth,lastHeight);
+ }
+ } else if(cmd[0]=="delete_forward"){
+ if(cursor.line==tb.numLines()-1&&cursor.x==tb.lineLen(cursor.line)){
+ bel();
+ } else {
+ tb.erase(cursor.line,cursor.x);
+ renewLayout(screen[0].line,screen[0].part,lastWidth,lastHeight);
+ }
} else {
THROW("Unknown command");
}
+ cerr<<"New cursor is (line="<<cursor.line<<", x="<<cursor.x<<")"<<endl;
}
static string showChar(char c){
@@ -234,6 +256,14 @@ void Buffer::show(i64 atx,i64 aty,i64 width,i64 height){
}
screenX++;
}
+
+ //Maybe the cursor should be positioned AFTER this line
+ if(curScreenX==-1&&screen[y].line==cursor.line&&cursor.x==tb.lineLen(cursor.line)&&
+ (y==(i64)screen.size()-1||screen[y+1].line>cursor.line)){
+ if(screenX==atx+width)curScreenX=screenX-1;
+ else curScreenX=screenX;
+ curScreenY=aty+y;
+ }
}
setstyle(&textStyle);
if(y<height)fillrect(atx,aty+y,width,height-y,' ');
diff --git a/config.cpp b/config.cpp
index 0f57f3a..4fe5de9 100644
--- a/config.cpp
+++ b/config.cpp
@@ -9,6 +9,8 @@ using namespace std;
Keybindings global_keybindings={
{KEY_CTRL+'Q',{"quit_app"}},
{'\n',{"insert_newline"}},
+ {KEY_BACKSPACE,{"delete_backward"}},
+ {KEY_DELETE,{"delete_forward"}},
};
class Init{public: Init(){