summaryrefslogtreecommitdiffstats
path: root/src/shift.c
blob: bced2567ac81d7b691691a07abe692bd8235e2ff (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include "shift.h"
#include "sc.h"
#include "vmtbl.h"   // for growtbl
#include "cmds.h"

// Shift a range of 'ENTS'
// TODO rewrite shift_range (without using copyent), so it doesn't depend on
// shift_cells, shift_cells_up, shift_cells_down, shift_cells_left,
// shift_cells_right. And delete those
/*
void shift_range(int direction, int arg, int tlrow, int tlcol, int brrow, int brcol) {

    currow = tlrow;
    curcol = tlcol;

    info("%d %d %d %d", direction, arg, brrow - tlrow + 1, brcol - tlcol + 1);

    if (direction == 'j')      while(arg--) shift_cells_down (brrow - tlrow + 1, brcol - tlcol + 1);
    else if (direction == 'k') while(arg--) shift_cells_up   (brrow - tlrow + 1, brcol - tlcol + 1);
    else if (direction == 'l') while(arg--) shift_cells_right(brrow - tlrow + 1, brcol - tlcol + 1);
    else if (direction == 'h') while(arg--) shift_cells_left (brrow - tlrow + 1, brcol - tlcol + 1);

    return;
}
*/

void shift_range(int delta_rows, int delta_cols, int tlrow, int tlcol, int brrow, int brcol) {
    currow = tlrow;
    curcol = tlcol;

    if (delta_rows > 0)      shift_cells_down (brrow - tlrow + 1, brcol - tlcol + 1);
    else if (delta_rows < 0) shift_cells_up   (brrow - tlrow + 1, brcol - tlcol + 1);

    if (delta_cols > 0)      shift_cells_right(brrow - tlrow + 1, brcol - tlcol + 1);
    else if (delta_cols < 0) shift_cells_left (brrow - tlrow + 1, brcol - tlcol + 1);

    return;
}

// Shift cells down
void shift_cells_down(int deltarows, int deltacols) {
    int r, c;
    struct ent **pp;
    register struct ent * p;
    register struct ent *n;
    int lim = maxrow - currow + 1;

    // Move cell content
    if (currow > maxrow) maxrow = currow;
    maxrow += deltarows;

    lim = maxrow - lim;
    if ((maxrow >= maxrows) && !growtbl(GROWROW, maxrow, 0))
        return;

    for (r = maxrow; r > lim; r--) {
        for (c = curcol; c < curcol + deltacols; c++) {
            p = *ATBL(tbl, r - deltarows, c);
            if (p) {
                n = lookat(r, c);
                copyent(n, p, 1, 0, 0, 0, r - deltarows, c, 0);
                n->row += deltarows;
                p = (struct ent *)0;

                pp = ATBL(tbl, r - deltarows, c);
                clearent(*pp);
            }
        }
    }
    return;
}

// Shift cells left
void shift_cells_left(int deltarows, int deltacols) {
    int r, j, c;
    register struct ent *p;
    register struct ent *n;
    struct ent **pp;
    for (j = 0; j < deltacols; j++)
        for (r = currow; r < currow + deltarows; r++)
            for (c = curcol; c < maxcol; c++) {

                // Free 'ent' memory
                pp = ATBL(tbl, r, c);
                clearent(*pp);

                p = *ATBL(tbl, r, c + 1);
                if (p && ( (p->flags & is_valid) || (p->expr && (p->flags & is_strexpr)) || p->label )  ) {
                    n = lookat(r, c);
                    (void) copyent(n, p, 1, 0, 0, 0, r, c, 0); // copio p a n
                    n->col--;
                    pp = ATBL(tbl, r, c + 1);
                } else { // When shifting the cells from the last column
                    pp = ATBL(tbl, r, c);
                }
                clearent(*pp);
            }
    return;
}

// Shift cells right
void shift_cells_right(int deltarows, int deltacols) {
    int r, j, c;
    register struct ent *p;
    register struct ent *n;
    struct ent **pp;

    for (j = 0; j < deltacols; j++)
    for (r = currow; r < currow + deltarows; r++)
        for (c = maxcol; c >= curcol; c--) {
            if ((p = *ATBL(tbl, r, c))) {
                n = lookat(r, c + 1);
                (void) copyent(n, p, 1, 0, 0, 0, r, c, 0);
                n->col++;
                pp = ATBL(tbl, r, c);
                clearent(*pp);
            }
        }
    return;
}

// Shift cells up
void shift_cells_up(int deltarows, int deltacols) {
    register struct ent ** pp;
    register struct ent * n;
    register struct ent * p;
    int r, c, i;

    //if (currow + deltarows - 1 > maxrow) return;

    // Delete cell content
    for (i = 0; i < deltarows; i++)
        for (r = currow; r < maxrow; r++)
            for (c = curcol; c < curcol + deltacols; c++) {

                // Free 'ent' memory
                pp = ATBL(tbl, r, c);
                clearent(*pp);

                p = *ATBL(tbl, r+1, c);
                if (p && ( (p->flags & is_valid) || (p->expr && (p->flags & is_strexpr)) || p->label )  ) {
                    // Copy below cell
                    n = lookat(r, c);
                    (void) copyent(n, p, 1, 0, 0, 0, r, c, 0);
                    n->row--;
                    pp = ATBL(tbl, r+1, c);
                } else {
                    pp = ATBL(tbl, r, c);
                }
                clearent(*pp);
            }
    return;
}