-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslate.cpp
452 lines (412 loc) · 8.76 KB
/
slate.cpp
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
#include "slate.h"
using namespace std;
int y_offset = 0; // TODO: move to local scope
int tab_offset = 0;
int max_x, max_y;
#define DEBUG
void print_loc(int x, int y) {
#ifdef DEBUG
int oldx, oldy;
getyx(stdscr, oldy, oldx);
mvprintw(0, COLS - 20, "x: %d y: %d o: %d", x, y, y_offset);
move(oldy, oldx);
#endif
}
int main(int argc, char *argv[]) {
PagedGapBuffer page(CACHE_SIZE);
if(argc > 1) {
if(file_exists(argv[1])) {
load_file(&page, argv[1]);
}
else {
page.initialize(argv[1], BUFFER_SIZE-GAP_SIZE);
}
}
else { // initialize
page.initialize("untitled.txt", BUFFER_SIZE-GAP_SIZE);
}
/* curses interface */
initscr();
noecho();
keypad(stdscr, true);
getmaxyx(stdscr, max_y, max_x);
int beg = 0;
int end = WIN_SIZE;
int y, x; // position on screen
update_status("Press F4 to quit");
print_page(&page);
//getyx(stdscr, y, x);
move(0,0);
x = y = 0;
char status[NAME_LIMIT + 10];
while(true) {
print_loc(x, y);
beg = 0 + y_offset;
end = WIN_SIZE + y_offset;
int ch = getch();
update_status("Press F4 to quit"); // default text
switch(ch) {
case KEY_F(4):
if(prompt_yesno("Are you sure you want to quit?"))
goto endnc;
print_page(&page);
break;
case KEY_F(5):
// save_file(&page);
sprintf(status, "Saved as \'%s\'", page.filename);
update_status(status);
break;
case KEY_F(6):
prompt_string("Save As:", page.filename, NAME_LIMIT);
// save_file(&page);
sprintf(status, "Saved as \'%s\'", page.filename);
print_page(&page);
update_status(status);
break;
case KEY_UP:
move_up(&page, &x, &y);
break;
case KEY_DOWN:
move_down(&page, &x, &y);
break;
case KEY_LEFT:
move_left(&page, &x, &y);
break;
case KEY_RIGHT:
move_right(&page, &x, &y);
break;
case KEY_DC:
case 127: // backspace key...
case KEY_BACKSPACE:
remove_char(&page); // delete
move_left(&page, &x, &y); // char behind cursor
print_page(&page);
move(y, x);
break;
/*
case '\t':
for(i = 0; i < TAB_WIDTH; i++)
{
insert_char(&page.text[y + y_offset], ' ', x - 1);
print_page(&page, beg, end);
move_right(&page, &x, &y);
}
break;
*/
case '\n': // newline
insert_char(&page, '\n');
print_page(&page);
move_right(&page, &x, &y);
break;
default: // all other chars
if( isprint(ch) )
{
insert_char(&page, ch);
print_page(&page);
move_right(&page, &x, &y);
}
}
}
endnc:
/* end curses */
endwin();
//dest_page(&page);
return EXIT_SUCCESS;
} // main
// prints a message at the bottom of the window
void update_status(char *info) {
int oldy, oldx; getyx(stdscr, oldy, oldx);
attron(A_REVERSE);
move(LINES - 1, 0);
clrtoeol();
printw(info);
attroff(A_REVERSE);
move(oldy, oldx);
}
// inserts a character in the buffer
void insert_char(PagedGapBuffer *p, char ch) {
if (p->current->is_full()) {
p->split_buffer();
}
p->current->insert_char(ch);
}
// removes a character from the buffer
void remove_char(PagedGapBuffer *p) {
if (!p->current->is_at_left()) {
p->current->delete_char();
}
else if (p->current == p->gapBufferCache.begin())
return;
else {
p->move_backward();
p->current->delete_char();
}
// if the entire buffer gap is empty
if (p->current->is_empty()) {
p->remove_current();
}
}
/* movement */
void move_left(PagedGapBuffer *p, int *x, int *y) {
if (!p->current->is_at_left()) {
p->current->move_backward();
}
else if (p->current == p->gapBufferCache.begin())
return;
else {
p->move_backward();
p->current->move_backward();
}
if(*x > 0)
move(*y, --(*x));
else {
int count = 0;
list<GapBuffer>::iterator g = p->current;
int position = g->gapStart-1;
bool flag = true;
while (flag) {
while (position < 0) {
if (g->equals(*(p->gapBufferCache.begin()))) {
flag = false;
break;
}
else {
g--;
position = g->gapStart-1;
}
}
if (!flag || g->buffer[position] == '\n')
break;
count++;
position--;
}
count = count % (max_x+1); // if the previous line has more characters than the window width
--(*y);
*x = count;
move(*y, *x);
}
}
// moves the cursor to the right
void move_right(PagedGapBuffer *p, int *x, int *y) {
if (!p->current->is_at_right()) {
p->current->move_forward();
}
else if (p->current != p->gapBufferCache.end() && next(p->current) == p->gapBufferCache.end())
return;
else {
p->move_forward();
p->current->move_forward();
}
if (p->current->current_char() == '\n' || *x == max_x) {
*x = 0;
++(*y);
}
else
++(*x);
move(*y, *x);
}
// move the cursor position up
void move_up(PagedGapBuffer *p, int *x, int *y) {
int count = 0;
int chars = 0;
int initial_pos = *x;
bool flag = true;
while (count <= 1) {
if (p->current->is_at_left()) {
if (p->is_at_left()) {
flag = false;
break;
}
else {
p->move_backward();
}
}
if (p->current->current_char() == '\n')
count++;
p->current->move_backward();
chars++;
if (chars == max_x+1 && count==0)
break;
}
if (!flag && count==0) {
*x = 0;
move(*y, *x);
return;
}
if (chars == max_x+1 && count==0) {
--(*y);
move(*y, *x);
return;
}
count = 0;
while (true) {
if (p->current->is_at_right()) {
if (p->is_at_right())
break;
else {
p->move_forward();
}
}
p->current->move_forward();
count++;
if (p->current->current_char() == '\n' || count-1 == initial_pos)
break;
}
*x = count-1; // set the x position to the end of the line if it ends early
--(*y); // move to the previous line
move(*y, *x);
}
// move the cursor position down
void move_down(PagedGapBuffer *p, int *x, int *y) {
int count = 0;
int initial_pos = *x;
bool flag = true;
while (flag) {
if (p->current->is_at_right()) {
if (p->is_at_right())
break;
else {
p->move_forward();
}
}
p->current->move_forward();
count++;
if (p->current->current_char() == '\n' || count == max_x+1)
flag = false;
}
// reached the end of pagedGapBuffer, move cursor to the end of the line
if (flag) {
*x += count;
move(*y, *x);
return;
}
if (count == max_x+1) {
++(*y);
move(*y, *x);
return;
}
count = 0;
while (true) {
if (p->current->is_at_right()) {
if (p->is_at_right()) { // encountered end of pagedGapBuffer
break;
}
else {
p->move_forward();
}
}
p->current->move_forward();
count++;
if (p->current->current_char() == '\n' || count-1 == initial_pos)
break;
}
if (p->current->is_at_left() && !p->is_at_left()) {
p->move_backward();
}
p->current->move_backward();
*x = count-1; // set the x position to the end of the line if it ends early
++(*y); // move to the next line
move(*y, *x);
}
// load file data into file buffer
void load_file(PagedGapBuffer *p, char *filename) {
FILE *fp = fopen(filename, "r");
int size = get_file_size(filename);
p->initialize(filename, size-1);
if(fp == NULL) { // file doesn't exist yet. don't bother reading
return;
}
p->readData(fp);
fclose(fp);
}
/*
void save_file(PAGE *p)
{
FILE *fp = fopen(p->filename, "w");
int line, col;
for(line = 0; line < p->numlines; line++)
{
col = 0;
while(p->text[line].line[col] != '\0')
{
fputc(p->text[line].line[col], fp);
col++;
}
fputc('\n', fp);
}
fclose(fp);
} // save_file
*/
// print the page to the screen
void print_page(PagedGapBuffer *p) {
int position = p->startOffsetWithinBuffer;
int y, x;
bool flag = true;
x = y = 0;
for (list<GapBuffer>::iterator ctr = p->startBuffer; ctr != p->gapBufferCache.end() && flag; ctr++) {
while (position < ctr->gapStart) {
if (ctr->buffer[position] == '\n') {
y++;
x=0;
clrtoeol();
move(y,x);
}
else {
mvaddch(y,x,ctr->buffer[position]);
x++;
}
position++;
if (x > max_x) {
y++;
x=0;
}
if (y > max_y) {
flag = false;
break;
}
}
position = ctr->gapEnd + 1;
while (position < ctr->size) {
if (ctr->buffer[position] == '\n') {
y++;
x=0;
clrtoeol();
move(y,x);
}
else {
mvaddch(y,x,ctr->buffer[position]);
x++;
}
position++;
if (x > max_x) {
y++;
x=0;
}
if (y > max_y) {
flag = false;
break;
}
}
}
clrtoeol();
y++;
x=0;
move(y,x);
clrtoeol();
}
int file_exists(char *filename)
{
FILE *fp = fopen(filename, "r");
if(fp != NULL) {
fclose(fp);
return 1;
}
return 0;
}
// get file size
off_t get_file_size(const char *filename) {
struct stat st;
if (stat(filename, &st) == 0)
return st.st_size;
return -1;
}