From 99142b0661ba8f54e1ff8dda035c67be54cac6a6 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Mon, 9 Jan 2017 14:16:28 +0100 Subject: Backspace/delete and end-of-line cursor fix --- buffer.cpp | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) (limited to 'buffer.cpp') 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)){ + if(screenX==atx+width)curScreenX=screenX-1; + else curScreenX=screenX; + curScreenY=aty+y; + } } setstyle(&textStyle); if(y