Handle \r in input files - 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 79d3040398e884808312473b32e65a02a04feeef | |
parent f08805b3d2b41daefe6a552a569440a3e09b4b65 | |
Author: lumidify <[email protected]> | |
Date: Thu, 5 Sep 2024 09:27:09 +0200 | |
Handle \r in input files | |
Diffstat: | |
M buffer.c | 54 ++++++++++++-----------------… | |
M ledit.1 | 11 ++++++++++- | |
2 files changed, 30 insertions(+), 35 deletions(-) | |
--- | |
diff --git a/buffer.c b/buffer.c | |
@@ -58,11 +58,6 @@ static void resize_and_move_text_gap(ledit_line *line, size_… | |
static void resize_and_move_line_gap(ledit_buffer *buffer, size_t min_size, si… | |
/* | |
- * Similar to strchr, but the length of the text is given separately | |
- */ | |
-static char *strchr_len(char *text, char c, size_t len); | |
- | |
-/* | |
* Initialize a line with default values for its struct members. | |
*/ | |
static void init_line(ledit_buffer *buffer, ledit_line *line); | |
@@ -352,9 +347,11 @@ buffer_load_file(ledit_buffer *buffer, char *filename, siz… | |
if (ferror(file)) goto errorclose; | |
file_contents[len + off] = '\0'; | |
/* don't generate extra newline at end */ | |
- if (len + off > 0 && file_contents[len + off - 1] == '\n') { | |
- file_contents[len + off - 1] = '\0'; | |
- len--; | |
+ for (int i = 0; i < 2; i++) { | |
+ if (len + off > 0 && (file_contents[len + off - 1] == '\n' || … | |
+ file_contents[len + off - 1] = '\0'; | |
+ len--; | |
+ } | |
} | |
if (fclose(file)) goto error; | |
@@ -583,16 +580,6 @@ buffer_insert_text_base(ledit_buffer *buffer, size_t line_… | |
} | |
} | |
-/* FIXME: this isn't optimized like the standard version, but whatever */ | |
-static char * | |
-strchr_len(char *text, char c, size_t len) { | |
- for (size_t i = 0; i < len; i++) { | |
- if (text[i] == c) | |
- return text + i; | |
- } | |
- return NULL; | |
-} | |
- | |
/* FIXME: make these functions that call recalc* also be final as described ab… | |
static void | |
buffer_insert_text_with_newlines( | |
@@ -613,34 +600,33 @@ buffer_insert_text_with_newlines( | |
buffer_recalc_from_line(buffer, line_index); | |
} | |
-/* FIXME: also look for \r */ | |
static void | |
buffer_insert_text_with_newlines_base( | |
ledit_buffer *buffer, | |
size_t line_index, size_t index, | |
char *text, size_t len, | |
size_t *end_line_ret, size_t *end_byte_ret) { | |
- size_t rem_len = len; | |
- char *cur, *last = text; | |
size_t cur_line = line_index; | |
size_t cur_index = index; | |
- /* FIXME: strchr_len isn't really needed when the lines are normalized… | |
- while ((cur = strchr_len(last, '\n', rem_len)) != NULL) { | |
- /* FIXME: this is probably inefficient, but I don't have time … | |
- think about it right now */ | |
- buffer_append_line_base(buffer, cur_line, cur_index, 1); | |
- buffer_insert_text_base(buffer, cur_line, cur_index, last, cur… | |
- cur_index = 0; | |
- cur_line++; | |
- rem_len -= cur - last + 1; | |
- last = cur + 1; | |
+ size_t last_pos = 0; | |
+ for (size_t cur_pos = 0; cur_pos < len; cur_pos++) { | |
+ if (text[cur_pos] == '\n' || text[cur_pos] == '\r') { | |
+ buffer_append_line_base(buffer, cur_line, cur_index, 1… | |
+ buffer_insert_text_base(buffer, cur_line, cur_index, t… | |
+ cur_index = 0; | |
+ cur_line++; | |
+ /* handle \n\r or \r\n */ | |
+ if (cur_pos + 1 < len && (text[cur_pos + 1] == '\r' ||… | |
+ cur_pos++; | |
+ last_pos = cur_pos + 1; | |
+ } | |
} | |
- /* FIXME: check how legal this casting between pointers and size_t's i… | |
- buffer_insert_text_base(buffer, cur_line, cur_index, last, text + len … | |
+ if (last_pos < len) | |
+ buffer_insert_text_base(buffer, cur_line, cur_index, text + la… | |
if (end_line_ret) | |
*end_line_ret = cur_line; | |
if (end_byte_ret) | |
- *end_byte_ret = cur_index + text + len - last; | |
+ *end_byte_ret = cur_index + len - last_pos; | |
} | |
static void | |
diff --git a/ledit.1 b/ledit.1 | |
@@ -1,4 +1,4 @@ | |
-.Dd November 4, 2023 | |
+.Dd September 5, 2024 | |
.Dt LEDIT 1 | |
.Os | |
.Sh NAME | |
@@ -33,6 +33,15 @@ and the default bindings in | |
.Pa leditrc.example . | |
.Pp | |
WARNING: All input data is assumed to be utf8! | |
+ | |
+Input lines are split at | |
+.Sq \en , | |
+.Sq \er , | |
+.Sq \en\er , | |
+or | |
+.Sq \er\en , | |
+while lines written to output files only ever use | |
+.Sq \en . | |
.Sh ANTI-DESCRIPTION | |
.Nm | |
is not a code editor. |