summaryrefslogtreecommitdiff
path: root/buffer.cpp
blob: e32492b5499285ddf06ab4c04f8cf7f1a5a3fd33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <stdexcept>
#include <fstream>
#include <cassert>
#include <cmath>
#include <termio.h>

#include "buffer.h"
#include "manager.h"
#include "throw.h"

using namespace std;


Buffer::Buffer(Manager *manager)
	:manager(manager){}

void Buffer::receive(const Command &cmd){
	if(cmd[0]=="open_file"){
		const string &fname=cmd[1];
		filename=fname;
		ifstream file(fname);
		if(!file){
			manager->receive({"error","Cannot open file '"+fname+"'"});
			return;
		}
		tb.read(file);
	} else if(cmd[0]=="insert_char"){
		char c=cmd[1][0];
		if(c=='\n'){
			assert(false);
		} else {
			tb.insert(cursor.line,cursor.x,cmd[1][0]);
			cursor.x++;
		}
		//TODO: optimise this
		renewLayout(screen[0].line,screen[0].part,lastWidth,lastHeight);
	} else {
		THROW("Unknown command");
	}
}

static string showChar(char c){
	if(c>=32&&c<127)return {c};
	return {'\\','x',
	        "0123456789abcdef"[(unsigned char)c/16],
	        "0123456789abcdef"[(unsigned char)c%16]};
}

/*static i64 charWidth(char c){
	return showChar(c).size();
}*/

vector<Buffer::ScreenLine> Buffer::layoutLine(const string &line,i64 linenum,i64 width){
	assert(width>=4);
	vector<ScreenLine> layout;
	vector<Cell> cur;
	i64 x=0;
	i64 linepart=0,fromx=0;
	for(i64 i=0;i<(i64)line.size();i++){
		char c=line[i];
		string repr=showChar(c);
		assert(repr.size()>0);
		if(x+(i64)repr.size()>=width){
			layout.push_back({linenum,linepart,fromx,move(cur)});
			cur.clear();
			linepart++;
			fromx=i;
			x=0;
		}
		Cell cell={'\0',repr.size()!=1};
		for(char rc : repr){
			cell.c=rc;
			cur.push_back(cell);
			x++;
		}
	}
	if(cur.size()>0)layout.push_back({linenum,linepart,fromx,move(cur)});
	return layout;
}

//Takes width without gutter
void Buffer::performInitialLayout(i64 width,i64 height){
	assert(width>0&&height>0);
	const i64 targetCursorScreenY=height/2;

	screen.clear();
	if(tb.numLines()==0){
		screen.push_back({0,0,0,{}});
		return;
	}

	assert(cursor.line>=0&&cursor.x>=0&&
	       cursor.line<tb.numLines()&&cursor.x<=tb.lineLen(cursor.line));

	//Layout the line the cursor is on.
	screen=layoutLine(tb[cursor.line],cursor.line,width);
	i64 cursorScreenY=0;
	while(cursorScreenY+1<(i64)screen.size()&&screen[cursorScreenY+1].fromx>cursor.x){
		cursorScreenY++;
	}

	//First, layout lines above the cursor line until the cursor is at or past halfway
	//the screen, or the buffer is exhausted.
	i64 topLine=cursor.line-1; //logical line
	while(topLine>=0&&cursorScreenY<targetCursorScreenY){
		vector<ScreenLine> layout=layoutLine(tb[topLine],topLine,width);
		screen.insert(screen.begin(),layout.begin(),layout.end());
		cursorScreenY+=layout.size();
		topLine--;
	}

	//Remove any lines from the top until the cursor is exactly halfway the screen.
	//Store the removed lines in `spliced`.
	vector<ScreenLine> spliced;
	if(cursorScreenY>targetCursorScreenY){
		for(i64 i=0;i<cursorScreenY-targetCursorScreenY;i++){
			spliced.push_back(move(screen[i]));
		}
		screen.erase(screen.begin(),screen.begin()+(cursorScreenY-targetCursorScreenY));
	}

	//Then, layout lines *under* the cursor line until the screen is full, or the buffer is
	//exhausted.
	i64 bottomLine=cursor.line+1; //logical line
	while(bottomLine<tb.numLines()&&(i64)screen.size()<height){
		vector<ScreenLine> layout=layoutLine(tb[bottomLine],bottomLine,width);
		screen.insert(screen.end(),layout.begin(),layout.end());
		bottomLine++;
	}
	if((i64)screen.size()<height){
		//Oops, we overran a bit. Prune the end so that we fill the screen exactly.
		screen.resize(height);
	} else if((i64)screen.size()<height){
		//We didn't even manage to fill the whole screen. Try to fill up the gap, first with the
		//lines in `spliced`, and then with extra logical lines from the textblob.
		i64 endgap=height-screen.size();
		if(endgap<=(i64)spliced.size()){
			//`spliced` contains enough lines to fill the gap.
			screen.insert(screen.begin(),spliced.begin()+(spliced.size()-endgap),spliced.end());
		} else {
			//We'll have to pull in some more lines from the textblob.
			screen.insert(screen.begin(),spliced.begin(),spliced.end());
			while(topLine>=0&&(i64)screen.size()<height){
				vector<ScreenLine> layout=layoutLine(tb[topLine],topLine,width);
				screen.insert(screen.begin(),layout.begin(),layout.end());
				topLine--;
			}
			if((i64)screen.size()>height){
				//Oops, we overran a bit. Prune the end so that we fill the screen exactly.
				screen.resize(height);
			}
		}
	}
}

//Takes width without gutter
void Buffer::renewLayout(i64 topLine,i64 topPart,i64 width,i64 height){
	assert(width>0&&height>0);
	assert(topLine>=0&&topPart>=0);

	screen.clear();
	if(tb.numLines()==0){
		screen.push_back({0,0,0,{}});
		return;
	}

	screen=layoutLine(tb.getLine(topLine),topLine,width);
	if(topPart>0)screen.erase(screen.begin(),screen.begin()+topPart);

	i64 bottomLine=topLine+1;
	while(bottomLine<tb.numLines()&&(i64)screen.size()<height){
		vector<ScreenLine> layout=layoutLine(tb[bottomLine],bottomLine,width);
		screen.insert(screen.begin(),layout.begin(),layout.end());
		bottomLine++;
	}

	if((i64)screen.size()>height){
		screen.resize(height);
	}
}

i64 numberWidth(i64 number){
	if(number<0)return 1+numberWidth(-number);
	if(number==0)return 1;
	return (i64)log10(number)+1;
}

void Buffer::show(i64 atx,i64 aty,i64 width,i64 height){
	static const Style gutterStyle={3,9,false,false};
	static const Style textStyle={9,9,false,false};
	static const Style specialStyle={5,9,false,false};

	i64 gutterWidth=numberWidth(tb.numLines())+2;
	i64 newWidth=width-gutterWidth;
	if(newWidth!=lastWidth||height!=lastHeight){
		if(lastWidth==-1)performInitialLayout(newWidth,height);
		else renewLayout(screen[0].line,screen[0].part,newWidth,height);
		lastWidth=newWidth;
		lastHeight=height;
	}
	pushcursor();
	i64 y;
	for(y=0;y<(i64)screen.size();y++){
		moveto(atx,aty+y);
		setstyle(&gutterStyle);
		i64 nspaces=gutterWidth-1-numberWidth(screen[y].line+1);
		for(i64 i=0;i<nspaces;i++){
			tputc(' ');
		}
		tprintf("%lld ",screen[y].line+1);

		setstyle(&textStyle);
		for(const Cell &cell : screen[y].cells){
			if(cell.special)setstyle(&specialStyle);
			else setstyle(&textStyle);
			tputc(cell.c);
		}
	}
	setstyle(&textStyle);
	if(y<height)fillrect(atx,aty+y,width,height-y,' ');
}