6 char* buffer
= calloc(sizeof(char), 80); // start at 80 chars per line
7 int* indices
= calloc(sizeof(int), 40);
11 fputs("Out of memory, buffer_init()\n", stderr
);
21 .line_indices
= indices
,
24 .cursor
= (position_t
) {0,0}
28 buffer_t
* buffer_init_point ()
30 buffer_t
* result
= calloc(sizeof(buffer_t
), 1);
31 char* buffer
= calloc(sizeof(char), 80); // start at 80 chars per line
32 int* indices
= calloc(sizeof(int), 40);
34 if (!buffer
|| !result
)
36 fputs("Out of memory, buffer_init()\n", stderr
);
42 result
->gap_start
= 0;
44 result
->line_indices
= indices
;
45 result
->num_lines
= 1;
46 result
->name
= "untitled";
47 result
->cursor
= (position_t
) {0,0};
52 void buffer_free (buffer_t
* buffer
)
56 buffer
->gap_start
= 0;
60 void buffer_inspect (buffer_t buffer
)
62 printf("BUFFER DETAILS\n"
66 "GAP_START : INDEX %d\n"
67 "GAP_END : INDEX %d\n"
69 "CURSOR POS : y(%d), x(%d)\n"
71 buffer
.name
, buffer
.size
,
72 buffer
.gap_start
, buffer
.gap_end
,
73 buffer
.num_lines
, buffer
.cursor
.y
,
76 for (int i
= 0; i
< buffer
.num_lines
; i
++)
78 printf("%d, ", buffer
.line_indices
[i
]);
85 for (int i
= 0; i
< (int) buffer
.size
; i
++)
87 if (buffer
.buf
[i
] == '\n')
89 else if (buffer
.buf
[i
] == '\0')
92 printf("%c, ", buffer
.buf
[i
]);
97 void buffer_print (buffer_t buffer
)
99 char* head
= calloc(sizeof(char), buffer
.gap_start
+ 1);
100 char* tail
= calloc(sizeof(char), (buffer
.size
- buffer
.gap_end
) + 1);
102 for (int i
= 0; i
< buffer
.gap_start
; i
++)
104 head
[i
] = buffer
.buf
[i
];
108 for (int i
= buffer
.gap_end
+ 1; i
< (int) buffer
.size
; i
++)
110 tail
[count
] = buffer
.buf
[i
];
114 printf("BUFFER DETAILS\n"
118 "GAP_START : INDEX %d\n"
119 "GAP_END : INDEX %d\n"
121 "CURSOR POS : y(%d), x(%d)\n"
123 buffer
.name
, buffer
.size
,
124 buffer
.gap_start
, buffer
.gap_end
,
125 buffer
.num_lines
, buffer
.cursor
.y
,
128 for (int i
= 0; i
< buffer
.num_lines
; i
++)
130 printf("%d, ", buffer
.line_indices
[i
]);
136 "%s<C>%s\n\n", head
, tail
);
142 void buffer_grow (buffer_t
* buffer
, unsigned int add
)
144 char* new_buf
= calloc(sizeof(char), buffer
->size
+ add
);
145 int* new_indices
= calloc(sizeof(int), buffer
->size
);
146 int new_end
= buffer
->gap_end
+ add
;
147 int new_size
= buffer
->size
+ add
;
149 for (int i
= 0; i
< buffer
->gap_start
; i
++)
150 new_buf
[i
] = buffer
->buf
[i
];
152 for (int i
= new_end
; i
< new_size
; i
++)
153 new_buf
[i
] = buffer
->buf
[i
- add
];
155 for (int i
= 0; i
< buffer
->num_lines
; i
++)
156 new_indices
[i
] = buffer
->line_indices
[i
];
159 free(buffer
->line_indices
);
160 buffer
->buf
= new_buf
;
161 buffer
->line_indices
= new_indices
;
162 buffer
->gap_end
= new_end
;
163 buffer
->size
= new_size
;
167 void buffer_insert_char (buffer_t
* buffer
, char new)
169 int gap_length
= buffer
->gap_end
- buffer
->gap_start
;
172 buffer_grow(buffer
, buffer
->size
+ buffer
->size
);
174 buffer
->buf
[buffer
->gap_start
] = new;
177 buffer
->line_indices
[buffer
->num_lines
] = buffer
->gap_start
;
178 buffer
->cursor
.x
= 0;
184 /* buffer->line_indices[buffer->cursor.y + 1]++; */
191 void buffer_backspace (buffer_t
* buffer
)
193 char to_erase
= buffer
->buf
[buffer
->gap_start
- 1];
194 if (to_erase
== '\n')
196 int current_index
= buffer
->line_indices
[buffer
->cursor
.y
];
197 int x_pos
= buffer
->gap_start
- current_index
;
199 buffer
->cursor
.x
= x_pos
;
204 buffer
->buf
[buffer
->gap_start
- 1] = '\0';
208 void buffer_left (buffer_t
* buffer
)
210 if (buffer
->gap_start
== 0)
213 char to_move
= buffer
->buf
[buffer
->gap_start
- 1];
216 int current_line_index
= buffer
->line_indices
[buffer
->cursor
.y
];
217 int prev_line_index
= buffer
->line_indices
[buffer
->cursor
.y
- 1];
218 unsigned int prev_line_length
= current_line_index
- prev_line_index
;
220 buffer
->cursor
.x
= (int) prev_line_length
- 1;
225 buffer
->buf
[buffer
->gap_end
] = to_move
;
226 buffer
->buf
[buffer
->gap_start
- 1] = '\0';
232 void buffer_right (buffer_t
* buffer
)
234 if (buffer
->gap_end
== (int) buffer
->size
- 1)
237 char to_move
= buffer
->buf
[buffer
->gap_end
+ 1];
240 buffer
->cursor
.x
= 0;
246 buffer
->buf
[buffer
->gap_start
] = to_move
;
247 buffer
->buf
[buffer
->gap_end
+ 1] = '\0';
253 void buffer_up (buffer_t
* buffer
)
255 int x_pos
= buffer
->cursor
.x
;
256 int current_line_index
= buffer
->line_indices
[buffer
->cursor
.y
];
258 if (buffer
->cursor
.y
== 0)
261 int prev_line_index
= buffer
->line_indices
[buffer
->cursor
.y
- 1];
262 int prev_line_length
= current_line_index
- prev_line_index
;
264 if (x_pos
> prev_line_length
)
266 for (int i
= 0; i
< (x_pos
+ 1); i
++)
271 int to_move
= prev_line_length
;
272 for (int i
= 0; i
< to_move
; i
++)
277 void buffer_down (buffer_t
* buffer
)
279 int x_pos
= buffer
->cursor
.x
;
281 int next_line_start
= buffer
->line_indices
[buffer
->cursor
.y
+ 1];
282 int next_line_end
= buffer
->line_indices
[buffer
->cursor
.y
+ 2];
283 int next_line_length
= next_line_end
- next_line_start
;
285 int current_line_index
= buffer
->line_indices
[buffer
->cursor
.y
];
286 int current_line_length
= next_line_start
- current_line_index
;
289 if (!next_line_start
|| !next_line_end
)
291 int gap_length
= buffer
->gap_end
- buffer
->gap_start
;
292 int occupied
= buffer
->size
- gap_length
;
293 next_line_length
= occupied
- next_line_start
;
296 if (x_pos
> next_line_length
) //account for zero-indexing
297 to_move
= (current_line_length
- x_pos
) + next_line_length
- 1;
300 to_move
= current_line_length
;
302 for (int i
= 0; i
< to_move
; i
++)
303 buffer_right(buffer
);
306 void buffer_set_contents (buffer_t
* buffer
, char string
[])
308 int length
= strlen(string
);
309 for (int i
= 0; i
< length
; i
++)
310 buffer_insert_char(buffer
, string
[i
]);
313 char* buffer_to_string (buffer_t buffer
)
315 int occupied
= buffer
.size
- (buffer
.gap_end
- buffer
.gap_start
);
316 char* head
= calloc(sizeof(char), buffer
.gap_start
+ 1);
317 char* tail
= calloc(sizeof(char), (buffer
.size
- buffer
.gap_end
) + 1);
318 char* result
= calloc(sizeof(char), occupied
+ 1);
320 for (int i
= 0; i
< buffer
.gap_start
; i
++)
322 head
[i
] = buffer
.buf
[i
];
326 for (int i
= buffer
.gap_end
+ 1; i
< (int) buffer
.size
; i
++)
328 tail
[count
] = buffer
.buf
[i
];
332 strcat(result
, head
);
333 strcat(result
, tail
);
340 void buffer_jump_word (buffer_t
* buffer
, char delim
, int dir
)
349 to_move
= buffer
->buf
[buffer
->gap_end
+ 1];
350 buffer_right(buffer
);
354 to_move
= buffer
->buf
[buffer
->gap_start
- 1];
358 if ((to_move
== delim
) ||
359 (buffer
->gap_start
== 0) ||
360 (buffer
->gap_end
== (int) (buffer
->size
- 1)))