From 6d6f45c1e266ffb3206498301a7a6a1b1b22ec3c Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Tue, 10 Jan 2017 21:03:00 +0100 Subject: Moving up, and bugfixes - moving up! - insertion auto-scrolls *correctly* - moving down can also land *after* the line --- buffer.cpp | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/buffer.cpp b/buffer.cpp index 61cb15b..afb1c93 100644 --- a/buffer.cpp +++ b/buffer.cpp @@ -36,18 +36,20 @@ void Buffer::handleCommand(const Command &cmd){ assert(c!='\n'); tb.insert(cursor.line,cursor.x,cmd[1][0]); cursor.x++; + relayoutScreen(); if(findCursorInScreen().x==-1){ screen.erase(screen.begin()); + relayoutScreen(); } - relayoutScreen(); } else if(cmd[0]=="insert_newline"){ tb.insert(cursor.line,cursor.x,'\n'); cursor.x=0; cursor.line++; + relayoutScreen(); if(findCursorInScreen().x==-1){ screen.erase(screen.begin()); + relayoutScreen(); } - relayoutScreen(); } else if(cmd[0]=="delete_backward"){ if(cursor.x==0&&cursor.line==0){ bel(); @@ -128,7 +130,39 @@ void Buffer::handleCommand(const Command &cmd){ } cursor.line=screen[sp.y].line; if(sp.x>=(i64)screen[sp.y].cells.size()){ - cursor.x=tb.lineLen(cursor.line); + if(screen[sp.y].cells.size()==0)cursor.x=0; + else { + cursor.x=screen[sp.y].cells.back().linex; + //Now if we would've landed *after* the end of the line, go there. + if(cursor.x==tb.lineLen(cursor.line)-1)cursor.x++; + } + } else { + cursor.x=screen[sp.y].cells[sp.x].linex; + } + } else if(cmd[0]=="move_upward"){ + Screenpos sp=findCursorInScreen(); + if(sp.x==-1){ + bel(); + return; + } + if(sp.y==0){ + if(screen[sp.y].line==0&&screen[sp.y].part==0){ + if(cursor.x==0)bel(); + else cursor.x=0; + return; + } + scrollUp(); + } else { + sp.y--; + } + cursor.line=screen[sp.y].line; + if(sp.x>=(i64)screen[sp.y].cells.size()){ + if(screen[sp.y].cells.size()==0)cursor.x=0; + else { + cursor.x=screen[sp.y].cells.back().linex; + //Now if we would've landed *after* the end of the line, go there. + if(cursor.x==tb.lineLen(cursor.line)-1)cursor.x++; + } } else { cursor.x=screen[sp.y].cells[sp.x].linex; } @@ -150,7 +184,7 @@ static string showChar(char c){ } vector Buffer::layoutLine(const string &line,i64 linenum,i64 width){ - assert(width>=4); + assert(width>=5); //5 == (maximum char repr width) + 1, where the 1 is an extra column on the right if(line.size()==0){ return {{linenum,0,0,{}}}; } -- cgit v1.2.3-70-g09d2