summaryrefslogblamecommitdiffstats
path: root/private/sdktools/vi/screen.c
blob: 35d01bfe7482fd8b508335b95573cc1997d1d124 (plain) (tree)
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




































































































































































































































































































































































































































































































































































































































































































































                                                                               
/*
 *
 * Routines to manipulate the screen representations.
 */

#include "stevie.h"

/*
 * This gets set if we ignored an update request while input was pending.
 * We check this when the input is drained to see if the screen should be
 * updated.
 */
bool_t  need_redraw = FALSE;

/*
 * The following variable is set (in filetonext) to the number of physical
 * lines taken by the line the cursor is on. We use this to avoid extra
 * calls to plines(). The optimized routines lfiletonext() and lnexttoscreen()
 * make sure that the size of the cursor line hasn't changed. If so, lines
 * below the cursor will move up or down and we need to call the routines
 * filetonext() and nexttoscreen() to examine the entire screen.
 */
static  int     Cline_size;     /* size (in rows) of the cursor line */
static  int     Cline_row;      /* starting row of the cursor line */

static  char    *mkline();      /* calculate line string for "number" mode */

/*
 * filetonext()
 *
 * Based on the current value of Topchar, transfer a screenfull of
 * stuff from Filemem to Nextscreen, and update Botchar.
 */

static void
filetonext()
{
        register int    row, col;
        register char   *screenp = Nextscreen;
        LNPTR    memp;
        LNPTR    save;                   /* save pos. in case line won't fit */
        register char   *endscreen;
        register char   *nextrow;
        char    extra[16];
        int     nextra = 0;
        register int    c;
        int     n;
        bool_t  done;           /* if TRUE, we hit the end of the file */
        bool_t  didline;        /* if TRUE, we finished the last line */
        int     srow;           /* starting row of the current line */
        int     lno;            /* number of the line we're doing */
        int     coff;           /* column offset */

        coff = P(P_NU) ? 8 : 0;

        save = memp = *Topchar;

        if (P(P_NU))
                lno = cntllines(Filemem, Topchar);

        /*
         * The number of rows shown is Rows-1.
         * The last line is the status/command line.
         */
        endscreen = &screenp[(Rows-1)*Columns];

        done = didline = FALSE;
        srow = row = col = 0;
        /*
         * We go one past the end of the screen so we can find out if the
         * last line fit on the screen or not.
         */
        while ( screenp <= endscreen && !done) {


                if (P(P_NU) && col == 0 && memp.index == 0) {
                        strcpy(extra, mkline(lno++));
                        nextra = 8;
                }

                /* Get the next character to put on the screen. */

                /* The 'extra' array contains the extra stuff that is */
                /* inserted to represent special characters (tabs, and */
                /* other non-printable stuff.  The order in the 'extra' */
                /* array is reversed. */

                if ( nextra > 0 )
                        c = extra[--nextra];
                else {
                        c = (unsigned)(0xff & gchar(&memp));
                        if (inc(&memp) == -1)
                                done = 1;
                        /* when getting a character from the file, we */
                        /* may have to turn it into something else on */
                        /* the way to putting it into 'Nextscreen'. */
                        if ( c == TAB && !P(P_LS) ) {
                                strcpy(extra,"        ");
                                /* tab amount depends on current column */
                                nextra = ((P(P_TS)-1) - (col - coff)%P(P_TS));
                                c = ' ';
                        }
                        else if ( c == NUL && P(P_LS) ) {
                                extra[0] = NUL;
                                nextra = 1;
                                c = '$';
                        } else if ((n = chars[c].ch_size) > 1 ) {
                                char *p;
                                nextra = 0;
                                p = chars[c].ch_str;
                                /* copy 'ch-str'ing into 'extra' in reverse */
                                while ( n > 1 )
                                        extra[nextra++] = p[--n];
                                c = p[0];
                        }
                }

                if (screenp == endscreen) {
                        /*
                         * We're one past the end of the screen. If the
                         * current character is null, then we really did
                         * finish, so set didline = TRUE. In either case,
                         * break out because we're done.
                         */
                        dec(&memp);
                        if (memp.index != 0 && c == NUL) {
                                didline = TRUE;
                                inc(&memp);
                        }
                        break;
                }

                if ( c == NUL ) {
                        srow = ++row;
                        /*
                         * Save this position in case the next line won't
                         * fit on the screen completely.
                         */
                        save = memp;
                        /* get pointer to start of next row */
                        nextrow = &Nextscreen[row*Columns];
                        /* blank out the rest of this row */
                        while ( screenp != nextrow )
                                *screenp++ = ' ';
                        col = 0;
                        continue;
                }
                if ( col >= Columns ) {
                        row++;
                        col = 0;
                }
                /* store the character in Nextscreen */
                *screenp++ = (char)c;
                col++;
        }
        /*
         * If we didn't hit the end of the file, and we didn't finish
         * the last line we were working on, then the line didn't fit.
         */
        if (!done && !didline) {
                /*
                 * Clear the rest of the screen and mark the unused lines.
                 */
                screenp = &Nextscreen[srow * Columns];
                while (screenp < endscreen)
                        *screenp++ = ' ';
                for (; srow < (Rows-1) ;srow++)
                        Nextscreen[srow * Columns] = '@';
                *Botchar = save;
                return;
        }
        /* make sure the rest of the screen is blank */
        while ( screenp < endscreen )
                *screenp++ = ' ';
        /* put '~'s on rows that aren't part of the file. */
        if ( col != 0 )
                row++;
        while ( row < Rows ) {
                Nextscreen[row*Columns] = '~';
                row++;
        }
        if (done)       /* we hit the end of the file */
                *Botchar = *Fileend;
        else
                *Botchar = memp;        /* FIX - prev? */
}

/*
 * nexttoscreen
 *
 * Transfer the contents of Nextscreen to the screen, using Realscreen
 * to avoid unnecessary output.
 */
static void
nexttoscreen()
{
        register char   *np = Nextscreen;
        register char   *rp = Realscreen;
        register char   *endscreen;
        register int    row = 0, col = 0;
        int     gorow = -1, gocol = -1;

        if (anyinput()) {
                need_redraw = TRUE;
                return;
        }

        endscreen = &np[(Rows-1)*Columns];

        InvisibleCursor();

        for ( ; np < endscreen ; np++,rp++ ) {
                /* If desired screen (contents of Nextscreen) does not */
                /* match what's really there, put it there. */
                if ( *np != *rp ) {
                        /* if we are positioned at the right place, */
                        /* we don't have to use windgoto(). */
                        if (gocol != col || gorow != row) {
                                /*
                                 * If we're just off by one, don't send
                                 * an entire esc. seq. (this happens a lot!)
                                 */
                                if (gorow == row && gocol+1 == col) {
                                        outchar(*(np-1));
                                        gocol++;
                                } else
                                        windgoto(gorow=row,gocol=col);
                        }
                        outchar(*rp = *np);
                        gocol++;
                }
                if ( ++col >= Columns ) {
                        col = 0;
                        row++;
                }
        }
        VisibleCursor();
}

/*
 * lfiletonext() - like filetonext() but only for cursor line
 *
 * Returns true if the size of the cursor line (in rows) hasn't changed.
 * This determines whether or not we need to call filetonext() to examine
 * the entire screen for changes.
 */
static bool_t
lfiletonext()
{
        register int    row, col;
        register char   *screenp;
        LNPTR    memp;
        register char   *nextrow;
        char    extra[16];
        int     nextra = 0;
        register int    c;
        int     n;
        bool_t  eof;
        int     lno;            /* number of the line we're doing */
        int     coff;           /* column offset */

        coff = P(P_NU) ? 8 : 0;

        /*
         * This should be done more efficiently.
         */
        if (P(P_NU))
                lno = cntllines(Filemem, Curschar);

        screenp = Nextscreen + (Cline_row * Columns);

        memp = *Curschar;
        memp.index = 0;

        eof = FALSE;
        col = 0;
        row = Cline_row;

        while (!eof) {

                if (P(P_NU) && col == 0 && memp.index == 0) {
                        strcpy(extra, mkline(lno));
                        nextra = 8;
                }

                /* Get the next character to put on the screen. */

                /* The 'extra' array contains the extra stuff that is */
                /* inserted to represent special characters (tabs, and */
                /* other non-printable stuff.  The order in the 'extra' */
                /* array is reversed. */

                if ( nextra > 0 )
                        c = extra[--nextra];
                else {
                        c = (unsigned)(0xff & gchar(&memp));
                        if (inc(&memp) == -1)
                                eof = TRUE;
                        /* when getting a character from the file, we */
                        /* may have to turn it into something else on */
                        /* the way to putting it into 'Nextscreen'. */
                        if ( c == TAB && !P(P_LS) ) {
                                strcpy(extra,"        ");
                                /* tab amount depends on current column */
                                nextra = ((P(P_TS)-1) - (col - coff)%P(P_TS));
                                c = ' ';
                        } else if ( c == NUL && P(P_LS) ) {
                                extra[0] = NUL;
                                nextra = 1;
                                c = '$';
                        } else if (c != NUL && (n=chars[c].ch_size) > 1 )
                        {
                                char *p;
                                nextra = 0;
                                p = chars[c].ch_str;
                                /* copy 'ch-str'ing into 'extra' in reverse */
                                while ( n > 1 )
                                        extra[nextra++] = p[--n];
                                c = p[0];
                        }
                }

                if ( c == NUL ) {
                        row++;
                        /* get pointer to start of next row */
                        nextrow = &Nextscreen[row*Columns];
                        /* blank out the rest of this row */
                        while ( screenp != nextrow )
                                *screenp++ = ' ';
                        col = 0;
                        break;
                }

                if ( col >= Columns ) {
                        row++;
                        col = 0;
                }
                /* store the character in Nextscreen */
                *screenp++ = (char)c;
                col++;
        }
        return ((row - Cline_row) == Cline_size);
}

/*
 * lnexttoscreen
 *
 * Like nexttoscreen() but only for the cursor line.
 */
static void
lnexttoscreen()
{
        register char   *np = Nextscreen + (Cline_row * Columns);
        register char   *rp = Realscreen + (Cline_row * Columns);
        register char   *endline;
        register int    row, col;
        int     gorow = -1, gocol = -1;

        if (anyinput()) {
                need_redraw = TRUE;
                return;
        }

        endline = np + (Cline_size * Columns);

        row = Cline_row;
        col = 0;

        InvisibleCursor();

        for ( ; np < endline ; np++,rp++ ) {
                /* If desired screen (contents of Nextscreen) does not */
                /* match what's really there, put it there. */
                if ( *np != *rp ) {
                        /* if we are positioned at the right place, */
                        /* we don't have to use windgoto(). */
                        if (gocol != col || gorow != row) {
                                /*
                                 * If we're just off by one, don't send
                                 * an entire esc. seq. (this happens a lot!)
                                 */
                                if (gorow == row && gocol+1 == col) {
                                        outchar(*(np-1));
                                        gocol++;
                                } else
                                        windgoto(gorow=row,gocol=col);
                        }
                        outchar(*rp = *np);
                        gocol++;
                }
                if ( ++col >= Columns ) {
                        col = 0;
                        row++;
                }
        }
        VisibleCursor();
}

static char *
mkline(n)
register int    n;
{
        static  char    lbuf[9];
        register int    i = 2;

        strcpy(lbuf, "        ");

        lbuf[i++] = (char)((n % 10) + '0');
        n /= 10;
        if (n != 0) {
                lbuf[i++] = (char)((n % 10) + '0');
                n /= 10;
        }
        if (n != 0) {
                lbuf[i++] = (char)((n % 10) + '0');
                n /= 10;
        }
        if (n != 0) {
                lbuf[i++] = (char)((n % 10) + '0');
                n /= 10;
        }
        if (n != 0) {
                lbuf[i++] = (char)((n % 10) + '0');
                n /= 10;
        }
        return lbuf;
}

/*
 * updateline() - update the line the cursor is on
 *
 * Updateline() is called after changes that only affect the line that
 * the cursor is on. This improves performance tremendously for normal
 * insert mode operation. The only thing we have to watch for is when
 * the cursor line grows or shrinks around a row boundary. This means
 * we have to repaint other parts of the screen appropriately. If
 * lfiletonext() returns FALSE, the size of the cursor line (in rows)
 * has changed and we have to call updatescreen() to do a complete job.
 */
void
updateline()
{
        if (!lfiletonext())
                updatescreen(); /* bag it, do the whole screen */
        else
                lnexttoscreen();
}

void
updatescreen()
{
        extern  bool_t  interactive;

        if (interactive) {
                filetonext();
                nexttoscreen();
        }
}

/*
 * prt_line() - print the given line
 */
void
prt_line(s)
char    *s;
{
        register int    si = 0;
        register int    c;
        register int    col = 0;

        char    extra[16];
        int     nextra = 0;
        int     n;

        for (;;) {

                if ( nextra > 0 )
                        c = extra[--nextra];
                else {
                        c = s[si++];
                        if ( c == TAB && !P(P_LS) ) {
                                strcpy(extra, "        ");
                                /* tab amount depends on current column */
                                nextra = (P(P_TS) - 1) - col%P(P_TS);
                                c = ' ';
                        } else if ( c == NUL && P(P_LS) ) {
                                extra[0] = NUL;
                                nextra = 1;
                                c = '$';
                        } else if ( c != NUL && (n=chars[c].ch_size) > 1 ) {
                                char    *p;

                                nextra = 0;
                                p = chars[c].ch_str;
                                /* copy 'ch-str'ing into 'extra' in reverse */
                                while ( n > 1 )
                                        extra[nextra++] = p[--n];
                                c = p[0];
                        }
                }

                if ( c == NUL )
                        break;

                outchar(c);
                col++;
        }
}

void
screenclear()
{
        register char   *rp, *np;
        register char   *end;

        ClearDisplay();

        rp  = Realscreen;
        end = Realscreen + Rows * Columns;
        np  = Nextscreen;

        /* blank out the stored screens */
        while (rp != end)
                *rp++ = *np++ = ' ';
}

void
cursupdate()
{
        register LNPTR   *p;
        register int    icnt, c, nlines;
        register int    i;
        int     didinc;

        if (bufempty()) {               /* special case - file is empty */
                *Topchar  = *Filemem;
                *Curschar = *Filemem;
        } else if ( LINEOF(Curschar) < LINEOF(Topchar) ) {
                nlines = cntllines(Curschar,Topchar);
                /* if the cursor is above the top of */
                /* the screen, put it at the top of the screen.. */
                *Topchar = *Curschar;
                Topchar->index = 0;
                /* ... and, if we weren't very close to begin with, */
                /* we scroll so that the line is close to the middle. */
                if ( nlines > Rows/3 ) {
                        for (i=0, p = Topchar; i < Rows/3 ;i++, *Topchar = *p)
                                if ((p = prevline(p)) == NULL)
                                        break;
                } else
                        s_ins(0, nlines-1);
                updatescreen();
        }
        else if (LINEOF(Curschar) >= LINEOF(Botchar)) {
                nlines = cntllines(Botchar,Curschar);
                /* If the cursor is off the bottom of the screen, */
                /* put it at the top of the screen.. */
                /* ... and back up */
                if ( nlines > Rows/3 ) {
                        p = Curschar;
                        for (i=0; i < (2*Rows)/3 ;i++)
                                if ((p = prevline(p)) == NULL)
                                        break;
                        *Topchar = *p;
                } else {
                        scrollup(nlines+1);
                }
                updatescreen();
        }

        Cursrow = Curscol = Cursvcol = 0;
        for ( p=Topchar; p->linep != Curschar->linep ;p = nextline(p) )
                Cursrow += plines(p);

        Cline_row = Cursrow;
        Cline_size = plines(p);

        if (P(P_NU))
                Curscol = 8;

        for (i=0; i <= Curschar->index ;i++) {
                c = Curschar->linep->s[i];
                /* A tab gets expanded, depending on the current column */
                if ( c == TAB && !P(P_LS) )
                        icnt = P(P_TS) - (Cursvcol % P(P_TS));
                else
                        icnt = chars[(unsigned)(c & 0xff)].ch_size;
                Curscol += icnt;
                Cursvcol += icnt;
                if ( Curscol >= Columns ) {
                        Curscol -= Columns;
                        Cursrow++;
                        didinc = TRUE;
                }
                else
                        didinc = FALSE;
        }
        if (didinc)
                Cursrow--;

        if (c == TAB && State == NORMAL && !P(P_LS)) {
                Curscol--;
                Cursvcol--;
        } else {
                Curscol -= icnt;
                Cursvcol -= icnt;
        }
        if (Curscol < 0)
                Curscol += Columns;

        if (set_want_col) {
                Curswant = Cursvcol;
                set_want_col = FALSE;
        }
}

/*
 * The rest of the routines in this file perform screen manipulations.
 * The given operation is performed physically on the screen. The
 * corresponding change is also made to the internal screen image.
 * In this way, the editor anticipates the effect of editing changes
 * on the appearance of the screen. That way, when we call screenupdate
 * a complete redraw isn't usually necessary. Another advantage is that
 * we can keep adding code to anticipate screen changes, and in the
 * meantime, everything still works.
 */

/*
 * s_ins(row, nlines) - insert 'nlines' lines at 'row'
 */
void
s_ins(row, nlines)
int     row;
int     nlines;
{
        register char   *s, *d;         /* src & dest for block copy */
        register char   *e;             /* end point for copy */

        SaveCursor();

        // clip to screen

        if(row <= Rows-2-nlines) {
            Scroll(row,0,Rows-2-nlines,Columns-1,row+nlines,0);
            EraseNLinesAtRow(nlines,row);
        } else {
            // just erase to end of screen
            EraseNLinesAtRow(Rows-2-row+1,row);
        }

        windgoto(Rows-1, 0);    /* delete any garbage that may have */
        EraseLine();
        RestoreCursor();

        /*
         * Now do a block move to update the internal screen image
         */
        d = Realscreen + (Columns * (Rows - 1)) - 1;
        s = d - (Columns * nlines);
        e = Realscreen + (Columns * row);

        while (s >= e)
                *d-- = *s--;

        /*
         * Clear the inserted lines
         */
        s = Realscreen + (row * Columns);
        e = s + (nlines * Columns);
        while (s < e)
                *s++ = ' ';
}

/*
 * s_del(row, nlines) - delete 'nlines' lines at 'row'
 */
void
s_del(row, nlines)
int     row;
int     nlines;
{
        register char   *s, *d, *e;

        SaveCursor();
        windgoto(Rows-1,0);
        EraseLine();                        // erase status line
        windgoto(row,0);

        if(row + nlines >= Rows - 1) {      // more than a screenfull?
            EraseNLinesAtRow(Rows-row-1,row);
        } else {
            Scroll(row+nlines,0,Rows-2,Columns-1,row,0);
            EraseNLinesAtRow(nlines,Rows-nlines-1);
        }
        RestoreCursor();

        /*
         * do a block move to update the internal image
         */
        d = Realscreen + (row * Columns);
        s = d + (nlines * Columns);
        e = Realscreen + ((Rows - 1) * Columns);

        while (s < e)
                *d++ = *s++;

        while (d < e)           /* clear the lines at the bottom */
                *d++ = ' ';
}