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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
|
#include <algorithm>
#include <fstream>
#include <stdexcept>
#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){}
Buffer::Buffer(Manager *manager,bool singleLineMode)
:manager(manager),singleLineMode(singleLineMode){}
void Buffer::handleCommand(const Command &cmd){
//TODO: optimise the relayoutScreen'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;
}
i64 nread=tb.read(file);
cursor.line=0;
cursor.x=0;
screen.clear();
dirty=false;
manager->receive({"info","Opened, read "+to_string(nread)+" characters"});
} else if(cmd[0]=="save_file"){
if(filename.size()==0){
manager->receive({"save_prompt"});
return;
}
ofstream file(filename);
if(!file){
manager->receive({"error","Cannot open file '"+filename+"' for writing"});
return;
}
i64 nwritten=tb.write(file);
dirty=false;
manager->receive({"info","Saved, written "+to_string(nwritten)+" characters"});
} else if(cmd[0]=="insert_char"){
char c=cmd[1][0];
assert(c!='\n');
tb.insert(cursor.line,cursor.x,cmd[1][0]);
dirty=true;
cursor.x++;
relayoutScreen();
if(findCursorInScreen().x==-1){
screen.erase(screen.begin());
relayoutScreen();
}
} else if(cmd[0]=="insert_newline"){
tb.insert(cursor.line,cursor.x,'\n');
dirty=true;
cursor.x=0;
cursor.line++;
relayoutScreen();
if(findCursorInScreen().x==-1){
screen.erase(screen.begin());
relayoutScreen();
}
} else if(cmd[0]=="delete_backward"){
if(cursor.x==0&&cursor.line==0){
bel();
return;
}
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);
}
dirty=true;
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();
return;
}
tb.erase(cursor.line,cursor.x);
dirty=true;
relayoutScreen();
} else if(cmd[0]=="move_forward"){
if(cursor.line==tb.numLines()-1&&cursor.x==tb.lineLen(cursor.line)){
bel();
return;
}
if(cursor.x==tb.lineLen(cursor.line)){
cursor.x=0;
cursor.line++;
} 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();
return;
}
if(cursor.x==0){
cursor.line--;
cursor.x=tb.lineLen(cursor.line);
} else {
cursor.x--;
}
if(findCursorInScreen().x==-1){
scrollUp();
}
} else if(cmd[0]=="move_downward"){
Screenpos sp=findCursorInScreen();
if(sp.x==-1){
bel();
return;
}
if(sp.y==(i64)screen.size()-1){
if(screen[sp.y].line==tb.numLines()-1&&
(screen[sp.y].cells.size()==0
?tb.lineLen(screen[sp.y].line)==0
:screen[sp.y].cells.back().linex==tb.lineLen(screen[sp.y].line)-1)){
//so screen[sp.y] contains the end of that logical line
if(cursor.x==tb.lineLen(screen[sp.y].line)){
bel();
} else {
cursor.x=tb.lineLen(screen[sp.y].line);
}
return;
}
screen.erase(screen.begin());
relayoutScreen();
} else {
sp.y++;
}
cursor.line=screen[sp.y].line;
if(sp.x>=(i64)screen[sp.y].cells.size()){
if(screen[sp.y].cells.size()==0)cursor.x=0;
else {
cursor.x=screen[sp.y].cells.back().linex;
//Now if we would've landed *after* the end of the line, go there.
if(cursor.x==tb.lineLen(cursor.line)-1)cursor.x++;
}
} else {
cursor.x=screen[sp.y].cells[sp.x].linex;
}
} else if(cmd[0]=="move_upward"){
Screenpos sp=findCursorInScreen();
if(sp.x==-1){
bel();
return;
}
if(sp.y==0){
if(screen[sp.y].line==0&&screen[sp.y].part==0){
if(cursor.x==0)bel();
else cursor.x=0;
return;
}
scrollUp();
} else {
sp.y--;
}
cursor.line=screen[sp.y].line;
if(sp.x>=(i64)screen[sp.y].cells.size()){
if(screen[sp.y].cells.size()==0)cursor.x=0;
else {
cursor.x=screen[sp.y].cells.back().linex;
//Now if we would've landed *after* the end of the line, go there.
if(cursor.x==tb.lineLen(cursor.line)-1)cursor.x++;
}
} else {
cursor.x=screen[sp.y].cells[sp.x].linex;
}
} else if(cmd[0]=="move_pagedown"){
Screenpos sp=findCursorInScreen();
if(sp.x==-1||screen.size()==0){
bel();
return;
}
i64 lastLine=screen.back().line;
i64 lastX=screen.back().cells.size()==0 ? screen.back().fromx : screen.back().cells.back().linex+1;
i64 newTopLine,newTopPart;
if(lastX>=tb.lineLen(lastLine)){
if(lastLine==tb.numLines()-1){
cursor.line=lastLine;
cursor.x=lastX;
return;
}
newTopLine=lastLine+1;
newTopPart=0;
} else {
newTopLine=lastLine;
newTopPart=screen.back().part+1;
}
vector<ScreenLine> screenCopy=screen;
renewLayout(newTopLine,newTopPart,lastWidth,lastHeight);
if((i64)screen.size()<lastHeight){
i64 gap=lastHeight-screen.size();
screen.insert(screen.begin(),screenCopy.begin()+(screenCopy.size()-gap),screenCopy.end());
cursor.line=screen.back().line;
if(screen.back().cells.size()==0)cursor.x=screen.back().fromx;
else cursor.x=screen.back().cells.back().linex+1;
} else {
cursor.line=screen[sp.y].line;
if((i64)screen[sp.y].cells.size()<sp.x){
if(screen[sp.y].cells.size()==0)cursor.x=screen[sp.y].fromx;
else cursor.x=screen[sp.y].cells.back().linex+1;
}
}
} else if(cmd[0]=="cancel"){
bel(); //nothing to cancel, apparently
} else {
cerr<<"Unknown command: "<<cmd[0]<<endl;
bel();
}
}
void Buffer::receive(const Command &cmd){
handleCommand(cmd);
// 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>=5); //5 == (maximum char repr width) + 1, where the 1 is an extra column on the right
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);
}
}
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};
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};
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);
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};
}
}
static 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 gutterContinuationStyle={1,9,false,true};
static const Style textStyle={9,9,false,false};
static const Style specialStyle={5,9,false,false};
i64 gutterWidth=singleLineMode ? 0 : numberWidth(tb.numLines())+2;
i64 newWidth=width-gutterWidth;
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;
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++){
if(!singleLineMode){
if(screen[y].fromx==0){
setstyle(&gutterStyle);
moveto(atx+gutterWidth-1-numberWidth(screen[y].line+1),aty+y);
tprintf("%" PRIi64,screen[y].line+1);
} else if(y==0){
setstyle(&gutterContinuationStyle);
moveto(atx+gutterWidth-2-numberWidth(screen[y].line+1),aty+y);
tprintf("^%" PRIi64,screen[y].line+1);
}
}
moveto(atx+gutterWidth,aty+y);
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);
}
}
string Buffer::getText() const {
if(singleLineMode){
string line=tb.fullText();
line.pop_back();
return line;
} else {
return tb.fullText();
}
}
bool Buffer::isDirty() const {
return dirty;
}
|