diff options
-rw-r--r-- | degesch.c | 55 |
1 files changed, 37 insertions, 18 deletions
@@ -938,18 +938,45 @@ buffer_activate (struct app_context *ctx, struct buffer *buffer) refresh_prompt (ctx); } -/// Activate the n-th buffer, counting from one -static bool -buffer_goto (struct app_context *ctx, int n) +static struct buffer * +buffer_at_index (struct app_context *ctx, int n) { int i = 0; LIST_FOR_EACH (struct buffer, iter, ctx->buffers) if (++i == n) - { - buffer_activate (ctx, iter); - return true; - } - return false; + return iter; + return NULL; +} + +static struct buffer * +buffer_next (struct app_context *ctx, int count) +{ + struct buffer *new_buffer = ctx->current_buffer; + while (count-- > 0) + if (!(new_buffer = new_buffer->next)) + new_buffer = ctx->buffers; + return new_buffer; +} + +static struct buffer * +buffer_previous (struct app_context *ctx, int count) +{ + struct buffer *new_buffer = ctx->current_buffer; + while (count-- > 0) + if (!(new_buffer = new_buffer->prev)) + new_buffer = ctx->buffers_tail; + return new_buffer; +} + +static bool +buffer_goto (struct app_context *ctx, int n) +{ + struct buffer *buffer = buffer_at_index (ctx, n); + if (!buffer) + return false; + + buffer_activate (ctx, buffer); + return true; } static int @@ -1375,11 +1402,7 @@ on_readline_previous_buffer (int count, int key) if (!ctx->current_buffer) return 0; - struct buffer *new_buffer = ctx->current_buffer; - while (count-- > 0) - if (!(new_buffer = new_buffer->prev)) - new_buffer = ctx->buffers_tail; - buffer_activate (ctx, new_buffer); + buffer_activate (ctx, buffer_previous (ctx, count)); return 0; } @@ -1392,11 +1415,7 @@ on_readline_next_buffer (int count, int key) if (!ctx->current_buffer) return 0; - struct buffer *new_buffer = ctx->current_buffer; - while (count-- > 0) - if (!(new_buffer = new_buffer->next)) - new_buffer = ctx->buffers; - buffer_activate (ctx, new_buffer); + buffer_activate (ctx, buffer_next (ctx, count)); return 0; } |