summaryrefslogtreecommitdiff
path: root/buffer.cpp
blob: 78f5e163c0ba62d8bd1b60c17907bbf7e3e4fff6 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include <stdexcept>
#include <fstream>
#include <cassert>
#include <cmath>
#include <cinttypes>
#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){
	//TODO: optimise the renewLayout's in this function

	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++;
		}
		renewLayout(screen[0].line,screen[0].part,lastWidth,lastHeight);
	} else if(cmd[0]=="insert_newline"){
		tb.insert(cursor.line,cursor.x,'\n');
		cursor.x=0;
		cursor.line++;
		renewLayout(screen[0].line,screen[0].part,lastWidth,lastHeight);
	} else if(cmd[0]=="delete_backward"){
		if(cursor.x==0&&cursor.line==0){
			bel();
		} else {
			if(cursor.x>0){
				cursor.x--;
				tb.erase(cursor.line,cursor.x);
			} else {
				cursor.line--;
				cursor.x=tb.lineLen(cursor.line);
				tb.erase(cursor.line,cursor.x);
			}
			renewLayout(screen[0].line,screen[0].part,lastWidth,lastHeight);
		}
	} else if(cmd[0]=="delete_forward"){
		if(cursor.line==tb.numLines()-1&&cursor.x==tb.lineLen(cursor.line)){
			bel();
		} else {
			tb.erase(cursor.line,cursor.x);
			renewLayout(screen[0].line,screen[0].part,lastWidth,lastHeight);
		}
	} else if(cmd[0]=="move_forward"){
		if(cursor.line==tb.numLines()-1&&cursor.x==tb.lineLen(cursor.line)){
			bel();
		} else {
			if(cursor.x==tb.lineLen(cursor.line)){
				cursor.x=0;
				cursor.line++;
			} else {
				cursor.x++;
			}
			renewLayout(screen[0].line,screen[0].part,lastWidth,lastHeight);
		}
	} else if(cmd[0]=="move_backward"){
		if(cursor.line==0&&cursor.x==0){
			bel();
		} else {
			if(cursor.x==0){
				cursor.line--;
				cursor.x=tb.lineLen(cursor.line);
			} else {
				cursor.x--;
			}
			renewLayout(screen[0].line,screen[0].part,lastWidth,lastHeight);
		}
	} else {
		THROW("Unknown command");
	}
	//cerr<<"New cursor is (line="<<cursor.line<<", x="<<cursor.x<<")"<<endl;
}

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

vector<Buffer::ScreenLine> Buffer::layoutLine(const string &line,i64 linenum,i64 width){
	assert(width>=4);
	if(line.size()==0){
		return {{linenum,0,0,{}}};
	}
	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;
		bool special=false;
		if(c=='\t'){
			repr=string(4-cur.size()%4,' ');
		} else {
			repr=showChar(c);
			special=repr.size()>1;
		}
		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',special,i};
		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());
		// cerr<<"Added bottomLine="<<bottomLine<<" (tb.numLines()="<<tb.numLines()<<")"<<endl;
		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.end(),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;
	}

	setstyle(&textStyle);
	fillrect(atx,aty,width,height,' ');

	pushcursor();
	i64 y;
	i64 curScreenX=-1,curScreenY=-1;
	// cerr<<"Buffer::show: screen.size()="<<screen.size()<<endl;
	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("%" PRIi64 " ",screen[y].line+1);

		i64 screenX=atx+gutterWidth;
		setstyle(&textStyle);
		for(const Cell &cell : screen[y].cells){
			if(cell.special)setstyle(&specialStyle);
			else setstyle(&textStyle);
			tputc(cell.c);
			if(curScreenX==-1&&cell.linex==cursor.x&&screen[y].line==cursor.line){
				curScreenX=screenX;
				curScreenY=aty+y;
			}
			screenX++;
		}

		//Maybe the cursor should be positioned AFTER this line
		if(curScreenX==-1&&screen[y].line==cursor.line&&cursor.x==tb.lineLen(cursor.line)&&
		   (y==(i64)screen.size()-1||screen[y+1].line>cursor.line)){
			if(screenX==atx+width)curScreenX=screenX-1;
			else curScreenX=screenX;
			curScreenY=aty+y;
		}
	}
	setstyle(&textStyle);
	if(y<height)fillrect(atx,aty+y,width,height-y,' ');
	popcursor();
	if(curScreenX!=-1){
		moveto(curScreenX,curScreenY);
	}
}