commit 13045afb3112a363f771edc2d1f632889d8cdf9e
parent 625a8fad5969b39a1ba8484a68935c27ba4a05a7
Author: lumidify <nobody@lumidify.org>
Date: Mon, 9 Sep 2024 11:17:47 +0200
Don't remove empty lines
This bug was introduced in commit 79d3040398e884808312473b32e65a02a04feeef
when handling of \r was introduced.
Diffstat:
1 file changed, 15 insertions(+), 4 deletions(-)
diff --git a/buffer.c b/buffer.c
@@ -347,10 +347,17 @@ buffer_load_file(ledit_buffer *buffer, char *filename, size_t line, char **errst
if (ferror(file)) goto errorclose;
file_contents[len + off] = '\0';
/* don't generate extra newline at end */
- for (int i = 0; i < 2; i++) {
- if (len + off > 0 && (file_contents[len + off - 1] == '\n' || file_contents[len + off - 1] == '\r')) {
- file_contents[len + off - 1] = '\0';
+ /* lastc is needed to avoid removing two newlines at the end if
+ only \r or \n is used as the line separator */
+ char lastc = '\0';
+ for (int i = 0; i < 2 && len > 0; i++) {
+ char *c = file_contents + len + off - 1;
+ if (*c != lastc && (*c == '\n' || *c == '\r')) {
+ lastc = *c;
+ *c = '\0';
len--;
+ } else {
+ break;
}
}
if (fclose(file)) goto error;
@@ -616,8 +623,12 @@ buffer_insert_text_with_newlines_base(
cur_index = 0;
cur_line++;
/* handle \n\r or \r\n */
- if (cur_pos + 1 < len && (text[cur_pos + 1] == '\r' || text[cur_pos + 1] == '\n'))
+ /* the two chars are compared to make sure that \n\n or \r\r are
+ still handled as separate newlines */
+ if (cur_pos + 1 < len && text[cur_pos] != text[cur_pos + 1] &&
+ (text[cur_pos + 1] == '\r' || text[cur_pos + 1] == '\n')) {
cur_pos++;
+ }
last_pos = cur_pos + 1;
}
}