-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05undo.c
102 lines (82 loc) · 2.6 KB
/
05undo.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
#include "common.h"
ViWin*% ViWin*::initialize(ViWin*% self, int y, int x, int width, int height, Vi* vi) version 5
{
auto result = inherit(self, y, x, width, height, vi);
result.undo = new list<list<wstring>*%>.initialize();
result.undoScroll = new list<int>.initialize();
result.undoCursorX = new list<int>.initialize();
result.undoCursorY = new list<int>.initialize();
result.undoIndex = 0;
return result;
}
void ViWin*::pushUndo(ViWin* self) version 5
{
self.undo.delete(self.undoIndex, -1);
self.undoScroll.delete(self.undoIndex, -1);
self.undoCursorX.delete(self.undoIndex, -1);
self.undoCursorY.delete(self.undoIndex, -1);
auto undo = clone self.texts;
self.undo.push_back(undo);
self.undoCursorX.push_back(self.cursorX);
self.undoScroll.push_back(self.scroll);
self.undoCursorY.push_back(self.cursorY);
self.undoIndex++;
}
void ViWin*::redo(ViWin* self)
{
if(self.undoIndex < self.undo.length()-1)
{
self.undoIndex++;
auto undo = clone self.undo.item(self.undoIndex, null);
auto cursor_x = self.undoCursorX.item(self.undoIndex, -1);
auto scroll = self.undoScroll.item(self.undoIndex, -1);
auto cursor_y = self.undoCursorY.item(self.undoIndex, -1);
if(undo != null && cursor_x != -1 && cursor_y != -1 && scroll != -1)
{
self.texts = undo;
self.cursorX = cursor_x;
self.cursorY = cursor_y;
self.scroll = scroll;
}
}
}
void ViWin*::undo(ViWin* self)
{
if(self.undoIndex == self.undo.length())
{
self.pushUndo();
self.undoIndex--;
}
if(self.undoIndex > 0) {
self.undoIndex--;
auto undo = clone self.undo.item(self.undoIndex, null);
auto cursor_x = self.undoCursorX.item(self.undoIndex, -1);
auto cursor_y = self.undoCursorY.item(self.undoIndex, -1);
auto scroll = self.undoScroll.item(self.undoIndex, -1);
if(undo != null && cursor_x != -1 && cursor_y != -1 && scroll != -1)
{
self.texts = undo;
self.cursorX = cursor_x;
self.cursorY = cursor_y;
self.scroll = scroll;
}
}
}
Vi*% Vi*::initialize(Vi*% self) version 5
{
auto result = inherit(self);
result.events.replace('u', void lambda(Vi* self, int key)
{
self.activeWin.undo();
});
result.events.replace('r'-'a'+1, void lambda(Vi* self, int key)
{
self.activeWin.redo();
});
return result;
}
void Vi*::enterInsertMode(Vi* self) version 5
{
inherit(self);
self.activeWin.pushUndo();
}