-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.c
More file actions
278 lines (233 loc) · 6.26 KB
/
buffer.c
File metadata and controls
278 lines (233 loc) · 6.26 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
#include "marisa.h"
extern editorConfig E;
extern wchar_t *cwd; /* current working directory of the program */
int
bufferRowCxToRx (erow *row, int beg, int cx)
{
int rx = 0;
for (int i = beg; i < cx; i++)
{
wchar_t w = row->chars[i];
if (w == '\t')
{
rx += (MARISA_TAB_STOP) - (rx % MARISA_TAB_STOP);
}
else
{
rx += wcwidth(w);
}
}
return rx;
}
void
bufferUpdateRow (erow *row)
{
int tabs = 0;
int idx = 0;
for (int i = 0; i < row->size; i++)
if (row->chars[i] == '\t')
tabs++;
free(row->render);
row->render = (wchar_t *) malloc((row->size + tabs * MARISA_TAB_STOP + 1) * sizeof(wchar_t));
for (int i = 0; i < row->size; i++)
{
if (row->chars[i] == '\t')
{
row->render[idx++] = ' ';
while (idx % MARISA_TAB_STOP != 0) row->render[idx++] = ' ';
}
else
{
row->render[idx++] = row->chars[i];
}
}
row->render[idx] = L'\0';
row->rsize = idx;
}
void
bufferInsertRow (int at, wchar_t *s, size_t len)
{
if (at < 0 || at > BUFFER->numrows) return;
BUFFER->row = (erow *) realloc(BUFFER->row, sizeof(erow) * (BUFFER->numrows + 1));
memmove(&BUFFER->row[at + 1], &BUFFER->row[at], sizeof(erow) * (BUFFER->numrows - at));
BUFFER->row[at].size = len;
BUFFER->row[at].chars = (wchar_t *) malloc((len + 1) * sizeof(wchar_t));
wmemcpy(BUFFER->row[at].chars, s, len);
BUFFER->row[at].chars[len] = L'\0';
BUFFER->row[at].rsize = 0;
BUFFER->row[at].render = NULL;
bufferUpdateRow (&BUFFER->row[at]);
BUFFER->numrows++;
BUFFER->flags.dirty = DIRTY;
}
void
bufferFreeRow (erow *row)
{
free(row->render);
free(row->chars);
}
void
bufferDelRow (int at)
{
if (at < 0 || at >= BUFFER->numrows)
return;
bufferFreeRow(&BUFFER->row[at]);
memmove(&BUFFER->row[at], &BUFFER->row[at + 1], sizeof(erow) * (BUFFER->numrows -at -1));
BUFFER->numrows--;
BUFFER->flags.dirty = DIRTY;
}
void
bufferRowInsertChar (erow *row, int lineIndex, int c)
{
if (lineIndex < 0 || lineIndex > row->size)
lineIndex = row->size;
row->chars = (wchar_t *) realloc(row->chars, (row->size + 2) * sizeof(wchar_t));
wmemmove(&row->chars[lineIndex + 1], &row->chars[lineIndex], row->size - lineIndex + 1);
row->size++;
row->chars[lineIndex] = c;
bufferUpdateRow (row);
BUFFER->flags.dirty = DIRTY;
}
void
bufferRowAppendString (erow *row, wchar_t *s, size_t len)
{
row->chars = (wchar_t *) realloc(row->chars, (row->size + len + 1) * sizeof(wchar_t));
wmemcpy(&row->chars[row->size], s, len);
row->size += len;
row->chars[row->size] = L'\0';
bufferUpdateRow (row);
BUFFER->flags.dirty = DIRTY;
}
/* TODO; without bound checking, it segfaults */
void
bufferDelRegion (void)
{
#define region E.regionMarked
/* region[4] = {begY, begX, endY, endX} */
int minRow = region[0] <= region[2] ? region[0] : region[2];
int minCol = region[0] <= region[2] ? region[1] : region[3];
int maxRow = region[0] > region[2] ? region[0] : region[2];
int maxCol = region[0] > region[2] ? region[1] : region[3];
#undef region
/* first row in a multirow region */
int firstRowColEnd = minRow == maxRow ? maxCol : BUFFER->row[minRow].size;
wmemmove(&BUFFER->row[minRow].chars[minCol],
&BUFFER->row[minRow].chars[firstRowColEnd], BUFFER->row[maxRow].size - firstRowColEnd);
BUFFER->row[minRow].size -= firstRowColEnd - minCol;
if (BUFFER->row[minRow].size == 0)
{
bufferDelRow(minRow);
}
else
{
bufferUpdateRow(&BUFFER->row[minRow]);
}
/* Inbetween rows in a multirow region */
while(minRow < maxRow)
{
bufferDelRow(minRow);
maxRow--;
}
/* Last row in a multirow region */
if (minRow != maxRow)
{
wmemmove(&BUFFER->row[maxRow].chars[0],
&BUFFER->row[maxRow].chars[maxCol], BUFFER->row[maxRow].size - maxCol);
BUFFER->row[maxRow].size -= maxCol;
bufferUpdateRow(&BUFFER->row[maxRow]);
if (BUFFER->row[maxRow].size == 0)
bufferDelRow(maxRow);
}
FRAME->cx = minCol;
FRAME->cy = minRow;
}
void
bufferRowDelChar (erow *row, int at)
{
if (at < 0 || at >= row->size)
return;
wmemmove(&row->chars[at], &row->chars[at + 1], row->size - at);
row->size--;
bufferUpdateRow(row);
BUFFER->flags.dirty = DIRTY;
}
/*
* free the buffer, might need to free the dymanically allocated memory manually with free()
* close the file
*/
void killBuffer (textEditorBuffer *bufToBeKilled)
{
if (bufToBeKilled->flags.dirty == DIRTY)
{
/* TODO: send a editorMessage using the Message interface */
editorSave();
}
guint bufIndex;
getBufferIndexName(bufToBeKilled, &bufIndex);
g_ptr_array_remove_index_fast(E.buffers, bufIndex);
}
void readOnlyToggle (void)
{
return;
}
/* this function is called when user input is needed */
void userBufferName (void)
{
return;
}
void saveBuffer (void)
{
return;
}
void saveBufferToTemp (void)
{
return;
}
void listBuffers (void)
{
return;
}
void newBuffer (char **bufferName, char *fileName)
{
assert(bufferName != NULL);
if (E.buffers == NULL)
E.buffers = g_ptr_array_sized_new(10);
textEditorBuffer *newBuffer = g_new(textEditorBuffer, 1);
newBuffer->size = 0;
newBuffer->length = 0;
newBuffer->numrows = 0;
newBuffer->flags.dirty = CLEAN;
newBuffer->flags.dirty = NORMAL_MODE;
newBuffer->filename = fileName;
/*
* TODO: just append a hash or something to the bufferName to make it unique
* Have a hashtable with the corisponding bufferList buffer name and the actual buffer name (the one with the has)
* Show the user something like /home/john/Documents/programming/ -- test.c so they can distinguish between files with the same name
* The actual buffer name in the program would be like test.c_asx823
*/
newBuffer->buffername = *bufferName;
newBuffer->row = NULL;
g_ptr_array_add(E.buffers, newBuffer);
}
gboolean
bufferNamesEquals (gconstpointer a, gconstpointer b)
{
if (strcmp(((const textEditorBuffer *) a)->buffername, ((const textEditorBuffer *) b)->buffername) == 0)
return TRUE;
return FALSE;
}
gboolean
bufferFileEquals (gconstpointer a, gconstpointer b)
{
if (strcmp(((const textEditorBuffer *) a)->filename, ((const textEditorBuffer *) b)->filename) == 0)
return TRUE;
return FALSE;
}
/* TODO: not used */
/* static gint */
/* sortBufferName (gconstpointer a, gconstpointer b) */
/* { */
/* const textEditorBuffer *bufferA = *((textEditorBuffer **) a); */
/* const textEditorBuffer *bufferB = *((textEditorBuffer **) b); */
/* return g_ascii_strcasecmp (bufferA->buffername, bufferB->buffername); */
/* } */