forked from SuperIlu/DOjS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit.c
1206 lines (1089 loc) · 31.4 KB
/
edit.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
/*
MIT License
Copyright (c) 2019 Andre Seidelt <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <bios.h>
#include <conio.h>
#include <keys.h>
#include <pc.h>
#include "DOjS.h"
#include "dialog.h"
#include "edi_render.h"
#include "edit.h"
#include "lines.h"
#include "util.h"
/************
** defines **
************/
#define EDI_TEMPLATE BOOT_DIR "template.txt" //!< filename for template file
#define EDI_HELPFILE BOOT_DIR "help.txt" //!< filename for help file
#define EDI_CNP_SIZE 4096 //!< initial size of Copy&Paste buffer
/************************
** function prototypes **
************************/
static char* edi_load(edi_t* edi, char* fname);
static char* edi_save(edi_t* edi, char* fname);
static bool edi_do_up(edi_t* edi);
static bool edi_do_down(edi_t* edi);
static void edi_do_left(edi_t* edi);
static void edi_do_right(edi_t* edi);
static void edi_do_bs(edi_t* edi);
static void edi_do_del(edi_t* edi);
static void edi_do_enter(edi_t* edi, bool autoindent);
static void edi_do_wordright(edi_t* edi);
static void edi_do_wordleft(edi_t* edi);
static void edi_del_wordright(edi_t* edi);
static void edi_del_wordleft(edi_t* edi);
static void edi_do_tab(edi_t* edi);
static void edi_do_save(edi_t* edi);
static void edi_do_goto_line(edi_t* edi);
static bool edi_cmp(char* find, char* txt, int remainder);
static void edi_do_find(edi_t* edi, char find_buffer[80]);
static void edi_do_start(edi_t* edi);
static void edi_do_end(edi_t* edi);
static void edi_do_pageup(edi_t* edi);
static void edi_do_pagedown(edi_t* edi);
static void edi_do_insert_char(edi_t* edi, char ch);
static void edi_clear_cnp(edi_t* edi);
static void edi_copy_line(edi_t* edi, line_t* l, int start, int end, bool newline);
static void edi_do_copy(edi_t* edi);
static void edi_do_cut(edi_t* edi);
static void edi_do_paste(edi_t* edi);
static void edi_do_del_sel(edi_t* edi);
static edi_exit_t edi_loop(edi_t* edi);
static edi_t* edi_init(char* fname);
static void edi_shutdown(edi_t* edi);
static void edi_start_selection(edi_t* edi);
static void edi_goto_line(edi_t* edi, line_t* l, int line_num);
static void edi_do_del_line(edi_t* edi);
static void edi_do_backtab(edi_t* edi);
static char* edi_get_context(edi_t* edi);
/*********************
** static functions **
*********************/
/**
* @brief load file into edi at current line
*
* @param edi the edi system to work on.
* @param fname name of the file to load.
*
* @return an error message or NULL if all went well.
*/
static char* edi_load(edi_t* edi, char* fname) {
assert(edi);
assert(fname);
FILE* f = fopen(fname, "r");
if (!f) {
return strerror(errno);
}
int ch;
while ((ch = getc(f)) != EOF) {
if (ch == '\n') {
// new line
edi->current->newline = true;
line_t* l = lin_newline();
if (l) {
lin_insertline(edi, edi->current, l); // append a new line
edi->current = l;
} else {
fclose(f);
return "Out of memory!";
}
} else if (ch == '\t') {
lin_appendch(edi, edi->current, ' ');
while (edi->current->length % EDI_TABSIZE != 0) {
lin_appendch(edi, edi->current, ' '); // fill with spaces 'till next tabstop
}
} else if (ch != '\r') {
lin_appendch(edi, edi->current, ch);
}
}
fclose(f);
edi->top = edi->current = edi->first;
edi->x = 0;
edi->y = 0;
edi->changed = false;
edi->name = fname;
return NULL;
}
/**
* @brief save all lines from edi to file
*
* @param edi the edi system to work on.
* @param fname name to use for saving.
*
* @return an error message or NULL if all went well.
*/
static char* edi_save(edi_t* edi, char* fname) {
FILE* f = fopen(fname, "w");
if (!f) {
return strerror(errno);
}
line_t* l = edi->first;
while (l) {
fwrite(l->txt, l->length, 1, f);
if (l->newline) {
fputc('\n', f);
}
l = l->next;
}
fclose(f);
edi->changed = false;
return NULL;
}
/**
* @brief editor command: do UP.
*
* @param edi the edi system to work on.
*/
static bool edi_do_up(edi_t* edi) {
if (edi->current->prev) {
edi->current = edi->current->prev;
if (edi->y > 0) {
edi->y--;
} else {
edi->top = edi->top->prev;
}
if (edi->x > edi->current->length) {
edi->x = edi->current->length;
}
edi->num--;
return true;
} else {
return false;
}
}
/**
* @brief editor command: do DOWN.
*
* @param edi the edi system to work on.
*/
static bool edi_do_down(edi_t* edi) {
if (edi->current->next) {
edi->current = edi->current->next;
if (edi->y < edi->height - 2) {
edi->y++;
} else {
edi->top = edi->top->next;
}
if (edi->x > edi->current->length) {
edi->x = edi->current->length;
}
edi->num++;
return true;
} else {
return false;
}
}
/**
* @brief editor command: do LEFT.
*
* @param edi the edi system to work on.
*/
static void edi_do_left(edi_t* edi) {
if (edi->x > 0) {
edi->x--;
} else if (edi->current->prev) {
if (edi_do_up(edi)) {
edi->x = edi->current->length;
}
}
}
/**
* @brief editor command: do RIGHT.
*
* @param edi the edi system to work on.
*/
static void edi_do_right(edi_t* edi) {
if (edi->x < edi->current->length) {
edi->x++;
} else if (edi->current->next) {
if (edi_do_down(edi)) {
edi->x = 0;
}
}
}
/**
* @brief editor command: do BACKSPACE.
*
* @param edi the edi system to work on.
*/
static void edi_do_bs(edi_t* edi) {
if (edi->x == 0 && edi->current->prev) {
edi_do_up(edi);
int x = edi->current->length;
lin_joinnext(edi, edi->current);
edi->x = x;
} else {
if (edi->current->length > 0 && edi->x > 0) {
lin_delch_left(edi, edi->current, edi->x);
edi_do_left(edi);
}
}
}
/**
* @brief editor command: do DELETE.
*
* @param edi the edi system to work on.
*/
static void edi_do_del(edi_t* edi) {
if (edi->x >= edi->current->length && edi->current->next) {
lin_joinnext(edi, edi->current);
} else {
if (edi->current->length > 0 && edi->x < edi->current->length) {
lin_delch_right(edi, edi->current, edi->x);
}
}
}
/**
* @brief delete current line.
*
* @param edi the edi system to work on.
*/
static void edi_do_del_line(edi_t* edi) {
lin_removeline(edi, edi->current);
if (edi->x > edi->current->length) {
edi->x = edi->current->length;
}
}
/**
* @brief editor command: do NEWLINE.
*
* @param edi the edi system to work on.
* @param autoindent true for autoindentation, false to just insert a new line.
*/
static void edi_do_enter(edi_t* edi, bool autoindent) {
int leading_spaces = 0;
while ((leading_spaces < edi->current->length) && (edi->current->txt[leading_spaces] == ' ')) {
leading_spaces++;
}
if (edi->x == edi->current->length) {
line_t* l = lin_newline();
if (l) {
l->newline = true;
lin_insertline(edi, edi->current, l);
}
} else {
lin_splitline(edi, edi->current, edi->x);
}
edi->x = 0;
edi_do_down(edi);
if (autoindent) {
for (int i = 0; i < leading_spaces; i++) {
edi_do_insert_char(edi, ' ');
}
}
}
/**
* @brief editor command: jump one word right.
*
* @param edi the edi system to work on.
*/
static void edi_do_wordright(edi_t* edi) {
if (isblank(edi->current->txt[edi->x])) {
while (isblank(edi->current->txt[edi->x])) {
edi_do_right(edi);
}
} else if (isalnum(edi->current->txt[edi->x])) {
while (isalnum(edi->current->txt[edi->x])) {
edi_do_right(edi);
}
} else {
edi_do_right(edi);
}
}
/**
* @brief editor command: jump one word left.
*
* @param edi the edi system to work on.
*/
static void edi_do_wordleft(edi_t* edi) {
if (edi->x > 0 && isblank(edi->current->txt[edi->x - 1])) {
while (edi->x > 0 && isblank(edi->current->txt[edi->x - 1])) {
edi_do_left(edi);
}
} else if (edi->x > 0 && isalnum(edi->current->txt[edi->x - 1])) {
while (edi->x > 0 && isalnum(edi->current->txt[edi->x - 1])) {
edi_do_left(edi);
}
} else {
edi_do_left(edi);
}
}
/**
* @brief editor command: delete word to the right.
*
* @param edi the edi system to work on.
*/
static void edi_del_wordright(edi_t* edi) {
if (isalnum(edi->current->txt[edi->x])) {
while (isalnum(edi->current->txt[edi->x])) {
edi_do_del(edi);
}
} else {
edi_do_del(edi);
}
}
/**
* @brief editor command: delete one word to the left.
*
* @param edi the edi system to work on.
*/
static void edi_del_wordleft(edi_t* edi) {
if (edi->x > 0 && isalnum(edi->current->txt[edi->x - 1])) {
while (edi->x > 0 && isalnum(edi->current->txt[edi->x - 1])) {
edi_do_bs(edi);
}
} else {
edi_do_bs(edi);
}
}
/**
* @brief editor command: insert TAB.
*
* @param edi the edi system to work on.
*/
static void edi_do_tab(edi_t* edi) {
lin_insertch(edi, edi->current, edi->x, ' ');
edi_do_right(edi);
while (edi->x % EDI_TABSIZE != 0) {
lin_insertch(edi, edi->current, edi->x, ' ');
edi_do_right(edi);
}
}
/**
* @brief remove up to 4 leading spaces.
*
* @param edi the edi system to work on.
*/
static void edi_do_backtab(edi_t* edi) {
int oldX = edi->x;
edi->x = 0;
int i = 0;
while ((i < EDI_TABSIZE) && (edi->current->txt[0] == ' ')) {
edi_do_del(edi);
i++;
}
edi->x = oldX - i;
}
/**
* @brief editor command: save the file.
*
* @param edi the edi system to work on.
*/
static void edi_do_save(edi_t* edi) {
char* error = edi_save(edi, edi->name);
if (error) {
dia_show_message(edi, error);
}
}
/**
* @brief go to line nr in file.
*
* @param edi the edi system to work on.
*/
static void edi_do_goto_line(edi_t* edi) {
char buff[DIA_ASK_SIZE];
buff[0] = 0;
dia_ask_text(edi, buff, "1234567890", "<Enter Line number, ENTER or ESC>");
int line = atoi(buff) - 1;
line_t* l = lin_find(edi, line);
if (l) {
edi_goto_line(edi, l, line);
} else {
dia_show_message(edi, "Line not found!");
}
}
/**
* @brief go to specific line.
*
* @param edi the edi system to work on.
* @param l the line.
* @param line_num the line number.
*/
static void edi_goto_line(edi_t* edi, line_t* l, int line_num) {
if (l) {
edi->current = edi->top = l;
if (edi->x > edi->current->length) {
edi->x = edi->current->length;
}
edi->y = 0;
edi->num = line_num;
}
}
/**
* @brief compare find to txt.
*
* @param find null terminated string to find.
* @param txt text buffer (not null terminated).
* @param remainder number of chars left in buffer.
*
* @return true if the string was found
* @return false if the string was not found
*/
static bool edi_cmp(char* find, char* txt, int remainder) {
if (remainder < strlen(find)) {
return false;
}
while (*find) {
if (tolower(*find) != tolower(*txt)) {
return false;
}
txt++;
find++;
}
return true;
}
/**
* @brief try to find string in edi startin with current line/x-pos
*
* @param edi the edi system to work on.
* @param find_buffer the search string
*/
static void edi_do_find(edi_t* edi, char find_buffer[80]) {
if (strlen(find_buffer)) {
int xPos = edi->x + 1;
line_t* l = edi->current;
int line_inc = 0;
while (true) {
if (edi_cmp(find_buffer, &l->txt[xPos], l->length - xPos)) {
// found
edi->current = edi->top = l;
edi->x = xPos;
edi->y = 0;
edi->num += line_inc;
break;
}
if (xPos < l->length) {
xPos++;
} else if (l->next) {
l = l->next;
line_inc++;
xPos = 0;
} else {
// EOF
dia_show_message(edi, "Search string not found.");
break;
}
}
}
}
/**
* @brief jump to start of file.
*
* @param edi the edi system to work on.
*/
static void edi_do_start(edi_t* edi) {
edi->current = edi->top = edi->first;
edi->x = 0;
edi->y = 0;
edi->num = 1;
}
/**
* @brief jump to end of file.
*
* @param edi the edi system to work on.
*/
static void edi_do_end(edi_t* edi) {
line_t* l = edi->current;
while (l->next) {
l = l->next;
edi->num++;
}
edi->current = edi->top = l;
edi->x = l->length;
edi->y = 0;
}
/**
* @brief jump up one page.
*
* @param edi the edi system to work on.
*/
static void edi_do_pageup(edi_t* edi) {
int i = edi->height - 1;
line_t* new_top = edi->top;
line_t* new_cur = edi->current;
while (i && (new_top->prev != NULL) && (new_cur->prev != NULL)) {
new_top = new_top->prev;
new_cur = new_cur->prev;
i--;
edi->num--;
}
assert(new_top);
assert(new_cur);
edi->top = new_top;
edi->current = new_cur;
if (edi->x > edi->current->length) {
edi->x = edi->current->length;
}
}
/**
* @brief jump down one page.
*
* @param edi the edi system to work on.
*/
static void edi_do_pagedown(edi_t* edi) {
int i = edi->height - 1;
line_t* new_top = edi->top;
line_t* new_cur = edi->current;
while (i && (new_top->next != NULL) && (new_cur->next != NULL)) {
new_top = new_top->next;
new_cur = new_cur->next;
i--;
edi->num++;
}
assert(new_top);
assert(new_cur);
edi->top = new_top;
edi->current = new_cur;
if (edi->x > edi->current->length) {
edi->x = edi->current->length;
}
}
/**
* @brief insert a character at cursor location.
*
* @param edi the edi system to work on.
* @param ch the character to insert (no newlines, these must be inserted using edi_do_enter()).
*/
static void edi_do_insert_char(edi_t* edi, char ch) {
lin_insertch(edi, edi->current, edi->x, ch);
edi_do_right(edi);
}
/**
* @brief clear current cnp buffer.
*
* @param edi the edi system to work on.
*/
static void edi_clear_cnp(edi_t* edi) {
if (!edi->cnp) {
edi->cnp = malloc(EDI_CNP_SIZE); // TODO: handle alloc failure differently?
assert(edi->cnp);
}
edi->cnp[0] = 0;
edi->cnp_pos = 0;
edi->cnp_size = EDI_CNP_SIZE;
}
/**
* @brief add the given line to the current cnp-buffer.
*
* @param edi the edi system to work on.
* @param l the line.
* @param start first character of line to add.
* @param end last character of line to add.
* @param newline true to terminate the copy with a newline, false to just copy the line and not add a newline.
*/
static void edi_copy_line(edi_t* edi, line_t* l, int start, int end, bool newline) {
if (end - start + 2 > edi->cnp_size - edi->cnp_pos) {
int newSize = edi->cnp_size * 2;
edi->cnp = realloc(edi->cnp, newSize); // TODO: handle alloc failure differently?
assert(edi->cnp);
edi->cnp_size = newSize;
}
volatile char* dst = edi->cnp;
volatile char* src = l->txt;
while (start < end) {
dst[edi->cnp_pos] = src[start];
start++;
edi->cnp_pos++;
}
// append newline and null byte
if (newline) {
edi->cnp[edi->cnp_pos] = '\n';
edi->cnp_pos++;
}
edi->cnp[edi->cnp_pos] = 0;
}
/**
* @brief handle CTRL-C copy command.
*
* @param edi the edi system to work on.
*/
static void edi_do_copy(edi_t* edi) {
if (EDI_IS_SEL(edi)) {
edi_clear_cnp(edi);
cnp_t cnp;
edi_get_cnp(edi, &cnp);
line_t* sLine = lin_find(edi, cnp.startY - 1);
line_t* eLine = lin_find(edi, cnp.endY - 1);
if (sLine == eLine) {
// do copy when start==end
edi_copy_line(edi, sLine, cnp.startX, cnp.endX, false);
} else {
// do copy for multiple lines
edi_copy_line(edi, sLine, cnp.startX, sLine->length, true); // copy from first line
sLine = sLine->next;
// copy in-between lines
while (sLine != eLine) {
edi_copy_line(edi, sLine, 0, sLine->length, true);
EDIF("%s\n", sLine->txt);
sLine = sLine->next;
}
// copy last line
edi_copy_line(edi, sLine, 0, cnp.endX, false);
}
}
if (edi->cnp && edi->cnp_pos > 0) {
EDIF("cnp='%s'\n", edi->cnp);
} else {
EDIF("cnp='%s'\n", "EMPTY");
}
}
/**
* @brief handle CTRL-X cut command.
*
* @param edi the edi system to work on.
*/
static void edi_do_cut(edi_t* edi) {
edi_do_copy(edi);
edi_do_del_sel(edi);
}
/**
* @brief handle CTRL-V paste command.
*
* @param edi the edi system to work on.
*/
static void edi_do_paste(edi_t* edi) {
if (edi->cnp_pos) {
if (EDI_IS_SEL(edi)) {
edi_do_del_sel(edi);
}
// now paste text
for (int i = 0; i < edi->cnp_pos; i++) {
if (edi->cnp[i] == '\n') {
edi_do_enter(edi, false);
} else {
edi_do_insert_char(edi, edi->cnp[i]);
}
}
edi_clear_selection(edi);
}
}
/**
* @brief delete selected text.
*
* @param edi the edi system to work on.
*/
static void edi_do_del_sel(edi_t* edi) {
if (EDI_IS_SEL(edi)) {
cnp_t cnp;
edi_get_cnp(edi, &cnp);
line_t* sLine = lin_find(edi, cnp.startY - 1);
line_t* eLine = lin_find(edi, cnp.endY - 1);
if (sLine == eLine) {
edi->x = cnp.startX;
// delete when start==end
for (int i = 0; i < cnp.endX - cnp.startX; i++) {
lin_delch_right(edi, sLine, cnp.startX);
}
} else {
if (cnp.cursor_at_end) {
// move up until cursor is at line after selection start
while (edi->current != sLine->next) {
edi_do_up(edi);
EDIF("UP cur=%p, sLine=%p, eLine=%p\n", edi->current, sLine, eLine);
}
} else {
// move down once
edi_do_down(edi);
EDIF("DOWN cur=%p, sLine=%p, eLine=%p\n", edi->current, sLine, eLine);
}
// now we are at the line after selection start, delete all lines until sLine is next to eLine
while (sLine->next != eLine) {
edi_do_del_line(edi);
EDIF("DEL cur=%p, sLine=%p, eLine=%p\n", edi->current, sLine, eLine);
}
edi_do_up(edi);
edi->x = cnp.startX;
// now lines are next to each other and cursor at start of selection
int del_length = (sLine->length - cnp.startX) + cnp.endX + 1;
for (int i = 0; i < del_length; i++) {
EDIF("Now at %d, '%s'\n", edi->x, sLine->txt);
edi_do_del(edi);
}
}
edi_clear_selection(edi);
}
}
/**
* @brief extract the HELP context under the cursor
*
* @param edi the edi system to work on.
*
* @return a string buffer if a context could be found or NULL if not. The buffer needs to be free()d.
*/
static char* edi_get_context(edi_t* edi) {
int start = edi->x;
int end = edi->x;
// find first char of context
while (start > 0 && isalnum(edi->current->txt[start])) {
start--;
}
start++;
// find last char of context
while (end < edi->current->length && isalnum(edi->current->txt[end])) {
end++;
}
if (start == end) {
return NULL;
}
int ctx_size = (end - start);
EDIF("start=%d, end=%d, size=%d, txt[start]=%c, txt[end]=%c\n", start, end, ctx_size, edi->current->txt[start], edi->current->txt[end]);
char* ctx = malloc(ctx_size + 1);
if (!ctx) {
return NULL;
}
memcpy(ctx, &edi->current->txt[start], ctx_size);
ctx[ctx_size] = 0x00;
EDIF("ctx=%s\n", ctx);
return ctx;
}
/**
* @brief editor input loop.
*
* @param edi the edi system to work on.
*
* @return description how the user quit the editor.
*/
static edi_exit_t edi_loop(edi_t* edi) {
int last_help_pos = 0;
char find_buffer[DIA_ASK_SIZE] = {0};
edi_exit_t exit = EDI_KEEPRUNNING;
while (exit == EDI_KEEPRUNNING) {
int ch = getxkey();
int shift = bioskey(_NKEYBRD_SHIFTSTATUS);
// EDIF("ch = %02X, shift = %02X\n", ch, shift);
switch (ch) {
case K_Left:
case K_ELeft:
if (EDI_SHIFT_DOWN(shift)) {
edi_start_selection(edi);
} else {
edi_clear_selection(edi);
}
edi_do_left(edi);
break;
case K_Right:
case K_ERight:
if (EDI_SHIFT_DOWN(shift)) {
edi_start_selection(edi);
} else {
edi_clear_selection(edi);
}
edi_do_right(edi);
break;
case K_Down:
case K_EDown:
if (EDI_SHIFT_DOWN(shift)) {
edi_start_selection(edi);
} else {
edi_clear_selection(edi);
}
edi_do_down(edi);
break;
case K_Up:
case K_EUp:
if (EDI_SHIFT_DOWN(shift)) {
edi_start_selection(edi);
} else {
edi_clear_selection(edi);
}
edi_do_up(edi);
break;
case K_BackSpace:
if (EDI_IS_SEL(edi)) {
edi_do_del_sel(edi);
} else {
edi_do_bs(edi);
}
break;
case K_Control_Backspace:
edi_del_wordleft(edi);
break;
case K_Control_Delete:
case K_Control_EDelete:
edi_del_wordright(edi);
break;
case K_Delete:
case K_EDelete:
if (EDI_IS_SEL(edi)) {
edi_do_del_sel(edi);
} else {
edi_do_del(edi);
}
break;
case K_Control_D: // delete line
edi_do_del_line(edi);
break;
case K_Control_L: // jump to line
edi_do_goto_line(edi);
break;
case K_Control_C: // Copy
edi_do_copy(edi);
break;
case K_Control_X: // Cut
edi_do_cut(edi);
break;
case K_Control_V: // Paste
edi_do_paste(edi);
break;
case K_Return:
edi_do_enter(edi, true);
break;
case K_Home:
case K_EHome:
if (EDI_SHIFT_DOWN(shift)) {
edi_start_selection(edi);
} else {
edi_clear_selection(edi);
}
edi->x = 0;
break;
case K_End:
case K_EEnd:
if (EDI_SHIFT_DOWN(shift)) {
edi_start_selection(edi);
} else {
edi_clear_selection(edi);
}
edi->x = edi->current->length;
break;
case K_Control_Home:
case K_Control_EHome:
if (EDI_SHIFT_DOWN(shift)) {
edi_start_selection(edi);
} else {
edi_clear_selection(edi);
}
edi_do_start(edi);
break;
case K_Control_End:
case K_Control_EEnd:
if (EDI_SHIFT_DOWN(shift)) {
edi_start_selection(edi);
} else {
edi_clear_selection(edi);
}
edi_do_end(edi);
break;
case K_Control_Left:
case K_Control_ELeft:
if (EDI_SHIFT_DOWN(shift)) {
edi_start_selection(edi);
} else {
edi_clear_selection(edi);
}
edi_do_wordleft(edi);
break;
case K_Control_Right:
case K_Control_ERight:
if (EDI_SHIFT_DOWN(shift)) {
edi_start_selection(edi);