-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcedit.c
More file actions
executable file
·1392 lines (1230 loc) · 30.4 KB
/
cedit.c
File metadata and controls
executable file
·1392 lines (1230 loc) · 30.4 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
/*** FEATURE TEST MACROS ***/
#define _DEFAULT_SOURCE
#define _BSD_SOURCE
#define _GNU_SOURCE
/*** INCLUDES ***/
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
/*** DEFINITIONS ***/
#define CEDIT_VERSION "1.0"
#define CEDIT_TAB_STOP 8
#define CEDIT_QUIT_COUNT 2
#define ctrl(key) ((key)&0x1f)
#define HL_HIGHLIGHT_NUMBERS (1 << 0)
#define HL_HIGHLIGHT_STRINGS (1 << 1)
/*** GLOBAL DECLARATIONS ***/
enum ceditKey
{
BACKSPACE = 127,
ARROW_LEFT = 1000,
ARROW_RIGHT,
ARROW_UP,
ARROW_DOWN,
DEL_KEY,
HOME_KEY,
END_KEY,
PAGE_UP,
PAGE_DOWN
};
enum ceditSyntaxHighlight
{
HL_NORMAL = 0,
HL_COMMENT,
HL_MLCOMMENT,
HL_KEYWORD1,
HL_KEYWORD2,
HL_STRING,
HL_NUMBER,
HL_MATCH
};
struct bufferContainer
{
char *b;
int length;
};
struct ceditSyntax
{
char *fileType;
char **fileMatch;
char **keywords;
char *singleLineCommentStart;
char *multiLineCommentStart;
char *multiLineCommentEnd;
int flags;
};
typedef struct editorRow
{
int index;
int size;
int rSize;
int hlOpenComment;
char *characters;
char *render;
unsigned char *hl;
} editorRow;
struct ceditConfig
{
int cursorX, cursorY;
int rowX;
int rowOff;
int columnOff;
int terminalRows;
int terminalColumns;
int rowNum;
int modified;
char *fileName;
char statusMessage[80];
time_t statusMessageTime;
struct ceditSyntax *syntax;
struct termios terminalDefault;
editorRow *row;
} Cedit;
/*** SYNTAX DEFINITIONS ***/
char *extensionC[] = {".character", ".h", ".cpp", NULL};
char *keywordsC[] = {
"switch", "if", "while", "for", "break", "continue", "return", "else",
"struct", "union", "typedef", "static", "enum", "class", "case",
"int|", "long|", "double|", "float|", "char|", "unsigned|", "signed|",
"void|", NULL};
struct ceditSyntax HLDB[] = {
{"character",
extensionC,
keywordsC,
"//", "/*", "*/",
HL_HIGHLIGHT_NUMBERS | HL_HIGHLIGHT_STRINGS},
};
#define HLDB_ENTRIES (sizeof(HLDB) / sizeof(HLDB[0]))
/*** FUNCTION PROTOTYPES ***/
void startCedit();
void terminateProgram(const char *errorMessage);
void rawModeOff();
void rawModeOn();
void ceditUpdateSyntax(editorRow *row);
void ceditHighlightSyntax();
void ceditUpdateRow(editorRow *row);
void ceditInsertRow(int at, char *s, size_t length);
void ceditFreeRow(editorRow *row);
void ceditDeleteRow(int at);
void ceditRowInsertCharacter(editorRow *row, int at, int character);
void ceditRowAppendString(editorRow *row, char *s, size_t length);
void ceditRowDeleteCharacter(editorRow *row, int at);
void ceditInsertCharacter(int character);
void ceditInsertNewline();
void ceditDeleteCharacter();
void ceditOpen(char *fileName);
void ceditSave();
void ceditFindCallback(char *query, int key);
void ceditFind();
void appendBuffer(struct bufferContainer *bc, const char *s, int length);
void freeBuffer(struct bufferContainer *bc);
void ceditScroll();
void ceditPrintRows(struct bufferContainer *bc);
void ceditDrawStatusBar(struct bufferContainer *bc);
void ceditDrawMessageBar(struct bufferContainer *bc);
void ceditRefreshTerminal();
void ceditSetStatusMessage(const char *fmt, ...);
void ceditMoveCursor(int key);
void ceditProcessKeypress();
int ceditReadCharacter();
int getCursorPosition(int *rows, int *columns);
int ceditRowCursorTransformCxtoRx(editorRow *row, int cursorX);
int ceditRowCursorTransformRxToCx(editorRow *row, int rowX);
int getTerminalSize(int *rows, int *columns);
int isSeparator(int character);
int ceditSyntaxColoring(int hl);
char *ceditPrompt(char *prompt, void (*callback)(char *, int));
char *ceditRowToString(int *bufferLength);
/*** TERMINAL MANIPULATION ***/
void terminateProgram(const char *errorMessage)
{
write(STDOUT_FILENO, "\x1b[2J", 4);
write(STDOUT_FILENO, "\x1b[H", 3);
perror(errorMessage);
write(STDOUT_FILENO, "\n\r", 2);
exit(1);
}
void usageProgram()
{
char msg[] = "Cedit Usage:\n\rOpen new file:\t\t./cedit\n\rEdit existing file:\t./cedit filename\n\r";
write(STDOUT_FILENO, msg, sizeof(msg));
exit(1);
}
void rawModeOff()
{
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &Cedit.terminalDefault) == -1)
terminateProgram("Tcsetattr Error!");
}
void rawModeOn()
{
if (tcgetattr(STDIN_FILENO, &Cedit.terminalDefault) == -1)
terminateProgram("Tcgetattr Error!");
atexit(rawModeOff);
struct termios rawMode = Cedit.terminalDefault;
//Input flags
rawMode.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
/*
c_iflag : Input Flags
Turning off:
IXON - Ctrl+S and Ctrl+Q (input transmission shortcuts)
ICRNL - Ctrl+M (carriage return)
BRKINT - Ctrl+C (terminate program)
INPCK - Parity Checking
ISTRIP - Strip 8th bit
*/
//Output Flags
rawMode.c_oflag &= ~(OPOST);
/*
c_oflag : Output Flags
Turning off:
OPOST - Post processing of Output (Carriage return and newline formatting)
*/
//Local Flags
rawMode.c_lflag &= ~(ECHO | ICANON | ISIG | IEXTEN);
/*
c_lflag : Local Flags
Turning off:
ECHO - Echo to the terminal
ICANON - Canonical mode (Turning off causes keypresses to be processed one by one)
ISIG - Ctrl+C, Ctrl+Z
IEXTEN - Ctrl+V
*/
//Control Flags
rawMode.c_cflag |= ~(CS8);
/*
c_cflag : Control Flags
Turning on:
CS8 - Set 8 bits per byte
*/
rawMode.c_cc[VMIN] = 0;
rawMode.c_cc[VTIME] = 1;
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &rawMode) == -1)
terminateProgram("Tcsetattr Error!");
}
int ceditReadCharacter()
{
int readReturn;
char character;
while ((readReturn = read(STDIN_FILENO, &character, 1)) != 1)
{
if (readReturn == -1 && errno != EAGAIN)
terminateProgram("Read Error!");
}
if (character == '\x1b')
{
char escapeSequence[3];
if (read(STDIN_FILENO, &escapeSequence[0], 1) != 1)
return '\x1b';
if (read(STDIN_FILENO, &escapeSequence[1], 1) != 1)
return '\x1b';
if (escapeSequence[0] == '[')
{
if (escapeSequence[1] >= '0' && escapeSequence[1] <= '9')
{
if (read(STDIN_FILENO, &escapeSequence[2], 1) != 1)
return '\x1b';
if (escapeSequence[2] == '~')
{
switch (escapeSequence[1])
{
case '1':
return HOME_KEY;
case '3':
return DEL_KEY;
case '4':
return END_KEY;
case '5':
return PAGE_UP;
case '6':
return PAGE_DOWN;
case '7':
return HOME_KEY;
case '8':
return END_KEY;
}
}
}
else
{
switch (escapeSequence[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_KEY;
case 'F':
return END_KEY;
}
}
}
else if (escapeSequence[0] == 'O')
{
switch (escapeSequence[1])
{
case 'H':
return HOME_KEY;
case 'F':
return END_KEY;
}
}
return '\x1b';
}
else
{
return character;
}
}
int getCursorPosition(int *rows, int *columns)
{
char buffer[32];
unsigned int i = 0;
if (write(STDOUT_FILENO, "\x1b[6n", 4) != 4)
return -1;
while (i < sizeof(buffer) - 1)
{
if (read(STDIN_FILENO, &buffer[i], 1) != 1)
break;
if (buffer[i] == 'R')
break;
i++;
}
buffer[i] = '\0';
if (buffer[0] != '\x1b' || buffer[1] != '[')
return -1;
if (sscanf(&buffer[2], "%d;%d", rows, columns) != 2)
return -1;
return 0;
}
int getTerminalSize(int *rows, int *columns)
{
struct winsize terminalSize;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &terminalSize) == -1 || terminalSize.ws_col == 0)
{
if (write(STDOUT_FILENO, "\x1b[999C\x1b[999B", 12) != 12)
return -1;
return getCursorPosition(rows, columns);
}
else
{
*columns = terminalSize.ws_col;
*rows = terminalSize.ws_row;
return 0;
}
}
/*** SYNTAX HIGHLIGHTING ***/
int isSeparator(int character)
{
return isspace(character) || character == '\0' || strchr(",.()+-/*=~%<>[];", character) != NULL;
}
void ceditUpdateSyntax(editorRow *row)
{
row->hl = realloc(row->hl, row->rSize);
memset(row->hl, HL_NORMAL, row->rSize);
if (Cedit.syntax == NULL)
return;
char **keywords = Cedit.syntax->keywords;
char *scs = Cedit.syntax->singleLineCommentStart;
char *mcs = Cedit.syntax->multiLineCommentStart;
char *mce = Cedit.syntax->multiLineCommentEnd;
int scsLength = scs ? strlen(scs) : 0;
int mcsLength = mcs ? strlen(mcs) : 0;
int mceLength = mce ? strlen(mce) : 0;
int prevSep = 1;
int inString = 0;
int inComment = (row->index > 0 && Cedit.row[row->index - 1].hlOpenComment);
int i = 0;
while (i < row->rSize)
{
char character = row->render[i];
unsigned char prev_hl = (i > 0) ? row->hl[i - 1] : HL_NORMAL;
if (scsLength && !inString && !inComment)
{
if (!strncmp(&row->render[i], scs, scsLength))
{
memset(&row->hl[i], HL_COMMENT, row->rSize - i);
break;
}
}
if (mcsLength && mceLength && !inString)
{
if (inComment)
{
row->hl[i] = HL_MLCOMMENT;
if (!strncmp(&row->render[i], mce, mceLength))
{
memset(&row->hl[i], HL_MLCOMMENT, mceLength);
i += mceLength;
inComment = 0;
prevSep = 1;
continue;
}
else
{
i++;
continue;
}
}
else if (!strncmp(&row->render[i], mcs, mcsLength))
{
memset(&row->hl[i], HL_MLCOMMENT, mcsLength);
i += mcsLength;
inComment = 1;
continue;
}
}
if (Cedit.syntax->flags & HL_HIGHLIGHT_STRINGS)
{
if (inString)
{
row->hl[i] = HL_STRING;
if (character == '\\' && i + 1 < row->rSize)
{
row->hl[i + 1] = HL_STRING;
i += 2;
continue;
}
if (character == inString)
inString = 0;
i++;
prevSep = 1;
continue;
}
else
{
if (character == '"' || character == '\'')
{
inString = character;
row->hl[i] = HL_STRING;
i++;
continue;
}
}
}
if (Cedit.syntax->flags & HL_HIGHLIGHT_NUMBERS)
{
if ((isdigit(character) && (prevSep || prev_hl == HL_NUMBER)) ||
(character == '.' && prev_hl == HL_NUMBER))
{
row->hl[i] = HL_NUMBER;
i++;
prevSep = 0;
continue;
}
}
if (prevSep)
{
int j;
for (j = 0; keywords[j]; j++)
{
int klen = strlen(keywords[j]);
int kw2 = keywords[j][klen - 1] == '|';
if (kw2)
klen--;
if (!strncmp(&row->render[i], keywords[j], klen) &&
isSeparator(row->render[i + klen]))
{
memset(&row->hl[i], kw2 ? HL_KEYWORD2 : HL_KEYWORD1, klen);
i += klen;
break;
}
}
if (keywords[j] != NULL)
{
prevSep = 0;
continue;
}
}
prevSep = isSeparator(character);
i++;
}
int changed = (row->hlOpenComment != inComment);
row->hlOpenComment = inComment;
if (changed && row->index + 1 < Cedit.rowNum)
ceditUpdateSyntax(&Cedit.row[row->index + 1]);
}
int ceditSyntaxColoring(int hl)
{
switch (hl)
{
case HL_COMMENT:
case HL_MLCOMMENT:
return 36;
case HL_KEYWORD1:
return 33;
case HL_KEYWORD2:
return 32;
case HL_STRING:
return 35;
case HL_NUMBER:
return 31;
case HL_MATCH:
return 34;
default:
return 37;
}
}
void ceditHighlightSyntax()
{
Cedit.syntax = NULL;
if (Cedit.fileName == NULL)
return;
char *ext = strrchr(Cedit.fileName, '.');
for (unsigned int j = 0; j < HLDB_ENTRIES; j++)
{
struct ceditSyntax *s = &HLDB[j];
unsigned int i = 0;
while (s->fileMatch[i])
{
int isExtension = (s->fileMatch[i][0] == '.');
if ((isExtension && ext && !strcmp(ext, s->fileMatch[i])) ||
(!isExtension && strstr(Cedit.fileName, s->fileMatch[i])))
{
Cedit.syntax = s;
int fileRow;
for (fileRow = 0; fileRow < Cedit.rowNum; fileRow++)
{
ceditUpdateSyntax(&Cedit.row[fileRow]);
}
return;
}
i++;
}
}
}
/*** ROW OPERATIONS ***/
int ceditRowCursorTransformCxtoRx(editorRow *row, int cursorX)
{
int rowX = 0;
int j;
for (j = 0; j < cursorX; j++)
{
if (row->characters[j] == '\t')
rowX += (CEDIT_TAB_STOP - 1) - (rowX % CEDIT_TAB_STOP);
rowX++;
}
return rowX;
}
int ceditRowCursorTransformRxToCx(editorRow *row, int rowX)
{
int currentRx = 0;
int cursorX;
for (cursorX = 0; cursorX < row->size; cursorX++)
{
if (row->characters[cursorX] == '\t')
currentRx += (CEDIT_TAB_STOP - 1) - (currentRx % CEDIT_TAB_STOP);
currentRx++;
if (currentRx > rowX)
return cursorX;
}
return cursorX;
}
void ceditUpdateRow(editorRow *row)
{
int tabs = 0;
int j;
for (j = 0; j < row->size; j++)
if (row->characters[j] == '\t')
tabs++;
free(row->render);
row->render = malloc(row->size + tabs * (CEDIT_TAB_STOP - 1) + 1);
int index = 0;
for (j = 0; j < row->size; j++)
{
if (row->characters[j] == '\t')
{
row->render[index++] = ' ';
while (index % CEDIT_TAB_STOP != 0)
row->render[index++] = ' ';
}
else
{
row->render[index++] = row->characters[j];
}
}
row->render[index] = '\0';
row->rSize = index;
ceditUpdateSyntax(row);
}
void ceditInsertRow(int at, char *s, size_t length)
{
if (at < 0 || at > Cedit.rowNum)
return;
Cedit.row = realloc(Cedit.row, sizeof(editorRow) * (Cedit.rowNum + 1));
memmove(&Cedit.row[at + 1], &Cedit.row[at], sizeof(editorRow) * (Cedit.rowNum - at));
for (int j = at + 1; j <= Cedit.rowNum; j++)
Cedit.row[j].index++;
Cedit.row[at].index = at;
Cedit.row[at].size = length;
Cedit.row[at].characters = malloc(length + 1);
memcpy(Cedit.row[at].characters, s, length);
Cedit.row[at].characters[length] = '\0';
Cedit.row[at].rSize = 0;
Cedit.row[at].render = NULL;
Cedit.row[at].hl = NULL;
Cedit.row[at].hlOpenComment = 0;
ceditUpdateRow(&Cedit.row[at]);
Cedit.rowNum++;
Cedit.modified++;
}
void ceditFreeRow(editorRow *row)
{
free(row->render);
free(row->characters);
free(row->hl);
}
void ceditDeleteRow(int at)
{
if (at < 0 || at >= Cedit.rowNum)
return;
ceditFreeRow(&Cedit.row[at]);
memmove(&Cedit.row[at], &Cedit.row[at + 1], sizeof(editorRow) * (Cedit.rowNum - at - 1));
for (int j = at; j < Cedit.rowNum - 1; j++)
Cedit.row[j].index--;
Cedit.rowNum--;
Cedit.modified++;
}
void ceditRowInsertCharacter(editorRow *row, int at, int character)
{
if (at < 0 || at > row->size)
at = row->size;
row->characters = realloc(row->characters, row->size + 2);
memmove(&row->characters[at + 1], &row->characters[at], row->size - at + 1);
row->size++;
row->characters[at] = character;
ceditUpdateRow(row);
Cedit.modified++;
}
void ceditRowAppendString(editorRow *row, char *s, size_t length)
{
row->characters = realloc(row->characters, row->size + length + 1);
memcpy(&row->characters[row->size], s, length);
row->size += length;
row->characters[row->size] = '\0';
ceditUpdateRow(row);
Cedit.modified++;
}
void ceditRowDeleteCharacter(editorRow *row, int at)
{
if (at < 0 || at >= row->size)
return;
memmove(&row->characters[at], &row->characters[at + 1], row->size - at);
row->size--;
ceditUpdateRow(row);
Cedit.modified++;
}
/*** CEDIT OPERATIONS ***/
void ceditInsertCharacter(int character)
{
if (Cedit.cursorY == Cedit.rowNum)
{
ceditInsertRow(Cedit.rowNum, "", 0);
}
ceditRowInsertCharacter(&Cedit.row[Cedit.cursorY], Cedit.cursorX, character);
Cedit.cursorX++;
}
void ceditInsertNewline()
{
if (Cedit.cursorX == 0)
{
ceditInsertRow(Cedit.cursorY, "", 0);
}
else
{
editorRow *row = &Cedit.row[Cedit.cursorY];
ceditInsertRow(Cedit.cursorY + 1, &row->characters[Cedit.cursorX], row->size - Cedit.cursorX);
row = &Cedit.row[Cedit.cursorY];
row->size = Cedit.cursorX;
row->characters[row->size] = '\0';
ceditUpdateRow(row);
}
Cedit.cursorY++;
Cedit.cursorX = 0;
}
void ceditDeleteCharacter()
{
if (Cedit.cursorY == Cedit.rowNum)
return;
if (Cedit.cursorX == 0 && Cedit.cursorY == 0)
return;
editorRow *row = &Cedit.row[Cedit.cursorY];
if (Cedit.cursorX > 0)
{
ceditRowDeleteCharacter(row, Cedit.cursorX - 1);
Cedit.cursorX--;
}
else
{
Cedit.cursorX = Cedit.row[Cedit.cursorY - 1].size;
ceditRowAppendString(&Cedit.row[Cedit.cursorY - 1], row->characters, row->size);
ceditDeleteRow(Cedit.cursorY);
Cedit.cursorY--;
}
}
/*** FILE OPERATIONS ***/
char *ceditRowToString(int *bufferLength)
{
int totalLength = 0;
int j;
for (j = 0; j < Cedit.rowNum; j++)
totalLength += Cedit.row[j].size + 1;
*bufferLength = totalLength;
char *buffer = malloc(totalLength);
char *p = buffer;
for (j = 0; j < Cedit.rowNum; j++)
{
memcpy(p, Cedit.row[j].characters, Cedit.row[j].size);
p += Cedit.row[j].size;
*p = '\n';
p++;
}
return buffer;
}
void ceditOpen(char *fileName)
{
free(Cedit.fileName);
Cedit.fileName = strdup(fileName);
ceditHighlightSyntax();
FILE *fp = fopen(fileName, "r");
if (!fp)
terminateProgram("File Open Error!");
char *line = NULL;
size_t lineCap = 0;
ssize_t lineLength;
while ((lineLength = getline(&line, &lineCap, fp)) != -1)
{
while (lineLength > 0 && (line[lineLength - 1] == '\n' ||
line[lineLength - 1] == '\r'))
lineLength--;
ceditInsertRow(Cedit.rowNum, line, lineLength);
}
free(line);
fclose(fp);
Cedit.modified = 0;
}
void ceditSave()
{
if (Cedit.fileName == NULL)
{
Cedit.fileName = ceditPrompt("Save as: %s (ESC to cancel)", NULL);
if (Cedit.fileName == NULL)
{
ceditSetStatusMessage("Save aborted");
return;
}
ceditHighlightSyntax();
}
int length;
char *buffer = ceditRowToString(&length);
int fd = open(Cedit.fileName, O_RDWR | O_CREAT, 0644);
if (fd != -1)
{
if (ftruncate(fd, length) != -1)
{
if (write(fd, buffer, length) == length)
{
close(fd);
free(buffer);
Cedit.modified = 0;
ceditSetStatusMessage("%d bytes written to disk", length);
return;
}
}
close(fd);
}
free(buffer);
ceditSetStatusMessage("Can't save! I/O error: %s", strerror(errno));
}
/*** FIND OPERATIONS ***/
void ceditFindCallback(char *query, int key)
{
static int lastMatch = -1;
static int direction = 1;
static int savedHlLine;
static char *savedHl = NULL;
if (savedHl)
{
memcpy(Cedit.row[savedHlLine].hl, savedHl, Cedit.row[savedHlLine].rSize);
free(savedHl);
savedHl = NULL;
}
if (key == '\r' || key == '\x1b')
{
lastMatch = -1;
direction = 1;
return;
}
else if (key == ARROW_RIGHT || key == ARROW_DOWN)
{
direction = 1;
}
else if (key == ARROW_LEFT || key == ARROW_UP)
{
direction = -1;
}
else
{
lastMatch = -1;
direction = 1;
}
if (lastMatch == -1)
direction = 1;
int current = lastMatch;
int i;
for (i = 0; i < Cedit.rowNum; i++)
{
current += direction;
if (current == -1)
current = Cedit.rowNum - 1;
else if (current == Cedit.rowNum)
current = 0;
editorRow *row = &Cedit.row[current];
char *match = strstr(row->render, query);
if (match)
{
lastMatch = current;
Cedit.cursorY = current;
Cedit.cursorX = ceditRowCursorTransformRxToCx(row, match - row->render);
Cedit.rowOff = Cedit.rowNum;
savedHlLine = current;
savedHl = malloc(row->rSize);
memcpy(savedHl, row->hl, row->rSize);
memset(&row->hl[match - row->render], HL_MATCH, strlen(query));
break;
}
}
}
void ceditFind()
{
int savedCursorX = Cedit.cursorX;
int savedCursorY = Cedit.cursorY;
int savedColumn = Cedit.columnOff;
int savedRow = Cedit.rowOff;
char *query = ceditPrompt("Search: %s (Use ESC/Arrows/Enter)",
ceditFindCallback);
if (query)
{
free(query);
}
else
{
Cedit.cursorX = savedCursorX;
Cedit.cursorY = savedCursorY;
Cedit.columnOff = savedColumn;
Cedit.rowOff = savedRow;
}
}
/*** BUFFER FUNCTIONS ***/
#define BUFFER_INITIALIZATION \
{ \
NULL, 0 \
}
void appendBuffer(struct bufferContainer *bc, const char *s, int length)
{
char *new = realloc(bc->b, bc->length + length);
if (new == NULL)
return;
memcpy(&new[bc->length], s, length);
bc->b = new;
bc->length += length;
}
void freeBuffer(struct bufferContainer *bc)
{
free(bc->b);
}
/*** OUTPUT OPERATIONS ***/
void ceditScroll()
{
Cedit.rowX = 0;
if (Cedit.cursorY < Cedit.rowNum)
{
Cedit.rowX = ceditRowCursorTransformCxtoRx(&Cedit.row[Cedit.cursorY], Cedit.cursorX);
}
if (Cedit.cursorY < Cedit.rowOff)
{
Cedit.rowOff = Cedit.cursorY;
}
if (Cedit.cursorY >= Cedit.rowOff + Cedit.terminalRows)
{
Cedit.rowOff = Cedit.cursorY - Cedit.terminalRows + 1;
}
if (Cedit.rowX < Cedit.columnOff)
{
Cedit.columnOff = Cedit.rowX;
}
if (Cedit.rowX >= Cedit.columnOff + Cedit.terminalColumns)
{
Cedit.columnOff = Cedit.rowX - Cedit.terminalColumns + 1;
}
}
void ceditPrintRows(struct bufferContainer *bc)
{
int y;
for (y = 0; y < Cedit.terminalRows; y++)
{
int fileRow = y + Cedit.rowOff;
if (fileRow >= Cedit.rowNum)