diff options
author | Přemysl Janouch <p.janouch@gmail.com> | 2015-04-17 21:21:30 +0200 |
---|---|---|
committer | Přemysl Janouch <p.janouch@gmail.com> | 2015-04-17 21:26:08 +0200 |
commit | 2d91a2771447fa19a8f597eedc8e797b4a2fc6d6 (patch) | |
tree | 4a8530b230fb4bfee8809fa707e33b82adff5747 | |
parent | e5b52fcd76c94f6349cddb0e1872ff8b2109acea (diff) | |
download | xK-2d91a2771447fa19a8f597eedc8e797b4a2fc6d6.tar.gz xK-2d91a2771447fa19a8f597eedc8e797b4a2fc6d6.tar.xz xK-2d91a2771447fa19a8f597eedc8e797b4a2fc6d6.zip |
degesch: factor out buffer_{previous,next}()
-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; } |