summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlbert Cervin <albert@acervin.com>2023-04-06 21:47:09 +0200
committerAlbert Cervin <albert@acervin.com>2023-04-06 21:47:09 +0200
commitb5ed4cf757afc50afb6ac499eee7b87a2648fa4c (patch)
treef996336af26cdd74d0e1267f7b3127ab24c6372a
parenta8e6015bd5d46132f62a64e62f80a199260dc62d (diff)
downloaddged-b5ed4cf757afc50afb6ac499eee7b87a2648fa4c.tar.gz
dged-b5ed4cf757afc50afb6ac499eee7b87a2648fa4c.tar.xz
dged-b5ed4cf757afc50afb6ac499eee7b87a2648fa4c.zip
fix what I messed up
Undo was relying on buffer_goto being 0-indexed when it comes to lines, whereas the new --line flag was not.
-rw-r--r--src/buffer.c2
-rw-r--r--src/main.c4
-rw-r--r--src/text.c33
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;
diff --git a/src/main.c b/src/main.c
index 40de819..c0b6b0c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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,
diff --git a/src/text.c b/src/text.c
index b6c0426..cdbb796 100644
--- a/src/text.c
+++ b/src/text.c
@@ -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) {