-
Notifications
You must be signed in to change notification settings - Fork 41
/
ed.refresh.c
1337 lines (1237 loc) · 33.2 KB
/
ed.refresh.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
/*
* ed.refresh.c: Lower level screen refreshing functions
*/
/*-
* Copyright (c) 1980, 1991 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "sh.h"
#include "ed.h"
/* #define DEBUG_UPDATE */
/* #define DEBUG_REFRESH */
/* #define DEBUG_LITERAL */
/* refresh.c -- refresh the current set of lines on the screen */
Char *litptr;
static int vcursor_h, vcursor_v;
static int rprompt_h, rprompt_v;
static int MakeLiteral (Char *, int, Char);
static int Draw (Char *, int, int);
static void Vdraw (Char, int);
static void RefreshPromptpart (Char *);
static void update_line (Char *, Char *, int);
static void str_insert (Char *, int, int, Char *, int);
static void str_delete (Char *, int, int, int);
static void str_cp (Char *, Char *, int);
#ifndef WINNT_NATIVE
static
#else
extern
#endif
void PutPlusOne (Char, int);
static void cpy_pad_spaces (Char *, Char *, int);
#if defined(DEBUG_UPDATE) || defined(DEBUG_REFRESH) || defined(DEBUG_LITERAL)
static void reprintf (char *, ...);
#ifdef DEBUG_UPDATE
static void dprintstr (char *, const Char *, const Char *);
static void
dprintstr(char *str, const Char *f, const Char *t)
{
reprintf("%s:\"", str);
while (f < t) {
if (ASC(*f) & ~ASCII)
reprintf("[%x]", *f++);
else
reprintf("%c", CTL_ESC(ASCII & ASC(*f++)));
}
reprintf("\"\r\n");
}
#endif /* DEBUG_UPDATE */
/* reprintf():
* Print to $DEBUGTTY, so that we can test editing on one pty, and
* print debugging stuff on another. Don't interrupt the shell while
* debugging cause you'll mangle up the file descriptors!
*/
static void
reprintf(char *fmt, ...)
{
static int fd = -1;
char *dtty;
if ((dtty = getenv("DEBUGTTY"))) {
int o;
va_list va;
va_start(va, fmt);
if (fd == -1)
fd = xopen(dtty, O_RDWR);
o = SHOUT;
flush();
SHOUT = fd;
xvprintf(fmt, va);
va_end(va);
flush();
SHOUT = o;
}
}
#endif /* DEBUG_UPDATE || DEBUG_REFRESH || DEBUG_LITERAL */
static int litlen = 0, litalloc = 0;
static int MakeLiteral(Char *str, int len, Char addlit)
{
int i, addlitlen = 0;
Char *addlitptr = 0;
if (addlit) {
if ((addlit & LITERAL) != 0) {
addlitptr = litptr + (addlit & ~LITERAL) * LIT_FACTOR;
addlitlen = Strlen(addlitptr);
} else {
addlitptr = &addlit;
addlitlen = 1;
}
for (i = 0; i < litlen; i += LIT_FACTOR)
if (!Strncmp(addlitptr, litptr + i, addlitlen) && !Strncmp(str, litptr + i + addlitlen, len) && litptr[i + addlitlen + len] == 0)
return (i / LIT_FACTOR) | LITERAL;
} else {
addlitlen = 0;
for (i = 0; i < litlen; i += LIT_FACTOR)
if (!Strncmp(str, litptr + i, len) && litptr[i + len] == 0)
return (i / LIT_FACTOR) | LITERAL;
}
if (litlen + addlitlen + len + 1 + (LIT_FACTOR - 1) > litalloc) {
Char *newlitptr;
int add = 256;
while (len + addlitlen + 1 + (LIT_FACTOR - 1) > add)
add *= 2;
newlitptr = xrealloc(litptr, (litalloc + add) * sizeof(Char));
if (!newlitptr)
return '?';
litptr = newlitptr;
litalloc += add;
if (addlitptr && addlitptr != &addlit)
addlitptr = litptr + (addlit & ~LITERAL) * LIT_FACTOR;
}
i = litlen / LIT_FACTOR;
if (i >= LITERAL || i == CHAR_DBWIDTH)
return '?';
if (addlitptr) {
Strncpy(litptr + litlen, addlitptr, addlitlen);
litlen += addlitlen;
}
Strncpy(litptr + litlen, str, len);
litlen += len;
do
litptr[litlen++] = 0;
while (litlen % LIT_FACTOR);
return i | LITERAL;
}
/* draw char at cp, expand tabs, ctl chars */
static int
Draw(Char *cp, int nocomb, int drawPrompt)
{
int w, i, lv, lh;
Char c, attr;
#ifdef WIDE_STRINGS
if (!drawPrompt) { /* draw command-line */
attr = 0;
c = *cp;
} else { /* draw prompt */
/* prompt with attributes(UNDER,BOLD,STANDOUT) */
if (*cp & (UNDER | BOLD | STANDOUT)) { /* *cp >= STANDOUT */
/* example)
* We can't distinguish whether (*cp=)0x02ffffff is
* U+02FFFFFF or U+00FFFFFF|STANDOUT.
* We handle as U+00FFFFFF|STANDOUT, only when drawing prompt. */
attr = (*cp & ATTRIBUTES);
/* ~(UNDER | BOLD | STANDOUT) = 0xf1ffffff */
c = *cp & ~(UNDER | BOLD | STANDOUT);
/* if c is ctrl code, we handle *cp as havnig no attributes */
if ((c < 0x20 && c >= 0) || c == 0x7f) {
attr = 0;
c = *cp;
}
} else { /* prompt without attributes */
attr = 0;
c = *cp;
}
}
#else
attr = *cp & ~CHAR;
c = *cp & CHAR;
#endif
w = NLSClassify(c, nocomb, drawPrompt);
switch (w) {
case NLSCLASS_NL:
Vdraw('\0', 0); /* assure end of line */
vcursor_h = 0; /* reset cursor pos */
vcursor_v++;
break;
case NLSCLASS_TAB:
do {
Vdraw(' ', 1);
} while ((vcursor_h & 07) != 0);
break;
case NLSCLASS_CTRL:
Vdraw('^' | attr, 1);
if (c == CTL_ESC('\177')) {
Vdraw('?' | attr, 1);
} else {
#ifdef IS_ASCII
/* uncontrolify it; works only for iso8859-1 like sets */
Vdraw(c | 0100 | attr, 1);
#else
Vdraw(_toebcdic[_toascii[c]|0100] | attr, 1);
#endif
}
break;
case NLSCLASS_ILLEGAL:
Vdraw('\\' | attr, 1);
Vdraw((((c >> 6) & 7) + '0') | attr, 1);
Vdraw((((c >> 3) & 7) + '0') | attr, 1);
Vdraw(((c & 7) + '0') | attr, 1);
break;
case NLSCLASS_ILLEGAL2:
case NLSCLASS_ILLEGAL3:
case NLSCLASS_ILLEGAL4:
case NLSCLASS_ILLEGAL5:
Vdraw('\\', 1);
Vdraw('U', 1);
Vdraw('+', 1);
for (i = 16 + 4 * (-w-5); i >= 0; i -= 4)
Vdraw("0123456789ABCDEF"[(c >> i) & 15] | attr, 1);
break;
case 0:
lv = vcursor_v;
lh = vcursor_h;
for (;;) {
lh--;
if (lh < 0) {
lv--;
if (lv < 0)
break;
lh = Strlen(Vdisplay[lv]) - 1;
}
if (Vdisplay[lv][lh] != CHAR_DBWIDTH)
break;
}
if (lv < 0) {
Vdraw('\\' | attr, 1);
Vdraw((((c >> 6) & 7) + '0') | attr, 1);
Vdraw((((c >> 3) & 7) + '0') | attr, 1);
Vdraw(((c & 7) + '0') | attr, 1);
break;
}
Vdisplay[lv][lh] = MakeLiteral(cp, 1, Vdisplay[lv][lh]);
break;
default:
Vdraw(*cp, w);
break;
}
return 1;
}
static void
Vdraw(Char c, int width) /* draw char c onto V lines */
{
#ifdef DEBUG_REFRESH
# ifdef SHORT_STRINGS
reprintf("Vdrawing %6.6o '%c' %d\r\n", (unsigned)c, (int)(c & ASCII), width);
# else
reprintf("Vdrawing %3.3o '%c' %d\r\n", (unsigned)c, (int)c, width);
# endif /* SHORT_STRNGS */
#endif /* DEBUG_REFRESH */
/* Hopefully this is what all the terminals do with multi-column characters
that "span line breaks". */
while (vcursor_h + width > TermH)
Vdraw(' ', 1);
Vdisplay[vcursor_v][vcursor_h] = c;
if (width)
vcursor_h++; /* advance to next place */
while (--width > 0)
Vdisplay[vcursor_v][vcursor_h++] = CHAR_DBWIDTH;
if (vcursor_h >= TermH) {
Vdisplay[vcursor_v][TermH] = '\0'; /* assure end of line */
vcursor_h = 0; /* reset it. */
vcursor_v++;
#ifdef DEBUG_REFRESH
if (vcursor_v >= TermV) { /* should NEVER happen. */
reprintf("\r\nVdraw: vcursor_v overflow! Vcursor_v == %d > %d\r\n",
vcursor_v, TermV);
abort();
}
#endif /* DEBUG_REFRESH */
}
}
/*
* RefreshPromptpart()
* draws a prompt element, expanding literals (we know it's ASCIZ)
*/
static void
RefreshPromptpart(Char *buf)
{
Char *cp;
int w;
if (buf == NULL)
return;
for (cp = buf; *cp; ) {
if (*cp & LITERAL) {
Char *litstart = cp;
while (*cp & LITERAL)
cp++;
if (*cp) {
w = NLSWidth(*cp & CHAR);
Vdraw(MakeLiteral(litstart, cp + 1 - litstart, 0), w);
cp++;
}
else {
/*
* XXX: This is a bug, we lose the last literal, if it is not
* followed by a normal character, but it is too hard to fix
*/
break;
}
}
else
cp += Draw(cp, cp == buf, 1);
}
}
/*
* Refresh()
* draws the new virtual screen image from the current input
* line, then goes line-by-line changing the real image to the new
* virtual image. The routine to re-draw a line can be replaced
* easily in hopes of a smarter one being placed there.
*/
#ifndef WINNT_NATIVE
static
#endif
int OldvcV = 0;
void
Refresh(void)
{
int cur_line;
Char *cp;
int cur_h, cur_v = 0, new_vcv;
int rhdiff;
Char oldgetting;
#ifdef DEBUG_REFRESH
reprintf("Prompt = :%s:\r\n", short2str(Prompt));
reprintf("InputBuf = :%s:\r\n", short2str(InputBuf));
#endif /* DEBUG_REFRESH */
oldgetting = GettingInput;
GettingInput = 0; /* avoid re-entrance via SIGWINCH */
/* reset the Vdraw cursor, temporarily draw rprompt to calculate its size */
vcursor_h = 0;
vcursor_v = 0;
RefreshPromptpart(RPrompt);
rprompt_h = vcursor_h;
rprompt_v = vcursor_v;
/* reset the Vdraw cursor, draw prompt */
vcursor_h = 0;
vcursor_v = 0;
RefreshPromptpart(Prompt);
cur_h = -1; /* set flag in case I'm not set */
/* draw the current input buffer */
for (cp = InputBuf; (cp < LastChar); ) {
if (cp >= Cursor && cur_h == -1) {
cur_h = vcursor_h; /* save for later */
cur_v = vcursor_v;
Cursor = cp;
}
cp += Draw(cp, cp == InputBuf, 0);
}
if (cur_h == -1) { /* if I haven't been set yet, I'm at the end */
cur_h = vcursor_h;
cur_v = vcursor_v;
}
rhdiff = TermH - vcursor_h - rprompt_h;
if (rprompt_h != 0 && rprompt_v == 0 && vcursor_v == 0 && rhdiff > 1) {
/*
* have a right-hand side prompt that will fit on
* the end of the first line with at least one
* character gap to the input buffer.
*/
while (--rhdiff > 0) /* pad out with spaces */
Vdraw(' ', 1);
RefreshPromptpart(RPrompt);
}
else {
rprompt_h = 0; /* flag "not using rprompt" */
rprompt_v = 0;
}
new_vcv = vcursor_v; /* must be done BEFORE the NUL is written */
Vdraw('\0', 1); /* put NUL on end */
#if defined (DEBUG_REFRESH)
reprintf("TermH=%d, vcur_h=%d, vcur_v=%d, Vdisplay[0]=\r\n:%80.80s:\r\n",
TermH, vcursor_h, vcursor_v, short2str(Vdisplay[0]));
#endif /* DEBUG_REFRESH */
#ifdef DEBUG_UPDATE
reprintf("updating %d lines.\r\n", new_vcv);
#endif /* DEBUG_UPDATE */
for (cur_line = 0; cur_line <= new_vcv; cur_line++) {
/* NOTE THAT update_line MAY CHANGE Display[cur_line] */
update_line(Display[cur_line], Vdisplay[cur_line], cur_line);
#ifdef WINNT_NATIVE
flush();
#endif /* WINNT_NATIVE */
/*
* Copy the new line to be the current one, and pad out with spaces
* to the full width of the terminal so that if we try moving the
* cursor by writing the character that is at the end of the
* screen line, it won't be a NUL or some old leftover stuff.
*/
cpy_pad_spaces(Display[cur_line], Vdisplay[cur_line], TermH);
}
#ifdef DEBUG_REFRESH
reprintf("\r\nvcursor_v = %d, OldvcV = %d, cur_line = %d\r\n",
vcursor_v, OldvcV, cur_line);
#endif /* DEBUG_REFRESH */
if (OldvcV > new_vcv) {
for (; cur_line <= OldvcV; cur_line++) {
update_line(Display[cur_line], STRNULL, cur_line);
*Display[cur_line] = '\0';
}
}
OldvcV = new_vcv; /* set for next time */
#ifdef DEBUG_REFRESH
reprintf("\r\nCursorH = %d, CursorV = %d, cur_h = %d, cur_v = %d\r\n",
CursorH, CursorV, cur_h, cur_v);
#endif /* DEBUG_REFRESH */
#ifdef WINNT_NATIVE
flush();
#endif /* WINNT_NATIVE */
MoveToLine(cur_v); /* go to where the cursor is */
MoveToChar(cur_h);
SetAttributes(0); /* Clear all attributes */
flush(); /* send the output... */
GettingInput = oldgetting; /* reset to old value */
}
#ifdef notdef
GotoBottom(void)
{ /* used to go to last used screen line */
MoveToLine(OldvcV);
}
#endif
void
PastBottom(void)
{ /* used to go to last used screen line */
MoveToLine(OldvcV);
(void) putraw('\r');
(void) putraw('\n');
ClearDisp();
flush();
}
/* insert num characters of s into d (in front of the character) at dat,
maximum length of d is dlen */
static void
str_insert(Char *d, int dat, int dlen, Char *s, int num)
{
Char *a, *b;
if (num <= 0)
return;
if (num > dlen - dat)
num = dlen - dat;
#ifdef DEBUG_REFRESH
reprintf("str_insert() starting: %d at %d max %d, d == \"%s\"\n",
num, dat, dlen, short2str(d));
reprintf("s == \"%s\"n", short2str(s));
#endif /* DEBUG_REFRESH */
/* open up the space for num chars */
if (num > 0) {
b = d + dlen - 1;
a = b - num;
while (a >= &d[dat])
*b-- = *a--;
d[dlen] = '\0'; /* just in case */
}
#ifdef DEBUG_REFRESH
reprintf("str_insert() after insert: %d at %d max %d, d == \"%s\"\n",
num, dat, dlen, short2str(d));
reprintf("s == \"%s\"n", short2str(s));
#endif /* DEBUG_REFRESH */
/* copy the characters */
for (a = d + dat; (a < d + dlen) && (num > 0); num--)
*a++ = *s++;
#ifdef DEBUG_REFRESH
reprintf("str_insert() after copy: %d at %d max %d, d == \"%s\"\n",
num, dat, dlen, d, short2str(s));
reprintf("s == \"%s\"n", short2str(s));
#endif /* DEBUG_REFRESH */
}
/* delete num characters d at dat, maximum length of d is dlen */
static void
str_delete(Char *d, int dat, int dlen, int num)
{
Char *a, *b;
if (num <= 0)
return;
if (dat + num >= dlen) {
d[dat] = '\0';
return;
}
#ifdef DEBUG_REFRESH
reprintf("str_delete() starting: %d at %d max %d, d == \"%s\"\n",
num, dat, dlen, short2str(d));
#endif /* DEBUG_REFRESH */
/* open up the space for num chars */
if (num > 0) {
b = d + dat;
a = b + num;
while (a < &d[dlen])
*b++ = *a++;
d[dlen] = '\0'; /* just in case */
}
#ifdef DEBUG_REFRESH
reprintf("str_delete() after delete: %d at %d max %d, d == \"%s\"\n",
num, dat, dlen, short2str(d));
#endif /* DEBUG_REFRESH */
}
static void
str_cp(Char *a, Char *b, int n)
{
while (n-- && *b)
*a++ = *b++;
}
/* ****************************************************************
update_line() is based on finding the middle difference of each line
on the screen; vis:
/old first difference
/beginning of line | /old last same /old EOL
v v v v
old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
new: eddie> Oh, my little buggy says to me, as lurgid as
^ ^ ^ ^
\beginning of line | \new last same \new end of line
\new first difference
all are character pointers for the sake of speed. Special cases for
no differences, as well as for end of line additions must be handled.
**************************************************************** */
/* Minimum at which doing an insert it "worth it". This should be about
* half the "cost" of going into insert mode, inserting a character, and
* going back out. This should really be calculated from the termcap
* data... For the moment, a good number for ANSI terminals.
*/
#define MIN_END_KEEP 4
static void /* could be changed to make it smarter */
update_line(Char *old, Char *new, int cur_line)
{
Char *o, *n, *p, c;
Char *ofd, *ols, *oe, *nfd, *nls, *ne;
Char *osb, *ose, *nsb, *nse;
int fx, sx;
/*
* find first diff (won't be CHAR_DBWIDTH in either line)
*/
for (o = old, n = new; *o && (*o == *n); o++, n++)
continue;
ofd = o;
nfd = n;
/*
* Find the end of both old and new
*/
o = Strend(o);
/*
* Remove any trailing blanks off of the end, being careful not to
* back up past the beginning.
*/
if (!(adrof(STRhighlight) && MarkIsSet)) {
while (ofd < o) {
if (o[-1] != ' ')
break;
o--;
}
}
oe = o;
*oe = (Char) 0;
n = Strend(n);
/* remove blanks from end of new */
if (!(adrof(STRhighlight) && MarkIsSet)) {
while (nfd < n) {
if (n[-1] != ' ')
break;
n--;
}
}
ne = n;
*ne = (Char) 0;
/*
* if no diff, continue to next line of redraw
*/
if (*ofd == '\0' && *nfd == '\0') {
#ifdef DEBUG_UPDATE
reprintf("no difference.\r\n");
#endif /* DEBUG_UPDATE */
return;
}
/*
* find last same pointer
*/
while ((o > ofd) && (n > nfd) && (*--o == *--n))
continue;
if (*o != *n) {
o++;
n++;
}
while (*o == CHAR_DBWIDTH) {
o++;
n++;
}
ols = o;
nls = n;
/*
* find same begining and same end
*/
osb = ols;
nsb = nls;
ose = ols;
nse = nls;
/*
* case 1: insert: scan from nfd to nls looking for *ofd
*/
if (*ofd) {
for (c = *ofd, n = nfd; n < nls; n++) {
if (c == *n) {
for (o = ofd, p = n; p < nls && o < ols && *o == *p; o++, p++)
continue;
/*
* if the new match is longer and it's worth keeping, then we
* take it
*/
if (((nse - nsb) < (p - n)) && (2 * (p - n) > n - nfd)) {
nsb = n;
nse = p;
osb = ofd;
ose = o;
}
}
}
}
/*
* case 2: delete: scan from ofd to ols looking for *nfd
*/
if (*nfd) {
for (c = *nfd, o = ofd; o < ols; o++) {
if (c == *o) {
for (n = nfd, p = o; p < ols && n < nls && *p == *n; p++, n++)
continue;
/*
* if the new match is longer and it's worth keeping, then we
* take it
*/
if (((ose - osb) < (p - o)) && (2 * (p - o) > o - ofd)) {
nsb = nfd;
nse = n;
osb = o;
ose = p;
}
}
}
}
#ifdef notdef
/*
* If `last same' is before `same end' re-adjust
*/
if (ols < ose)
ols = ose;
if (nls < nse)
nls = nse;
#endif
/*
* Pragmatics I: If old trailing whitespace or not enough characters to
* save to be worth it, then don't save the last same info.
*/
if ((oe - ols) < MIN_END_KEEP) {
ols = oe;
nls = ne;
}
/*
* Pragmatics II: if the terminal isn't smart enough, make the data dumber
* so the smart update doesn't try anything fancy
*/
/*
* fx is the number of characters we need to insert/delete: in the
* beginning to bring the two same begins together
*/
fx = (int) ((nsb - nfd) - (osb - ofd));
/*
* sx is the number of characters we need to insert/delete: in the end to
* bring the two same last parts together
*/
sx = (int) ((nls - nse) - (ols - ose));
if (!T_CanIns) {
if (fx > 0) {
osb = ols;
ose = ols;
nsb = nls;
nse = nls;
}
if (sx > 0) {
ols = oe;
nls = ne;
}
if ((ols - ofd) < (nls - nfd)) {
ols = oe;
nls = ne;
}
}
if (!T_CanDel) {
if (fx < 0) {
osb = ols;
ose = ols;
nsb = nls;
nse = nls;
}
if (sx < 0) {
ols = oe;
nls = ne;
}
if ((ols - ofd) > (nls - nfd)) {
ols = oe;
nls = ne;
}
}
/*
* Pragmatics III: make sure the middle shifted pointers are correct if
* they don't point to anything (we may have moved ols or nls).
*/
/* if the change isn't worth it, don't bother */
/* was: if (osb == ose) */
if ((ose - osb) < MIN_END_KEEP) {
osb = ols;
ose = ols;
nsb = nls;
nse = nls;
}
/*
* Now that we are done with pragmatics we recompute fx, sx
*/
fx = (int) ((nsb - nfd) - (osb - ofd));
sx = (int) ((nls - nse) - (ols - ose));
#ifdef DEBUG_UPDATE
reprintf("\n");
reprintf("ofd %d, osb %d, ose %d, ols %d, oe %d\n",
ofd - old, osb - old, ose - old, ols - old, oe - old);
reprintf("nfd %d, nsb %d, nse %d, nls %d, ne %d\n",
nfd - new, nsb - new, nse - new, nls - new, ne - new);
reprintf("xxx-xxx:\"00000000001111111111222222222233333333334\"\r\n");
reprintf("xxx-xxx:\"01234567890123456789012345678901234567890\"\r\n");
dprintstr("old- oe", old, oe);
dprintstr("new- ne", new, ne);
dprintstr("old-ofd", old, ofd);
dprintstr("new-nfd", new, nfd);
dprintstr("ofd-osb", ofd, osb);
dprintstr("nfd-nsb", nfd, nsb);
dprintstr("osb-ose", osb, ose);
dprintstr("nsb-nse", nsb, nse);
dprintstr("ose-ols", ose, ols);
dprintstr("nse-nls", nse, nls);
dprintstr("ols- oe", ols, oe);
dprintstr("nls- ne", nls, ne);
#endif /* DEBUG_UPDATE */
/*
* CursorV to this line cur_line MUST be in this routine so that if we
* don't have to change the line, we don't move to it. CursorH to first
* diff char
*/
MoveToLine(cur_line);
/*
* at this point we have something like this:
*
* /old /ofd /osb /ose /ols /oe
* v.....................v v..................v v........v
* eddie> Oh, my fredded gruntle-buggy is to me, as foo var lurgid as
* eddie> Oh, my fredded quiux buggy is to me, as gruntle-lurgid as
* ^.....................^ ^..................^ ^........^
* \new \nfd \nsb \nse \nls \ne
*
* fx is the difference in length between the chars between nfd and
* nsb, and the chars between ofd and osb, and is thus the number of
* characters to delete if < 0 (new is shorter than old, as above),
* or insert (new is longer than short).
*
* sx is the same for the second differences.
*/
/*
* if we have a net insert on the first difference, AND inserting the net
* amount ((nsb-nfd) - (osb-ofd)) won't push the last useful character
* (which is ne if nls != ne, otherwise is nse) off the edge of the screen
* (TermH - 1) else we do the deletes first so that we keep everything we
* need to.
*/
/*
* if the last same is the same like the end, there is no last same part,
* otherwise we want to keep the last same part set p to the last useful
* old character
*/
p = (ols != oe) ? oe : ose;
/*
* if (There is a diffence in the beginning) && (we need to insert
* characters) && (the number of characters to insert is less than the term
* width) We need to do an insert! else if (we need to delete characters)
* We need to delete characters! else No insert or delete
*/
if ((nsb != nfd) && fx > 0 && ((p - old) + fx < TermH)) {
#ifdef DEBUG_UPDATE
reprintf("first diff insert at %d...\r\n", nfd - new);
#endif /* DEBUG_UPDATE */
/*
* Move to the first char to insert, where the first diff is.
*/
MoveToChar(nfd - new);
/*
* Check if we have stuff to keep at end
*/
if (nsb != ne) {
#ifdef DEBUG_UPDATE
reprintf("with stuff to keep at end\r\n");
#endif /* DEBUG_UPDATE */
/*
* insert fx chars of new starting at nfd
*/
if (fx > 0) {
#ifdef DEBUG_UPDATE
if (!T_CanIns)
reprintf(" ERROR: cannot insert in early first diff\n");
#endif /* DEBUG_UPDATE */
Insert_write(nfd, fx);
str_insert(old, (int) (ofd - old), TermH, nfd, fx);
}
/*
* write (nsb-nfd) - fx chars of new starting at (nfd + fx)
*/
so_write(nfd + fx, (nsb - nfd) - fx);
str_cp(ofd + fx, nfd + fx, (int) ((nsb - nfd) - fx));
}
else {
#ifdef DEBUG_UPDATE
reprintf("without anything to save\r\n");
#endif /* DEBUG_UPDATE */
so_write(nfd, (nsb - nfd));
str_cp(ofd, nfd, (int) (nsb - nfd));
/*
* Done
*/
return;
}
}
else if (fx < 0) {
#ifdef DEBUG_UPDATE
reprintf("first diff delete at %d...\r\n", ofd - old);
#endif /* DEBUG_UPDATE */
/*
* move to the first char to delete where the first diff is
*/
MoveToChar(ofd - old);
/*
* Check if we have stuff to save
*/
if (osb != oe) {
#ifdef DEBUG_UPDATE
reprintf("with stuff to save at end\r\n");
#endif /* DEBUG_UPDATE */
/*
* fx is less than zero *always* here but we check for code
* symmetry
*/
if (fx < 0) {
#ifdef DEBUG_UPDATE
if (!T_CanDel)
reprintf(" ERROR: cannot delete in first diff\n");
#endif /* DEBUG_UPDATE */
DeleteChars(-fx);
str_delete(old, (int) (ofd - old), TermH, -fx);
}
/*
* write (nsb-nfd) chars of new starting at nfd
*/
so_write(nfd, (nsb - nfd));
str_cp(ofd, nfd, (int) (nsb - nfd));
}
else {
#ifdef DEBUG_UPDATE
reprintf("but with nothing left to save\r\n");
#endif /* DEBUG_UPDATE */
/*
* write (nsb-nfd) chars of new starting at nfd
*/
so_write(nfd, (nsb - nfd));
#ifdef DEBUG_REFRESH
reprintf("cleareol %d\n", (oe - old) - (ne - new));
#endif /* DEBUG_UPDATE */
#ifndef WINNT_NATIVE
ClearEOL((oe - old) - (ne - new));
#else
/*
* The calculation above does not work too well on NT
*/
ClearEOL(TermH - CursorH);
#endif /*WINNT_NATIVE*/
/*
* Done
*/
return;
}
}
else
fx = 0;
if (sx < 0) {
#ifdef DEBUG_UPDATE
reprintf("second diff delete at %d...\r\n", (ose - old) + fx);
#endif /* DEBUG_UPDATE */
/*
* Check if we have stuff to delete
*/
/*
* fx is the number of characters inserted (+) or deleted (-)
*/
MoveToChar((ose - old) + fx);
/*
* Check if we have stuff to save
*/
if (ols != oe) {
#ifdef DEBUG_UPDATE
reprintf("with stuff to save at end\r\n");
#endif /* DEBUG_UPDATE */
/*
* Again a duplicate test.
*/
if (sx < 0) {
#ifdef DEBUG_UPDATE
if (!T_CanDel)
reprintf(" ERROR: cannot delete in second diff\n");
#endif /* DEBUG_UPDATE */
DeleteChars(-sx);
}
/*