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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
/*
* Copyright (C) 2012-2015 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 <bsd/sys/queue.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <Scintilla.h>
#include "sciteco.h"
#include "interface.h"
#include "ioview.h"
#include "undo.h"
#include "parser.h"
#include "expressions.h"
#include "qregisters.h"
#include "glob.h"
#include "error.h"
#include "ring.h"
namespace SciTECO {
namespace States {
StateEditFile editfile;
StateSaveFile savefile;
}
void
Buffer::UndoTokenClose::run(void)
{
ring.close(buffer);
/* NOTE: the buffer is NOT deleted on Token destruction */
delete buffer;
}
void
Buffer::save(const gchar *filename)
{
if (!filename && !Buffer::filename)
throw Error("File name expected");
IOView::save(filename ? : Buffer::filename);
/*
* Undirtify
* NOTE: info update is performed by set_filename()
*/
interface.undo_info_update(this);
undo.push_var(dirty) = false;
/*
* FIXME: necessary also if the filename was not specified but the file
* is (was) new, in order to canonicalize the filename.
* May be circumvented by cananonicalizing without requiring the file
* name to exist (like readlink -f)
* NOTE: undo_info_update is already called above
*/
undo.push_str(Buffer::filename);
set_filename(filename ? : Buffer::filename);
}
void
Ring::UndoTokenEdit::run(void)
{
/*
* assumes that buffer still has correct prev/next
* pointers
*/
if (buffer->next())
TAILQ_INSERT_BEFORE(buffer->next(), buffer, buffers);
else
TAILQ_INSERT_TAIL(&ring->head, buffer, buffers);
ring->current = buffer;
buffer->edit();
buffer = NULL;
}
tecoInt
Ring::get_id(Buffer *buffer)
{
tecoInt ret = 0;
Buffer *cur;
TAILQ_FOREACH(cur, &head, buffers) {
ret++;
if (cur == buffer)
break;
}
return ret;
}
Buffer *
Ring::find(const gchar *filename)
{
gchar *resolved = get_absolute_path(filename);
Buffer *cur;
TAILQ_FOREACH(cur, &head, buffers)
if (!g_strcmp0(cur->filename, resolved))
break;
g_free(resolved);
return cur;
}
Buffer *
Ring::find(tecoInt id)
{
Buffer *cur;
TAILQ_FOREACH(cur, &head, buffers)
if (!--id)
break;
return cur;
}
void
Ring::dirtify(void)
{
if (QRegisters::current || current->dirty)
return;
interface.undo_info_update(current);
undo.push_var(current->dirty);
current->dirty = true;
interface.info_update(current);
}
bool
Ring::is_any_dirty(void)
{
Buffer *cur;
TAILQ_FOREACH(cur, &head, buffers)
if (cur->dirty)
return true;
return false;
}
bool
Ring::edit(tecoInt id)
{
Buffer *buffer = find(id);
if (!buffer)
return false;
QRegisters::current = NULL;
current = buffer;
buffer->edit();
QRegisters::hook(QRegisters::HOOK_EDIT);
return true;
}
void
Ring::edit(const gchar *filename)
{
Buffer *buffer = find(filename);
QRegisters::current = NULL;
if (buffer) {
current = buffer;
buffer->edit();
QRegisters::hook(QRegisters::HOOK_EDIT);
} else {
buffer = new Buffer();
TAILQ_INSERT_TAIL(&head, buffer, buffers);
current = buffer;
undo_close();
if (filename && g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
buffer->edit();
buffer->load(filename);
interface.msg(InterfaceCurrent::MSG_INFO,
"Added file \"%s\" to ring", filename);
} else {
buffer->edit();
buffer->set_filename(filename);
if (filename)
interface.msg(InterfaceCurrent::MSG_INFO,
"Added new file \"%s\" to ring",
filename);
else
interface.msg(InterfaceCurrent::MSG_INFO,
"Added new unnamed file to ring.");
}
QRegisters::hook(QRegisters::HOOK_ADD);
}
}
void
Ring::close(Buffer *buffer)
{
TAILQ_REMOVE(&head, buffer, buffers);
if (buffer->filename)
interface.msg(InterfaceCurrent::MSG_INFO,
"Removed file \"%s\" from the ring",
buffer->filename);
else
interface.msg(InterfaceCurrent::MSG_INFO,
"Removed unnamed file from the ring.");
}
void
Ring::close(void)
{
Buffer *buffer = current;
QRegisters::hook(QRegisters::HOOK_CLOSE);
close(buffer);
current = buffer->next() ? : buffer->prev();
/* transfer responsibility to UndoToken object */
undo.push(new UndoTokenEdit(this, buffer));
if (current) {
current->edit();
QRegisters::hook(QRegisters::HOOK_EDIT);
} else {
edit((const gchar *)NULL);
}
}
void
Ring::set_scintilla_undo(bool state)
{
Buffer *cur;
TAILQ_FOREACH(cur, &head, buffers)
cur->set_scintilla_undo(state);
}
Ring::~Ring()
{
Buffer *buffer, *next;
TAILQ_FOREACH_SAFE(buffer, &head, buffers, next)
delete buffer;
}
/*
* Command states
*/
void
StateEditFile::do_edit(const gchar *filename)
{
current_doc_undo_edit();
ring.edit(filename);
}
void
StateEditFile::do_edit(tecoInt id)
{
current_doc_undo_edit();
if (!ring.edit(id))
throw Error("Invalid buffer id %" TECO_INTEGER_FORMAT, id);
}
/*$
* [n]EB[file]$ -- Open or edit file
* nEB$
*
* Opens or edits the file with name <file>.
* If <file> is not in the buffer ring it is opened,
* added to the ring and set as the currently edited
* buffer.
* If it already exists in the ring, it is merely
* made the current file.
* <file> may be omitted in which case the default
* unnamed buffer is created/edited.
* If an argument is specified as 0, EB will additionally
* display the buffer ring contents in the window's popup
* area.
* Naturally this only has any effect in interactive
* mode.
*
* <file> may also be a glob pattern, in which case
* all files matching the pattern are opened/edited.
* Globbing is performed exactly the same as the
* EN command does.
*
* File names of buffers in the ring are normalized
* by making them absolute.
* Any comparison on file names is performed using
* guessed or actual absolute file paths, so that
* one file may be referred to in many different ways
* (paths).
*
* <file> does not have to exist on disk.
* In this case, an empty buffer is created and its
* name is guessed from <file>.
* When the newly created buffer is first saved,
* the file is created on disk and the buffer's name
* will be updated to the absolute path of the file
* on disk.
*
* File names may also be tab-completed and string building
* characters are enabled by default.
*
* If <n> is greater than zero, the string argument
* must be empty.
* Instead <n> selects a buffer from the ring to edit.
* A value of 1 denotes the first buffer, 2 the second,
* ecetera.
*/
void
StateEditFile::initial(void)
{
tecoInt id = expressions.pop_num_calc(0, -1);
allowFilename = true;
if (id == 0) {
for (Buffer *cur = ring.first(); cur; cur = cur->next())
interface.popup_add(InterfaceCurrent::POPUP_FILE,
cur->filename ? : "(Unnamed)",
cur == ring.current);
interface.popup_show();
} else if (id > 0) {
allowFilename = false;
do_edit(id);
}
}
State *
StateEditFile::done(const gchar *str)
{
BEGIN_EXEC(&States::start);
if (!allowFilename) {
if (*str)
throw Error("If a buffer is selected by id, the <EB> "
"string argument must be empty");
return &States::start;
}
if (is_glob_pattern(str)) {
Globber globber(str);
gchar *filename;
while ((filename = globber.next()))
do_edit(filename);
} else {
do_edit(*str ? str : NULL);
}
return &States::start;
}
/*$
* EW$ -- Save current buffer or Q-Register
* EWfile$
*
* Saves the current buffer to disk.
* If the buffer was dirty, it will be clean afterwards.
* If the string argument <file> is not empty,
* the buffer is saved with the specified file name
* and is renamed in the ring.
*
* The EW command also works if the current document
* is a Q-Register, i.e. a Q-Register is edited.
* In this case, the string contents of the current
* Q-Register are saved to <file>.
* Q-Registers have no notion of associated file names,
* so <file> must be always specified.
*
* In interactive mode, EW is executed immediately and
* may be rubbed out.
* In order to support that, \*(ST creates so called
* save point files.
* It does not merely overwrite existing files when saving
* but moves them to save point files instead.
* Save point files are called \(lq.teco-\fIn\fP-\fIfilename\fP~\(rq,
* where <filename> is the name of the saved file and <n> is
* a number that is increased with every save operation.
* Save point files are always created in the same directory
* as the original file to ensure that no copying of the file
* on disk is necessary but only a rename of the file.
* When rubbing out the EW command, \*(ST restores the latest
* save point file by moving (renaming) it back to its
* original path \(em also not requiring any on-disk copying.
* \*(ST is impossible to crash, but just in case it still
* does it may leave behind these save point files which
* must be manually deleted by the user.
* Otherwise save point files are deleted on command line
* termination.
*
* File names may also be tab-completed and string building
* characters are enabled by default.
*/
State *
StateSaveFile::done(const gchar *str)
{
BEGIN_EXEC(&States::start);
if (QRegisters::current)
QRegisters::current->save(str);
else
ring.current->save(*str ? str : NULL);
return &States::start;
}
void
current_doc_undo_edit(void)
{
if (!QRegisters::current)
ring.undo_edit();
else
undo.push_var(QRegisters::current)->undo_edit();
}
} /* namespace SciTECO */
|