aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/ring.h
blob: d2595002938edfe5474dea1535ad545a42c15a45 (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
/*
 * Copyright (C) 2012-2014 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/>.
 */

#ifndef __RING_H
#define __RING_H

#include <bsd/sys/queue.h>

#include <glib.h>

#include <Scintilla.h>

#include "sciteco.h"
#include "interface.h"
#include "undo.h"
#include "qregisters.h"
#include "parser.h"
#include "ioview.h"

namespace SciTECO {

/*
 * Classes
 */

class Buffer : private IOView {
	TAILQ_ENTRY(Buffer) buffers;

	class UndoTokenClose : public UndoToken {
		Buffer *buffer;

	public:
		UndoTokenClose(Buffer *_buffer)
			      : UndoToken(), buffer(_buffer) {}

		void run(void);
	};

	inline void
	undo_close(void)
	{
		undo.push(new UndoTokenClose(this));
	}

public:
	gchar *filename;
	bool dirty;

	Buffer() : filename(NULL), dirty(false)
	{
		initialize();
		/* only have to do this once: */
		set_representations();
	}

	~Buffer()
	{
		g_free(filename);
	}

	inline Buffer *&
	next(void)
	{
		return TAILQ_NEXT(this, buffers);
	}
	inline Buffer *&
	prev(void)
	{
		TAILQ_HEAD(Head, Buffer);

		return TAILQ_PREV(this, Head, 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.show_view(this);
		interface.info_update(this);
	}
	inline void
	undo_edit(void)
	{
		interface.undo_info_update(this);
		interface.undo_show_view(this);
	}

	inline void
	load(const gchar *filename)
	{
		IOView::load(filename);

#if 0		/* NOTE: currently buffer cannot be dirty */
		interface.undo_info_update(this);
		undo.push_var(dirty) = false;
#endif

		set_filename(filename);
	}
	void save(const gchar *filename = NULL);

	/*
	 * Ring manages the buffer list and has privileged
	 * access.
	 */
	friend class Ring;
};

/* object declared in main.cpp */
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);
	};

	TAILQ_HEAD(Head, Buffer) head;

public:
	Buffer *current;

	Ring() : current(NULL)
	{
		TAILQ_INIT(&head);
	}
	~Ring();

	inline Buffer *
	first(void)
	{
		return TAILQ_FIRST(&head);
	}
	inline Buffer *
	last(void)
	{
		return TAILQ_LAST(&head, Head);
	}

	tecoInt get_id(Buffer *buffer);
	inline tecoInt
	get_id(void)
	{
		return get_id(current);
	}

	Buffer *find(const gchar *filename);
	Buffer *find(tecoInt id);

	void dirtify(void);
	bool is_any_dirty(void);

	bool edit(tecoInt id);
	void edit(const gchar *filename);
	inline void
	undo_edit(void)
	{
		undo.push_var(QRegisters::current);
		undo.push_var(current)->undo_edit();
	}

	void close(Buffer *buffer);
	void close(void);
	inline void
	undo_close(void)
	{
		current->undo_close();
	}

	void set_scintilla_undo(bool state);
} ring;

/*
 * Command states
 */

class StateEditFile : public StateExpectFile {
private:
	bool allowFilename;

	void do_edit(const gchar *filename);
	void do_edit(tecoInt id);

	void initial(void);
	State *done(const gchar *str);
};

class StateSaveFile : public StateExpectFile {
private:
	State *done(const gchar *str);
};

namespace States {
	extern StateEditFile	editfile;
	extern StateSaveFile	savefile;
}

/*
 * Helper functions applying to any current
 * document (whether a buffer or QRegister).
 * There's currently no better place to put them.
 */
void current_doc_undo_edit(void);

static inline bool
current_doc_must_undo(void)
{
	/*
	 * If there's no currently edited Q-Register
	 * we must be editing the current buffer
	 */
	return !QRegisters::current ||
	       QRegisters::current->must_undo;
}

} /* namespace SciTECO */

#endif