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
|
#ifndef __QBUFFERS_H
#define __QBUFFERS_H
#include <string.h>
#include <bsd/sys/queue.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <Scintilla.h>
#include "sciteco.h"
#include "interface.h"
#include "undo.h"
#include "rbtree.h"
#include "parser.h"
/*
* Auxiliary functions
*/
static inline bool
is_glob_pattern(const gchar *str)
{
return strchr(str, '*') || strchr(str, '?');
}
gchar *get_absolute_path(const gchar *path);
/*
* Classes
*/
class QRegister : public RBTree::RBEntry {
public:
gchar *name;
gint64 integer;
typedef void document;
document *string;
gint dot;
QRegister(const gchar *_name)
: name(g_strdup(_name)), integer(0), string(NULL), dot(0) {}
~QRegister()
{
if (string)
interface.ssm(SCI_RELEASEDOCUMENT, 0, (sptr_t)string);
g_free(name);
}
int
operator <(RBEntry &entry)
{
return g_strcmp0(name, ((QRegister &)entry).name);
}
inline document *
get_document(void)
{
if (!string)
string = (document *)interface.ssm(SCI_CREATEDOCUMENT);
return string;
}
void set_string(const gchar *str);
void undo_set_string(void);
gchar *get_string(void);
inline void
edit(void)
{
interface.ssm(SCI_SETDOCPOINTER, 0, (sptr_t)get_document());
interface.ssm(SCI_GOTOPOS, dot);
interface.info_update(this);
}
inline void
undo_edit(void)
{
interface.undo_info_update(this);
undo.push_msg(SCI_GOTOPOS, dot);
undo.push_msg(SCI_SETDOCPOINTER, 0, (sptr_t)get_document());
}
bool load(const gchar *filename);
};
extern class QRegisterTable : public RBTree {
inline void
initialize_register(const gchar *name)
{
QRegister *reg = new QRegister(name);
insert(reg);
/* make sure document is initialized */
reg->get_document();
}
public:
QRegister *current;
QRegisterTable() : RBTree(), current(NULL) {}
void initialize(void);
inline QRegister *
operator [](const gchar *name)
{
QRegister reg(name);
return (QRegister *)find(®);
}
inline QRegister *
operator [](gchar chr)
{
return operator []((gchar []){chr, '\0'});
}
void edit(QRegister *reg);
inline QRegister *
edit(const gchar *name)
{
QRegister *reg = operator [](name);
if (!reg)
return NULL;
edit(reg);
return reg;
}
inline void
undo_edit(void)
{
current->dot = interface.ssm(SCI_GETCURRENTPOS);
undo.push_var<QRegister*>(current);
current->undo_edit();
}
} qregisters;
class Buffer {
class UndoTokenClose : public UndoToken {
Buffer *buffer;
public:
UndoTokenClose(Buffer *_buffer)
: UndoToken(), buffer(_buffer) {}
void
run(void)
{
buffer->close();
/* NOTE: the buffer is NOT deleted on Token destruction */
delete buffer;
}
};
public:
LIST_ENTRY(Buffer) buffers;
gchar *filename;
gint dot;
gint savepoint_id;
/* set by Interfaces using Scintilla notifications */
bool dirty;
private:
typedef void document;
document *doc;
public:
Buffer() : filename(NULL), dot(0), savepoint_id(0), dirty(false)
{
doc = (document *)interface.ssm(SCI_CREATEDOCUMENT);
}
~Buffer()
{
interface.ssm(SCI_RELEASEDOCUMENT, 0, (sptr_t)doc);
g_free(filename);
}
inline Buffer *&
next(void)
{
return LIST_NEXT(this, buffers);
}
inline void
set_filename(const gchar *filename)
{
gchar *resolved = get_absolute_path(filename);
g_free(Buffer::filename);
Buffer::filename = resolved;
interface.info_update(this);
}
inline void
edit(void)
{
interface.ssm(SCI_SETDOCPOINTER, 0, (sptr_t)doc);
interface.ssm(SCI_GOTOPOS, dot);
interface.info_update(this);
}
inline void
undo_edit(void)
{
interface.undo_info_update(this);
undo.push_msg(SCI_GOTOPOS, dot);
undo.push_msg(SCI_SETDOCPOINTER, 0, (sptr_t)doc);
}
bool load(const gchar *filename);
void close(void);
inline void
undo_close(void)
{
undo.push(new UndoTokenClose(this));
}
};
extern class Ring {
/*
* Emitted after a buffer close
* The pointer is the only remaining reference to the buffer!
*/
class UndoTokenEdit : public UndoToken {
Ring *ring;
Buffer *buffer;
public:
UndoTokenEdit(Ring *_ring, Buffer *_buffer)
: UndoToken(), ring(_ring), buffer(_buffer) {}
~UndoTokenEdit()
{
if (buffer)
delete buffer;
}
void run(void);
};
class UndoTokenRemoveFile : public UndoToken {
gchar *filename;
public:
UndoTokenRemoveFile(const gchar *_filename)
: filename(g_strdup(_filename)) {}
~UndoTokenRemoveFile()
{
g_free(filename);
}
void
run(void)
{
g_unlink(filename);
}
};
LIST_HEAD(Head, Buffer) head;
public:
Buffer *current;
Ring() : current(NULL)
{
LIST_INIT(&head);
}
~Ring();
inline Buffer *
first(void)
{
return LIST_FIRST(&head);
}
Buffer *find(const gchar *filename);
bool is_any_dirty(void);
bool edit(const gchar *filename);
inline void
undo_edit(void)
{
current->dot = interface.ssm(SCI_GETCURRENTPOS);
undo.push_var<Buffer*>(current);
current->undo_edit();
}
bool save(const gchar *filename);
void close(void);
inline void
undo_close(void)
{
current->undo_close();
}
} ring;
/*
* Command states
*/
class StateEditFile : public StateExpectString {
private:
void do_edit(const gchar *filename);
void initial(void) throw (Error);
State *done(const gchar *str) throw (Error);
};
class StateSaveFile : public StateExpectString {
private:
State *done(const gchar *str) throw (Error);
};
class StateEQCommand : public StateExpectQReg {
private:
State *got_register(QRegister *reg) throw (Error);
};
class StateLoadQReg : public StateExpectString {
private:
State *done(const gchar *str) throw (Error);
};
class StateCtlUCommand : public StateExpectQReg {
private:
State *got_register(QRegister *reg) throw (Error);
};
class StateSetQRegString : public StateExpectString {
private:
State *done(const gchar *str) throw (Error);
};
class StateGetQRegInteger : public StateExpectQReg {
private:
State *got_register(QRegister *reg) throw (Error);
};
class StateSetQRegInteger : public StateExpectQReg {
private:
State *got_register(QRegister *reg) throw (Error);
};
class StateIncreaseQReg : public StateExpectQReg {
private:
State *got_register(QRegister *reg) throw (Error);
};
class StateMacro : public StateExpectQReg {
private:
State *got_register(QRegister *reg) throw (Error);
};
class StateCopyToQReg : public StateExpectQReg {
private:
State *got_register(QRegister *reg) throw (Error);
};
namespace States {
extern StateEditFile editfile;
extern StateSaveFile savefile;
extern StateEQCommand eqcommand;
extern StateLoadQReg loadqreg;
extern StateCtlUCommand ctlucommand;
extern StateSetQRegString setqregstring;
extern StateGetQRegInteger getqreginteger;
extern StateSetQRegInteger setqreginteger;
extern StateIncreaseQReg increaseqreg;
extern StateMacro macro;
extern StateCopyToQReg copytoqreg;
}
#endif
|