-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
1014 lines (923 loc) · 24.2 KB
/
main.c
File metadata and controls
1014 lines (923 loc) · 24.2 KB
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _DEFAULT_SOURCE
#define _BSD_SOURCE
#define _GNU_SOURCE
#define VER "1.0"
#define TABSIZE 4
#define QUIT_TIME 2
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <time.h>
#include <stdarg.h>
#include <fcntl.h>
typedef struct termios terminal;
//This struct contains row of text
typedef struct erow
{
int size;
char *chars;
int rsize;
char *render;
} erow;
/*This struct contains the buffer which we have to output in terminal*/
typedef struct abuf{
char *s;
int len;
} abuf;
#define ABUF_INIT {NULL, 0}
/**---terminal---**/
typedef enum keys{
ARROW_LEFT = 1000,
ARROW_RIGHT = 1001,
ARROW_UP = 1002,
ARROW_DOWN = 1003,
PAGE_UP = 1004,
PAGE_DOWN = 1005,
HOME = 1006,
END = 1007,
DEL = 1008,
BACKSPACE = 127,
} splKeys;
typedef struct estate
{
int cx, cy;
int rx;
unsigned short int screenrows;
unsigned short int screencols;
int numrow;
int colOff;
int rowOff; //for scrolling
erow *row; //Stores a row of text from the file
int dirty;
char *filename;
char statusMsg[80];
time_t statusMsgTime;
terminal original;
} editorState;
editorState config;
/***prototype*/
void setStatusMsg(const char *fmt, ...);
void refreshScreen();
char *promptUser(char *prompt, void(*callback)(char *, int));
/**
* @brief Outputs the error in screen and exits
*
* @param s
*/
void err(const char *s)
{
//Clear screen if error occurs then exit
write(STDOUT_FILENO, "\x1b[2J", 4);
write(STDOUT_FILENO, "\x1b[H", 3);
perror(s);
printf("\r\n");
exit(1);
}
void exitRawMode()
{
if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &config.original) == -1)
{
err("tcsetattr failure");
}
}
/**
* @brief Enables raw mode of the terminal
*
*/
void enableRawMode()
{
terminal raw;
if(tcgetattr(STDIN_FILENO, &config.original) == -1) err("tcgetattr failure"); //Storing terminal settings in struct raw
atexit(exitRawMode);
raw = config.original;
//Flags Used below
//ECHO - Turns off echoing
//ICANON - Turns off canonical mode (line by line input) No need to press enter
//ISIG - Stops SIGSTP SIGINT signals
//IXON - Stops XOFF XON signals
//ICRNL - ctrl+m should be /r but terminal translates it to /n
//OPOST - stops inserting /r by default
raw.c_iflag &= ~(ICRNL|IXON|BRKINT);
raw.c_lflag &= ~(ECHO|ICANON|ISIG|IEXTEN);
raw.c_oflag &= ~(OPOST);
raw.c_cc[VMIN] = 0;
raw.c_cc[VTIME] = 10;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}
/**row operations**/
int RowCxToRx(erow *row, int cx)
{
int rx = 0;
int j;
for(j = 0; j<cx; j++)
{
if(row->chars[j] =='\t')
{
rx += (TABSIZE-1) - (rx%TABSIZE);
}
rx++;
}
return rx;
}
int RowRxToCx(erow *row, int rx) {
int cur_rx = 0;
int cx;
for (cx = 0; cx < row->size; cx++) {
if (row->chars[cx] == '\t')
cur_rx += (TABSIZE - 1) - (cur_rx % TABSIZE);
cur_rx++;
if (cur_rx > rx) return cx;
}
return cx;
}
void updateRow(erow *row) {
int tabs = 0;
int j;
for (j = 0; j < row->size; j++)
{
if (row->chars[j] == '\t') tabs++;
}
free(row->render);
row->render = malloc(row->size + tabs*(TABSIZE-1) + 1);
int idx = 0;
for (j = 0; j < row->size; j++)
{
if (row->chars[j] == '\t')
{
int t = 1;
row->render[idx++] = ' ';
while (t % TABSIZE != 0)
{
row->render[idx++] = ' ';
t++;
}
}
else
{
row->render[idx++] = row->chars[j];
}
}
row->render[idx] = '\0';
row->rsize = idx;
}
void insertRow(int pos, char *s, size_t len)
{
if(pos < 0 || pos > config.numrow) return;
config.row = realloc(config.row, sizeof(erow) * (config.numrow + 1));
memmove(&config.row[pos + 1], &config.row[pos], sizeof(erow) * (config.numrow-pos));
//Adding one extra space for new row
config.row = realloc(config.row, (sizeof(erow)) * (config.numrow+1));
//Inserting the row info at the appropriate index
config.row[pos].size = len;
config.row[pos].chars = malloc(len+1);
strncpy((config.row[pos].chars), s, len);
config.row[pos].rsize = 0;
config.row[pos].render = NULL;
updateRow(&config.row[pos]);
config.numrow++;
config.dirty++;
}
void rowDelete(erow *row, int pos)
{
if(pos<0 || pos>=row->size) return; //Invalid position
memmove(&row->chars[pos], &row->chars[pos + 1], row->size - pos);
row->size--;
updateRow(row);
config.dirty++;
}
void rowInsertChar(erow *row, int pos, int c)
{
if(pos<0 || pos>row->size) pos = row->size;
row->chars = realloc(row->chars, row->size + 2);
//Shift the content one place right,eg H|ello becomes H_ello | is cursor
memmove(&row->chars[pos + 1], &row->chars[pos], row->size- pos + 1);
row->size++;
row->chars[pos] = c;
updateRow(row);
config.dirty++;
}
/**editor operations**/
void freeRow(erow *row)
{
free(row->chars);
free(row->render);
}
void DelRow(int pos)
{
if(pos<0 || pos >= config.numrow) return;
freeRow(&config.row[pos]); //freeing the current line
memmove(&config.row[pos], &config.row[pos+1], sizeof(erow) * (config.numrow - pos -1));
config.numrow--;
config.dirty++;
}
void joinRows(erow *row, char *s, size_t len)
{
row->chars = realloc(row->chars, row->size + len + 1);
strncpy(&row->chars[row->size], s, len);
row->size += len;
updateRow(row);
config.dirty++;
}
void DelChar()
{
if(config.cy == config.numrow) return; //last line do nothing
if(config.cx == 0 && config.cy == 0) return; //top left of screen
erow *row = &config.row[config.cy]; //ptr to current row
if(config.cx > 0)
{
rowDelete(row, config.cx - 1);
config.cx--;
}
else
{
config.cx = config.row[config.cy-1].size; //x becomes the size of prev line
joinRows(&config.row[config.cy-1], row->chars, row->size);
DelRow(config.cy);
config.cy--;
}
}
void insertChar(int c)
{
if(config.cy == config.numrow)
{
insertRow(config.numrow, "", 0);
}
rowInsertChar(&config.row[config.cy], config.cx, c);
config.cx++;
}
void insertNewLine()
{
if(config.cx == 0) //cursor at beginning of line
{
insertRow(config.cy, "", 0);
}
else
{
erow *row = &config.row[config.cy]; //ptr to current row
insertRow(config.cy + 1, &row->chars[config.cx], row->size - config.cx); //inserted new row
row = &config.row[config.cy];
row->size = config.cx; //trimming the current row
row->chars[row->size] = '\0'; //inserting null char
updateRow(row);
}
config.cy++;
config.cx = 0;
}
/**FILE I/O**/
char *rowToString(int *buflen)
{
int totlen = 0; //Total length of the file
int j;
for(j = 0; j<config.numrow; j++)
{
totlen += config.row[j].size + 1; //extra space for \n
}
*buflen = totlen;
char *buf = malloc(totlen); //allocating one big chunk of memory
char *p = buf;
for (j = 0; j < config.numrow; j++) {
memcpy(p, config.row[j].chars, config.row[j].size);
p += config.row[j].size; //incrementing p to the end
*p = '\n'; //Adding newline at the end
p++;
}
return buf;
}
void saveFile()
{
if(config.filename == NULL)
{
config.filename = promptUser("Save as: %s (ESC to Cancel)", NULL);
if(config.filename == NULL)
{
setStatusMsg("Save Aborted");
}
return;
}
int len;
char *buf = rowToString(&len);
//Permissions: user|group|other 06-rw 04-r 04-r ->0644
int fd = open(config.filename, O_RDWR | O_CREAT, 0644);
if(fd != -1)
{
if(ftruncate(fd, len) != -1)
{
if(write(fd, buf, len) == len)
{
close(fd);
free(buf);
config.dirty = 0;
setStatusMsg("%d bytes written to disk", len);
return;
}
}
close(fd);
}
free(buf);
setStatusMsg("Can't save. I/O error: %s", strerror(errno));
return;
}
/**
* @brief This function opens the file
*
* @param filename
*/
void editorOpen(char *filename)
{
FILE *fp = fopen(filename, "r");
if(!fp) err("fopen");
config.filename = strdup(filename);
char *line = NULL;
size_t linecap = 0;
ssize_t linelen;
while((linelen = getline(&line, &linecap, fp)) != -1) //read till EOF
{
while(linelen > 0 && (line[linelen-1] == '\r' || line[linelen-1] == '\n')) //trim /r/n
{
linelen--;
}
insertRow(config.numrow, line, linelen); //Insert the line in row struct
}
free(line);
config.dirty = 0;
fclose(fp);
}
/**FIND**/
void findCallback(char *query, int key)
{
static int lastMatch = -1;
static int dirn = 1;
if(key == '\r' || key == '\x1b')
{
//resetting values
lastMatch = -1;
dirn = 1;
return;
}
else if(key == ARROW_DOWN || key == ARROW_RIGHT)
{
dirn = 1;
}
else if(key == ARROW_LEFT || key == ARROW_UP)
{
dirn = -1;
}
else
{
lastMatch = -1;
dirn = 1;
}
if(lastMatch == -1) dirn = 1; //be default go fwd
int current = lastMatch;
int i;
for(i = 0; i<config.numrow; i++)
{
current += dirn;
//jmp to last row if too many back arrows
//basically wrap around
if(current == -1) current = config.numrow - 1;
else if(current == config.numrow) current = 0;
erow *row = &config.row[current];
char *match = strstr(row->render, query);
if(match)
{
lastMatch = current;
config.cy = current;
config.cy = i; //Jump to that line
config.cx = RowRxToCx(row, match-row->render); //jump to that position in line
config.rowOff = config.numrow;
break;
}
}
}
void findText()
{
int origCx = config.cx;
int origCy = config.cy;
int origColOff = config.colOff;
int origRowOff = config.rowOff;
char *query = promptUser("Search: %s (ESC to cancel)", findCallback);
if(query)
{
free(query);
}
else
{
config.cx = origCx;
config.cy = origCy;
config.colOff = origColOff;
config.rowOff = origRowOff;
}
}
/**Append Buffer**/
/**
* @brief This function appends the string to the buffer
*
* @param ab
* @param s
* @param len
*/
void abAppend(abuf *ab, const char *s, int len)
{
char *new = realloc(ab->s, ab->len+len); // allocating space for increasing size of string
if(new == NULL) err("Buffer Allocation problems");
//Can't use string function because they add 0 at the end
memcpy(new + ab->len, s, len); //Copy string s at the end
ab->s = new;
ab->len += len;
}
/**
* @brief This function frees the buffer
*
* @param ab
*/
void abFree(abuf *ab)
{
free(ab->s);
}
/**----input----**/
/**
* @brief This function reads the keypress
*
* @return int
*/
int readKey()
{
int nread;
char c;
while ((nread = read(STDIN_FILENO, &c, 1)) != 1)
{
if (nread == -1 && errno != EAGAIN) err("read");
}
if(c == '\x1b')
{
//If escape sequence is detected then read two more bytes
//Page down and up 4 bytes. esc[5~ esc[6~
//Arrow keys 3 bytes esc[A esc[B esc[C esc[D
char seq[3];
if(read(STDIN_FILENO, &seq[0], 1) != 1) return '\x1b';
if(read(STDIN_FILENO, &seq[1] , 1) != 1) return '\x1b';
if(seq[0] == '[')
{
if(seq[1] >= '0' && seq[1] <= '9')
{
if(read(STDIN_FILENO, &seq[2], 1) != 1) return '\x1b';
//If third byte not there return esc
if(seq[2] == '~') //Page up and down ends etc etc with ~
{
switch(seq[1])
{
case '5': return PAGE_UP;
case '6': return PAGE_DOWN;
case '1':
case '7': return HOME;
case '4':
case '8': return END;
case '3': return DEL;
}
}
}
else
{
switch(seq[1])
{
case 'A': return ARROW_UP;
case 'B': return ARROW_DOWN;
case 'C': return ARROW_RIGHT;
case 'D': return ARROW_LEFT;
case 'H': return HOME;
case 'F': return END;
}
}
}
return '\x1b';
}
else
{
return (int)c;
}
}
char *promptUser(char *prompt, void(*callback)(char *, int))
{
size_t bufsize = 128;
char *buf = malloc(bufsize); //Stores user input
size_t buflen = 0;
buf[0] = 0;
while(1)
{
setStatusMsg(prompt, buf);
refreshScreen();
int c = readKey();
if(c == DEL || c == CTRL('h') || c == BACKSPACE)
{
if(buflen != 0) buf[--buflen] = 0; //Inserting NULL at the position before
}
else if(c == '\r') //if enter is pressed
{
if(buflen != 0)
{
setStatusMsg("");
if (callback) callback(buf, c);
return buf;
}
}
else if(c == '\x1b') //Esc to cancel
{
setStatusMsg("");
if (callback) callback(buf, c);
free(buf);
return NULL;
}
else if(!iscntrl(c) && c<128)
{
if(buflen == bufsize -1)
{
bufsize = bufsize*2; //Increase buffer size when overflow
buf = realloc(buf, bufsize);
}
buf[buflen++] = c;
buf[buflen] = 0;
}
if (callback) callback(buf, c);
}
}
/**
* @brief This function moves the cursor
*
* @param key
*/
void moveCursor(int key)
{
erow *row;
if(config.cy >= config.numrow)
{
row = NULL; //If cursor moves past end of file, row is null
}
else
{
row = &config.row[config.cy]; //else row points to the current line in the file
}
switch(key)
{
case ARROW_LEFT:
if(config.cx != 0) config.cx--;
else if(config.cy > 0)
{
//moving cursor to end of prev line
config.cy--;
config.cx = config.row[config.cy].size;
}
break;
case ARROW_UP:
if(config.cy != 0) config.cy--;
break;
case ARROW_DOWN:
if(config.cy < config.numrow) config.cy++;
break;
case ARROW_RIGHT:
if(row && config.cx < row->size)
config.cx++; //increment cx only till length of the current row
else if (row && config.cx == row->size)
{
config.cy++;
config.cx = 0;
}
break;
}
//Since after moving from a long line to
//a shorter line cx remains same as that of position in longer line we need to
//modify cx
row = (config.cy>=config.numrow) ? NULL:&config.row[config.cy];
int rowlen = row ? row->size: 0;
if(config.cx > rowlen) config.cx = rowlen;
}
/**
* @brief This function processes the keypress
*
*/
void processKeyPress()
{
static int quit_time = QUIT_TIME;
int c = readKey();
switch(c)
{
//present in ttydefaults already included
case CTRL('q'):
if(config.dirty && quit_time>0)
{
setStatusMsg("WARNING !! Unsaved changes. "
"Press ctrl-q %d more times.", quit_time);
quit_time--;
return;
}
//clear screen then exit
write(STDOUT_FILENO, "\x1b[2J", 4);
write(STDOUT_FILENO, "\x1b[H", 3);
exit(0);
case CTRL('s'):
saveFile();
break;
case HOME:
config.cx = 0;
break;
case END:
if(config.cy<config.numrow) config.cx = config.row[config.cy].size-1;
break;
case PAGE_UP:
case PAGE_DOWN:
{
if (c == PAGE_UP) {
config.cy = config.rowOff;
} else if (c == PAGE_DOWN) {
config.cy = config.rowOff + config.screenrows - 1;
if (config.cy > config.numrow) config.cy = config.numrow;
}
int times = config.screenrows;
while (times--)
moveCursor(c == PAGE_UP ? ARROW_UP : ARROW_DOWN);
}
break;
case ARROW_UP:
case ARROW_DOWN:
case ARROW_LEFT:
case ARROW_RIGHT:
moveCursor(c);
break;
case '\r':
insertNewLine();
break;
case BACKSPACE:
case CTRL('h'):
case DEL:
if(c == DEL) moveCursor(ARROW_RIGHT);
DelChar();
break;
case '\x1b':
break;
case CTRL('f'):
findText();
break;
default:
insertChar(c);
break;
}
quit_time = QUIT_TIME;
}
/**
* @brief This function gets the cursor position
*
* @param row
* @param col
* @return int
* */
int getCursorPosition(unsigned short int *row, unsigned short int *col)
{
*row = *col = 0;
//ESC [ Ps n Ps = 6 means Command from host – Please report active position (using a CPR control sequence)
if(write(STDOUT_FILENO, "\x1b[6n", 4) != 4) return -1;
char parse_buf[32] = {0};
//Now the terminal will return an escape sequence of form 27[r;cR r = row, c = col. we have to parse it
short int i = 0;
while(i<(short int)(sizeof(parse_buf)-1))
{
if(read(STDIN_FILENO, &parse_buf[i], 1) != 1) break;
if (parse_buf[i] == 'R') break;
i++;
}
//No idea why this loop is insanely slow
// while(parse_buf[i] != 'R')
// {
// if(read(STDIN_FILENO, &parse_buf[i], 1) != 1) break;
// i++;
// }
// printf("\r\n&buf[i]: %s\r\n", &parse_buf[1]);
if(parse_buf[0] != '\x1b' || parse_buf[1] != '[')
{
return -1;
}
if(sscanf(&parse_buf[2], "%hu;%hu", row, col) != 2)
{
return -1;
}
return 0;
}
/**----output----**/
int getWindowSize(unsigned short int *rows, unsigned short int *cols)
{
struct winsize ws;
if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0)
{
if(write(STDOUT_FILENO, "\x1b[999C\x1b[999B", 12) != 12)
{
return -1;
}
int k = getCursorPosition(rows, cols);
err("WTF");
return k;
}
else {
*cols = ws.ws_col;
*rows = ws.ws_row;
return 0;
}
}
void scroll()
{
config.rx = 0;
if(config.cy<config.numrow)
{
config.rx = RowCxToRx(&config.row[config.cy], config.cx);
}
if(config.cy<config.rowOff)
{
config.rowOff = config.cy;
}
if(config.cy >= config.rowOff + config.screenrows)
{
config.rowOff = config.cy-config.screenrows + 1;
}
if(config.rx < config.colOff)
{
config.colOff = config.rx;
}
if(config.rx >= config.colOff + config.screencols)
{
config.colOff = config.rx-config.screencols + 1;
}
}
/**
* @brief This function draws the rows of the editor
*
* @param ab
*/
void drawRows(abuf *ab)
{
for(int y = 0; y<config.screenrows; y++)
{
int filerow = y+config.rowOff;
if(filerow >= config.numrow)
{
if(y>=config.numrow)
{
if(config.numrow == 0 && y == config.screenrows/3)
{
char welcomemsg[80] = {0};
int msglen = snprintf(welcomemsg, sizeof(welcomemsg), "TextEditor Version %s", VER);
if(msglen > config.screencols) msglen = config.screencols;
int padding = (config.screencols - msglen) / 2;
if (padding) {
abAppend(ab, "~", 1);
padding--;
}
while (padding--) abAppend(ab, " ", 1);
abAppend(ab, welcomemsg, msglen);
}
else
{
abAppend(ab, "~", 1);
}
}
}
else
{
int len = config.row[filerow].rsize - config.colOff;
if(len<0) len = 0;
if(len > config.screencols) len = config.screencols;
abAppend(ab, &config.row[filerow].render[config.colOff], len);
}
abAppend(ab, "\x1b[K", 3); //For clearing one line at a time
// if(y < config.screenrows-1)
// {
abAppend(ab, "\r\n", 2);
// }
}
}
void drawStatusBar(abuf *ab)
{
abAppend(ab, "\x1b[7m", 4);
char status[100];
char lno[30]; //Shows line number
int len = snprintf(status, sizeof(status), "%.20s - %d lines %s",
config.filename ? config.filename : "[Untitled]", config.numrow,
config.dirty != 0 ? "(modified)" : "(Unmodified)");
int rlen = snprintf(lno, sizeof(lno), "Ln %d, Col %d", config.cy+1, config.cx + 1);
if(len > config.screencols) len = config.screencols;
abAppend(ab, status , len);
while(len<config.screencols)
{
if(config.screencols - len == rlen)
{
abAppend(ab, lno, rlen);
break;
}
else
{
abAppend(ab, " ", 1);
len++;
}
}
abAppend(ab, "\x1b[0m", 4);
abAppend(ab, "\r\n", 2);
}
void setStatusMsg(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vsnprintf(config.statusMsg, sizeof(config.statusMsg), fmt, ap);
va_end(ap);
config.statusMsgTime = time(NULL);
}
void drawMsgBar(abuf *ab)
{
abAppend(ab, "\x1b[K", 3);
abAppend(ab, "\x1b[7m", 4);
int msglen = strlen(config.statusMsg);
if(msglen > config.screencols) msglen = config.screencols;
if(time(NULL) - config.statusMsgTime < 3)
{
if(msglen != 0)
{
abAppend(ab, "\x1b[7m", 4);
abAppend(ab, config.statusMsg, msglen);
}
for(int i = 0; i<config.screencols - msglen; i++)
{
abAppend(ab, " ", 1);
}
}
else
{
for(int i = 0; i<config.screencols; i++)
{
abAppend(ab, " ", 1);
}
}
abAppend(ab, "\x1b[m", 3);
}
/**
* @brief This function refreshes the screen after every keypress
*
*/
void refreshScreen()
{
scroll();
abuf ab = ABUF_INIT;
//0x1b-27
// //2 clears entire screen 0-till cursor 1-cursor till end
// abAppend(&ab, "\x1b[2J", 4);
abAppend(&ab, "\x1b[?25l", 6);
//Positioning cursor at beginning
abAppend(&ab, "\x1b[H", 3);
//then drawing
drawRows(&ab);
drawStatusBar(&ab);
drawMsgBar(&ab);
char buf[32];
int length = snprintf(buf, sizeof(buf), "\x1b[%d;%dH",
(config.cy - config.rowOff+1), (config.rx - config.colOff + 1));
if(length == 0) err("Cursor error");
abAppend(&ab, buf, strlen(buf));
abAppend(&ab, "\x1b[?25h", 6);
//Writes all the buffer at once
write(STDOUT_FILENO, ab.s, ab.len);
abFree(&ab);
}
/**init**/
/**
* @brief This function initializes the editor
*
*/
void initEditor()
{
config.cx = config.cy = 0;
config.numrow = 0;
config.row = NULL;
config.rowOff = config.colOff = 0;
config.rx = 0;
config.filename = NULL;
config.statusMsg[0] = 0;
config.statusMsgTime = 0;
config.dirty = 0;
if(getWindowSize(&config.screenrows, &config.screencols) == -1) err("getWindowSize");
config.screenrows -= 2; //Making two empty space at bottom of the screen
}
int main(int argc, char *argv[])