-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcom_undo.c
106 lines (90 loc) · 2.9 KB
/
com_undo.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
#include <stdio.h>
#include <string.h> /* for strchr() */
#include <ctype.h> /* for toupper */
#include "rlgetc.h"
#include "db.h"
#include "token.h"
#include "xwin.h"
#define UNUSED(x) (void)(x)
//
// This is piglet's undo/redo facility.
//
// All cells are kept in a symbol table of linked DB_TAB
// structures (see db.h) Inside the structure is a pointer to
// the current cell definition DEFLIST chain, and a pointer to
// an UNDO and a REDO stack. Every time we go through the
// command evaluation parse loop, we do a checksum on the current
// definition chain and the definition chain at the top of the
// UNDO stack (db_checkpoint()). If they differ, there has been some
// change made, so we make a copy of the current definition list and push it on
// the UNDO stack.
//
// When the user requests an UNDO, we pop the deflist undo stack,
// push it on the REDO stack, free the definition at currep, pop the
// undo stack again and set the currep edit pointer to point to it.
// We then make a copy of the definition list at currep and push it
// on the UNDO stack.
//
// The following table shows the state of the stacks throughout
// and edit/undo/redo cycle:
//
// UNDO CURREP REDO
// stack stack
// READIN: A A -
// CHANGE1: AB B -
// CHANGE2: ABC C -
// UNDO: AB B C
// UNDO: A A CB
// REDO: AB B C
// CHANGE: ABD D -
//
int com_undo(LEXER *lp, char *arg)
{
UNUSED(arg);
DB_DEFLIST *a;
/* check that we are editing a rep */
if (currep == NULL) {
printf("UNDO: must do \"EDIT <name>\" before UNDO\n");
token_flush_EOL(lp);
return(1);
}
if (currep->undo == NULL || stack_depth(&(currep->undo)) <= 1) {
printf("UNDO: nothing left to UNDO\n");
token_flush_EOL(lp);
return(1);
}
a = (DB_DEFLIST *) stack_pop(&(currep->undo));
stack_push(&(currep->redo), (char *) a);
db_free(currep->dbhead);
a = (DB_DEFLIST *) stack_pop(&(currep->undo));
currep->dbhead = a;
a = db_copy_deflist(currep->dbhead);
stack_push(&(currep->undo), (char *) a);
printf("(undo: %d, redo: %d) ", stack_depth(&(currep->undo))-1, stack_depth(&(currep->redo)));
need_redraw++;
return(0);
}
int com_redo(LEXER *lp, char *arg)
{
UNUSED(arg);
DB_DEFLIST *a;
/* check that we are editing a rep */
if (currep == NULL) {
printf("REDO: must do \"EDIT <name>\" before REDO\n");
token_flush_EOL(lp);
return(1);
}
if (currep->redo == NULL) {
printf("REDO: nothing left to REDO\n");
token_flush_EOL(lp);
return(1);
}
db_free(currep->dbhead);
a = (DB_DEFLIST *) stack_pop(&(currep->redo));
currep->dbhead = a;
a = db_copy_deflist(currep->dbhead);
stack_push(&(currep->undo), (char *) a);
printf("(undo: %d, redo: %d) ", stack_depth(&(currep->undo))-1, stack_depth(&(currep->redo)));
need_redraw++;
return(0);
}