-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuffer.c
315 lines (264 loc) · 7.18 KB
/
buffer.c
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
/* buffer.c, femto, Hugh Barney, Public Domain, 2017 */
#include <assert.h>
#include <string.h>
#include "header.h"
void buffer_init(buffer_t *bp)
{
bp->b_mark = NOMARK;
bp->b_point = 0;
bp->b_paren = NOPAREN;
bp->b_cpoint = 0;
bp->b_page = 0;
bp->b_epage = 0;
bp->b_size = 0;
bp->b_psize = 0;
bp->b_flags = 0;
bp->b_cnt = 0;
bp->b_buf = NULL;
bp->b_ebuf = NULL;
bp->b_gap = NULL;
bp->b_egap = NULL;
bp->b_next = NULL;
bp->b_bname[0] = '\0';
bp->b_fname[0] = '\0';
bp->b_utail = NULL;
bp->b_ucnt = -1;
}
void zero_buffer(buffer_t *bp)
{
/* reset the gap, make it the whole buffer */
bp->b_gap = bp->b_buf;
bp->b_egap = bp->b_ebuf;
bp->b_point = 0; /* goto start of buffer */
bp->b_mark = NOMARK;
}
/* get the size of the document in the buffer */
point_t document_size(buffer_t *bp)
{
return (bp->b_ebuf - bp->b_buf) - (bp->b_egap - bp->b_gap);
}
int buffer_is_empty(buffer_t *bp)
{
if (bp->b_gap == bp->b_buf && bp->b_egap == bp->b_ebuf)
return 1;
return 0;
}
/*
* 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 *bname, int cflag)
{
buffer_t *bp = NULL;
buffer_t *sb = NULL;
debug("find-buffer(%s, %d)\n", bname, cflag);
bp = bheadp;
while (bp != NULL) {
if (strcmp(bname, bp->b_bname) == 0) {
return (bp);
}
bp = bp->b_next;
}
if (cflag != FALSE) {
if ((bp = (buffer_t *) malloc (sizeof (buffer_t))) == NULL)
return (0);
buffer_init(bp);
assert(bp != NULL);
/* find the place in the list to insert this buffer */
if (bheadp == NULL) {
bheadp = bp;
} else if (strcmp(bheadp->b_bname, bname) > 0) {
/* insert at the begining */
bp->b_next = bheadp;
bheadp = bp;
} else {
for (sb = bheadp; sb->b_next != NULL; sb = sb->b_next)
if (strcmp (sb->b_next->b_bname, bname) > 0)
break;
/* and insert it */
bp->b_next = sb->b_next;
sb->b_next = bp;
}
safe_strncpy(bp->b_bname, bname, NBUFN);
if (bp->b_bname[0] == '*')
add_mode(bp, B_SPECIAL); /* special buffers start with * in the name */
else if (global_undo_mode)
add_mode(bp, B_UNDO);
/* a newly created buffer needs to have a gap otherwise it is not ready for insertion */
if (!growgap(bp, MIN_GAP_EXPAND))
msg(f_alloc);
}
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;
char bname[NBUFN];
bp = bheadp;
for (bp = bheadp; bp != NULL; bp = bp->b_next)
if (strcmp(fname, bp->b_fname) == 0)
return (bp);
make_buffer_name(bname, fname);
make_buffer_name_uniq(bname);
bp = find_buffer(bname, TRUE);
return (bp);
}
void add_mode(buffer_t *bp, buffer_flags_t mode)
{
/* we dont allow undo mode for special buffers */
if ( mode == B_UNDO && (bp->b_flags & B_SPECIAL))
return;
bp->b_flags |= mode;
}
void delete_mode(buffer_t *bp, buffer_flags_t mode)
{
bp->b_flags &= ~mode;
}
/*
* Unlink from the list of buffers
* Free the memory associated with the buffer
* assumes that buffer has been saved if modified
*/
int delete_buffer(buffer_t *bp)
{
buffer_t *sb = NULL;
/* we must have switched to a different buffer first */
assert(bp != curbp);
/* if buffer is the head buffer */
if (bp == bheadp) {
bheadp = bp->b_next;
} else {
/* find place where the bp buffer is next */
for (sb = bheadp; sb->b_next != bp && sb->b_next != NULL; sb = sb->b_next)
;
assert(sb->b_next == bp || sb->b_next == NULL);
sb->b_next = bp->b_next;
}
/* now we can delete */
free_undos(bp->b_utail);
free(bp->b_buf);
free(bp);
return TRUE;
}
void next_buffer()
{
assert(curbp != NULL);
assert(bheadp != NULL);
disassociate_b(curwp);
curbp = (curbp->b_next != NULL ? curbp->b_next : bheadp);
associate_b2w(curbp,curwp);
}
char* get_buffer_name(buffer_t *bp)
{
assert(bp->b_bname != NULL);
return bp->b_bname;
}
char* get_buffer_filename(buffer_t *bp)
{
assert(bp->b_fname != NULL);
return bp->b_fname;
}
char* get_buffer_modeline_name(buffer_t *bp)
{
if (bp->b_fname[0] != '\0')
return bp->b_fname;
return bp->b_bname;
}
int count_buffers()
{
buffer_t* bp;
int i;
for (i=0, bp=bheadp; bp != NULL; bp = bp->b_next)
i++;
return i;
}
int modified_buffers()
{
buffer_t* bp;
for (bp=bheadp; bp != NULL; bp = bp->b_next)
if (!(bp->b_flags & B_SPECIAL) && bp->b_flags & B_MODIFIED)
return TRUE;
return FALSE;
}
int delete_buffer_byname(char *bname)
{
buffer_t *bp = find_buffer(bname, FALSE);
int bcount = count_buffers();
if (bp == NULL) return FALSE;
/* if last buffer, create a scratch buffer */
if (bcount == 1) {
bp = find_buffer(str_scratch, TRUE);
}
/* switch out of buffer if we are the current buffer */
if (bp == curbp)
next_buffer();
assert(bp != curbp);
delete_buffer(bp);
return TRUE;
}
int select_buffer(char *bname)
{
buffer_t *bp = find_buffer(bname, TRUE);
assert(bp != NULL);
assert(curbp != NULL);
disassociate_b(curwp);
curbp = bp;
associate_b2w(curbp,curwp);
return TRUE;
}
/* a version of save buffer specifically for calling by lisp */
int save_buffer_byname(char *bname)
{
buffer_t *bp = find_buffer(bname, FALSE);
if (bp == NULL) return FALSE;
if (bp->b_fname[0] == '\0') return FALSE;
save_buffer(bp, bp->b_fname);
return TRUE;
}
char *get_current_bufname()
{
assert(curbp != NULL);
return get_buffer_name(curbp);
}
void list_buffers()
{
buffer_t *bp;
buffer_t *list_bp;
char mod_ch, over_ch;
char blank[] = " ";
static char report_line[NAME_MAX + 40];
char *bn;
char *fn;
list_bp = find_buffer(str_buffers, TRUE);
disassociate_b(curwp); /* we are leaving the old buffer for a new one */
curbp = list_bp;
associate_b2w(curbp, curwp);
clear_buffer(); /* throw away previous content */
/* 12 1234567 12345678901234567 */
insert_string("CO Size Buffer File\n");
insert_string("-- ------- ------ ----\n");
bp = bheadp;
while (bp != NULL) {
if (bp != list_bp) {
mod_ch = ((bp->b_flags & B_MODIFIED) ? '*' : ' ');
over_ch = ((bp->b_flags & B_OVERWRITE) ? 'O' : ' ');
bn = (bp->b_bname[0] != '\0' ? bp->b_bname : blank);
fn = (bp->b_fname[0] != '\0' ? bp->b_fname : blank);
sprintf(report_line, "%c%c %7d %-16s %s\n", mod_ch, over_ch, bp->b_size, bn, fn);
insert_string(report_line);
}
bp = bp->b_next;
}
}
/*
* Local Variables:
* c-file-style: "k&r"
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/