diff options
| -rw-r--r-- | src/buffer.c | 2 | ||||
| -rw-r--r-- | src/main.c | 4 | ||||
| -rw-r--r-- | src/text.c | 33 |
3 files changed, 24 insertions, 15 deletions
diff --git a/src/buffer.c b/src/buffer.c index 9e181e5..23a8ab1 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -330,7 +330,7 @@ bool moveh(struct buffer *buffer, int coldelta) { } void buffer_goto(struct buffer *buffer, uint32_t line, uint32_t col) { - int64_t linedelta = (int64_t)line - 1 - (int64_t)buffer->dot.line; + int64_t linedelta = (int64_t)line - (int64_t)buffer->dot.line; movev(buffer, linedelta); int64_t coldelta = (int64_t)col - (int64_t)buffer->dot.col; @@ -88,7 +88,7 @@ int main(int argc, char *argv[]) { {NULL, 0, NULL, 0}}; char *filename = NULL; - uint32_t jumpline = 0; + uint32_t jumpline = 1; bool goto_end = false; char ch; while ((ch = getopt_long(argc, argv, "el:", longopts, NULL)) != -1) { @@ -170,7 +170,7 @@ int main(int argc, char *argv[]) { if (goto_end) { buffer_goto_end(&initial_buffer); } else - buffer_goto(&initial_buffer, jumpline, 0); + buffer_goto(&initial_buffer, jumpline > 0 ? jumpline - 1 : 0, 0); } else { const char *welcome_txt = "Welcome to the editor for datagubbar 👴\n"; buffer_add_text(&initial_buffer, (uint8_t *)welcome_txt, @@ -245,17 +245,10 @@ void delete_line(struct text *text, uint32_t line) { text->lines[text->nlines].nchars = 0; } -void text_append(struct text *text, uint8_t *bytes, uint32_t nbytes, - uint32_t *lines_added, uint32_t *cols_added) { - uint32_t line = text->nlines > 0 ? text->nlines - 1 : 0; - uint32_t col = text_line_length(text, line); - - text_insert_at(text, line, col, bytes, nbytes, lines_added, cols_added); -} - -void text_insert_at(struct text *text, uint32_t line, uint32_t col, - uint8_t *bytes, uint32_t nbytes, uint32_t *lines_added, - uint32_t *cols_added) { +void text_insert_at_inner(struct text *text, uint32_t line, uint32_t col, + uint8_t *bytes, uint32_t nbytes, + uint32_t *lines_added, uint32_t *cols_added, + bool force_newline) { uint32_t linelen = 0, start_line = line; *cols_added = 0; @@ -270,7 +263,7 @@ void text_insert_at(struct text *text, uint32_t line, uint32_t col, col += nchars; // only insert a newline if we have to - if (linelen == 0 || col < text_line_length(text, line) || + if (force_newline || linelen == 0 || col < text_line_length(text, line) || line + 1 < text->nlines) { new_line_at(text, line, col); } @@ -294,6 +287,22 @@ void text_insert_at(struct text *text, uint32_t line, uint32_t col, *lines_added = line - start_line; } +void text_append(struct text *text, uint8_t *bytes, uint32_t nbytes, + uint32_t *lines_added, uint32_t *cols_added) { + uint32_t line = text->nlines > 0 ? text->nlines - 1 : 0; + uint32_t col = text_line_length(text, line); + + text_insert_at_inner(text, line, col, bytes, nbytes, lines_added, cols_added, + true); +} + +void text_insert_at(struct text *text, uint32_t line, uint32_t col, + uint8_t *bytes, uint32_t nbytes, uint32_t *lines_added, + uint32_t *cols_added) { + text_insert_at_inner(text, line, col, bytes, nbytes, lines_added, cols_added, + false); +} + void text_delete(struct text *text, uint32_t start_line, uint32_t start_col, uint32_t end_line, uint32_t end_col) { |
