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
|
/*
* Copyright (C) 2012-2024 Robin Haberkorn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <glib.h>
#include <glib/gstdio.h>
#include "sciteco.h"
#include "cmdline.h"
#include "undo.h"
//#define DEBUG
TECO_DEFINE_UNDO_SCALAR(gunichar);
TECO_DEFINE_UNDO_SCALAR(gint);
TECO_DEFINE_UNDO_SCALAR(guint);
TECO_DEFINE_UNDO_SCALAR(gsize);
TECO_DEFINE_UNDO_SCALAR(teco_int_t);
TECO_DEFINE_UNDO_SCALAR(gboolean);
TECO_DEFINE_UNDO_SCALAR(gconstpointer);
/**
* An undo token.
*
* Undo tokens are generated to revert any
* changes to the editor state, i.e. they
* define an action to take upon rubout.
*
* Undo tokens are organized into an undo stack.
*/
typedef struct teco_undo_token_t {
struct teco_undo_token_t *next;
teco_undo_action_t action_cb;
guint8 user_data[];
} teco_undo_token_t;
/**
* Stack of teco_undo_token_t lists.
*
* Each stack element represents
* a command line character (the undo tokens
* generated by that character), so it's OK
* to use a data structure that may need
* reallocation but is space efficient.
* This data structure allows us to omit the
* command line program counter from teco_undo_token_t
* but wastes a few bytes for input characters
* that produce no undo tokens (e.g. NOPs like space).
*/
static GPtrArray *teco_undo_heads;
gboolean teco_undo_enabled = FALSE;
static void __attribute__((constructor))
teco_undo_init(void)
{
teco_undo_heads = g_ptr_array_new();
}
/**
* Allocate and push undo token.
*
* This does nothing if undo is disabled and should
* not be used when ownership of some data is to be
* passed to the undo token.
*/
gpointer
teco_undo_push_size(teco_undo_action_t action_cb, gsize size)
{
if (!teco_undo_enabled)
return NULL;
teco_undo_token_t *token = g_malloc(sizeof(teco_undo_token_t) + size);
token->action_cb = action_cb;
#ifdef DEBUG
g_printf("UNDO PUSH %p\n", token);
#endif
/*
* There can very well be 0 undo tokens
* per input character (e.g. NOPs like space).
*/
while (teco_undo_heads->len <= teco_cmdline.pc)
g_ptr_array_add(teco_undo_heads, NULL);
g_assert(teco_undo_heads->len == teco_cmdline.pc+1);
token->next = g_ptr_array_index(teco_undo_heads,
teco_undo_heads->len-1);
g_ptr_array_index(teco_undo_heads, teco_undo_heads->len-1) = token;
return token->user_data;
}
void
teco_undo_pop(gsize pc)
{
while ((gint)teco_undo_heads->len > pc) {
teco_undo_token_t *top =
g_ptr_array_remove_index(teco_undo_heads,
teco_undo_heads->len-1);
while (top) {
teco_undo_token_t *next = top->next;
#ifdef DEBUG
g_printf("UNDO POP %p\n", top);
fflush(stdout);
#endif
top->action_cb(top->user_data, TRUE);
g_free(top);
top = next;
}
}
}
void
teco_undo_clear(void)
{
while (teco_undo_heads->len) {
teco_undo_token_t *top =
g_ptr_array_remove_index(teco_undo_heads,
teco_undo_heads->len-1);
while (top) {
teco_undo_token_t *next = top->next;
top->action_cb(top->user_data, FALSE);
g_free(top);
top = next;
}
}
}
/*
* NOTE: This destructor should always be run, even with NDEBUG,
* as there are undo tokens that release more than memory (e.g. files).
*/
static void __attribute__((destructor))
teco_undo_cleanup(void)
{
teco_undo_clear();
g_ptr_array_free(teco_undo_heads, TRUE);
}
|