summaryrefslogtreecommitdiff
path: root/buffer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'buffer.cpp')
-rw-r--r--buffer.cpp34
1 files changed, 32 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,' ');