forked from hughbarney/atto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.c
127 lines (107 loc) · 2.48 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
/* buffer.c, Atto Emacs, Hugh Barney, Public Domain, 2015 */
#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_cpoint = 0;
bp->b_page = 0;
bp->b_epage = 0;
bp->b_reframe = 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_fname[0] = '\0';
}
/* Find a buffer by filename or create if requested */
buffer_t* find_buffer (char *fname, int cflag)
{
buffer_t *bp = NULL;
buffer_t *sb = NULL;
bp = bheadp;
while (bp != NULL) {
if (strcmp (fname, bp->b_fname) == 0 || strcmp(fname, 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_fname, fname) > 0) {
/* insert at the beginning */
bp->b_next = bheadp;
bheadp = bp;
} else {
for (sb = bheadp; sb->b_next != NULL; sb = sb->b_next)
if (strcmp (sb->b_next->b_fname, fname) > 0)
break;
/* and insert it */
bp->b_next = sb->b_next;
sb->b_next = bp;
}
}
return bp;
}
/* unlink from the list of buffers, free associated memory, assumes 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(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)
{
return (strlen(bp->b_fname) > 0) ? bp->b_fname : 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_MODIFIED)
return TRUE;
return FALSE;
}