-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtedit.c
1945 lines (1680 loc) · 37.5 KB
/
tedit.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
#define _POSIX_C_SOURCE 200809L
#include <ncurses.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <termios.h>
#include "keys.h"
#define NEW_LINE "\n"
#define O_BINARY 0
#define MINEXTEND 32768
#define LINEBUF_EXTRA 32
#define TABSIZE 8
#define CLRSCR "\033[0J"
#define CLREOL "\033[K"
#define GOTOXY "\033[%d;%dH"
#define RESET_COLOR "\033[0m"
#ifdef COLOR
#define TEXT_COLOR "\033[44m\033[37m\033[1m"
#define SELECT_COLOR "\033[47m\033[37m\033[1m"
#define STATUS_COLOR "\033[0m\033[47m\033[30m"
#else
#define TEXT_COLOR "\033[0m"
#define SELECT_COLOR "\033[7m\033[1m"
#define STATUS_COLOR "\033[1m\033[7m"
#endif
//
// Editor data block
//
// Structure of split buffer:
//
// +------------------+------------------+------------------+
// | text before gap | gap | text after gap |
// +------------------+------------------+------------------+
// ^ ^ ^ ^
// | | | |
// start gap rest end
//
struct env;
struct undo {
int pos; // Editor position
int erased; // Size of erased contents
int inserted; // Size of inserted contents
unsigned char *undobuf; // Erased contents for undo
unsigned char *redobuf; // Inserted contents for redo
struct undo *next; // Next undo buffer
struct undo *prev; // Previous undo buffer
};
struct editor {
unsigned char *start; // Start of text buffer
unsigned char *gap; // Start of gap
unsigned char *rest; // End of gap
unsigned char *end; // End of text buffer
int toppos; // Text position for current top screen line
int topline; // Line number for top of screen
int margin; // Position for leftmost column on screen
int linepos; // Text position for current line
int line; // Current document line
int col; // Current document column
int lastcol; // Remembered column from last horizontal navigation
int anchor; // Anchor position for selection
struct undo *undohead; // Start of undo buffer list
struct undo *undotail; // End of undo buffer list
struct undo *undo; // Undo/redo boundary
int refresh; // Flag to trigger screen redraw
int lineupdate; // Flag to trigger redraw of current line
int dirty; // Dirty flag is set when the editor buffer has been changed
int newfile; // File is a new file
struct env *env; // Reference global editor environment
struct editor *next; // Next editor.
struct editor *prev; // Previous editor
char filename[FILENAME_MAX];
};
struct env {
struct editor *current; // Current editor
char *clipboard; // Clipboard
int clipsize; // Clipboard size
char *search; // Search text.
char *linebuf; // Scratch buffer.
int cols; // Console columns
int lines; // Console lines
int untitled; // Counter for untitled files
};
//
// Editor buffer functions
//
void clear_undo(struct editor *ed) {
struct undo *undo = ed->undohead;
while (undo) {
struct undo *next = undo->next;
free(undo->undobuf);
free(undo->redobuf);
free(undo);
undo = next;
}
ed->undohead = ed->undotail = ed->undo = NULL;
}
void reset_undo(struct editor *ed) {
while (ed->undotail != ed->undo) {
struct undo *undo = ed->undotail;
if (!undo) {
ed->undohead = NULL;
ed->undotail = NULL;
break;
}
ed->undotail = undo->prev;
if (undo->prev)
undo->prev->next = NULL;
free(undo->undobuf);
free(undo->redobuf);
free(undo);
}
ed->undo = ed->undotail;
}
struct editor *create_editor(struct env *env) {
struct editor *ed = (struct editor *) malloc(sizeof(struct editor));
memset(ed, 0, sizeof(struct editor));
if (env->current) {
ed->next = env->current->next;
ed->prev = env->current;
env->current->next->prev = ed;
env->current->next = ed;
} else {
ed->next = ed->prev = ed;
}
ed->env = env;
env->current = ed;
return ed;
}
void delete_editor(struct editor *ed) {
if (ed->next == ed) {
ed->env->current = NULL;
} else {
ed->env->current = ed->prev;
}
ed->next->prev = ed->prev;
ed->prev->next = ed->next;
if (ed->start)
free(ed->start);
clear_undo(ed);
free(ed);
}
struct editor *find_editor(struct env *env, char *filename) {
char fn[FILENAME_MAX];
struct editor *ed = env->current;
struct editor *start = ed;
if (!realpath(filename, fn))
strcpy(fn, filename);
do {
if (strcmp(fn, ed->filename) == 0)
return ed;
ed = ed->next;
} while (ed != start);
return NULL;
}
int new_file(struct editor *ed, char *filename) {
if (*filename) {
strcpy(ed->filename, filename);
} else {
sprintf(ed->filename, "Untitled-%d", ++ed->env->untitled);
ed->newfile = 1;
}
ed->start = (unsigned char *) malloc(MINEXTEND);
if (!ed->start)
return -1;
#ifdef DEBUG
memset(ed->start, 0, MINEXTEND);
#endif
ed->gap = ed->start;
ed->rest = ed->end = ed->gap + MINEXTEND;
ed->anchor = -1;
return 0;
}
int load_file(struct editor *ed, char *filename) {
struct stat statbuf;
int length;
if (!realpath(filename, ed->filename))
return -1;
int f = open(ed->filename, O_RDONLY | O_BINARY);
if (f < 0)
return -1;
if (fstat(f, &statbuf) < 0)
goto err;
length = statbuf.st_size;
ed->start = (unsigned char *) malloc(length + MINEXTEND);
if (!ed->start)
goto err;
#ifdef DEBUG
memset(ed->start, 0, length + MINEXTEND);
#endif
if (read(f, ed->start, length) != length)
goto err;
ed->gap = ed->start + length;
ed->rest = ed->end = ed->gap + MINEXTEND;
ed->anchor = -1;
close(f);
return 0;
err: close(f);
if (ed->start) {
free(ed->start);
ed->start = NULL;
}
return -1;
}
int save_file(struct editor *ed) {
int f;
f = open(ed->filename, O_CREAT | O_TRUNC | O_WRONLY, 0644);
if (f < 0)
return -1;
if (write(f, ed->start, ed->gap - ed->start) != ed->gap - ed->start)
goto err;
if (write(f, ed->rest, ed->end - ed->rest) != ed->end - ed->rest)
goto err;
close(f);
ed->dirty = 0;
clear_undo(ed);
return 0;
err: close(f);
return -1;
}
int text_length(struct editor *ed) {
return (ed->gap - ed->start) + (ed->end - ed->rest);
}
unsigned char *text_ptr(struct editor *ed, int pos) {
unsigned char *p = ed->start + pos;
if (p >= ed->gap)
p += (ed->rest - ed->gap);
return p;
}
void move_gap(struct editor *ed, int pos, int minsize) {
int gapsize = ed->rest - ed->gap;
unsigned char *p = text_ptr(ed, pos);
if (minsize < 0)
minsize = 0;
if (minsize <= gapsize) {
if (p != ed->rest) {
if (p < ed->gap) {
memmove(p + gapsize, p, ed->gap - p);
} else {
memmove(ed->gap, ed->rest, p - ed->rest);
}
ed->gap = ed->start + pos;
ed->rest = ed->gap + gapsize;
}
} else {
int newsize;
unsigned char *start;
unsigned char *gap;
unsigned char *rest;
unsigned char *end;
if (gapsize + MINEXTEND > minsize)
minsize = gapsize + MINEXTEND;
newsize = (ed->end - ed->start) - gapsize + minsize;
start = (unsigned char *) malloc(newsize); // TODO check for out of memory
gap = start + pos;
rest = gap + minsize;
end = start + newsize;
if (p < ed->gap) {
memcpy(start, ed->start, pos);
memcpy(rest, p, ed->gap - p);
memcpy(end - (ed->end - ed->rest), ed->rest, ed->end - ed->rest);
} else {
memcpy(start, ed->start, ed->gap - ed->start);
memcpy(start + (ed->gap - ed->start), ed->rest, p - ed->rest);
memcpy(rest, p, ed->end - p);
}
free(ed->start);
ed->start = start;
ed->gap = gap;
ed->rest = rest;
ed->end = end;
}
#ifdef DEBUG
memset(ed->gap, 0, ed->rest - ed->gap);
#endif
}
void close_gap(struct editor *ed) {
int len = text_length(ed);
move_gap(ed, len, 1);
ed->start[len] = 0;
}
int get(struct editor *ed, int pos) {
unsigned char *p = text_ptr(ed, pos);
if (p >= ed->end)
return -1;
return *p;
}
int copy(struct editor *ed, unsigned char *buf, int pos, int len) {
unsigned char *bufptr = buf;
unsigned char *p = ed->start + pos;
if (p >= ed->gap)
p += (ed->rest - ed->gap);
while (len > 0) {
if (p == ed->end)
break;
*bufptr++ = *p;
len--;
if (++p == ed->gap)
p = ed->rest;
}
return bufptr - buf;
}
void replace(struct editor *ed, int pos, int len, unsigned char *buf,
int bufsize, int doundo) {
unsigned char *p = ed->start + pos;
struct undo *undo;
// Store undo information
if (doundo) {
reset_undo(ed);
undo = ed->undotail;
if (undo && len == 0 && bufsize == 1 && undo->erased == 0
&& pos == undo->pos + undo->inserted) {
// Insert character at end of current redo buffer
undo->redobuf = realloc(undo->redobuf, undo->inserted + 1);
undo->redobuf[undo->inserted] = *buf;
undo->inserted++;
} else if (undo && len == 1 && bufsize == 0 && undo->inserted == 0
&& pos == undo->pos) {
// Erase character at end of current undo buffer
undo->undobuf = realloc(undo->undobuf, undo->erased + 1);
undo->undobuf[undo->erased] = get(ed, pos);
undo->erased++;
} else if (undo && len == 1 && bufsize == 0 && undo->inserted == 0
&& pos == undo->pos - 1) {
// Erase character at beginning of current undo buffer
undo->pos--;
undo->undobuf = realloc(undo->undobuf, undo->erased + 1);
memmove(undo->undobuf + 1, undo->undobuf, undo->erased);
undo->undobuf[0] = get(ed, pos);
undo->erased++;
} else {
// Create new undo buffer
undo = (struct undo *) malloc(sizeof(struct undo));
if (ed->undotail)
ed->undotail->next = undo;
undo->prev = ed->undotail;
undo->next = NULL;
ed->undotail = ed->undo = undo;
if (!ed->undohead)
ed->undohead = undo;
undo->pos = pos;
undo->erased = len;
undo->inserted = bufsize;
undo->undobuf = undo->redobuf = NULL;
if (len > 0) {
undo->undobuf = malloc(len);
copy(ed, undo->undobuf, pos, len);
}
if (bufsize > 0) {
undo->redobuf = malloc(bufsize);
memcpy(undo->redobuf, buf, bufsize);
}
}
}
if (bufsize == 0 && p <= ed->gap && p + len >= ed->gap) {
// Handle deletions at the edges of the gap
ed->rest += len - (ed->gap - p);
ed->gap = p;
} else {
// Move the gap
move_gap(ed, pos + len, bufsize - len);
// Replace contents
memcpy(ed->start + pos, buf, bufsize);
ed->gap = ed->start + pos + bufsize;
}
// Mark buffer as dirty
ed->dirty = 1;
}
void insert(struct editor *ed, int pos, unsigned char *buf, int bufsize) {
replace(ed, pos, 0, buf, bufsize, 1);
}
void erase_section(struct editor *ed, int pos, int len) {
replace(ed, pos, len, NULL, 0, 1);
}
//
// Navigation functions
//
int line_length(struct editor *ed, int linepos) {
int pos = linepos;
while (1) {
int ch = get(ed, pos);
if (ch < 0 || ch == '\n' || ch == '\r')
break;
pos++;
}
return pos - linepos;
}
int line_start(struct editor *ed, int pos) {
while (1) {
if (pos == 0)
break;
if (get(ed, pos - 1) == '\n')
break;
pos--;
}
return pos;
}
int next_line(struct editor *ed, int pos) {
while (1) {
int ch = get(ed, pos);
if (ch < 0)
return -1;
pos++;
if (ch == '\n')
return pos;
}
}
int prev_line(struct editor *ed, int pos) {
if (pos == 0)
return -1;
while (pos > 0) {
int ch = get(ed, --pos);
if (ch == '\n')
break;
}
while (pos > 0) {
int ch = get(ed, --pos);
if (ch == '\n')
return pos + 1;
}
return 0;
}
int column(struct editor *ed, int linepos, int col) {
unsigned char *p = text_ptr(ed, linepos);
int c = 0;
while (col > 0) {
if (p == ed->end)
break;
if (*p == '\t') {
int spaces = TABSIZE - c % TABSIZE;
c += spaces;
} else {
c++;
}
col--;
if (++p == ed->gap)
p = ed->rest;
}
return c;
}
void moveto(struct editor *ed, int pos, int center) {
int scroll = 0;
for (;;) {
int cur = ed->linepos + ed->col;
if (pos < cur) {
if (pos >= ed->linepos) {
ed->col = pos - ed->linepos;
} else {
ed->col = 0;
ed->linepos = prev_line(ed, ed->linepos);
ed->line--;
if (ed->topline > ed->line) {
ed->toppos = ed->linepos;
ed->topline--;
ed->refresh = 1;
scroll = 1;
}
}
} else if (pos > cur) {
int next = next_line(ed, ed->linepos);
if (next == -1) {
ed->col = text_length(ed) - ed->linepos;
break;
} else if (pos < next) {
ed->col = pos - ed->linepos;
} else {
ed->col = 0;
ed->linepos = next;
ed->line++;
if (ed->line >= ed->topline + ed->env->lines) {
ed->toppos = next_line(ed, ed->toppos);
ed->topline++;
ed->refresh = 1;
scroll = 1;
}
}
} else {
break;
}
}
if (scroll && center) {
int tl = ed->line - ed->env->lines / 2;
if (tl < 0)
tl = 0;
for (;;) {
if (ed->topline > tl) {
ed->toppos = prev_line(ed, ed->toppos);
ed->topline--;
} else if (ed->topline < tl) {
ed->toppos = next_line(ed, ed->toppos);
ed->topline++;
} else {
break;
}
}
}
}
//
// Text selection
//
int get_selection(struct editor *ed, int *start, int *end) {
if (ed->anchor == -1) {
*start = *end = -1;
return 0;
} else {
int pos = ed->linepos + ed->col;
if (pos == ed->anchor) {
*start = *end = -1;
return 0;
} else if (pos < ed->anchor) {
*start = pos;
*end = ed->anchor;
} else {
*start = ed->anchor;
*end = pos;
}
}
return 1;
}
int get_selected_text(struct editor *ed, char *buffer, int size) {
int selstart, selend, len;
if (!get_selection(ed, &selstart, &selend))
return 0;
len = selend - selstart;
if (len >= size)
return 0;
copy(ed, buffer, selstart, len);
buffer[len] = 0;
return len;
}
void update_selection(struct editor *ed, int select) {
if (select) {
if (ed->anchor == -1)
ed->anchor = ed->linepos + ed->col;
ed->refresh = 1;
} else {
if (ed->anchor != -1)
ed->refresh = 1;
ed->anchor = -1;
}
}
int erase_selection(struct editor *ed) {
int selstart, selend;
if (!get_selection(ed, &selstart, &selend))
return 0;
moveto(ed, selstart, 0);
erase_section(ed, selstart, selend - selstart);
ed->anchor = -1;
ed->refresh = 1;
return 1;
}
void select_all(struct editor *ed) {
ed->anchor = 0;
ed->refresh = 1;
moveto(ed, text_length(ed), 0);
}
//
// Screen functions
//
void get_console_size(struct env *env) {
struct winsize ws;
ioctl(0, TIOCGWINSZ, &ws);
env->cols = ws.ws_col;
env->lines = ws.ws_row - 1;
env->linebuf = realloc(env->linebuf, env->cols + LINEBUF_EXTRA);
}
void outch(char c) {
putchar(c);
}
void outbuf(char *buf, int len) {
fwrite(buf, 1, len, stdout);
}
void outstr(char *str) {
fputs(str, stdout);
}
void clear_screen() {
outstr(CLRSCR);
}
void gotoxy(int col, int line) {
char buf[32];
sprintf(buf, GOTOXY, line + 1, col + 1);
outstr(buf);
}
int prompt(struct editor *ed, char *msg) {
int maxlen, len, ch;
char *buf = ed->env->linebuf;
gotoxy(0, ed->env->lines);
outstr(STATUS_COLOR);
outstr(msg);
outstr(CLREOL);
len = 0;
maxlen = ed->env->cols - strlen(msg) - 1;
len = get_selected_text(ed, buf, maxlen);
outbuf(buf, len);
for (;;) {
fflush(stdout);
ch = getkey();
if (ch == KEY_ESC) {
return 0;
} else if (ch == KEY_ENTER) {
buf[len] = 0;
return len > 0;
} else if (ch == KEY_BACKSPACE) {
if (len > 0) {
outstr("\b \b");
len--;
}
} else if (ch >= ' ' && ch < 0x100 && len < maxlen) {
outch(ch);
buf[len++] = ch;
}
}
}
int ask() {
int ch = getchar();
return ch == 'y' || ch == 'Y';
}
//
// Display functions
//
void display_message(struct editor *ed, char *fmt, ...) {
va_list args;
va_start(args, fmt);
gotoxy(0, ed->env->lines);
outstr(STATUS_COLOR);
vprintf(fmt, args);
outstr(CLREOL TEXT_COLOR);
fflush(stdout);
va_end(args);
}
void draw_full_statusline(struct editor *ed) {
struct env *env = ed->env;
int namewidth = env->cols - 19;
gotoxy(0, env->lines);
sprintf(env->linebuf,
STATUS_COLOR "%*.*s%c Ln %-6dCol %-4d" CLREOL TEXT_COLOR,
-namewidth, namewidth, ed->filename, ed->dirty ? '*' : ' ',
ed->line + 1, column(ed, ed->linepos, ed->col) + 1);
outstr(env->linebuf);
#ifdef DEBUG
gotoxy(0, env->lines - 1);
sprintf(env->linebuf,
STATUS_COLOR "[%02X%02X%02X%02X%02X%02X]" TEXT_COLOR,
last_keys[0], last_keys[1], last_keys[2], last_keys[3], last_keys[4],
last_keys[5]);
outstr(env->linebuf);
#endif
}
void display_line(struct editor *ed, int pos, int fullline) {
int hilite = 0;
int col = 0;
int margin = ed->margin;
int maxcol = ed->env->cols + margin;
char *bufptr = ed->env->linebuf;
unsigned char *p = text_ptr(ed, pos);
int selstart, selend, ch;
char *s;
get_selection(ed, &selstart, &selend);
while (col < maxcol) {
if (margin == 0) {
if (!hilite && pos >= selstart && pos < selend) {
for (s = SELECT_COLOR; *s; s++)
*bufptr++ = *s;
hilite = 1;
} else if (hilite && pos >= selend) {
for (s = TEXT_COLOR; *s; s++)
*bufptr++ = *s;
hilite = 0;
}
}
if (p == ed->end)
break;
ch = *p;
if (ch == '\r' || ch == '\n')
break;
if (ch == '\t') {
int spaces = TABSIZE - col % TABSIZE;
while (spaces > 0 && col < maxcol) {
if (margin > 0) {
margin--;
} else {
*bufptr++ = ' ';
}
col++;
spaces--;
}
} else {
if (margin > 0) {
margin--;
} else {
*bufptr++ = ch;
}
col++;
}
if (++p == ed->gap)
p = ed->rest;
pos++;
}
if (hilite) {
while (col < maxcol) {
*bufptr++ = ' ';
col++;
}
} else {
if (col == margin)
*bufptr++ = ' ';
}
if (col < maxcol) {
for (s = CLREOL; *s; s++)
*bufptr++ = *s;
if (fullline) {
memcpy(bufptr, "\r\n", 2);
bufptr += 2;
}
}
if (hilite) {
for (s = TEXT_COLOR; *s; s++)
*bufptr++ = *s;
}
outbuf(ed->env->linebuf, bufptr - ed->env->linebuf);
}
void update_line(struct editor *ed) {
gotoxy(0, ed->line - ed->topline);
display_line(ed, ed->linepos, 0);
}
void draw_screen(struct editor *ed) {
int pos;
int i;
gotoxy(0, 0);
outstr(TEXT_COLOR);
pos = ed->toppos;
for (i = 0; i < ed->env->lines; i++) {
if (pos < 0) {
outstr(CLREOL "\r\n");
} else {
display_line(ed, pos, 1);
pos = next_line(ed, pos);
}
}
}
void position_cursor(struct editor *ed) {
int col = column(ed, ed->linepos, ed->col);
gotoxy(col - ed->margin, ed->line - ed->topline);
}
//
// Cursor movement
//
void adjust(struct editor *ed) {
int col;
int ll = line_length(ed, ed->linepos);
ed->col = ed->lastcol;
if (ed->col > ll)
ed->col = ll;
col = column(ed, ed->linepos, ed->col);
while (col < ed->margin) {
ed->margin -= 4;
if (ed->margin < 0)
ed->margin = 0;
ed->refresh = 1;
}
while (col - ed->margin >= ed->env->cols) {
ed->margin += 4;
ed->refresh = 1;
}
}
void up(struct editor *ed, int select) {
int newpos = prev_line(ed, ed->linepos);
if (newpos < 0)
return;
update_selection(ed, select);
ed->linepos = newpos;
ed->line--;
if (ed->line < ed->topline) {
ed->toppos = ed->linepos;
ed->topline = ed->line;
ed->refresh = 1;
}
adjust(ed);
}
void down(struct editor *ed, int select) {
int newpos = next_line(ed, ed->linepos);
if (newpos < 0)
return;
update_selection(ed, select);
ed->linepos = newpos;
ed->line++;
if (ed->line >= ed->topline + ed->env->lines) {
ed->toppos = next_line(ed, ed->toppos);
ed->topline++;
ed->refresh = 1;
}
adjust(ed);
}
void left(struct editor *ed, int select) {
update_selection(ed, select);
if (ed->col > 0) {
ed->col--;
} else {
int newpos = prev_line(ed, ed->linepos);
if (newpos < 0)
return;
ed->col = line_length(ed, newpos);
ed->linepos = newpos;
ed->line--;
if (ed->line < ed->topline) {
ed->toppos = ed->linepos;
ed->topline = ed->line;
ed->refresh = 1;
}
}
ed->lastcol = ed->col;
adjust(ed);
}
void right(struct editor *ed, int select) {
update_selection(ed, select);
if (ed->col < line_length(ed, ed->linepos)) {
ed->col++;
} else {
int newpos = next_line(ed, ed->linepos);
if (newpos < 0)
return;
ed->col = 0;
ed->linepos = newpos;
ed->line++;
if (ed->line >= ed->topline + ed->env->lines) {
ed->toppos = next_line(ed, ed->toppos);
ed->topline++;
ed->refresh = 1;
}
}
ed->lastcol = ed->col;
adjust(ed);
}
int wordchar(int ch) {
return (ch >= 'A' && ch <= 'Z')
|| (ch >= 'a' && ch <= 'z')
|| (ch >= '0' && ch <= '9');
}
void wordleft(struct editor *ed, int select) {
int pos, phase;
update_selection(ed, select);
pos = ed->linepos + ed->col;
phase = 0;
while (pos > 0) {
int ch = get(ed, pos - 1);
if (phase == 0) {