diff options
author | tomsmeding <tom.smeding@gmail.com> | 2017-01-10 10:47:49 +0100 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2017-01-10 10:47:49 +0100 |
commit | 14cac64019b3425eb40eb82471b0995d62b9a7a2 (patch) | |
tree | f116ae11a89957c6959013af56d7bf64d1ea13c5 | |
parent | d7657709a54750766aa2803279a7353e6a7fb6df (diff) |
Auto-scroll on move_forward (and fix fCIS)
-rw-r--r-- | buffer.cpp | 27 | ||||
-rw-r--r-- | long.txt | 52 |
2 files changed, 74 insertions, 5 deletions
@@ -71,6 +71,10 @@ void Buffer::handleCommand(const Command &cmd){ } else { cursor.x++; } + if(findCursorInScreen().x==-1){ + screen.erase(screen.begin()); + relayoutScreen(); + } } else if(cmd[0]=="move_backward"){ if(cursor.line==0&&cursor.x==0){ bel(); @@ -123,7 +127,7 @@ void Buffer::handleCommand(const Command &cmd){ void Buffer::receive(const Command &cmd){ handleCommand(cmd); - cerr<<"New cursor is (line="<<cursor.line<<", x="<<cursor.x<<")"<<endl; + // cerr<<"New cursor is (line="<<cursor.line<<", x="<<cursor.x<<")"<<endl; } static string showChar(char c){ @@ -280,18 +284,31 @@ void Buffer::relayoutScreen(){ //Returns {-1,-1} if not found Buffer::Screenpos Buffer::findCursorInScreen() const { if(screen.size()==0)return {-1,-1}; + if(cursor.x>tb.lineLen(cursor.line)){ + THROW("Invalid cursor position: past end of line"); + } auto lineit=lower_bound(screen.begin(),screen.end(),cursor.line,[this](const ScreenLine &sl,i64 line){ return sl.line<line||(sl.line==line&&sl.fromx<=cursor.x); }); if(lineit==screen.begin())return {-1,-1}; - --lineit; - const i64 y=distance(screen.begin(),lineit); - const vector<Cell> &cells=lineit->cells; + + const i64 y=lineit==screen.end()?screen.size()-1:distance(screen.begin(),lineit)-1; + if(screen[y].line!=cursor.line)return {-1,-1}; //cursor is below screen on a new line + const vector<Cell> &cells=screen[y].cells; auto chrit=lower_bound(cells.begin(),cells.end(),cursor.x,[](const Cell &cell,i64 x){ return cell.linex<x; }); const i64 x=distance(cells.begin(),chrit); - return {y,x}; + assert(x<=(i64)cells.size()); + if(x<(i64)cells.size())return {y,x}; + if(cells.size()==0){ + return {y,0}; + } + if(cells.back().linex==cursor.x-1&&cursor.x==tb.lineLen(cursor.line)){ + return {y,x}; + } else { + return {-1,-1}; + } } i64 numberWidth(i64 number){ diff --git a/long.txt b/long.txt new file mode 100644 index 0000000..5047a23 --- /dev/null +++ b/long.txt @@ -0,0 +1,52 @@ +s +o +m +e +_ +g +a +r +b +a +g +e +_ +f +i +l +e +_ +w +i +t +h +_ +a +_ +r +e +a +l +l +y +_ +l +o +n +g +_ +s +e +n +t +e +n +c +e +_ +that will do nothing really +at 50: +coming: +some garbage file with a really long sentence that will definitely exceed the width of my terminal in characters and do so without the help of any punctuation mark at all +but the second line is not that bad +fortunately |