-
Notifications
You must be signed in to change notification settings - Fork 1
/
line.c
1931 lines (1546 loc) · 39.4 KB
/
line.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
/*
* Text line handling.
*
* The functions in this file are a general set of line management
* utilities. They are the only routines that touch the text. They
* also touch the buffer and window structures, to make sure that the
* necessary updating gets done. There are routines in this file that
* handle the kill buffer too. It isn't here for any good reason.
*
* Note that this code only updates the dot and mark values in the
* window list. Since all the code acts on the current window, the
* buffer that we are editing must be being displayed, which means
* that "b_nwnd" is non zero, which means that the dot and mark values
* in the buffer headers are nonsense.
*/
/*
Summary of insert/overwrite into the current buffer functions:
linsert_str insert n copies of a raw, untranslated string. The function
everything else is built on.
linsert insert n copies of a byte, or n newlines.
linsert_ucs insert n copies of an UCS-4 character, translated to a given charset,
or n newlines.
linsert_overwrite insert or overwrite n copies of an UCS-4 character, translated to a
given charset, or n newlines.
linsert_general_string insert n copies of a string with a given charset, translating to
the buffer charset, and splitting lines on newlines. Used by yank().
linsert_overwrite_general_string insert or overwrite a string, otherwise as
linsert_general_string. Used in macro replay.
*/
#include "def.h"
#define RECOVER 1
#ifndef MALLOCROUND
#define MALLOCROUND 0
#endif
#if RECOVER
static void reduceoverhead(LINE *lp1);
#endif
#if TESTCMD == 2
int count_alloc=0, count_realloc=0;
int count_kalloc=0;
double count_kcopy=0.0;
#endif
INT forwchar(INT f, INT n);
INT backchar(INT f, INT n);
#ifndef NBLOCK
#define NBLOCK 16 /* Line block chunk size */
#endif
#ifndef KBLOCK
#define KBLOCK 256 /* Kill buffer block size. */
#endif
static char *kbufp = NULL; /* Kill buffer data. */
static INT kused = 0; /* # of bytes used in KB. */
static INT ksize = 0; /* # of bytes allocated in KB. */
static INT kstart = 0; /* # of first used byte in KB. */
INT kbuf_flag = 0;
charset_t kbuf_charset = NULL;
INT kbuf_wholelines = 0;
/* The global mark gets updated like all the window dot and marks */
LINE *gmarkp = NULL; /* Global mark pointer */
INT gmarko = 0; /* Global mark offset */
#ifdef UPDATE_DEBUG
INT update_debug = 0;
static void
do_debug()
{
refreshbuf(NULL);
update();
if (getkey(FALSE) == 'q') update_debug = 0;
}
INT
do_update_debug(INT f, INT n)
{
update_debug = 1;
return TRUE;
}
#endif
INT
readonly()
{
ewprintf("Buffer is read only");
return FALSE;
}
static INT
overflow()
{
ewprintf("Integer overflow when allocating memory");
return FALSE;
}
/*
* This routine allocates a block of memory large enough to hold a
* LINE containing "used" characters. The block is rounded up to
* whatever needs to be allocated. (use lallocx for lines likely to
* grow.) Return a pointer to the new block, or NULL if there isn't
* any memory left. Print a message in the message line if no space.
*
* Mg3a: use realloc.
*/
static LINE *
lrealloc(LINE *lp, INT used)
{
INT size, offset;
#if TESTCMD == 2
LINE *lpold = lp;
#endif
offset = offsetof(LINE, l_text);
if (used < 0 || used > MAXINT - offset - MALLOCROUND) {
overflow();
return NULL;
}
size = used + offset;
if (size < (INT)sizeof(LINE)) size = sizeof(LINE); /* use padding */
#if MALLOCROUND
size += (MALLOCROUND-1); /* round up to a size optimal to malloc */
size &= ~(MALLOCROUND-1);
#endif
if ((lp = (LINE *)realloc(lp, size)) == NULL) {
return outofmem(size);
}
lp->l_size = size - offset;
lp->l_used = used;
lp->l_flag = 0;
#if TESTCMD == 2
if (lp == lpold) count_realloc++; else count_alloc++;
#endif
return lp;
}
LINE *
lalloc(INT used)
{
return lrealloc(NULL, used);
}
/*
* Like lalloc, only round amount desired up because this line will
* probably grow. We always make room for at least one more char.
* (thus making 0 not a special case anymore.)
*
* Mg3a: realloc, and scale better if copy
*/
static LINE *
lreallocx(LINE *lp, INT used)
{
INT size;
if (used < 0 || used > MAXINT - (used>>4) - NBLOCK) {
overflow();
return NULL;
}
size = used + (used>>4) + NBLOCK;
if((lp = lrealloc(lp, size)) != NULL) lp->l_used = used;
return lp;
}
LINE *
lallocx(INT used)
{
return lreallocx(NULL, used);
}
/*
* Delete line "lp". Fix all of the links that might point at it (they
* are moved to offset 0 of the next line). Unlink the line from
* whatever buffer it might be in. Release the memory. The buffers are
* updated too; the magic conditions described in the above comments
* don't hold here.
*
* Mg3a: Add buffer as parameter to scale to many buffers. Deallocate
* mark if last line in the buffer.
*/
void
lfree(BUFFER *bp, LINE *lp, size_t pos)
{
WINDOW *wp;
LINE *blp = bp->b_linep;
if (gmarkp == lp) {
gmarkp = lforw(lp);
if (gmarkp == blp) gmarkp = NULL;
gmarko = 0;
}
for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
if (wp->w_bufp == bp) {
if (wp->w_linep == lp)
wp->w_linep = lp->l_fp;
if (wp->w_dotp == lp) {
wp->w_dotp = lp->l_fp;
wp->w_doto = 0;
}
if (wp->w_markp == lp) {
wp->w_markp = lp->l_fp;
if (wp->w_markp == blp) wp->w_markp = NULL;
wp->w_marko = 0;
}
if (wp->w_dotpos > pos) {
if (wp->w_dotpos <= pos + llength(lp)) {
wp->w_dotpos = pos;
} else {
wp->w_dotpos -= llength(lp) + 1;
}
}
}
}
if (bp->b_nwnd == 0) {
if (bp->b_dotp == lp) {
bp->b_dotp = lp->l_fp;
bp->b_doto = 0;
}
if (bp->b_markp == lp) {
bp->b_markp = lp->l_fp;
if (bp->b_markp == blp) bp->b_markp = NULL;
bp->b_marko = 0;
}
if (bp->b_dotpos > pos) {
if (bp->b_dotpos <= pos + llength(lp)) {
bp->b_dotpos = pos;
} else {
bp->b_dotpos -= llength(lp) + 1;
}
}
}
bp->b_size -= llength(lp);
if (lforw(lp) != blp) bp->b_size--;
lp->l_bp->l_fp = lp->l_fp;
lp->l_fp->l_bp = lp->l_bp;
free(lp);
}
/*
* This routine gets called when a character is changed in place in
* the current buffer. It updates all of the required flags in the
* buffer and window system. The flag used is passed as an argument;
* if the buffer is being displayed in more than 1 window we change
* EDIT to HARD. Set MODE if the mode line needs to be updated (the
* "*" has to be set).
*/
void
lchange(INT flag)
{
WINDOW *wp;
if (!(curbp->b_flag & BFCHG)) { /* First change, so */
flag |= WFMODE; /* update mode lines. */
curbp->b_flag |= BFCHG;
}
for (wp = wheadp; wp; wp = wp->w_wndp) {
if (wp->w_bufp == curbp) {
wp->w_flag |= flag;
if (wp != curwp) wp->w_flag |= WFHARD;
}
}
}
/*
* Insert "n" copies of the string "str" of length "len" at the
* current location of dot. In the easy case all that happens is the
* text is stored in the line. In the hard case, the line has to be
* reallocated. When the window list is updated, take special care; I
* screwed it up once. You always update dot in the current window.
* You update mark, and a dot in another window, if it is greater than
* the place where you did the insert. Return TRUE if all is well, and
* FALSE on errors.
*
* Mg3a: rewritten quite a bit, uses realloc
*/
INT
linsert_str(INT n, char *str, INT len)
{
char *cpdot, *startdot;
LINE *lp1;
LINE *lp2;
INT doto;
WINDOW *wp;
INT oldlen, newlen, nlen;
INT i, j;
size_t pos;
if (curbp->b_flag & BFREADONLY) return readonly();
if (n < 0 || len < 0) return FALSE;
if (n == 0 || len == 0) return TRUE;
#ifdef UPDATE_DEBUG
if (update_debug) do_debug();
#endif
if (curwp->w_dotp == curbp->b_linep) {
if (normalizebuffer(curbp) == FALSE) return FALSE;
}
lp1 = curwp->w_dotp;
oldlen = llength(lp1);
if (n == 1) {
if (len > MAXINT - oldlen) return overflow();
nlen = len;
} else {
if (len > MAXINT / n || n*len > MAXINT - oldlen) return overflow();
nlen = n*len;
}
newlen = oldlen + nlen;
lp2 = lp1;
if (newlen > lsize(lp1)) {
if ((lp2 = lreallocx(lp1, newlen)) == NULL)
return FALSE;
if (lp1 != lp2) {
lp2->l_bp->l_fp = lp2;
lp2->l_fp->l_bp = lp2;
}
}
// UNDO
u_modify();
//
lchange(WFEDIT);
lp2->l_flag = 0;
lsetlength(lp2, newlen);
doto = curwp->w_doto;
cpdot = <ext(lp2)[doto];
if (oldlen > doto) {
memmove(cpdot + nlen, cpdot, oldlen - doto);
}
startdot = cpdot;
for (i = 0; i < n; i++) {
if (len <= 4) {
for (j = 0; j < len; j++) {
*cpdot++ = str[j];
}
} else {
memcpy(cpdot, str, len);
cpdot += len;
}
}
if (gmarkp == lp1) {
gmarkp = lp2;
if (gmarko > doto) gmarko += nlen;
}
pos = curwp->w_dotpos;
for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
if (wp->w_bufp == curbp) {
if (wp->w_linep == lp1)
wp->w_linep = lp2;
if (wp->w_dotp == lp1) {
wp->w_dotp = lp2;
if (wp == curwp || wp->w_doto > doto)
wp->w_doto += nlen;
}
if (wp->w_dotpos > pos)
wp->w_dotpos += nlen;
if (wp->w_markp == lp1) {
wp->w_markp = lp2;
if (wp->w_marko > doto)
wp->w_marko += nlen;
}
}
}
curwp->w_dotpos += nlen;
curbp->b_size += nlen;
// UNDO
if (!u_entry(UFL_INSERT, pos, startdot, nlen)) return FALSE;
//
return TRUE;
}
/*
* Mg3a: as linsert_str, but a single byte. Also handle newline.
*/
INT
linsert(INT n, INT c)
{
char cc = c;
if (c == '\n') return lnewline_n(n);
return linsert_str(n, &cc, 1);
}
/*
* Mg3a: as linsert, but insert a full UCS-4 character according to
* the charset.
*/
INT
linsert_ucs(charset_t charset, INT n, INT c)
{
unsigned char buf[4];
INT len;
if (c == '\n') return lnewline_n(n);
ucs_chartostr(charset, c, buf, &len);
return linsert_str(n, (char *)&buf[0], len);
}
/*
* Mg3a: duplicate the current line below it
*
* IGNORE parameters
*/
INT
lduplicate(INT f, INT n)
{
INT s;
struct LINE *dotp = curwp->w_dotp;
INT doto = curwp->w_doto;
if (curbp->b_flag & BFREADONLY) return readonly();
/*
* Goto the end of line
* c&p from gotoeol(f,n);
*/
adjustpos(curwp->w_dotp, llength(curwp->w_dotp));
/*
* Insert a newline
*/
s = lnewline_n(1);
if (TRUE == s) {
linsert_str(1, ltext(dotp), llength(dotp)); /* Insert line contents */
}
/*
* restore the cursor
*/
adjustpos(dotp, doto);
return s;
}
#if 0 /* Not yet ready */
/*
* MG3A: swap current line and next line
*/
INT
lmovedown(INT f, INT n)
{
int i;
struct LINE *dotp = curwp->w_dotp;
/* Check you can do it */
if (curbp->b_flag & BFREADONLY)
return readonly();
if (n < 0) {
return lmoveup(f, -n);
}
for (i = 0; i< n; i++) {
struct LINE *prevp = dotp -> l_bp;
struct LINE *nextp = dotp -> l_fp;
struct LINE *followp = nextp -> l_fp;
nextp->l_bp = prevp;
nextp->l_fp = dotp;
dotp->l_bp = nextp;
dotp->l_fp = followp;
}
/*
* Mark buffer as dirty
*/
curbp->b_flag |= BFCHG;
/*
* refresh the display
*/
refreshbuf(curbp);
return TRUE;
}
/*
* Mg3a: move current line up
*/
INT
lmoveup(INT f, INT n)
{
int i;
struct LINE *dotp = curwp->w_dotp;
/* Check you can do it */
if (curbp->b_flag & BFREADONLY)
return readonly();
if (n < 0) {
return lmovedown(f, -n);
}
for (i=0; i<n;i++) {
struct LINE *prevp = dotp -> l_bp;
struct LINE *firstp = prevp -> l_fp;
struct LINE *nextp = dotp -> l_fp;
dotp->l_fp = prevp;
dotp->l_bp = firstp;
prevp->l_fp = nextp;
prevp->l_bp = dotp;
}
/*
* Mark buffer as dirty
*/
curbp->b_flag |= BFCHG;
/*
* refresh the display
*/
refreshbuf(curbp);
return TRUE;
}
#endif
/*
* Mg3a: erase characters to a visual width, preparing for a
* subsequent insert to implement overwrite mode.
*/
static INT
lerase_for_overwrite(INT width)
{
LINE *dotp;
INT doto, enddoto, col, endcol, goalcol, linelen;
INT c;
if (curbp->b_flag & BFREADONLY) return readonly();
dotp = curwp->w_dotp;
doto = curwp->w_doto;
if (width < 0) return column_overflow();
col = -1; // No tab yet
goalcol = width; // No tab yet
enddoto = doto;
endcol = 0; // No tab yet
linelen = llength(dotp);
while (enddoto < linelen && endcol < goalcol) {
c = ucs_char(curbp->charset, dotp, enddoto, NULL);
if (endcol > MAXINT - 8) return column_overflow();
if (c == '\t') {
if (col < 0) {
col = getfullcolumn(curwp, dotp, doto);
if (col > MAXINT - endcol - 8 || col > MAXINT - goalcol)
return column_overflow();
endcol += col;
goalcol += col;
}
endcol = (endcol | 7) + 1;
} else {
endcol += ucs_fulltermwidth(c);
}
enddoto = ucs_forward(curbp->charset, dotp, enddoto);
}
if (ldeleteraw(enddoto - doto, KNONE) == FALSE) return FALSE;
if (endcol > goalcol) {
doto = curwp->w_doto;
if (linsert(endcol - goalcol, ' ') == FALSE) return FALSE;
adjustpos(curwp->w_dotp, doto);
}
return TRUE;
}
/*
* Mg3a: as linsert_ucs (which always inserts), but also do overwrite
* mode.
*/
INT
linsert_overwrite(charset_t charset, INT n, INT c)
{
INT width;
if (!(curbp->b_flag & BFOVERWRITE) || c == '\t' || c == '\n' ||
curwp->w_doto == llength(curwp->w_dotp))
{
return linsert_ucs(charset, n, c);
}
if (curbp->b_flag & BFREADONLY) return readonly();
if (n < 0) return FALSE;
if (n == 0) return TRUE;
width = ucs_fulltermwidth(c);
if (width == 2 && n > MAXINT / 2) return column_overflow();
if (lerase_for_overwrite(width * n) == FALSE) return FALSE;
return linsert_ucs(charset, n, c);
}
/*
* Mg3a: add an entire line efficiently. The dot should be at the end
* of the current line. No window pointers need reset since it's an
* entirely new line. Does not do undo.
*/
INT
laddline(char *txt, INT len)
{
LINE *lp1, *lp2, *blp = curbp->b_linep;
size_t pos = curwp->w_dotpos;
WINDOW *wp;
size_t addlen = (lforw(blp) == blp) ? len : len + 1;
int wflag = (curbp->b_flag & BFCHG) ? WFHARD : (WFHARD|WFMODE);
#ifdef UPDATE_DEBUG
if (update_debug) do_debug();
#endif
lp1 = curwp->w_dotp;
if (curwp->w_doto != llength(lp1)) {
ewprintf("laddline: not at end of line");
goto error;
}
if ((lp2 = lalloc(len)) == NULL) goto error;
memcpy(ltext(lp2), txt, len);
lp2->l_fp = lp1->l_fp;
lp2->l_bp = lp1;
lp1->l_fp->l_bp = lp2;
lp1->l_fp = lp2;
curwp->w_dotp = lp2;
curwp->w_doto = len;
for (wp = wheadp; wp; wp = wp->w_wndp) {
if (wp->w_bufp == curbp) {
if (wp->w_dotpos > pos) wp->w_dotpos += addlen;
wp->w_flag |= wflag;
}
}
curwp->w_dotpos += addlen;
curbp->b_size += addlen;
curbp->b_flag |= BFCHG;
return TRUE;
// UNDO
error:
u_clear(curbp);
return FALSE;
//
}
/*
* Insert a newline into the buffer at the current location of dot in
* the current window.
*/
INT
lnewline()
{
LINE *lp1;
LINE *lp2;
INT doto;
INT nlen;
WINDOW *wp;
size_t pos = curwp->w_dotpos;
#ifdef UPDATE_DEBUG
if (update_debug) do_debug();
#endif
if (curbp->b_flag & BFREADONLY) return readonly();
if (curwp->w_dotp == curbp->b_linep) {
if (normalizebuffer(curbp) == FALSE) return FALSE;
}
lp1 = curwp->w_dotp; /* Get the address and */
lp1->l_flag = 0;
doto = curwp->w_doto; /* offset of "." */
// UNDO
u_modify();
if (!u_entry(UFL_INSERT, pos, "\n", 1)) return FALSE;
//
lchange(WFHARD);
if(doto == 0) { /* avoid unnecessary copying */
if((lp2 = lallocx(0)) == NULL) /* new first part */
goto error;
lp2->l_bp = lp1->l_bp;
lp1->l_bp->l_fp = lp2;
lp2->l_fp = lp1;
lp1->l_bp = lp2;
if (gmarkp == lp1 && gmarko == 0) gmarkp = lp2;
for(wp = wheadp; wp!=NULL; wp = wp->w_wndp) {
if (wp->w_bufp == curbp) {
if (wp->w_linep == lp1) wp->w_linep = lp2;
if (wp->w_dotp == lp1 && wp->w_doto == 0 && wp != curwp) wp->w_dotp = lp2;
if (wp->w_markp == lp1 && wp->w_marko == 0) wp->w_markp = lp2;
if (wp->w_dotpos > pos) wp->w_dotpos++;
}
}
curwp->w_dotpos++;
curbp->b_size++;
return TRUE;
}
nlen = llength(lp1) - doto; /* length of new part */
if((lp2=lallocx(nlen)) == NULL) /* New second half line */
goto error;
if(nlen!=0) bcopy(&lp1->l_text[doto], &lp2->l_text[0], nlen);
lp1->l_used = doto;
lp2->l_bp = lp1;
lp2->l_fp = lp1->l_fp;
lp1->l_fp = lp2;
lp2->l_fp->l_bp = lp2;
if (gmarkp == lp1 && gmarko > doto) {
gmarkp = lp2;
gmarko -= doto;
}
for(wp = wheadp; wp != NULL; wp = wp->w_wndp) { /* Windows */
if (wp->w_bufp == curbp) {
if (wp->w_dotp == lp1 && (wp->w_doto > doto || wp == curwp)) {
wp->w_dotp = lp2;
wp->w_doto -= doto;
}
if (wp->w_markp == lp1 && wp->w_marko > doto) {
wp->w_markp = lp2;
wp->w_marko -= doto;
}
if (wp->w_dotpos > pos) wp->w_dotpos++;
}
}
curwp->w_dotpos++;
curbp->b_size++;
#if RECOVER
reduceoverhead(lp1);
#endif
return TRUE;
// UNDO
error:
u_clear(curbp);
return FALSE;
//
}
/*
* Mg3a: insert "n" newlines.
*/
INT
lnewline_n(INT n)
{
INT s;
if (n < 0) return FALSE;
while (n--) {
if ((s = lnewline()) != TRUE) return s;
}
return TRUE;
}
/*
* This function deletes "n" characters, starting at dot. It
* understands how do deal with end of lines, etc. It returns TRUE if
* all of the characters were deleted, and FALSE if they were not
* (because dot ran into the end of the buffer. The "kflag" indicates
* either no insertion, or direction of insertion into the kill
* buffer.
*
* Mg3a: If a kill, store the buffer charset and flags with the
* killbuffer. Direction is also provided independently.
*/
/* Kill direction */
#define KDFORWARD 1
#define KDBACKWARD 0
static INT
ldelete_internal(INT n, INT kflag, INT direction)
{
char *cpdot;
LINE *dotp;
INT doto;
INT chunk;
WINDOW *wp;
INT savedonenewline = 0; /* save one newline to delete until the end */
INT ret = TRUE;
size_t dotpos;
#ifdef UPDATE_DEBUG
if (update_debug) do_debug();
#endif
while (n > 0) {
dotp = curwp->w_dotp;
doto = curwp->w_doto;
dotpos = curwp->w_dotpos;
dotp->l_flag = 0;
if (dotp == curbp->b_linep) /* Hit end of buffer. */
return FALSE;
if (direction == KDFORWARD) {
chunk = dotp->l_used - doto; /* Size of chunk. */
if (chunk > n)
chunk = n;
} else {
chunk = doto; /* Size of chunk. */
if (chunk > n)
chunk = n;
doto -= chunk;
dotpos -= chunk;
}
if (chunk == 0) { /* End of line, merge. */
if (direction == KDFORWARD) {
if (dotp == lback(curbp->b_linep)) {
ret = FALSE;
goto end; /* End of buffer. */
}
if (!savedonenewline) { /* Save one newline until later */
adjustpos3(lforw(curwp->w_dotp), 0, dotpos + 1);
}
} else {
if (dotp == lforw(curbp->b_linep)) {
ret = FALSE;
goto end; /* Beginning of buffer. */
}
adjustpos3(lback(dotp), llength(lback(dotp)), dotpos - 1);
}
lchange(WFHARD);
if (savedonenewline && ldelnewline_noundo() == FALSE)
return FALSE;
savedonenewline = 1;
if (kflag != KNONE) kinsert('\n', kflag);
n--;
continue;
}
#if TESTCMD == 2
count_kcopy += chunk;
#endif
lchange(WFEDIT);
cpdot = <ext(dotp)[doto]; /* Scrunch text. */
if (kflag == KFORW) {
memcpy(&kbufp[kused], cpdot, chunk);
kused += chunk;
} else if (kflag == KBACK) {
memcpy(&kbufp[kstart-chunk], cpdot, chunk);
kstart -= chunk;
} else if (kflag != KNONE) panic("broken ldelete call", 0);
if (doto + chunk != llength(dotp)) {
memmove(cpdot, cpdot + chunk, llength(dotp) - doto - chunk);
}
lsetlength(dotp, llength(dotp) - chunk);
if (gmarkp == dotp && gmarko >= doto) {
gmarko -= chunk;
if (gmarko < doto) gmarko = doto;
}
for(wp = wheadp; wp != NULL; wp = wp->w_wndp) {
if (wp->w_bufp == curbp) {
if (wp->w_dotp == dotp && wp->w_doto >= doto) {
wp->w_doto -= chunk;
if (wp->w_doto < doto)
wp->w_doto = doto;
}
if (wp->w_dotpos > dotpos) {
if (wp->w_dotpos < dotpos + chunk)
wp->w_dotpos = dotpos;
else
wp->w_dotpos -= chunk;
}
if (wp->w_markp == dotp && wp->w_marko >= doto) {
wp->w_marko -= chunk;
if (wp->w_marko < doto)
wp->w_marko = doto;
}
}
}
curbp->b_size -= chunk;
n -= chunk;
}
end:
if (savedonenewline) {
if (direction == KDFORWARD) endofpreviousline();
if (ldelnewline_noundo() == FALSE) return FALSE;
}
#if RECOVER
reduceoverhead(curwp->w_dotp);
#endif
return ret;
}
/*
* Do initial checks, do stuff with undo.
*/
// UNDO
static INT
ldelete_undo(LINE *lp, INT offset, INT n, INT kflag, INT direction, size_t pos)