From c87888f10dfb54590c5aae8311b7aff887193d9a Mon Sep 17 00:00:00 2001 From: Albert Cervin Date: Sun, 19 Nov 2023 20:00:22 +0100 Subject: Make goto line a bit more intelligent Now handles negative line numbers to mean "from the end". --- src/main/cmds.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'src/main/cmds.c') diff --git a/src/main/cmds.c b/src/main/cmds.c index 26e2628..5991f4a 100644 --- a/src/main/cmds.c +++ b/src/main/cmds.c @@ -580,9 +580,17 @@ static int32_t goto_line(struct command_ctx ctx, int argc, const char *argv[]) { return minibuffer_prompt(ctx, "line: "); } - uint32_t line = atoi(argv[0]); - buffer_view_goto(window_buffer_view(ctx.active_window), - (struct location){.line = line, .col = 0}); + struct buffer_view *v = window_buffer_view(ctx.active_window); + + int32_t line = atoi(argv[0]); + if (line < 0) { + uint32_t nlines = buffer_num_lines(v->buffer); + line = -line; + line = line >= nlines ? 0 : nlines - line; + } else if (line > 0) { + line = line - 1; + } + buffer_view_goto(v, (struct location){.line = line, .col = 0}); } void register_buffer_commands(struct commands *commands) { -- cgit v1.2.3