-
Notifications
You must be signed in to change notification settings - Fork 6
/
disp.c
1696 lines (1480 loc) · 36.3 KB
/
disp.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
/**************************************************************************
* This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is *
* provided by Jonathan and Jovehacks without charge and without *
* warranty. You may copy, modify, and/or distribute JOVE, provided that *
* this notice is included in all the source files and documentation. *
**************************************************************************/
#include "jove.h"
#include "jctype.h"
#include "chars.h"
#include "fp.h"
#include "disp.h"
#include "ask.h"
#include "extend.h"
#include "fmt.h"
#include "insert.h"
/* #include "io.h" */ /* for pwd() */
#ifdef IPROCS
# include "sysprocs.h"
# include "iproc.h"
#endif
#include "move.h"
#include "macros.h"
#include "screen.h"
#include "term.h"
#include "wind.h"
#ifdef MAC
# include "mac.h"
#else
# include <sys/stat.h>
#endif
/* define a couple of unique daddrs */
#define NOWHERE_DADDR (~NULL_DADDR | DDIRTY) /* not in tmp file */
#define UNSAVED_CURLINE_DADDR ((NOWHERE_DADDR - 1) | DDIRTY) /* not yet in tmp file */
struct screenline
*Screen = NULL, /* the screen (a bunch of screenline) */
*Curline = NULL; /* current line */
private void
DeTab proto((char *, int, char *, char *, bool)),
DoIDline proto((int)),
do_cl_eol proto((int)),
ModeLine proto((Window *, char *, int)),
GotoDot proto((void)),
UpdLine proto((int)),
UpdWindow proto((Window *, int));
#ifdef ID_CHAR
private void
DelChar proto((int, int, int)),
InsChar proto((int, int, int, char *));
private bool
IDchar proto ((char *, int)),
OkayDelete proto ((int, int, bool)),
OkayInsert proto ((int, int));
private int
NumSimilar proto ((char *, char *, int)),
IDcomp proto ((char *, char *, int));
#endif /* ID_CHAR */
private bool
AddLines proto((int, int)),
DelLines proto((int, int));
bool DisabledRedisplay = NO;
/* Kludge windows gets called by the routines that delete lines from the
* buffer. If the w->w_line or w->w_top are deleted and this procedure
* is not called, the redisplay routine will barf.
*/
void
ChkWindows(line1, line2)
LinePtr line1,
line2;
{
register Window *w = fwind;
register LinePtr lp,
lend = line2->l_next;
do {
if (w->w_bufp == curbuf) {
for (lp = line1->l_next; lp != lend; lp = lp->l_next) {
if (lp == w->w_top)
w->w_flags |= W_TOPGONE;
if (lp == w->w_line)
w->w_flags |= W_CURGONE;
}
}
w = w->w_next;
} while (w != fwind);
}
#ifdef WINRESIZE
volatile bool
ResizePending = NO; /* asynch request for screen resize */
private void
resize()
{
bool oldDisabledRedisplay = DisabledRedisplay;
int
oldILI = ILI,
oldCO = CO;
DisabledRedisplay = YES; /* prevent tragedy */
ResizePending = NO; /* early & safe */
ttsize(); /* update line (LI and ILI) and col (CO) info. */
if (oldILI != ILI || oldCO != CO) {
register int total;
register Window *wp;
/* Go through the window list, changing each window size in
* proportion to the resize. If the window would become too
* small, we delete it.
*
* Actually, we must do the deletion in
* a separate pass because del_wind donates the space to either
* neighbouring window. We test the windows in a funny order:
* top-most, then bottom-up. Although it isn't necessary
* for correctness, it means that we consider any donated
* space, cutting down the number of windows we decide to delete.
* Loop termination is tricky: fwind may have changed due to a
* del_wind. As a simple fix, we start over whenever we
* delete a window. Although this is O(n**2), it can't
* really be expensive.
*
* After scaling all the windows, we
* give any space remaining to the current window (which would
* have changed if the old current window had been deleted).
*
* This seems fairer than just resizing the current window.
*/
wp = fwind;
for (;;) {
int newsize = ILI * wp->w_height / oldILI;
if (newsize < 2) {
del_wind(wp);
wp = fwind;
} else {
wp = wp->w_prev;
if (wp == fwind)
break;
}
}
total = 0;
do {
int newsize = ILI * wp->w_height / oldILI;
wp->w_height = newsize;
total += newsize;
wp = wp->w_next;
} while (wp != fwind);
curwind->w_height += ILI - total;
/* Make a new screen structure */
make_scr();
/* Do a 'hard' update on the screen - clear and redraw */
ClAndRedraw();
#ifdef WIN32
ResizeWindow();
#endif
}
DisabledRedisplay = oldDisabledRedisplay;
}
#endif /* WINRESIZE */
private bool RingBell; /* So if we have a lot of errors ...
ring the bell only ONCE */
void
redisplay()
{
if (DisabledRedisplay)
return;
#ifdef WINRESIZE
do
#endif
{
register Window *w;
int
lineno,
i;
bool
done_ID = NO,
old_UpdModLine;
register struct scrimage
*des_p,
*phys_p;
#ifdef WINRESIZE
if (ResizePending)
resize();
#endif
curwind->w_line = curwind->w_bufp->b_dot;
curwind->w_char = curwind->w_bufp->b_char;
#ifdef MAC
/* To avoid calling redisplay() recursively,
* we must avoid calling CheckEvent(),
* so we must avoid calling charp().
*/
InputPending = NO;
#else
if (PreEmptOutput())
return;
#endif
if (RingBell) {
dobell(1);
RingBell = NO;
}
AbortCnt = ScrBufSize; /* initialize this now */
if (UpdMesg)
DrawMesg(YES);
for (lineno = 0, w = fwind; lineno < ILI; w = w->w_next) {
UpdWindow(w, lineno);
lineno += w->w_height;
}
/* Now that we've called update window, we can
* assume that the modeline will be updated. But
* if while redrawing the modeline the user types
* a character, ModeLine() is free to set this on
* again so that the modeline will be fully drawn
* at the next redisplay. Furthermore, if output
* is preempted, we'll restore the old value because
* we can't be sure that the updating has happened.
*/
old_UpdModLine = UpdModLine;
UpdModLine = NO;
des_p = DesiredScreen;
phys_p = PhysScreen;
for (i = 0; i < ILI; i++, des_p++, phys_p++) {
if (!done_ID && (des_p->s_id != phys_p->s_id)) {
DoIDline(i);
done_ID = YES;
}
if ((des_p->s_flags & (s_DIRTY | s_L_MOD))
|| des_p->s_id != phys_p->s_id
|| des_p->s_vln != phys_p->s_vln
|| des_p->s_offset != phys_p->s_offset)
UpdLine(i);
if (CheapPreEmptOutput()) {
if (old_UpdModLine)
UpdModLine = YES;
goto suppress;
}
}
if (Asking) {
int p = calc_pos(mesgbuf, AskingWidth);
jdbg("redisplay CO=%d aw=%d p=%d \"%s\"\n", CO, AskingWidth, p, mesgbuf);
Placur(ILI, jmin(CO - 2, p));
/* Nice kludge */
flushscreen();
} else {
GotoDot();
}
suppress: ;
}
#ifdef WINRESIZE
/**/ while (ResizePending);
#endif
#ifdef MAC
if (Windchange)
docontrols();
#endif
}
/* find_pos() returns the position on the line, that C_CHAR represents
* in LINE
*/
private int
find_pos(line, c_char)
LinePtr line;
int c_char;
{
return calc_pos(lcontents(line), c_char);
}
/* calc_pos calculates the screen column of character c_char.
*
* Note: the calc_pos, how_far, and DeTab must be in synch --
* each thinks it knows how characters are displayed.
*/
int
calc_pos(lp, c_char)
register char *lp;
register int c_char;
{
register int pos = 0;
register ZXchar c;
while ((--c_char >= 0) && (c = ZXC(*lp++)) != 0) {
if (c == '\t' && tabstop != 0) {
pos += TABDIST(pos);
} else if (jisprint(c)) {
pos += 1;
} else {
if (c <= DEL)
pos += 2;
else
pos += 4;
}
}
return pos;
}
volatile bool UpdModLine = NO;
bool UpdMesg = NO;
private void
DoIDline(start)
int start;
{
register struct scrimage *des_p = &DesiredScreen[start];
struct scrimage *phys_p = &PhysScreen[start];
register int i,
j;
/* Some changes have been made. Try for insert or delete lines.
* If either case has happened, Addlines and/or DelLines will do
* necessary scrolling, also CONVERTING PhysScreen to account for the
* physical changes. The comparison continues from where the
* insertion/deletion takes place; this doesn't happen very often,
* usually it happens with more than one window with the same
* buffer.
*/
#ifdef TERMCAP
if (!CanScroll)
return; /* We should never have been called! */
#endif
for (i = start; i < ILI; i++, des_p++, phys_p++)
if (des_p->s_id != phys_p->s_id)
break;
for (; i < ILI; i++) {
for (j = i + 1; j < ILI; j++) {
des_p = &DesiredScreen[j];
phys_p = &PhysScreen[j];
if (des_p->s_id != NULL_DADDR && des_p->s_id == phys_p->s_id)
break;
if (des_p->s_id == PhysScreen[i].s_id) {
if (des_p->s_id == NULL_DADDR)
continue;
if (AddLines(i, j - i)) {
DoIDline(j);
return;
}
break;
}
if ((des_p = &DesiredScreen[i])->s_id == phys_p->s_id) {
if (des_p->s_id == NULL_DADDR)
continue;
if (DelLines(i, j - i)) {
DoIDline(i);
return;
}
break;
}
}
}
}
/* Make DesiredScreen reflect what the screen should look like when we are done
* with the redisplay. This deals with horizontal scrolling. Also makes
* sure the current line of the Window is in the window.
*/
bool ScrollAll = NO; /* VAR: when current line scrolls, scroll whole window? */
int ScrollWidth = 10; /* VAR: unit of horizontal scrolling */
private void
UpdWindow(w, start)
register Window *w;
int start;
{
LinePtr lp;
long i,
upper, /* top of window */
lower; /* bottom of window */
int
strt_col, /* starting print column of current line */
ntries = 0; /* # of tries at updating window */
register struct scrimage *des_p,
*phys_p;
Buffer *bp = w->w_bufp;
do {
if (w->w_flags & W_CURGONE) {
w->w_line = bp->b_dot;
w->w_char = bp->b_char;
}
if (w->w_flags & W_TOPGONE)
CentWind(w); /* reset topline of screen */
w->w_flags &= ~(W_CURGONE | W_TOPGONE);
/* make sure that the current line is in the window */
upper = start;
lower = upper + WSIZE(w);
for (i = upper, lp = w->w_top; ; lp = lp->l_next, i++) {
if (i == lower || lp == NULL) {
/* we've run out of window without finding dot */
ntries += 1;
if (ntries == 1) {
CalcWind(w);
} else if (ntries == 2) {
w->w_top = w->w_line = w->w_bufp->b_first;
writef("\rERROR in redisplay: I got hopelessly lost!");
dobell(2);
} else {
writef("\n\rOops, still lost, quitting ...\r\n");
finish(-1); /* die! */
/*NOTREACHED*/
}
break;
}
if (lp == w->w_line) {
ntries = 0; /* happiness: dot is in window */
break;
}
}
} while (ntries != 0);
/* first do some calculations for the current line */
{
int
nw = W_NUMWIDTH(w),
dot_col,
end_col;
strt_col = ScrollAll? w->w_LRscroll : PhysScreen[i].s_offset;
end_col = strt_col + (CO - 1) - (nw + SIWIDTH(strt_col));
/* Right now we are displaying from strt_col to
* end_col of the buffer line. These are PRINT
* columns, not actual characters.
*/
dot_col = w->w_dotcol = find_pos(w->w_line, w->w_char);
/* if the new dotcol is out of range, reselect
* a horizontal window
*/
if (PhysScreen[i].s_offset == -1
|| !(strt_col <= dot_col && dot_col < end_col))
{
/* If dot_col is within first step left of screen, step left.
* Otherwise, if ditto for right.
* Otherwise, if it is in first screenwidth, start from begining.
* Otherwise, center dot_col.
* Fudge: if a scroll left would work except for the necessary
* appearance of an ! on the left, we scroll an extra column.
*/
int
step = jmin(ScrollWidth, end_col - strt_col);
strt_col =
strt_col > dot_col && strt_col - step <= dot_col
? jmax(strt_col - step, 0)
: dot_col >= end_col && dot_col < end_col + step
? jmin(strt_col + step
+ (strt_col == 0 && dot_col == end_col + step - 1? 1 : 0)
, dot_col)
: dot_col < ((CO - 1) - nw)
? 0
: dot_col - ((CO - nw) / 2);
if (ScrollAll) {
if (w->w_LRscroll != strt_col)
UpdModLine = YES;
w->w_LRscroll = strt_col;
}
}
w->w_dotline = i;
w->w_dotcol = dot_col + nw + SIWIDTH(strt_col);
}
lp = w->w_top;
des_p = &DesiredScreen[upper];
phys_p = &PhysScreen[upper];
for (i = upper; i < lower; i++, des_p++, phys_p++) {
if (lp != NULL) {
des_p->s_offset = (lp == w->w_line)? strt_col : w->w_LRscroll;
des_p->s_flags = isdirty(lp) ? s_L_MOD : 0;
des_p->s_vln = (w->w_flags & W_NUMLINES)?
w->w_topnum + (i - upper) : 0;
des_p->s_id = (lp == curline && DOLsave)?
UNSAVED_CURLINE_DADDR : lp->l_dline & ~DDIRTY;
des_p->s_lp = lp;
lp = lp->l_next;
} else {
/* display line beyond end of buffer */
static const struct scrimage
clean_plate = { 0, 0, 0, NULL_DADDR, NULL, NULL };
*des_p = clean_plate;
if (phys_p->s_id != NULL_DADDR)
des_p->s_flags = s_DIRTY;
}
des_p->s_window = w;
}
/* mode line: */
/* ??? The following assignment to des_p->s_id is tricky:
*
* We count on the cast pointer value being distinct from
* any other daddr, but equal to itself. Turning
* the "DDIRTY" bit on should ensure that it is distinct
* from legitimate daddr values (except for NOWHERE_DADDR
* and UNSAVED_CURLINE_DADDR).
* If sizeof(Buffer *)>sizeof(daddr), nothing ensures that
* these pointers are even distinct from each other.
*
* There also seems to be an assumption that every modeline
* for a particular buffer will be the same. This is not
* always the case: the last modeline on the screen is usually
* different from any other modeline, even for the same buffer.
* Currently, I think that only very contrived cases could cause
* problems (probably involving window resizing).
* Further problems will arise if JOVE is changed so that there are
* other ways in which a modeline can reflect the window state
* (instead of just the buffer state).
*
* -- DHR
*/
des_p->s_window = w;
des_p->s_id = (jwid_t) w->w_bufp | DDIRTY;
des_p->s_flags = (des_p->s_id != phys_p->s_id || UpdModLine)?
s_MODELINE | s_DIRTY : 0;
des_p->s_offset = 0;
des_p->s_vln = 0;
des_p->s_lp = NULL;
#ifdef MAC
if (UpdModLine)
Modechange = YES;
if (w == curwind && w->w_control != NULL)
SetScrollBar(w);
#endif
}
/* Write whatever is in mesgbuf (maybe we are Asking, or just printed
* a message). Turns off the UpdateMesg line flag.
*/
void
DrawMesg(abortable)
bool abortable;
{
char outbuf[MAXCOLS + PPWIDTH]; /* assert(CO <= MAXCOLS); */
#ifndef MAC /* same reason as in redisplay() */
if (PreEmptOutput())
return;
#endif
i_set(ILI, 0);
jdbg("DrawMesg aw=%d \"%s\"\n", AskingWidth, mesgbuf);
DeTab(mesgbuf, 0, outbuf, outbuf + CO, NO);
if (swrite(outbuf, NOEFFECT, abortable)) {
cl_eol();
UpdMesg = NO;
}
flushscreen();
}
/* Goto the current position in the current window. Presumably redisplay()
* has already been called, and curwind->{w_dotline,w_dotcol} have been set
* correctly.
*/
private void
GotoDot()
{
if (!CheapPreEmptOutput()) {
Placur(curwind->w_dotline,
curwind->w_dotcol - PhysScreen[curwind->w_dotline].s_offset);
flushscreen();
}
}
private int
UntilEqual(start)
register int start;
{
register struct scrimage *des_p = &DesiredScreen[start],
*phys_p = &PhysScreen[start];
while ((start < ILI) && (des_p->s_id != phys_p->s_id)) {
des_p += 1;
phys_p += 1;
start += 1;
}
return start;
}
/* Calls the routine to do the physical changes, and changes PhysScreen to
* reflect those changes.
*/
private bool
AddLines(at, num)
register int at,
num;
{
register int i;
int bottom = UntilEqual(at + num);
if (num == 0 || num >= ((bottom - 1) - at))
return NO; /* we did nothing */
v_ins_line(num, at, bottom - 1);
/* Now change PhysScreen to account for the physical change. */
for (i = bottom - 1; i - num >= at; i--)
PhysScreen[i] = PhysScreen[i - num];
for (i = 0; i < num; i++)
PhysScreen[at + i].s_id = NULL_DADDR;
return YES; /* we did something */
}
private bool
DelLines(at, num)
register int at,
num;
{
register int i;
int bottom = UntilEqual(at + num);
if (num == 0 || num >= ((bottom - 1) - at))
return NO;
v_del_line(num, at, bottom - 1);
for (i = at; num + i < bottom; i++)
PhysScreen[i] = PhysScreen[num + i];
for (i = bottom - num; i < bottom; i++)
PhysScreen[i].s_id = NULL_DADDR;
return YES;
}
bool MarkHighlighting = YES; /* VAR: highlight mark when visible */
/* Update line linenum in window w. Only set PhysScreen to DesiredScreen
* if the swrite or cl_eol works, that is nothing is interrupted by
* characters typed.
*/
private void
UpdLine(linenum)
register int linenum;
{
register struct scrimage *des_p = &DesiredScreen[linenum];
register Window *w = des_p->s_window;
char outbuf[MAXCOLS + PPWIDTH]; /* assert(CO <= MAXCOLS); */
i_set(linenum, 0);
if (des_p->s_flags & s_MODELINE) {
ModeLine(w, outbuf, linenum);
} else if (des_p->s_id != NULL_DADDR) {
char *lptr;
int fromcol = W_NUMWIDTH(w);
#ifdef HIGHLIGHTING
static struct LErange lr = {0, 0, LENULLPROC, US_effect};
Mark *mark = b_curmark(w->w_bufp);
bool marked_line = (MarkHighlighting
# ifdef TERMCAP
&& US != NULL
# endif
&& mark != NULL
&& mark->m_line == des_p->s_lp);
#endif /* HIGHLIGHTING */
des_p->s_lp->l_dline &= ~DDIRTY;
des_p->s_flags &= ~(s_DIRTY | s_L_MOD);
if (w->w_flags & W_NUMLINES) {
outbuf[fromcol-2] = ' ';
swritef(outbuf, sizeof(outbuf), "%6D ", des_p->s_vln);
if (outbuf[fromcol-2] != ' ') {
outbuf[fromcol-2] = '*';
outbuf[fromcol-1] = ' ';
}
}
if (des_p->s_offset != 0) {
outbuf[fromcol++] = '!';
outbuf[fromcol] = '\0';
}
lptr = lcontents(des_p->s_lp);
DeTab(lptr, des_p->s_offset, outbuf + fromcol,
outbuf + CO, (w->w_flags & W_VISSPACE) != 0);
#ifdef HIGHLIGHTING
if (marked_line) {
lr.start = calc_pos(lptr, mark->m_char)
- des_p->s_offset + fromcol;
lr.width = 1;
if (lr.start < sizeof(outbuf) - 1 && outbuf[lr.start] == '\0') {
outbuf[lr.start] = ' ';
outbuf[lr.start + 1] = '\0';
}
}
#endif /* HIGHLIGHTING */
#ifdef ID_CHAR
/* REMIND: This code, along with the rest of the
* ID_CHAR, belongs in the screen driver for
* termcap based systems. mac and pc's and other
* window-based drivers don't give a hoot about
* ID_CHAR.
*/
/* attempt to exploit insert or delete character capability
* but only if not highlighting some part of the line
*/
if (UseIC && Curline->s_effects == NOEFFECT
# ifdef HIGHLIGHTING
&& !marked_line
# endif /* HIGHLIGHTING */
) {
if (IDchar(outbuf, linenum)) {
/* success: clean up and go home */
PhysScreen[linenum] = *des_p;
return;
}
/* failure: re-initialize various cursors */
i_set(linenum, 0);
}
#endif /* ID_CHAR */
if (swrite(outbuf,
#ifdef HIGHLIGHTING
marked_line ? &lr : NOEFFECT,
#else
NOEFFECT,
#endif
YES))
{
do_cl_eol(linenum);
} else {
/* interrupted: mark indeterminate state */
PhysScreen[linenum].s_id = NOWHERE_DADDR;
}
} else if (PhysScreen[linenum].s_id != NULL_DADDR) {
/* not the same ... make sure */
do_cl_eol(linenum);
}
}
private void
do_cl_eol(linenum)
register int linenum;
{
cl_eol();
PhysScreen[linenum] = DesiredScreen[linenum];
}
/* Expand tabs (and other funny characters) of a section of "buf"
* into "outbuf".
*
* Note: outbuf must allow for at least PPWIDTH extra characters.
* This is sufficient room for one extra character to be displayed,
* streamlining the code.
*
* Note: the calc_pos, how_far, and DeTab must be in synch --
* each thinks it knows how characters are displayed.
*/
private void
DeTab(src, start_offset, dst, dst_limit, visspace)
char *src;
int start_offset;
char *dst;
char *dst_limit;
bool visspace;
{
ZXchar c;
int offset = start_offset;
/* At any time, the number of characters we've output is
* start_offset - offset. This is needed to correctly
* calculate TABDIST() without having to add another
* variable (pos) to be incremented for each call to addc.
*/
#define addc(ch) { if (--offset < 0) *dst++ = (ch); }
while ((c = ZXC(*src++)) != '\0') {
if (c == '\t' && tabstop != 0) {
int nchars = TABDIST(start_offset - offset);
c = visspace? '>' : ' ';
while (--nchars > 0 && dst < dst_limit) {
addc(c);
c = ' ';
}
} else if (jisprint(c)) {
if (visspace && c == ' ')
c = '_';
} else {
char buf[PPWIDTH];
char *p;
PPchar(c, buf);
/* assert(buf[0] != '\0'); */
for (p = buf; (c = *p++), *p != '\0'; )
addc(c);
}
if (--offset < 0) {
*dst++ = c;
if (dst >= dst_limit) {
/* we've run out of real estate: truncate and flag it */
dst = dst_limit-1;
*dst++ = '!';
break;
}
}
}
#undef addc
*dst = '\0';
}
#ifdef ID_CHAR
/* From here to the end of the file is code that tries to utilize the
* insert/delete character feature on some terminals. It is very confusing
* and not so well written code, AND there is a lot of it. You may want
* to use the space for something else.
*/
bool IN_INSmode = NO;
void
INSmode(on)
bool on;
{
if (on != IN_INSmode) {
putpad(on? IM : EI, 1);
IN_INSmode = on;
}
}
/* ID character routines full of special cases and other fun stuff like that.
* It actually works though ...
*
* Returns Non-Zero if you are finished (no differences left).
*/
private bool
IDchar(new, lineno)
register char *new;
int lineno;
{
register int col = 0;
struct screenline *sline = &Screen[lineno];
register char *old = sline->s_line;
int newlen = strlen(new);
for (;;) {
int oldlen = sline->s_roof - old;
int i;
for (; ; col++) {
if (col == oldlen || col == newlen)
return oldlen == newlen; /* one ended; happy if both ended */
if (old[col] != new[col])
break;
}
/* col now is first difference, and not the end of either */
/* see if an insertion will help */
for (i = col + 1; i < newlen; i++) {
if (new[i] == old[col]) {
/* The number of saved characters is (roughly)
* the number of characters we can retain after
* the insertion, minus the number that we
* could have salvaged without moving them.
*/
int NumSaved = IDcomp(new + i, old + col, oldlen-col)
- NumSimilar(new + col, old + col, jmin(i, oldlen)-col);
if (OkayInsert(NumSaved, i - col)) {
InsChar(lineno, col, i - col, new);
col = i;
break;
}
}
}
if (i != newlen)
continue;
/* see if a deletion will help */
for (i = col + 1; i < oldlen; i++) {
if (new[col] == old[i]) {
int NumSaved = IDcomp(new + col, old + i, oldlen - i);
if (OkayDelete(NumSaved, i - col, newlen == oldlen)) {
DelChar(lineno, col, i - col);
break;
}
}
}
if (i != oldlen)
continue;
return NO;
}
}
private int
NumSimilar(s, t, n)
register char *s,
*t;
int n;
{
register int num = 0;
while (n--)
if (*s++ == *t++)
num += 1;
return num;
}
private int
IDcomp(s, t, len)
register char *s, /* NUL terminated */
*t; /* len chars */
int len;
{
register int i;
int num = 0,
nonspace = 0;
for (i = 0; i < len; i++) {
char c = *s++;
if (c == '\0' || c != *t++)
break;
if (c != ' ')
nonspace = 1;
num += nonspace;
}
return num;
}
private bool
OkayDelete(Saved, num, samelength)
int Saved,
num;
bool samelength;
{
/* If the old and the new have different lengths, then the competition
* will have to clear to end of line. We take that into consideration.
* Note: we must avoid multiplying by INFINITY.
*/
return Saved + (samelength ? 0 : CElen) >
jmin(MDClen, DC != NULL? DClen * num : INFINITY);
}
private bool
OkayInsert(Saved, num)
int Saved,
num;
{
# ifdef NCURSES_BUG
/* Note: with the ncurses version of termcap/terminfo, we must use
* any one of insert mode, insert character, or multiple insert character.
*/
register int n = INFINITY;
if (IC != NULL) /* Per character prefixes */
n = num * IClen;
n = jmin(n, MIClen);
n = jmin(n, IMEIlen);
# else /* !NCURSES_BUG */
/* Note: the way termcap/terminfo is defined, we must use *both*
* IC and IM to insert, but normally only one will be defined.
* See terminfo(5), under the heading "Insert/Delete Character".
*/
register int n = 0;
if (IC != NULL) /* Per character prefixes */
n = jmin(num * IClen, MIClen);
if (!IN_INSmode)