Make newline behavior consistent with vim
Assume every line ends with a newline, including the last line
This commit is contained in:
parent
58de733df4
commit
c4af9bda7c
2 changed files with 9 additions and 3 deletions
5
hello.c
Normal file
5
hello.c
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
printf("hello world\n");
|
||||||
|
}
|
|
@ -62,6 +62,9 @@ ERRCODE buffer_read_file(struct buffer *buffer, const char *filename) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (buffer->rows[buffer->n_rows - 1]->n_chars == 0)
|
||||||
|
buffer_delete_row(buffer, buffer->n_rows - 1);
|
||||||
|
|
||||||
END:
|
END:
|
||||||
if (file != NULL)
|
if (file != NULL)
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
@ -186,7 +189,6 @@ static char *buffer_get_string(struct buffer *buffer, size_t *n_chars) {
|
||||||
|
|
||||||
for (int i = 0; i < buffer->n_rows; i++)
|
for (int i = 0; i < buffer->n_rows; i++)
|
||||||
*n_chars += buffer->rows[i]->n_chars + 1;
|
*n_chars += buffer->rows[i]->n_chars + 1;
|
||||||
*n_chars -= 1;
|
|
||||||
|
|
||||||
char *write_buffer = malloc(*n_chars);
|
char *write_buffer = malloc(*n_chars);
|
||||||
for (int i = 0, j = 0; i < buffer->n_rows; i++) {
|
for (int i = 0, j = 0; i < buffer->n_rows; i++) {
|
||||||
|
@ -194,8 +196,7 @@ static char *buffer_get_string(struct buffer *buffer, size_t *n_chars) {
|
||||||
|
|
||||||
memcpy(write_buffer + j, row->chars, row->n_chars);
|
memcpy(write_buffer + j, row->chars, row->n_chars);
|
||||||
j += row->n_chars;
|
j += row->n_chars;
|
||||||
if (i < buffer->n_rows - 1)
|
write_buffer[j++] = '\n';
|
||||||
write_buffer[j++] = '\n';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return write_buffer;
|
return write_buffer;
|
||||||
|
|
Reference in a new issue