summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-01-10 20:32:46 +0100
committertomsmeding <tom.smeding@gmail.com>2017-01-10 20:36:04 +0100
commit8f3e73313ea0937935d95af7a5d265eaf6009580 (patch)
tree97a330534c0a9c8bfc944ce3359838d695f1c5f5
parent14cac64019b3425eb40eb82471b0995d62b9a7a2 (diff)
Backscrolling and misc future bugfixes
-rw-r--r--buffer.cpp45
-rw-r--r--buffer.h1
2 files changed, 40 insertions, 6 deletions
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"<<endl;
+ }
}
void Buffer::relayoutScreen(){
renewLayout(screen[0].line,screen[0].part,lastWidth,lastHeight);
}
+void Buffer::scrollUp(){
+ assert(screen.size()!=0);
+ if(screen[0].part>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;
diff --git a/buffer.h b/buffer.h
index 4c15703..64d7aa4 100644
--- a/buffer.h
+++ b/buffer.h
@@ -42,6 +42,7 @@ class Buffer{
void performInitialLayout(i64 width,i64 height);
void renewLayout(i64 topLine,i64 topPart,i64 width,i64 height);
void relayoutScreen();
+ void scrollUp();
Screenpos findCursorInScreen() const;