summaryrefslogtreecommitdiffstats
path: root/src/undo.c
blob: cb96e8eb998cc2c3d3529297a2c35a3e8c760f57 (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
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
#ifdef UNDO
/*
----------------------------------------------------------------------------------------
UNDO and REDO features works with an 'undo' struct list.
Which contains:
      p_ant: pointer to 'undo' struct. If NULL, this node is the first change
             for the session.
      struct ent * added: 'ent' elements added by the change
      struct ent * removed: 'ent' elements removed by the change
      struct undo_range_shift * range_shift: range shifted by change
      row_hidded: integers list (int *) hidden rows on screen
      row_showed: integers list (int *) visible rows on screen
      col_hidded: integers list (int *) hidden columns on screen
      col_showed: integers list (int *) visible columns on screen
             NOTE: the first position of the lists contains (number of elements - 1) in the list
      p_sig: pointer to 'undo' struct, If NULL, this node is the last change in
             the session.

Follows one level UNDO/REDO scheme. A change (C1) is made, then an UNDO operation, and
another change (C2). From there later changes are removed.
Scheme:

+ C1 -> + -> UNDO -
^                 \
|_                |
  \---------------/
|
|
|
 \-> C2 --> + ...

undo_shift_range struct contains:
    int delta_rows: delta rows for the range shift
    int delta_cols: delta columns for the range shift
    int tlrow:      Upper left row defining the range of the shift
                    (As if a cell range shift is made)
    int tlcol:      Upper left column defining the range of the shift
                    (As if a cell range shift is made)
    int brrow:      Lower right row defining the range of the shift
                    (As if a cell range shift is made)
    int brcol:      Lower right column defining the range of the shift
                    (As if a cell range shift is made)

Implemented actions for UNDO/REDO:
1. Remove content from cell or range
2. Input content into a cell
3. Edit a cell
4. Change alginment of range or cell
5. Paste range or cell
6. Shift range or cell with sh, sj, sk, sl
7. Insert row or column
8. Delete row or column
9. Paste row or column
10. Hide/show rows and columns
11. Sort of a range
12. Change in the format of a range or cell
13. '-' and '+' commands in normal mode
14. Lock and unlock of cells
15. datefmt command

NOT implemented:
1. Change format of an entire column
2. Recover equations after redo of changes over ents that have equations on them.

----------------------------------------------------------------------------------------
*/

#include <stdlib.h>
#include "undo.h"
#include "macros.h"
#include "curses.h"
#include "conf.h"
#include "sc.h"
#include "cmds.h"
#include "color.h" // for set_ucolor
#include "marks.h"
#include "shift.h"

// undolist
static struct undo * undo_list = NULL;

// current position in the list
static int undo_list_pos = 0;

// Number of elements in the list
static int undo_list_len = 0;

// Temporal variable
static struct undo undo_item;

// Init 'unto_item'
void create_undo_action() {
    undo_item.added       = NULL;
    undo_item.removed     = NULL;
    undo_item.p_ant       = NULL;
    undo_item.p_sig       = NULL;
    undo_item.range_shift = NULL;

    undo_item.row_hidded  = NULL;
    undo_item.row_showed  = NULL;
    undo_item.col_hidded  = NULL;
    undo_item.col_showed  = NULL;
    return;
}

// Save undo_item copy with 'ent' elements modified, and the undo range shift
// struct into the undolist
void end_undo_action() {
    add_to_undolist(undo_item);

    // in case we need to dismissed this undo_item!
    if ((undo_item.added      == NULL &&
        undo_item.removed     == NULL && undo_item.range_shift == NULL &&
        undo_item.row_hidded  == NULL && undo_item.row_showed  == NULL &&
        undo_item.col_hidded  == NULL && undo_item.col_showed  == NULL) || loading) {
        if (undo_list->p_ant != NULL) undo_list = undo_list->p_ant;
        undo_list_pos--;
        clear_from_current_pos();
    }

    return;
}

// Add a undo node to the undolist,
// allocate memory for undo struct,
// fill variable with undo_item value and append it to the list
void add_to_undolist(struct undo u) {
        // If not at the end of the list, remove from the end
        if ( undo_list != NULL && undo_list_pos != len_undo_list() )
            clear_from_current_pos();

        struct undo * ul = (struct undo *) malloc (sizeof(struct undo));
        ul->p_sig = NULL;

        // Add 'ent' elements
        ul->added = u.added;
        ul->removed = u.removed;
        ul->range_shift = u.range_shift;
        ul->row_hidded = u.row_hidded;
        ul->col_hidded = u.col_hidded;
        ul->row_showed = u.row_showed;
        ul->col_showed = u.col_showed;

        if (undo_list == NULL) {
            ul->p_ant = NULL;
            undo_list = ul;
        } else {
            ul->p_ant = undo_list;

            // Voy hasta el final de la lista
            while (undo_list->p_sig != NULL) undo_list = undo_list->p_sig;

            undo_list->p_sig = ul;
            undo_list = undo_list->p_sig;
        }
        undo_list_pos++;
        undo_list_len++;
    return;
}

// Cascade free UNDO node memory
void free_undo_node(struct undo * ul) {

    struct ent * de;
    struct ent * en;
    struct undo * e;

    // Remove from current position
    while (ul != NULL) {
        en = ul->added;
        while (en != NULL) {
            de = en->next;
            clearent(en);
            free(en);
            en = de;
        }
        en = ul->removed;
        while (en != NULL) {
            de = en->next;
            clearent(en);
            free(en);
            en = de;
        }
        e = ul->p_sig;

        if (ul->range_shift != NULL) free(ul->range_shift); // Free undo_range_shift memory
        if (ul->row_hidded  != NULL) free(ul->row_hidded); // Free hidden row memory
        if (ul->col_hidded  != NULL) free(ul->col_hidded); // Free hidden col memory
        if (ul->row_showed  != NULL) free(ul->row_showed); // Free showed row memory
        if (ul->col_showed  != NULL) free(ul->col_showed); // Free showed col memory

        free(ul);
        undo_list_len--;
        ul = e;
    }
    return;
}

// Remove nodes below the current position from the undolist
void clear_from_current_pos() {
    if (undo_list == NULL) return;

    if (undo_list->p_ant == NULL) {
        free_undo_node(undo_list);
        undo_list = NULL;
    } else {
        struct undo * ul = undo_list->p_sig; // Previous
        free_undo_node(ul);
        undo_list->p_sig = NULL;
    }

    return;
}

// Remove undolist content
void clear_undo_list () {
    if (undo_list == NULL) return;

    // Go to the beginning of the list
    while (undo_list->p_ant != NULL ) {
        undo_list = undo_list->p_ant;
    }

    struct undo * ul = undo_list;

    free_undo_node(ul);

    undo_list = NULL;
    undo_list_pos = 0;

    return;
}

int len_undo_list() {
    return undo_list_len;
}

// Take a range of 'ent' elements and create new ones (as many as elements
// inside the specified range).
// Then copy the content of the original ones to the new ones and save them into
// the 'added' or 'removed' list of undo_item, according to the char type.
void copy_to_undostruct (int row_desde, int col_desde, int row_hasta, int col_hasta, char type) {
    int c, r;
    for (r = row_desde; r <= row_hasta; r++)
        for (c = col_desde; c <= col_hasta; c++) {
            struct ent * e = (struct ent *) malloc( (unsigned) sizeof(struct ent) );
            cleanent(e);
            copyent(e, lookat(r, c), 0, 0, 0, 0, r, c, 0);

            // Append 'ent' element at the beginning
            if (type == 'a') {
                e->next = undo_item.added;
                undo_item.added = e;
            } else {
                e->next = undo_item.removed;
                undo_item.removed = e;
            }

        }
    return;
}

// Takes a range, a rows an