Don't remove empty lines - ledit - Text editor (WIP) | |
git clone git://lumidify.org/ledit.git (fast, but not encrypted) | |
git clone https://lumidify.org/ledit.git (encrypted, but very slow) | |
git clone git://4kcetb7mo7hj6grozzybxtotsub5bempzo4lirzc3437amof2c2impyd.onion/… | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit 13045afb3112a363f771edc2d1f632889d8cdf9e | |
parent 625a8fad5969b39a1ba8484a68935c27ba4a05a7 | |
Author: lumidify <[email protected]> | |
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: | |
M buffer.c | 19 +++++++++++++++---- | |
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, si… | |
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] = '\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' ||… | |
+ /* the two chars are compared to make sure that \n\n o… | |
+ still handled as separate newlines */ | |
+ if (cur_pos + 1 < len && text[cur_pos] != text[cur_pos… | |
+ (text[cur_pos + 1] == '\r' || text[cur_pos + 1] ==… | |
cur_pos++; | |
+ } | |
last_pos = cur_pos + 1; | |
} | |
} |