summaryrefslogtreecommitdiffstats
path: root/src/yank.c
blob: ff46223ca6dbc08e9d1e9a5abc0fb393dafbb996 (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
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
/*
 * Yanklist doesn't keep references to 'ent' elements, it creates new nodes.
 * the yanklist is constantly cleaned out.
 * Ex.: When removing 'ent' elements with `dc`, the original ones are removed
 * and new elements are created in the yanklist.
 * Important: each 'ent' element should keep a row and col.
 */
#include "sc.h"
#include "stdlib.h"
#include "marks.h"
#include "cmds.h"
#include "dep_graph.h"
#include "xmalloc.h" // for scxfree

#ifdef UNDO
#include "undo.h"
#endif

extern struct ent * forw_row(int arg);
extern struct ent * back_row(int arg);
extern struct ent * forw_col(int arg);
extern struct ent * back_col(int arg);

int yank_arg;                 // number of rows and columns yanked. Used for commands like `4yr`
char type_of_yank;            // yank type. c=col, r=row, a=range, e=cell, '\0'=no yanking
static struct ent * yanklist;

void init_yanklist() {
    type_of_yank = '\0';
    yanklist = NULL;
}

struct ent * get_yanklist() {
    return yanklist;
}

// Remove yank 'ent' elements and free corresponding memory
void free_yanklist () {
    if (yanklist == NULL) return;
    int c;
    struct ent * r = yanklist;
    struct ent * e;
    while (r != NULL) {
        e = r->next;

        if (r->format) scxfree(r->format);
        if (r->label) scxfree(r->label);
        if (r->expr) efree(r->expr);
        if (r->ucolor) free(r->ucolor);

        free(r);
        r = e;
    }

    for (c = 0; c < COLFORMATS; c++) {
        if (colformat[c] != NULL)
            scxfree(colformat[c]);
        colformat[c] = NULL;
    }

    yanklist = NULL;
    return;
}

// Count and return number of entries in the yanklist
int count_yank_ents() {
    int i = 0;

    struct ent * r = yanklist;
    while (r != NULL) {
        i++;
        r = r->next;
    }
    return i;
}

// Add 'ent' element to the yanklist
void add_ent_to_yanklist(struct ent * item) {
    // Create and initialize the 'ent'
    struct ent * i_ent = (struct ent *) malloc (sizeof(struct ent));
    (i_ent)->label = (char *)0;
    (i_ent)->row = 0;
    (i_ent)->col = 0;
    (i_ent)->flags = may_sync;
    (i_ent)->expr = (struct enode *)0;
    (i_ent)->v = (double) 0.0;
    (i_ent)->format = (char *)0;
    (i_ent)->cellerror = CELLOK;
    (i_ent)->next = NULL;
    (i_ent)->ucolor = NULL;
    (i_ent)->pad = 0;

    // Copy 'item' content to 'i_ent'
    (void) copyent(i_ent, item, 0, 0, 0, 0, 0, 0, 0);

    (i_ent)->row = item->row;
    (i_ent)->col = item->col;

    // If yanklist is empty, insert at the beginning
    if (yanklist == NULL) {
        yanklist = i_ent;
        return;
    }

    // If yanklist is NOT empty, insert at the end
    struct ent * r = yanklist;
    struct ent * ant;
    while (r != NULL) {
        ant = r;
        r = r->next;
    }
    ant->next = i_ent;
    return;
}

/*
 * yank a range of ents
 * ARG: number of rows or columns yanked. Used in commands like `4yr`
 * TYPE: yank type. c=col, r=row, a=range, e=cell, '\0'=no yanking, 's' sort
 * This two args are used for pasting.
 */
void yank_area(int tlrow, int tlcol, int brrow, int brcol, char type, int arg) {
    int r,c;
    free_yanklist();
    type_of_yank = type;
    yank_arg = arg;

    for (r = tlrow; r <= brrow; r++)
        for (c = tlcol; c <= brcol; c++) {
            struct ent * elm = *ATBL(tbl, r, c);

            // Important: each 'ent' element keeps the corresponding row and col
            if (elm == NULL) elm = lookat(r, c);

            add_ent_to_yanklist(elm);
        }
    return;
}

/*
 * paste yanked ents:
 * this function is used for paste ents that were yanked with yr yc dr dc..
 * it is also used for sorting.
 * if above == 1, paste is done above current row or to the right of current col.
 * ents that were yanked using yy or yanked ents of a range, are always pasted in currow and curcol positions.
 * diffr: diff between current rows and the yanked 'ent'
 * diffc: diff between current cols and the yanked 'ent'
 * When sorting, row and col values can vary from yank to paste time, so diffr
 * should be zero.
 * When implementing column sorting, diffc should be zero as well!
 * type indicates if pasting format only, value only or the whole content
 * yank type: c=col, r=row, a=range, e=cell, '\0'=no yanking
 * returns -1 if locked cells are found. 0 otherwise.
 */
int paste_yanked_ents(int above, int type_paste) {
    if (! count_yank_ents()) return 0;

    struct ent * yl = yanklist;
    struct ent * yll = yl;
    int diffr = 0, diffc = 0 , ignorelock = 0;

    #ifdef UNDO
    create_undo_action();
    #endif

    if (type_of_yank == 's') {                               // paste a range that was yanked in the sort function
        diffr = 0;
        diffc = curcol - yl->col;
        ignorelock = 1;

    } else if (type_of_yank == 'a' || type_of_yank == 'e') { // paste cell or range
        diffr = currow - yl->row;
        diffc = curcol - yl->col;

    } else if (type_of_yank == 'r') {                        // paste row
        int c = yank_arg;
        #ifdef UNDO
        copy_to_undostruct(currow + ! above, 0, currow + ! above - 1 + yank_arg, maxcol, 'd');
        #endif
        while (c--) above ? insert_row(0) : insert_row(1);
        if (! above) currow = forw_row(1)->row;              // paste below
        diffr = currow - yl->row;
        diffc = yl->col;
        fix_marks(yank_arg, 0, currow, maxrow, 0, maxcol);
        #ifdef UNDO
        save_undo_range_shift(yank_arg, 0, currow, 0, currow - 1 + yank_arg, maxcol);
        #endif

    } else if (type_of_yank == 'c') {                        // paste col
        int c = yank_arg;
        #ifdef UNDO
        copy_to_undostruct(0, curcol + above, maxrow, curcol + above - 1 + yank_arg, 'd');
        #endif
        while (c--) above ? insert_col(1) : insert_col(0);   // insert cols to the right if above or to the left
        diffr = yl->row;
        diffc = curcol - yl->col;
        fix_marks(0, yank_arg, 0, maxrow, curcol, maxcol);
        #ifdef UNDO
        save_undo_range_shift(0, yank_arg, 0, curcol, maxrow, curcol - 1 + yank_arg);
        #endif
    }

    // first check if there are any locked cells
    // if so, just return
    if (type_of_yank == 'a' || type_of_yank == 'e') {
        while (yll != NULL) {
            int r = yll->row + diffr;
            int c = yll->col + diffc;
            checkbounds(&r, &c);
            if (any_locked_cells(yll->row + diffr, yll->col + diffc, yll->row + diffr, yll->col + diffc))
                return -1;
            yll = yll->next;
        }
    }

    // otherwise continue
    // por cada ent en yanklist
    while (yl != NULL) {
        #ifdef UNDO
        copy_to_undostruct(yl->row + diffr, yl->col + diffc, yl->row + diffr, yl->col + diffc, 'd');
        #endif

        // here we delete current content of "destino" ent
        if (type_paste == 'a' || type_paste == 's')
            erase_area(yl->row + diffr, yl->col + diffc, yl->row + diffr, yl->col + diffc, ignorelock, 0);

        /*struct ent **pp = ATBL(tbl, yl->row + diffr, yl->col + diffc);
        if (*pp && ( ! ((*pp)->flags & is_locked) )) {
            mark_ent_as_deleted(*pp, TRUE);
            *pp = NULL;
        }*/

        struct ent * destino