forked from hughbarney/femto
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuffer.c
More file actions
279 lines (237 loc) · 6.56 KB
/
Copy pathbuffer.c
File metadata and controls
279 lines (237 loc) · 6.56 KB
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
/* buffer.c, femto, Hugh Barney, Public Domain, 2017
*
* Buffer management.
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "femto.h"
#include "window.h"
#include "undo.h"
#include "buffer.h"
#include "gap.h"
#include "command.h"
/* Globals */
buffer_t *curbp; /* current buffer */
Object *mode_c = &(Object) { .string = "C" };
Object *mode_python = &(Object) { .string = "Python" };
Object *mode_lisp = &(Object) { .string = "Lisp" };
Object *mode_dired = &(Object) { .string = "Dired" };
Object *mode_buffers = &(Object) { .string = "mode-buffers" };
Object *mode_buffers_name = &(Object) { .string = "Buffer Menu" };
Object *mode_git = &(Object) { .string = "Git" };
Object *mode_oxo = &(Object) { .string = "OXO" };
void femto_buffer_register(Interpreter *interp)
{
flisp_register_constant(interp, mode_c, mode_c);
flisp_register_constant(interp, mode_lisp, mode_lisp);
flisp_register_constant(interp, mode_python, mode_python);
flisp_register_constant(interp, mode_dired, mode_dired);
mode_buffers_name->type = type_string;
flisp_register_constant(interp, mode_buffers, mode_buffers_name);
flisp_register_constant(interp, mode_git, mode_git);
flisp_register_constant(interp, mode_oxo, mode_oxo);
}
void buffer_init(buffer_t *bp)
{
bp->name = NULL;
bp->b_next = NULL;
bp->fname = NULL;
bp->b_mark = NOMARK;
bp->b_point = 0;
bp->mode = nil;
bp->modified = false;
bp->overwrite = false;
bp->readonly = false;
bp->undo = true;
bp->special = false;
bp->b_paren = NOPAREN;
bp->b_cpoint = 0;
bp->b_page = 0;
bp->b_epage = 0;
bp->b_reframe = 0;
bp->b_size = 0;
bp->b_psize = 0;
bp->b_buf = NULL;
bp->b_ebuf = NULL;
bp->b_gap = NULL;
bp->b_egap = NULL;
bp->b_cnt = 0;
bp->b_utail = NULL;
bp->b_ucnt = -1;
}
#define BUFFER_B_PREV(PREV, BUF) for (PREV = BUF; PREV->b_next != BUF; PREV = PREV->b_next)
/** new_buffer() - allocate, initialize and register a buffer.
*
* @param name .. name of the buffer.
* @returns pointer to buffer or NULL if buffer name is empty or allocation fails.
*
* The buffer is put in front of the list.
*
* If there is no *scratch* buffer already we create one.
*
* It is an error to create a buffer with a name that already exists.
*
*/
buffer_t *new_buffer(char *name)
{
buffer_t *bp, *sb;
if (name == NULL || name[0] == '\n')
return NULL;
if ((bp = (buffer_t *) malloc (sizeof (buffer_t))) == NULL)
return NULL;
buffer_init(bp);
/* a newly created buffer needs to have a gap otherwise it is not ready for insertion */
if (!growgap(bp, MIN_GAP_EXPAND))
goto new_buffer_error;
if (curbp == NULL) {
/* assure there is a scratch buffer */
debug("new_buffer(): curbp is NULL, creating %s buffer\n", str_scratch);
if (!set_buffer_name(bp, str_scratch))
goto new_buffer_error;
curbp = bp;
if (strcmp(bp->name, str_scratch) == 0) {
bp->b_next = bp;
bp->special = true;
return bp;
}
if ((bp = new_buffer(name)) == NULL)
return NULL;
}
debug("new_buffer(): creating %s buffer\n", name);
if (!set_buffer_name(bp, name))
goto new_buffer_error;
BUFFER_B_PREV(sb, curbp);
sb->b_next = bp;
bp->b_next = curbp;
curbp = bp;
return bp;
new_buffer_error:
free(bp);
debug("new_buffer(): failed to allocate memory\n");
return NULL;
}
buffer_t *search_buffer(char *name)
{
buffer_t *bp = curbp;
do
if (strcmp(name, bp->name) == 0)
return bp;
while ((bp = bp->b_next) != curbp);
return NULL;
}
/*
* Find a buffer, by buffer name. Return a pointer to the buffer_t
* structure associated with it. If the buffer is not found and the
* "cflag" is TRUE, create it.
*/
buffer_t *find_buffer(char *name, bool cflag)
{
buffer_t *bp = NULL;
debug("find-buffer(%s, %d)\n", name, cflag);
bp = search_buffer(name);
if (bp == NULL && cflag)
bp = new_buffer(name);
return bp;
}
/*
* Given a file name, either find the buffer it uses, or create a new
* empty buffer to put it in.
*/
buffer_t *find_buffer_by_fname(char *fname)
{
buffer_t *bp;
for (bp = curbp; bp != curbp; bp = bp->b_next) {
if (bp->fname == NULL)
continue;
if (strcmp(fname, bp->fname) == 0)
return bp;
}
return NULL;
}
/** set_buffer_name() name or rename buffer
*
* @param buffer .. pointer to buffer struct.
* @param name .. pointer to string
*
* @returns TRUE on success, else FALSE.
*
* If the buffer already has a name it is deallocated.
*
*/
bool set_buffer_name(buffer_t *buffer, char *name)
{
if (buffer->name != NULL)
free(buffer->name);
return (buffer->name = strdup(name)) != NULL;
}
/** delete_buffer() - deallocate and unregister a buffer.
*
* @param bp .. buffer
*
* @returns TRUE on success, FALSE if we try to delete the *scratch*
* buffer
*
* Assure that the head points to a live buffer and neither the
* *scratch* nore the current buffer is deleted.
*
* Unlink from the list of buffers and free the memory associated with
* the buffer.
*
* Assumes that buffer has been saved if modified
*/
bool delete_buffer(buffer_t *bp)
{
buffer_t *sb;
window_t *wp;
if (bp == curbp || strcmp(bp->name, str_scratch) == 0)
return false;
BUFFER_B_PREV(sb, bp);
if (bp == sb) {
/* lone buffer */
curbp = NULL; /* from scratch */
curbp = new_buffer(str_scratch);
if (curbp == NULL)
return false;
}
else if (bp == curbp)
/* advance curbp before deletion */
curbp = curbp->b_next;
else
sb->b_next = bp->b_next;
/* disassociate all windows */
while (bp->b_cnt) {
wp = find_window(bp->name);
if (wp == NULL)
return false; /* Note: this would be a programming error and should be at least logged */
disassociate_b(wp);
associate_b2w(curbp, wp);
}
/* now we can delete */
free_undos(bp->b_utail);
free(bp->b_buf);
free(bp->name);
free(bp->fname);
free(bp);
return true;
}
/* Move buffer to the front of the buffer list */
void pull_buffer(buffer_t *bp)
{
buffer_t *sb;
if (bp == curbp)
return;
BUFFER_B_PREV(sb, bp);
sb->b_next = bp->b_next;
BUFFER_B_PREV(sb, curbp);
sb->b_next = bp;
bp->b_next = curbp;
curbp = bp;
}
/*
* Local Variables:
* c-file-style: "k&r"
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/