From 8f3e73313ea0937935d95af7a5d265eaf6009580 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Tue, 10 Jan 2017 20:32:46 +0100 Subject: Backscrolling and misc future bugfixes --- buffer.cpp | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) (limited to 'buffer.cpp') diff --git a/buffer.cpp b/buffer.cpp index 1ace091..61cb15b 100644 --- a/buffer.cpp +++ b/buffer.cpp @@ -28,16 +28,25 @@ void Buffer::handleCommand(const Command &cmd){ return; } tb.read(file); + cursor.line=0; + cursor.x=0; + screen.clear(); } else if(cmd[0]=="insert_char"){ char c=cmd[1][0]; assert(c!='\n'); tb.insert(cursor.line,cursor.x,cmd[1][0]); cursor.x++; + if(findCursorInScreen().x==-1){ + screen.erase(screen.begin()); + } relayoutScreen(); } else if(cmd[0]=="insert_newline"){ tb.insert(cursor.line,cursor.x,'\n'); cursor.x=0; cursor.line++; + if(findCursorInScreen().x==-1){ + screen.erase(screen.begin()); + } relayoutScreen(); } else if(cmd[0]=="delete_backward"){ if(cursor.x==0&&cursor.line==0){ @@ -52,7 +61,11 @@ void Buffer::handleCommand(const Command &cmd){ cursor.x=tb.lineLen(cursor.line); tb.erase(cursor.line,cursor.x); } - relayoutScreen(); + if(findCursorInScreen().x==-1){ + scrollUp(); + } else { + relayoutScreen(); + } } else if(cmd[0]=="delete_forward"){ if(cursor.line==tb.numLines()-1&&cursor.x==tb.lineLen(cursor.line)){ bel(); @@ -86,6 +99,9 @@ void Buffer::handleCommand(const Command &cmd){ } else { cursor.x--; } + if(findCursorInScreen().x==-1){ + scrollUp(); + } } else if(cmd[0]=="move_downward"){ Screenpos sp=findCursorInScreen(); if(sp.x==-1){ @@ -110,10 +126,6 @@ void Buffer::handleCommand(const Command &cmd){ } else { sp.y++; } - if(sp.y>=(i64)screen.size()){ - bel(); - return; - } cursor.line=screen[sp.y].line; if(sp.x>=(i64)screen[sp.y].cells.size()){ cursor.x=tb.lineLen(cursor.line); @@ -275,12 +287,33 @@ void Buffer::renewLayout(i64 topLine,i64 topPart,i64 width,i64 height){ if((i64)screen.size()>height){ screen.resize(height); } + + if(findCursorInScreen().x==-1){ //We lost the cursor! Let's find it again + performInitialLayout(width,height); + cerr<<"Re-performing initial layout because cursor was lost in renewLayout"<0){ + renewLayout(screen[0].line,screen[0].part-1,lastWidth,lastHeight); + return; + } + if(screen[0].line==0){ + bel(); + return; + } + + i64 topLine=screen[0].line-1; + screen=layoutLine(tb.getLine(topLine),topLine,lastWidth); + renewLayout(screen.back().line,screen.back().part,lastWidth,lastHeight); +} + //Returns {-1,-1} if not found Buffer::Screenpos Buffer::findCursorInScreen() const { if(screen.size()==0)return {-1,-1}; @@ -325,7 +358,7 @@ void Buffer::show(i64 atx,i64 aty,i64 width,i64 height){ i64 gutterWidth=numberWidth(tb.numLines())+2; i64 newWidth=width-gutterWidth; - if(newWidth!=lastWidth||height!=lastHeight){ + if(newWidth!=lastWidth||height!=lastHeight||screen.size()==0){ if(lastWidth==-1)performInitialLayout(newWidth,height); else renewLayout(screen[0].line,screen[0].part,newWidth,height); lastWidth=newWidth; -- cgit v1.2.3-54-g00ecf