From 2d91a2771447fa19a8f597eedc8e797b4a2fc6d6 Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Fri, 17 Apr 2015 21:21:30 +0200 Subject: degesch: factor out buffer_{previous,next}() --- degesch.c | 55 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/degesch.c b/degesch.c index b15b464..c5debdb 100644 --- a/degesch.c +++ b/degesch.c @@ -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; } -- cgit v1.2.3-70-g09d2