-
Notifications
You must be signed in to change notification settings - Fork 6
/
screen.c
1019 lines (888 loc) · 22.9 KB
/
screen.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 "fp.h"
#include "chars.h"
#include "jctype.h"
#include "disp.h"
#include "extend.h"
#include "fmt.h"
#include "term.h"
#include "mac.h"
#include "screen.h"
#include "wind.h"
int AbortCnt,
tabstop = 8; /* VAR: expand tabs to this number of spaces */
struct scrimage
*DesiredScreen = NULL,
*PhysScreen = NULL;
private struct screenline *Savelines = NULL; /* scratch entries (LI of them) */
private void LEclear proto((struct screenline *)); /* free s_effects component */
private char *cursor; /* offset into current Line */
char *cursend;
/* Position in tercap screen. INFINITY means "don't know". */
int CapCol,
CapLine;
private int
i_line,
i_col;
void
make_scr()
{
register int i;
register struct screenline *ns;
register char *nsp;
static char *screenchars = NULL;
static volatile int oldLI = 0;
/* In case we are RESHAPING the window! */
if (DesiredScreen != NULL)
free((UnivPtr) DesiredScreen);
if (PhysScreen != NULL)
free((UnivPtr) PhysScreen);
i = oldLI;
oldLI = 0;
if (Savelines != NULL) {
/* Note: each screenline in Savelines has a null s_effects
* (or is uninitialized). LEclear must not be applied.
*/
free((UnivPtr) Savelines);
}
if (Screen != NULL) {
#ifdef HIGHLIGHTING
for (ns = Screen; ns != &Screen[i]; ns++)
LEclear(ns);
#endif
free((UnivPtr) Screen);
}
if (screenchars != NULL)
free((UnivPtr) screenchars); /* free all the screen data */
DesiredScreen = (struct scrimage *) malloc((unsigned) LI * sizeof (struct scrimage));
PhysScreen = (struct scrimage *) malloc((unsigned) LI * sizeof (struct scrimage));
Savelines = (struct screenline *)
malloc((unsigned) LI * sizeof(struct screenline));
ns = Screen = (struct screenline *)
malloc((unsigned) LI * sizeof(struct screenline));
nsp = screenchars = (char *) malloc((size_t)CO * LI);
if (DesiredScreen == NULL
|| PhysScreen == NULL
|| Savelines == NULL
|| ns == NULL
|| nsp == NULL)
{
writef("\n\rCannot malloc screen!\n");
finish(-1); /* die! */
}
for (i = 0; i < LI; i++) {
ns->s_line = nsp;
/* End of Line (nsp[CO-1] is never used) */
ns->s_roof = nsp + CO - 1;
ns->s_effects = NOEFFECT;
nsp += CO;
ns += 1;
/* ??? The following is a fudge to placate Purify.
* There is a real bug here, so we squash it with
* a sledge hammer. What is the correct fix?
*/
{
register struct scrimage *p;
p = &PhysScreen[i];
p->s_offset = 0;
p->s_flags = 0;
p->s_vln = 0;
p->s_id = NULL_DADDR;
p->s_lp = NULL;
p->s_window = NULL;
p = &DesiredScreen[i];
p->s_offset = 0;
p->s_flags = 0;
p->s_vln = 0;
p->s_id = NULL_DADDR;
p->s_lp = NULL;
p->s_window = NULL;
}
}
oldLI = LI;
SO_off();
#ifdef HIGHLIGHTING
US_effect(NO);
#endif
cl_scr(NO);
}
void
clrline(cp1, cp2)
register char *cp1,
*cp2;
{
while (cp1 < cp2)
*cp1++ = ' ';
}
/* Output one character (if necessary) at the current position */
#ifdef MAC
/* Character output to bit-mapped screen is very expensive. It makes
* much more sense to write the entire line at once. So, we print all
* the characters, whether already there or not, once the line is
* complete.
*/
private unsigned char sput_buf[255];
private size_t sput_len = 0;
private void
sput_start()
{
/* if (i_line != CapLine || i_col != CapCol) */
NPlacur(i_line, i_col);
sput_len = 0;
}
private void
sput_end()
{
if (sput_len != 0) {
writetext(sput_buf, sput_len);
sput_len = 0;
}
}
private void
sputc(c)
register char c;
{
/* if line gets too long for sput_buf, ignore subsequent chars */
if (sput_len < sizeof(sput_buf)) {
*cursor++ = c;
sput_buf[sput_len++] = (c == '0')? 0xAF /* slashed zero */ : c;
CapCol++;
i_col++;
}
}
#else /* !MAC */
# ifdef HIGHLIGHTING
# define CharChanged(c) (*cursor != (char) (c))
# else /* !HIGHLIGHTING */
private bool ChangeEffect = NO;
# define CharChanged(c) (ChangeEffect || *cursor != (char) (c))
# endif /* !HIGHLIGHTING */
# ifdef IBMPCDOS
/* On PC, we think that trying to avoid painting the character
* is slower than just doing it. I wonder if this is true.
*/
# define sputc(c) do_sputc(c)
# else /* !IBMPCDOS */
# define sputc(c) { \
if (CharChanged(c)) { \
do_sputc(c); \
} else { \
cursor += 1; \
i_col += 1; \
} \
}
# endif /* !IBMPCDOS */
private void
do_sputc(c)
register char c;
{
if (CharChanged(c)) {
# ifdef ID_CHAR
INSmode(NO);
# endif
if (i_line != CapLine || i_col != CapCol)
Placur(i_line, i_col);
*cursor++ = c;
# ifdef TERMCAP
if (UL && c == '_' && *cursor != ' ')
putstr(" \b"); /* Erase so '_' looks right. */
# endif
scr_putchar(c);
AbortCnt -= 1;
CapCol += 1;
} else {
cursor += 1;
}
i_col += 1;
}
#endif /* !MAC */
#ifdef HIGHLIGHTING
private void (*real_effect) ptrproto((bool));
private void
do_hlsputc(hl, oldhl, c)
register const struct LErange *hl; /* desired highlighting */
register const struct LErange *oldhl; /* previous highlighting */
char c;
{
/* assert: hl != NULL && oldhl != NULL
* In other words, hl and oldhl must point to real LErange structs.
*/
/* The following two initializing expressions use the peculiar
* properties of unsigneds to make an efficient range test.
*/
void
(*virtual_effect) ptrproto((bool)) =
(unsigned)i_col - hl->start < hl->width? hl->high : hl->norm,
(*underlying_effect) ptrproto((bool)) =
(unsigned)i_col - oldhl->start < oldhl->width? oldhl->high : oldhl->norm;
if (*cursor != c || virtual_effect != underlying_effect) {
# ifdef ID_CHAR
INSmode(NO);
# endif
if (i_line != CapLine || i_col != CapCol)
Placur(i_line, i_col);
if (virtual_effect != real_effect) {
if (real_effect != NULL)
real_effect(NO);
/* instantaneously in neutral state */
if (virtual_effect != NULL)
virtual_effect(YES);
real_effect = virtual_effect;
}
# ifdef TERMCAP
if (UL && c == '_' && *cursor != ' ')
putstr(" \b"); /* Erase so '_' looks right. */
# endif
*cursor++ = c;
scr_putchar(c);
AbortCnt -= 1;
CapCol += 1;
} else {
cursor += 1;
}
i_col += 1;
}
#endif /* HIGHLIGHTING */
void
cl_eol()
{
if (cursor == Curline->s_line)
LEclear(Curline); /* in case swrite was not called (hack!) */
if (cursor < Curline->s_roof) {
#ifdef TERMCAP
if (CE) {
Placur(i_line, i_col);
putpad(CE, 1);
clrline(cursor, Curline->s_roof);
} else {
/* Ugh. The slow way for dumb terminals. */
register char *savecp = cursor;
while (cursor < Curline->s_roof)
sputc(' ');
cursor = savecp;
}
#else /* !TERMCAP */
Placur(i_line, i_col);
clr_eoln(); /* MAC and PCSCR define this */
clrline(cursor, Curline->s_roof);
#endif /* !TERMCAP */
Curline->s_roof = cursor;
}
}
void
cl_scr(doit)
bool doit;
{
register int i;
register struct screenline *sp = Screen;
for (i = 0; i < LI; i++, sp++) {
LEclear(sp);
clrline(sp->s_line, sp->s_roof);
sp->s_roof = sp->s_line;
PhysScreen[i].s_id = NULL_DADDR;
}
if (doit) {
clr_page();
CapCol = CapLine = 0;
UpdMesg = YES;
}
}
/* routines to manage a pool of LErange structs */
#ifdef HIGHLIGHTING
union LEspace {
struct LErange le;
union LEspace *next;
};
private union LEspace *LEfreeHead = NULL;
private struct LErange *
LEnew()
{
struct LErange *ret;
if (LEfreeHead == NULL) {
LEfreeHead = (union LEspace *) emalloc(sizeof(union LEspace));
LEfreeHead->next = NULL;
}
ret = &LEfreeHead->le;
LEfreeHead = LEfreeHead->next;
return ret;
}
#endif /* HIGHLIGHTING */
private void
LEclear(sl)
struct screenline *sl;
{
#ifdef HIGHLIGHTING
if (sl->s_effects != NOEFFECT) {
((union LEspace *) sl->s_effects)->next = LEfreeHead;
LEfreeHead = (union LEspace *) sl->s_effects;
}
#endif /* HIGHLIGHTING */
sl->s_effects = NOEFFECT;
}
/* Write `line' at the current position of `cursor'. Stop when we
* reach the end of the screen. Aborts if there is a character
* waiting.
*
* Note: All callers must have "DeTabed" "line", or processed
* it equivalently -- it is presumed that line contains only
* displayable characters.
*/
bool
swrite(line, hl, abortable)
register char *line;
LineEffects hl;
bool abortable;
{
register int n = cursend - cursor;
bool aborted = NO;
/* Unfortunately, neither of our LineEffects representation
* is suitable for representing the state of a partially
* updated line. Consequently, this routine unconditionally
* replaces the old hl with the new. To ensure that the new
* hl is correct, we compute MinCol to indicate how far in
* the line we must get, and will not abort until we have
* reached at least that column.
*
* This is unacceptably ugly. We really must switch to a better
* representation
*/
int MinCol = 0;
#ifdef HIGHLIGHTING
/* If either the old line or the new line has effects,
* we know that some effects processing is necessary.
* If so, we ensure that the old line has an effect
* by adding a no-op effect if necessary: this is needed
* to ensure Placur does not get into trouble. (UGLY!)
*/
struct LErange *oldhl = Curline->s_effects;
static const struct LErange nohl = { 0, 0, LENULLPROC, LENULLPROC };
if (oldhl != NOEFFECT) {
int w = Curline->s_roof - Curline->s_line;
if (oldhl->norm != NULL)
MinCol = w;
if (oldhl->high != NULL && w > (int)oldhl->start)
MinCol = jmax(MinCol, jmin(w, (int) (oldhl->start + oldhl->width)));
}
/* If either the old line or the new line has effects,
* we know that some effects processing is necessary.
* If so, we ensure that the old line has an effect
* by adding a no-op effect if necessary: this is needed
* to ensure Placur does not get into trouble. (UGLY!)
*/
if (hl != NOEFFECT) {
if (hl->high != NULL)
MinCol = jmax(MinCol, (int) (hl->start + hl->width));
if (oldhl == NOEFFECT) {
oldhl = Curline->s_effects = LEnew(); /* keep Placur on-track */
*oldhl = nohl;
}
}
real_effect = LENULLPROC;
#else /* !HIGHLIGHTING */
if (Curline->s_effects != hl)
MinCol = Curline->s_roof - Curline->s_line; /* must obliterate old */
#endif /* !HIGHLIGHTING */
if (n > 0) {
register ZXchar c;
#ifdef HIGHLIGHTING
/* nnhl: non-NULL version of hl (possibly
* a no-op) to reduce the cases handled.
*/
const struct LErange *nnhl = hl == NOEFFECT? &nohl : hl;
# define spit(c) { if (oldhl != NULL) do_hlsputc(nnhl,oldhl,c); else sputc(c); }
#else /* !HIGHLIGHTING */
# define spit(c) sputc(c)
# ifdef MAC
sput_start(); /* Okay, because no interruption possible */
# else /* !MAC */
if (hl != Curline->s_effects)
ChangeEffect = YES;
# endif /* !MAC */
if (hl != NOEFFECT)
SO_effect(YES);
#endif /* !HIGHLIGHTING */
while ((c = ZXC(*line++)) != '\0') {
if (abortable && i_col >= MinCol && AbortCnt < 0) {
AbortCnt = ScrBufSize;
if (PreEmptOutput()) {
aborted = YES;
break;
}
}
#ifdef TERMCAP
if (Hazeltine && c == '~')
c = '`';
#endif
#ifdef CODEPAGE437
/* ??? Some archane mapping of IBM PC characters.
* According to the appendix of the Microsoft MSDOS
* Operating System 5.0 User's Guide and Reference,
* in Code Page 437 (USA English) ' ', 0x00, and 0xFF are
* blank and 0x01 is a face.
*/
if (c == 0xFF)
c = 1;
else if (c == ' ' && hl != NOEFFECT)
c = 0xFF;
#endif /* CODEPAGE437 */
if (--n <= 0) {
/* We've got one more column -- how will we spend it?
* ??? This is probably redundant -- callers do truncation.
*/
if (*line != '\0')
c = '!';
spit(c);
break;
}
spit(c);
}
#ifdef HIGHLIGHTING
if (real_effect != NULL)
real_effect(NO);
#else /* !HIGHLIGHTING */
# ifdef MAC
sput_end(); /* flush before reverting SO */
# else /* !MAC */
ChangeEffect = NO;
# endif /* !MAC */
if (hl != NOEFFECT)
SO_off();
#endif /* !HIGHLIGHTING */
if (cursor > Curline->s_roof)
Curline->s_roof = cursor;
# undef spit
}
#ifdef HIGHLIGHTING
if (hl == NOEFFECT)
LEclear(Curline);
else
*(Curline->s_effects) = *hl;
#else /* !HIGHLIGHTING */
Curline->s_effects = hl;
#endif /* !HIGHLIGHTING */
return !aborted;
}
void
i_set(nline, ncol)
register int nline,
ncol;
{
Curline = &Screen[nline];
cursor = Curline->s_line + ncol;
cursend = &Curline->s_line[CO - 1];
i_line = nline;
i_col = ncol;
}
void
SO_off()
{
SO_effect(NO);
}
#ifdef TERMCAP
void
SO_effect(on)
bool on;
{
/* If there are magic cookies, then WHERE the SO string is
* printed decides where the SO actually starts on the screen.
* So it's important to make sure the cursor is positioned there
* anyway. I think this is right.
*/
if (SG != 0) {
Placur(i_line, i_col);
i_col += SG;
CapCol += SG;
cursor += SG;
}
putpad(on? SO : SE, 1);
}
# ifdef HIGHLIGHTING
void
US_effect(on)
bool on;
{
if (UG == 0) /* not used if magic cookies */
putpad(on? US : UE, 1);
}
# endif /* HIGHLIGHTING */
#endif /* TERMCAP */
/* Insert `num' lines at top, but leave all the lines BELOW `bottom'
* alone (at least they won't look any different when we are done).
* This changes the screen array AND does the physical changes.
*/
void
v_ins_line(num, top, bottom)
int num,
top,
bottom;
{
register int i;
/* assert(num <= bottom-top+1) */
/* Blank and save the screen pointers that will fall off the end. */
for(i = 0; i < num; i++) {
struct screenline *sp = &Screen[bottom - i];
clrline(sp->s_line, sp->s_roof);
sp->s_roof = sp->s_line;
LEclear(sp);
Savelines[i] = *sp;
}
/* Num number of bottom lines will be lost.
* Copy everything down num number of times.
*/
for (i = bottom-num; i >= top; i--)
Screen[i + num] = Screen[i];
/* Insert the now-blank saved ones at the top. */
for (i = 0; i < num; i++)
Screen[top + i] = Savelines[i];
i_lines(top, bottom, num);
}
/* Delete `num' lines starting at `top' leaving the lines below `bottom'
* alone. This updates the internal image as well as the physical image.
*/
void
v_del_line(num, top, bottom)
int num,
top,
bottom;
{
register int i;
/* assert(num <= bottom-top+1) */
/* Blank and save the lines to be deleted from the top. */
for (i = 0; i < num; i++) {
struct screenline *sp = &Screen[top + i];
clrline(sp->s_line, sp->s_roof);
sp->s_roof = sp->s_line;
LEclear(sp);
Savelines[i] = *sp;
}
/* Copy everything up num number of lines. */
for (i = top; i + num <= bottom; i++)
Screen[i] = Screen[i + num];
/* Restore the now-blank lost lines */
for (i = 0; i < num; i++)
Screen[bottom - i] = Savelines[i];
d_lines(top, bottom, num);
}
#ifdef TERMCAP /* remainder of this file */
/* The cursor optimization happens here. You may decide that this
* is going too far with cursor optimization, or perhaps it should
* limit the amount of checking to when the output speed is slow.
* What ever turns you on ...
*/
struct cursaddr {
int cm_numchars;
void (*cm_proc) ();
};
private char *Cmstr;
private struct cursaddr *HorMin,
*VertMin,
*DirectMin;
private void
ForTab proto((int)),
RetTab proto((int)),
DownMotion proto((int)),
UpMotion proto((int)),
GoDirect proto((int, int)),
HomeGo proto((int, int)),
BottomUp proto((int, int));
private struct cursaddr WarpHor[] = {
{ 0, ForTab },
{ 0, RetTab }
};
private struct cursaddr WarpVert[] = {
{ 0, DownMotion },
{ 0, UpMotion }
};
private struct cursaddr WarpDirect[] = {
{ 0, GoDirect },
{ 0, HomeGo },
{ 0, BottomUp }
};
# define FORTAB 0 /* Forward using tabs */
# define RETFORTAB 1 /* Beginning of line and then tabs */
# define NUMHOR 2
# define DOWN 0 /* Move down */
# define UPMOVE 1 /* Move up */
# define NUMVERT 2
# define DIRECT 0 /* Using CM */
# define HOME 1 /* HOME */
# define LOWER 2 /* Lower Line */
# define NUMDIRECT 3
# define home() Placur(0, 0)
# define LowLine() { putpad(LL, 1); CapLine = ILI; CapCol = 0; }
# define PrintHo() { putpad(HO, 1); CapLine = CapCol = 0; }
private void
GoDirect(line, col)
register int line,
col;
{
putpad(Cmstr, 1);
CapLine = line;
CapCol = col;
}
private void
RetTab(col)
register int col;
{
scr_putchar('\r');
CapCol = 0;
ForTab(col);
}
private void
HomeGo(line, col)
int line,
col;
{
PrintHo();
DownMotion(line);
ForTab(col);
}
private void
BottomUp(line, col)
register int line,
col;
{
LowLine();
UpMotion(line);
ForTab(col);
}
/* Tries to move forward using tabs (if possible). It tabs to the
* closest tabstop which means it may go past 'destcol' and backspace
* to it.
* Note: changes to this routine must be matched by changes in ForNum.
*/
private void
ForTab(to)
int to;
{
if ((to > CapCol+1) && TABS && (phystab > 0)) {
register int tabgoal,
ntabs,
pts = phystab;
tabgoal = to + (pts / 2);
tabgoal -= (tabgoal % pts);
/* Don't tab to last place or else it is likely to screw up. */
if (tabgoal >= CO)
tabgoal -= pts;
ntabs = (tabgoal / pts) - (CapCol / pts);
/* If tabbing moves past goal, and goal is more cols back
* than we would have had to move forward from our original
* position, tab is counterproductive. Notice that if our
* original motion would have been backwards, tab loses too,
* so we need not write abs(to-CapCol).
*/
if (tabgoal > to && tabgoal-to >= to-CapCol)
ntabs = 0;
while (--ntabs >= 0) {
scr_putchar('\t');
CapCol = tabgoal; /* idempotent */
}
}
if (to > CapCol) {
register char *cp = &Screen[CapLine].s_line[CapCol];
# ifdef ID_CHAR
INSmode(NO); /* we're not just a motion */
# endif
while (to > CapCol) {
scr_putchar(*cp++);
CapCol++;
}
}
while (to < CapCol) {
putpad(BC, 1);
CapCol--;
}
}
private void
DownMotion(destline)
register int destline;
{
register int nlines = destline - CapLine;
while (--nlines >= 0) {
putpad(DO, 1);
CapLine = destline; /* idempotent */
}
}
private void
UpMotion(destline)
register int destline;
{
register int nchars = CapLine - destline;
while (--nchars >= 0) {
putpad(UP, 1);
CapLine = destline; /* idempotent */
}
}
private int ForNum proto((int from, int to));
void
Placur(line, col)
int line,
col;
{
# define CursMin(which,addrs,max) { \
register int best = 0, \
i; \
register struct cursaddr *cp; \
for (cp = &(addrs)[1], i = 1; i < (max); i++, cp++) \
if (cp->cm_numchars < (addrs)[best].cm_numchars) \
best = i; \
(which) = &(addrs)[best]; \
}
if (line == CapLine && col == CapCol)
return; /* We are already there. */
/* Number of characters to move horizontally for each case.
* 1: Try tabbing to the correct place.
* 2: Try going to the beginning of the line, and then tab.
*/
{
int dcol = col - CapCol; /* Number of columns to move */
int xtracost = 0; /* Misc addition to cost. */
# ifdef ID_CHAR
if (IN_INSmode && MI)
xtracost = IMEIlen;
/* If we're already in insert mode, it is likely that we will
* want to be in insert mode again, after the insert.
*/
# endif
if (dcol == 1 || dcol == 0) { /* Most common case. */
HorMin = &WarpHor[FORTAB];
HorMin->cm_numchars = dcol + xtracost;
} else {
/* if CapCol is unknown, FORTAB is impossible */
WarpHor[FORTAB].cm_numchars = CapCol == INFINITY? INFINITY :
xtracost + ForNum(CapCol, col);
WarpHor[RETFORTAB].cm_numchars = xtracost + 1 + ForNum(0, col);
/* Which is the shortest of the bunch */
CursMin(HorMin, WarpHor, NUMHOR);
}
}
/* Moving vertically is more simple. */
{
int dline = line - CapLine; /* Number of lines to move */
WarpVert[DOWN].cm_numchars = dline >= 0 ? dline : INFINITY;
WarpVert[UPMOVE].cm_numchars = dline < 0 ? ((-dline) * UPlen) : INFINITY;
}
/* Which of these is simpler */
CursMin(VertMin, WarpVert, NUMVERT);
/* Homing first and lowering first are considered
* direct motions.
* Homing first's total is the sum of the cost of homing
* and the sum of tabbing (if possible) to the right.
*/
if (Screen[line].s_effects != NOEFFECT && CM != NULL) {
/* We are going to a line with inversion or underlining;
* Don't try any clever stuff
*/
DirectMin = &WarpDirect[DIRECT];
DirectMin->cm_numchars = 0;
Cmstr = targ2(CM, col, line);
} else if (VertMin->cm_numchars + HorMin->cm_numchars <= 3) {
/* Since no direct method is ever shorter than 3 chars, don't try it. */
DirectMin = &WarpDirect[DIRECT]; /* A dummy ... */
DirectMin->cm_numchars = INFINITY;
} else {
WarpDirect[DIRECT].cm_numchars = CM != NULL ?
strlen(Cmstr = targ2(CM, col, line)) : INFINITY;
WarpDirect[HOME].cm_numchars = HOlen + line +
WarpHor[RETFORTAB].cm_numchars;
WarpDirect[LOWER].cm_numchars = LLlen + ((ILI - line) * UPlen) +
WarpHor[RETFORTAB].cm_numchars;
CursMin(DirectMin, WarpDirect, NUMDIRECT);
}
if (HorMin->cm_numchars + VertMin->cm_numchars < DirectMin->cm_numchars) {
if (line != CapLine)
(*(void (*)ptrproto((int)))VertMin->cm_proc)(line);
if (col != CapCol) {
# ifdef ID_CHAR
INSmode(NO); /* We may use real characters ... */
# endif
(*(void (*)ptrproto((int)))HorMin->cm_proc)(col);
}
} else {
# ifdef ID_CHAR
if (IN_INSmode && !MI)
INSmode(NO);
# endif
(*(void (*)ptrproto((int, int)))DirectMin->cm_proc)(line, col);
}
}
/* Figures out how many characters ForTab() would use to move forward
* using tabs (if possible).
* Note: changes to this routine must be matched by changes in ForTab.
* An exception is that any cost for leaving insert mode has been
* accounted for by our caller.
*/
private int
ForNum(from, to)
register int from;
int to;
{
register int tabgoal,
pts = phystab;
int ntabs = 0;
if ((to > from+1) && TABS && (pts > 0)) {
tabgoal = to + (pts / 2);
tabgoal -= (tabgoal % pts);
if (tabgoal >= CO)
tabgoal -= pts;
ntabs = (tabgoal / pts) - (from / pts);
/* If tabbing moves past goal, and goal is more cols back
* than we would have had to move forward from our original
* position, tab is counterproductive. Notice that if our
* original motion would have been backwards, tab loses too,
* so we need not write abs(to-from).
*/
if (tabgoal > to && tabgoal-to >= to-from)
ntabs = 0;
if (ntabs != 0)
from = tabgoal;
}
return ntabs + (from>to? from-to : to-from);
}
void
i_lines(top, bottom, num)
int top,
bottom,
num;
{
if (CS) {
putpad(targ2(CS, bottom, top), 1);
CapCol = CapLine = INFINITY; /* actually: unknown */
Placur(top, 0);
putmulti(SR, M_SR, num, bottom - top);
putpad(targ2(CS, ILI, 0), 1);
CapCol = CapLine = INFINITY; /* actually: unknown */
} else {
Placur(bottom - num + 1, 0);
putmulti(DL, M_DL, num, ILI - CapLine);
Placur(top, 0);
putmulti(AL, M_AL, num, ILI - CapLine);
}
}
void
d_lines(top, bottom, num)
int top,