-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor.c
executable file
·1137 lines (989 loc) · 27.7 KB
/
editor.c
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
#include "editor.h"
#include "file.h"
#include "main.h"
#include "menu.h"
#include "colors.h"
#include "finder.h"
#include "keypress.h"
#include <graphx.h>
#include <fileioc.h>
#include <keypadc.h>
#include <fontlibc.h>
#include <stdbool.h>
#include <ti/getcsc.h>
#include <debug.h>
// does what it says
void wipeEditor();
void debug_printEditorLines(void);
void debug_printEditor(void);
// erases every variable in the editor
void wipeEditor()
{
// wipe text buffer (XXX)
for (int i = 0; i < MAX_DATA_SIZE + 1; i++)
{
editor.buffer[i] = 0;
}
for(int i = 0; i < MAX_LINES_ON_EDITOR_SCREEN; i++)
{
editor.linePointers[i] = NULL;
editor.lineTerminated[i] = false;
}
// initialize some buffer metadata
editor.bufferEnd = editor.buffer + MAX_DATA_SIZE - 1;
editor.dataSize = 0;
editor.file = NULL;
editor.lineOffset = 0;
editor.redrawText = false;
editor.redrawAll = false;
editor.cursorInsert = NULL;
editor.afterCursor = NULL;
editor.cursorRow = 0;
editor.cursorCol = 0;
editor.desiredCol = 0;
editor.textMode = LOWERCASE;
}
// loads the menu bar
void initEditor()
{
editor.menuBar = loadEditorMenuBar();
}
enum programState runEditor()
{
wipeEditor();
editor.file = &finder.files[finder.selectedFile];
loadFileData(editor.file);
editor_LoadUnwrappedScreen(editor.afterCursor, 0);
drawEditor();
// wait for all keys to be released before we start looping to prevent accidental input
waitForAllKeysReleased();
while(programState == EDITOR)
{
if(editor.redrawText == true)
{
editor.redrawText = false;
gfx_SetDrawBuffer();
drawEditorBackground();
drawEditorText();
drawEditorCursor();
gfx_SwapDraw();
}
kb_Scan();
if(kb_IsDown(kb_KeyClear))
{
while(kb_IsDown(kb_KeyClear)) kb_Scan();
return FINDER;
}
// moving cursor
else if(kb_Data[7])
{
if(kb_IsDown(kb_KeyRight))
{
moveCursorRight();
editor.redrawText = true;
debug_printEditor();
}
else if(kb_IsDown(kb_KeyLeft))
{
moveCursorLeft();
editor.redrawText = true;
debug_printEditor();
}
else if(kb_IsDown(kb_KeyUp))
{
moveCursorUp();
editor.redrawText = true;
debug_printEditor();
}
else if(kb_IsDown(kb_KeyDown))
{
moveCursorDown();
editor.redrawText = true;
debug_printEditor();
}
}
// text insertion
else
{
sk_key_t keyPressed = getSingleCSCKey();
char inputChar = '\0';
if(keyPressed)
{
inputChar = getCharFromKeyPress(editor.textMode, keyPressed, true);
bool successfulInput = inputCharacter(inputChar);
debug_printEditor();
}
}
}
return programState;
}
void drawEditor()
{
gfx_SetDrawBuffer();
drawEditorBackground();
drawMenuBar(editor.menuBar, -1);
drawEditorText();
drawEditorCursor();
gfx_BlitBuffer();
}
void drawEditorBackground()
{
// header bar
gfx_SetColor(editorBackgroundColor);
gfx_FillRectangle_NoClip(EDITOR_HEADER_BAR_X, EDITOR_HEADER_BAR_Y, EDITOR_HEADER_BAR_WIDTH, EDITOR_HEADER_BAR_HEIGHT - 1);
gfx_SetColor(editorHeaderBarBorderColor);
gfx_HorizLine_NoClip(EDITOR_HEADER_BAR_X, EDITOR_HEADER_BAR_Y + EDITOR_HEADER_BAR_HEIGHT - 1, EDITOR_HEADER_BAR_WIDTH);
// body
gfx_SetColor(editorBackgroundColor);
gfx_FillRectangle_NoClip(EDITOR_BODY_X, EDITOR_BODY_Y, EDITOR_HEADER_BAR_WIDTH, EDITOR_BODY_HEIGHT);
}
void drawEditorText()
{
fontlib_SetForegroundColor(black);
for(int i = 0, y = EDITOR_FIRST_LINE_Y; i < MAX_LINES_ON_EDITOR_SCREEN; i++, y+= EDITOR_LINE_VERT_SPACING)
{
fontlib_SetCursorPosition(2, y);
drawLine(editor.linePointers[i], editor.lineLengths[editor.lineOffset + i]);
}
}
struct menuBar *loadEditorMenuBar()
{
static struct menuBar menuBar =
{
.menues =
{
{
.name = "Home",
.numOptions = 0,
.funcPtrs =
{
NULL,
}
},
{
.name = "Actions",
.numOptions = 3,
.options =
{
"Go To End",
"Erase",
"Refresh"
},
.funcPtrs =
{
NULL,
NULL,
NULL,
}
},
{
.name = "Settings",
.numOptions = 0,
.funcPtrs =
{
NULL,
}
},
{
.name = "Edit",
.numOptions = 5,
.options =
{
"Undo",
"Redo",
"Paste",
"Find",
"Replace",
},
.funcPtrs =
{
NULL,
NULL,
NULL,
NULL,
NULL,
}
},
{
.name = "File",
.numOptions = 7,
.options =
{
"Save",
"Close",
"Rename",
"Clone",
"Encrypt",
"Info",
"Delete",
},
.funcPtrs =
{
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
}
},
}
};
return &menuBar;
}
char *editor_LoadWord(char *readPos, int *lenBuffer, int *widthBuffer, int maxWidth)
{
bool leftOfCursor;
char *prevReadPos;
int len = 0, width = 0;
// see editor.h line 27 for the buffer layout
// || (readPos >= editor.afterCursor && readPos <= editor.bufferEnd))
// first, we read words from the left of the cursor
if(readPos >= editor.buffer && readPos < editor.cursorInsert)
{
// when we hit a Null Byte or New Line Code, return a pointer to it
// treat spaces as individual words
while(*readPos != '\0' && *readPos !='\n' && readPos < editor.cursorInsert)
{
if(*readPos == ' ')
{
if(len == 0)
{
width = fontlib_GetGlyphWidth(' ');
len = 1;
readPos++;
}
break;
}
width += fontlib_GetGlyphWidth(*readPos);
prevReadPos = readPos;
readPos++;
len++;
// once a word gets too long, go back a character and return
if(width > maxWidth && len > 1)
{
// readPos = prevReadPos;
width -= fontlib_GetGlyphWidth(*readPos);
len--;
break;
}
if(leftOfCursor && readPos >= editor.cursorInsert)
{
dbg_printf("switching from left to right side of buffer\n");
leftOfCursor = false;
readPos = editor.afterCursor;
}
}
}
// if we got a bad read pointer for a parameter
else
{
*widthBuffer = 0;
*lenBuffer = 0;
return NULL;
}
*lenBuffer = len;
*widthBuffer = width;
// double check the reading pointer we return isn't in between the two two buffer sections
if(readPos >= editor.cursorInsert && readPos < editor.afterCursor)
{
readPos = editor.afterCursor;
}
return readPos;
}
char* editor_LoadWrappedLine(char *readPos, int *lenBuffer)
{
char *prevReadPos;
int lineLen = 0, lineWidth = 0;
int wordLen = 0, wordWidth = 0;
while(1)
{
prevReadPos = readPos;
readPos = editor_LoadWord(readPos, &wordLen, &wordWidth, MAX_LINE_PIXEL_WIDTH);
// we only get Null if we passed an invalid pointer to editor_LoadWord
if(readPos == NULL)
{
*lenBuffer = 0;
return NULL;
}
// if the word fits on the line
if(lineWidth + wordWidth <= MAX_LINE_PIXEL_WIDTH)
{
lineLen += wordLen;
lineWidth += wordWidth;
// // if the word ended with a space
// if(*readPos == ' ')
// {
// // if the space can fit on the line
// if((fontlib_GetGlyphWidth(' ') + lineWidth) <= MAX_LINE_PIXEL_WIDTH)
// {
// readPos++;
// lineWidth += fontlib_GetGlyphWidth(' ');
// lineLen++;
// }
// // if the space can't fit on the line
// else
// {
// break;
// }
// }
// if we hit a new line code
if(*readPos == '\n')
{
readPos++;
break;
}
// if we hit the end of the buffer
if(readPos > editor.bufferEnd)
{
*lenBuffer = lineLen;
dbg_printf("Finished reading out buffer\n");
return NULL;
}
// if the word was too long for a line and simply had to be trimmed
// else
// {
// lineLen = wordLen;
// break;
// }
}
// XXX perhaps will add a statement for if a word is really long...but for now loadWord cuts words off at the width of the screen
// so I guess I'm good for now?
// if there are too many words on the line to fit another, end the line
else
{
// go back to the last word
readPos = prevReadPos;
break;
}
}
*lenBuffer = lineLen;
return readPos;
}
char *editor_LoadUnwrappedLine(char *readPos, int maxWidth, int *lenBuffer, bool *lineTerminated)
{
*lenBuffer = 0;
*lineTerminated = false;
int width = 0;
while((readPos != NULL) && (readPos <= editor.bufferEnd))
{
// if we hit a newline code, return the next character after the code
if(*readPos == '\n')
{
*lineTerminated = true;
dbg_printf("\nreturning line readPos: %p\n\n", readPos);
return getNextBufferChar(readPos);
}
// if the character fits on the line, add it
else if(width + fontlib_GetGlyphWidth(*readPos) <= maxWidth)
{
width += fontlib_GetGlyphWidth(*readPos);
(*lenBuffer)++;
readPos = getNextBufferChar(readPos);
}
// if the character doesn't fit on the line, return it as the start of the next line
else
{
return readPos;
}
}
dbg_printf("Read to end of file. Last character was: %c\n", *(readPos - 1));
return NULL;
}
char *getNextLinePtrFromLoadedLine(char *start, int length)
{
if(start >= editor.afterCursor)
{
return start + length;
}
else
{
if(editor.cursorInsert - start >= length)
{
return start + length;
}
else
{
return editor.afterCursor + (length - (editor.cursorInsert - start));
}
}
}
char *getNextBufferChar(char *cur)
{
cur++;
if(cur > editor.bufferEnd)
{
return NULL;
}
else if((cur >= editor.cursorInsert) && (cur < editor.afterCursor))
{
return editor.afterCursor;
}
else
{
return cur;
}
}
char *getPrevBufferChar(char *cur)
{
cur--;
if(cur < editor.buffer)
{
return NULL;
}
if((cur >= editor.cursorInsert) && (cur < editor.afterCursor))
{
return editor.cursorInsert;
}
return cur;
}
void drawLine(char *start, int len)
{
int i = 0;
while((i < len) && (*start != '\n'))
{
fontlib_DrawGlyph(*start);
start = getNextBufferChar(start);
i++;
}
}
/*
bool editor_ScrollDownWrapped(void)
{
char *lastLine = editor.linePointers[MAX_LINES_ON_EDITOR_SCREEN -1];
int lastLineLen = editor.lineLengths[editor.lineOffset + MAX_LINES_ON_EDITOR_SCREEN - 1];
char *newLine = lastLine + lastLineLen;
int newLineLen;
// if there aren't any more lines to load
if((lastLine == NULL) || (newLine > editor.bufferEnd))
{
return false;
}
// since we can load another line, shift all the onscreen line pointers up one
for(int i = 0; i < MAX_LINES_ON_EDITOR_SCREEN - 1; i++)
{
editor.linePointers[i] = editor.linePointers[i + 1];
editor.lineLengths[i] = editor.lineLengths[i + 1];
}
// increment the line offset
editor.lineOffset++;
// set the pointer of the new line and load its length
editor.linePointers[MAX_LINES_ON_EDITOR_SCREEN - 1] = newLine;
// XXX change to load wrapped line
editor_LoadUnwrappedLine(newLine, &newLineLen, MAX_LINE_PIXEL_WIDTH);
editor.lineLengths[editor.lineOffset + MAX_LINES_ON_EDITOR_SCREEN - 1] = newLineLen;
// success!
return true;
}
*/
/*
bool editor_ScrollDownUnwrapped(void)
{
int newLineLen;
char *newLine;
// if all the text in the file doesn't add up to a full screen, then we obviously can't scroll down
if(editor.linePointers[MAX_LINES_ON_EDITOR_SCREEN - 1] == NULL)
{
return false;
}
// if we're on the last line, we can't scroll down any more
if(editor.linePointers[MAX_LINES_ON_EDITOR_SCREEN - 1] + editor.lineLengths[MAX_LINES_ON_EDITOR_SCREEN - 1] > editor.bufferEnd)
{
return false;
}
for(int i = 0; i < (MAX_LINES_ON_EDITOR_SCREEN - 1); i++)
{
editor.linePointers[i] = editor.linePointers[i + 1];
editor.lineLengths[i] = editor.lineLengths[i + 1];
}
// XXX refactor later (one if else statement)
// make the pointer to the new line at the bottom of the screen equal to the previous line plus the previous line's length
// BUT, take into account the buffer gap
if(editor.linePointers[MAX_LINES_ON_EDITOR_SCREEN - 2] < editor.cursorInsert)
{
// in the case that the cursor is on the bottom line, we jump over the buffer gap
if(editor.cursorInsert - editor.linePointers[MAX_LINES_ON_EDITOR_SCREEN - 2] < editor.lineLengths[MAX_LINES_ON_EDITOR_SCREEN - 2])
{
newLine = editor.afterCursor + editor.lineLengths[MAX_LINES_ON_EDITOR_SCREEN - 2] - (editor.cursorInsert - editor.linePointers[MAX_LINES_ON_EDITOR_SCREEN - 2]);
}
else
{
newLine = editor.linePointers[MAX_LINES_ON_EDITOR_SCREEN - 2] + editor.lineLengths[MAX_LINES_ON_EDITOR_SCREEN - 2];
}
}
else
{
newLine = editor.linePointers[MAX_LINES_ON_EDITOR_SCREEN - 2] + editor.lineLengths[MAX_LINES_ON_EDITOR_SCREEN - 2];
}
editor_LoadUnwrappedLine(newLine, &newLineLen, MAX_LINE_PIXEL_WIDTH);
editor.linePointers[MAX_LINES_ON_EDITOR_SCREEN - 1] = newLine;
editor.lineLengths[MAX_LINES_ON_EDITOR_SCREEN - 1] = newLineLen;
return true;
}
*/
// XXX take into account split buffer when cursorCol > 0 and is on bottom line and we're scrolling down :P
bool editor_ScrollDownUnwrapped(void)
{
char *lastLine = editor.linePointers[MAX_LINES_ON_EDITOR_SCREEN - 1];
int lastLineLen = editor.lineLengths[editor.lineOffset + MAX_LINES_ON_EDITOR_SCREEN - 1];
char *newLine = getNextLinePtrFromLoadedLine(lastLine, lastLineLen);
int newLineLen;
bool lineTerminated;
// if there aren't any more lines to load
if((lastLine == NULL) || newLine > editor.bufferEnd)
{
return false;
}
// since we can load another line, shift all the onscreen line pointers up one
for(int i = 0; i < MAX_LINES_ON_EDITOR_SCREEN - 1; i++)
{
editor.linePointers[i] = editor.linePointers[i + 1];
}
// increment the line offset
editor.lineOffset++;
// set the pointer of the new line and load its length
editor.linePointers[MAX_LINES_ON_EDITOR_SCREEN - 1] = newLine;
editor_LoadUnwrappedLine(newLine, MAX_LINE_PIXEL_WIDTH, &newLineLen, &lineTerminated);
editor.lineLengths[editor.lineOffset + MAX_LINES_ON_EDITOR_SCREEN - 1] = newLineLen;
return true;
}
bool editor_ScrollUpUnwrapped(void)
{
// if we can't scroll up, too bad so sad
if(editor.lineOffset < 1)
{
return false;
}
// shift all the line pointers down one
for(int i = (MAX_LINES_ON_EDITOR_SCREEN - 1); i >= 1; i--)
{
editor.linePointers[i] = editor.linePointers[i - 1];
}
// decrement the line offset
editor.lineOffset--;
// to find the pointer to the line we are scrolling up to,
// subtract its stored length from the pointer to the line right below it (MAKE SURE TO FACTOR IN THE SPLIT BUFFER and line pointers when the cursor column = 0)
if(editor.cursorCol == 0)
{
editor.linePointers[0] = editor.cursorInsert - editor.lineLengths[editor.lineOffset];
}
else
{
editor.linePointers[0] = editor.linePointers[1] - editor.lineLengths[editor.lineOffset];
}
// we scrolled up, so success!
return true;
}
void editor_LoadUnwrappedScreen(char *startingPtr, int startingLine)
{
int length;
bool isTerminated;
for(int i = startingLine, lineIndex = editor.lineOffset; (i < MAX_LINES_ON_EDITOR_SCREEN); i++, lineIndex++)
{
if(startingPtr == NULL)
{
editor.linePointers[i] = NULL;
editor.lineTerminated[i] = false;
editor.lineLengths[lineIndex] = 0;
}
else
{
editor.linePointers[i] = startingPtr;
startingPtr = editor_LoadUnwrappedLine(editor.linePointers[i], MAX_LINE_PIXEL_WIDTH, &length, &isTerminated);
editor.lineLengths[lineIndex] = length;
dbg_printf("\n\nloaded line with length %d and pointer %p\n\n", length, editor.linePointers[i]);
}
}
}
void drawEditorCursor(void)
{
int y = EDITOR_FIRST_LINE_Y + (editor.cursorRow * EDITOR_LINE_VERT_SPACING);
int x = getCursorX();
gfx_SetColor(cursorColor);
gfx_VertLine_NoClip(x, y, EDITOR_LINE_VERT_SPACING);
gfx_VertLine_NoClip(x + 1, y, EDITOR_LINE_VERT_SPACING);
}
int getCursorX(void)
{
char *linePtr = editor.linePointers[editor.cursorRow];
// we have a padding of 2 pixels on each side of the screen
// but the cursor should be slightly more to the left side, so make it 1 pixel
int x = 1;
for(int i = 0; i < editor.cursorCol; i++)
{
x += fontlib_GetGlyphWidth(*linePtr);
linePtr = getNextBufferChar(linePtr);
}
return x;
}
bool moveCursorLeft(void)
{
// check if moving the cursor left makes us scroll up
if(editor.cursorRow == 0 && editor.cursorCol == 0)
{
if(editor_ScrollUpUnwrapped() == true)
{
editor.cursorRow++;
}
else
{
return false;
}
}
// if we're not at the far left side of the line
if(editor.cursorCol > 0)
{
editor.cursorInsert--;
editor.afterCursor--;
editor.cursorCol--;
*(editor.afterCursor) = *(editor.cursorInsert);
*(editor.cursorInsert) = '\0';
}
// if we're going to have to move up a line
else if(editor.cursorCol == 0)
{
// if the line above ends with a newline, don't count the newline when calculating the cursor column
if(*(editor.cursorInsert - 1) == '\n')
{
editor.cursorCol = editor.lineLengths[editor.lineOffset + editor.cursorRow - 1];
editor.cursorRow--;
editor.afterCursor--;
*(editor.afterCursor) = '\n';
editor.cursorInsert--;
*(editor.cursorInsert) = '\0';
}
else
{
editor.cursorCol = editor.lineLengths[editor.lineOffset + editor.cursorRow - 1];
editor.cursorRow--;
}
}
// if we moved the cursor to the far left side of any line, we need to update the pointer to the beginning of the line
// to point to the right side of the split buffer
if(editor.cursorCol == 0)
{
// dbg_printf("updated line %d's pointer to %p\n", editor.lineOffset + editor.cursorRow, editor.afterCursor);
editor.linePointers[editor.cursorRow] = editor.afterCursor;
}
editor.desiredCol = editor.cursorCol;
// dbg_printf("Cursor Pos - Row: %d, Col: %d, Insert: %p, AfterCursor: %p, LinePointer: %p, Len: %d\n", editor.cursorRow, editor.cursorCol, editor.cursorInsert, editor.afterCursor, editor.linePointers[editor.cursorRow], editor.lineLengths[editor.lineOffset + editor.cursorRow]);
return true;
}
bool moveCursorRight(void)
{
// if the cursor is already at the end of the file, it can't move farther
// XXX replaced the >= with >, shouldn't cause problems but keep in mind
if(editor.afterCursor > editor.bufferEnd)
{
dbg_printf("At end of file\n");
return false;
}
// check if moving the cursor right makes us scroll down
// first check if we're on the last line...
if(editor.cursorRow == (MAX_LINES_ON_EDITOR_SCREEN - 1))
{
// then, check if the cursor is at the end of the visible line (excluding newline codes)
if((*(editor.afterCursor) == '\n') || (editor.cursorCol == editor.lineLengths[editor.lineOffset + editor.cursorRow]))
{
// XXX delete the return true (it's just for debugging purposes)
if(editor_ScrollDownUnwrapped() == true)
{
editor.cursorCol = 0;
if(*(editor.afterCursor) == '\n')
{
*editor.cursorInsert = '\n';
editor.cursorInsert++;
*editor.afterCursor = '\0';
*editor.afterCursor++; // XXX VERY SUS not sure what i was thinking earlier i might need to take off the *
}
}
else
{
dbg_printf("couldn't scroll down\n");
return false;
}
}
}
// if we have to update the pointer to the beginning of the line since we'll move the first character to the left side of the split buffer
// NOTE: the pointer is wrong until we have actually moved the first character into the left side of the split buffer
if(editor.cursorCol == 0)
{
editor.linePointers[editor.cursorRow] = editor.cursorInsert;
}
// if the line being moved through ends on a newline code, we have to move the code to the left side of the split buffer
if(*(editor.afterCursor) == '\n')
{
editor.cursorCol = 0;
editor.cursorRow++;
*(editor.cursorInsert) = *(editor.afterCursor);
editor.cursorInsert++;
*(editor.afterCursor) = '\0';
editor.afterCursor++;
}
// if the cursor needs to move down a line without updating the split buffer
else if(editor.cursorCol == editor.lineLengths[editor.lineOffset + editor.cursorRow])
{
editor.cursorCol = 0;
editor.cursorRow++;
}
else
{
*(editor.cursorInsert) = *(editor.afterCursor);
editor.cursorInsert++;
*(editor.afterCursor) = '\0';
editor.afterCursor++;
editor.cursorCol++;
}
editor.desiredCol = editor.cursorCol;
// dbg_printf("Cursor Pos - Row: %d, Col: %d, Insert: %p, AfterCursor: %p, LinePointer: %p, Len: %d\n", editor.cursorRow, editor.cursorCol, editor.cursorInsert, editor.afterCursor, editor.linePointers[editor.cursorRow], editor.lineLengths[editor.lineOffset + editor.cursorRow]);
return true;
}
bool moveCursorUp(void)
{
// if we're at the very beginning of the file, we can't move the cursor up
if((editor.cursorRow <= 0) && (editor.lineOffset <= 0) && (editor.cursorCol <= 0))
{
return false;
}
// if we're at the top of the screen, try scrolling up 1 line
if(editor.cursorRow == 0)
{
bool scrollUpHuh = editor_ScrollUpUnwrapped();
if(!scrollUpHuh)
{
dbg_printf("scrolling up failed. Moving cursor to the start of the line instead\n");
}
else
{
dbg_printf("scrolling up worked\n");
editor.cursorRow++;
}
}
int newCol;
int newRow;
int newLineLen;
int dataShiftSize;
// this if-else block calculates the new cursor position and how much data we need to shift to the right
// if our cursor is still on the first line (which means we couldn't scroll up), then move the cursor to the start of the line (standard behavior)
if(editor.cursorRow == 0)
{
dbg_printf("Moving to start of line\n");
newCol = 0;
newRow = 0;
editor.desiredCol = 0;
dataShiftSize = editor.cursorCol;
}
// if we're actually going to move up a line
else
{
dbg_printf("Moving up a line\n");
newCol = editor.desiredCol;
newRow = editor.cursorRow - 1;
newLineLen = editor.lineLengths[editor.lineOffset + newRow];
// if we have to move the cursor left to fit on a smaller line
if(newLineLen <= newCol)
{
if(*(editor.linePointers[newRow] + newLineLen) == '\n')
{
newCol = newLineLen - 1;
}
else
{
newCol = newLineLen;
}
}
dataShiftSize = editor.cursorInsert - (editor.linePointers[newRow] + newCol);
// update the current line's pointer, if necessary, to make it point to the right side of the split buffer
if(editor.cursorCol > 0)
{
editor.linePointers[editor.cursorRow] = editor.afterCursor - editor.cursorCol;
}
}
dbg_printf("Going to shift %d bytes\n", dataShiftSize);
// now that we know how much data to shift and all that stuff, actually shift it!
for(int i = 0; i < dataShiftSize; i++)
{
editor.afterCursor--;
editor.cursorInsert--;
*(editor.afterCursor) = *(editor.cursorInsert);
*(editor.cursorInsert) = '\0';
}
editor.cursorCol = newCol;
editor.cursorRow = newRow;
if(newCol == 0)
{
editor.linePointers[newRow] = editor.afterCursor;
}
dbg_printf("Editor data:\nCursor Insert: %p, After Cursor %p, curLine pointer %p\nCursorCol %d, CursorRow %d, Desired Col %d\n", editor.cursorInsert, editor.afterCursor, editor.linePointers[editor.cursorRow], editor.cursorCol, editor.cursorRow, editor.desiredCol);
return true;
}
// XXX maybe don't redraw everything when you move the cursor - just redraw the characters on either side of the cursor
bool moveCursorDown(void)
{
// XXX think through this statement. might be sus
if(editor.afterCursor > editor.bufferEnd)
{
return false;
}
// if we're at the bottom of the screen, try scrolling down a line
// if we're at the end of the file, simply move the cursor to the end of the line
if(editor.cursorRow == (MAX_LINES_ON_EDITOR_SCREEN - 1))
{
bool scrollDownHuh = editor_ScrollDownUnwrapped();
if(scrollDownHuh)
{
editor.cursorRow--;
}
else
{
moveCursorToEndOfLine();
return false;
}
}
// if we're on the last line anywhere, then move the cursor to the end of the current line
if(editor.linePointers[editor.cursorRow + 1] == NULL)
{
moveCursorToEndOfLine();
return true;
}
int newCursorCol = editor.lineLengths[editor.lineOffset + editor.cursorRow + 1];
if(*(editor.linePointers[editor.cursorRow + 1] + newCursorCol - 1) == '\n')
{
newCursorCol--;
}
if(newCursorCol > editor.desiredCol)
{
newCursorCol = editor.desiredCol;
}
// shift the buffer data left until the beginning of the next line
int shiftSize = (editor.linePointers[editor.cursorRow + 1] + newCursorCol) - editor.afterCursor;
for(int i = 0; i < shiftSize; i++)
{
*(editor.cursorInsert) = *(editor.afterCursor);
editor.cursorInsert++;
*(editor.afterCursor) = '\0';
editor.afterCursor++;
}
// update either the next line or the previous line's pointer to point to the left side of the buffer
// depending on which line's beginning we skipped over
if(editor.cursorCol > 0)
{
editor.linePointers[editor.cursorRow + 1] = editor.cursorInsert - newCursorCol;
}
else
{