From 42d88f87f554c7133193972e0b6b92e9a9537c88 Mon Sep 17 00:00:00 2001
From: Přemysl Janouch
Date: Thu, 10 Mar 2016 00:07:59 +0100
Subject: degesch: add unbound commands for buffer movement
---
degesch.c | 86 ++++++++++++++++++++++++++++++++++++++++++---------------------
1 file changed, 58 insertions(+), 28 deletions(-)
diff --git a/degesch.c b/degesch.c
index fc4d654..fcff422 100644
--- a/degesch.c
+++ b/degesch.c
@@ -3928,6 +3928,29 @@ buffer_goto (struct app_context *ctx, int n)
return true;
}
+static int
+buffer_count (struct app_context *ctx)
+{
+ int total = 0;
+ LIST_FOR_EACH (struct buffer, iter, ctx->buffers)
+ total++;
+ return total;
+}
+
+static void
+buffer_move (struct app_context *ctx, struct buffer *buffer, int n)
+{
+ hard_assert (n >= 1 && n <= buffer_count (ctx));
+ LIST_UNLINK_WITH_TAIL (ctx->buffers, ctx->buffers_tail, buffer);
+
+ struct buffer *following = ctx->buffers;
+ while (--n && following)
+ following = following->next;
+
+ LIST_INSERT_WITH_TAIL (ctx->buffers, ctx->buffers_tail, buffer, following);
+ refresh_prompt (ctx);
+}
+
static int
buffer_get_index (struct app_context *ctx, struct buffer *buffer)
{
@@ -9612,27 +9635,13 @@ handle_buffer_move (struct app_context *ctx, struct handler_args *a)
if (!xstrtoul (&request, a->arguments, 10))
return false;
- unsigned long total = 0;
- LIST_FOR_EACH (struct buffer, iter, ctx->buffers)
- total++;
-
- if (request == 0 || request > total)
+ if (request == 0 || request > (unsigned long) buffer_count (ctx))
{
log_global_error (ctx, "#s: #s",
"Can't move buffer", "requested position is out of range");
return true;
}
-
- struct buffer *buffer = a->buffer;
- LIST_UNLINK_WITH_TAIL (ctx->buffers, ctx->buffers_tail, buffer);
-
- struct buffer *following = ctx->buffers;
- while (--request && following)
- following = following->next;
-
- LIST_INSERT_WITH_TAIL (ctx->buffers, ctx->buffers_tail, buffer, following);
-
- refresh_prompt (ctx);
+ buffer_move (ctx, a->buffer, request);
return true;
}
@@ -11802,6 +11811,25 @@ on_goto_activity (int count, int key, void *user_data)
return true;
}
+static bool
+on_move_buffer_left (int count, int key, void *user_data)
+{
+ (void) key;
+
+ struct app_context *ctx = user_data;
+ int total = buffer_count (ctx);
+ int n = buffer_get_index (ctx, ctx->current_buffer) - count;
+ if (n <= 0) n += total * (1 + -n / total);
+ buffer_move (ctx, ctx->current_buffer, (n - 1) % total + 1);
+ return true;
+}
+
+static bool
+on_move_buffer_right (int count, int key, void *user_data)
+{
+ return on_move_buffer_left (-count, key, user_data);
+}
+
static bool
on_redraw_screen (int count, int key, void *user_data)
{
@@ -11839,18 +11867,20 @@ input_add_functions (void *user_data)
{
struct app_context *ctx = user_data;
#define XX(...) CALL_ (ctx->input, register_fn, __VA_ARGS__, ctx);
- XX ("previous-buffer", "Previous buffer", on_previous_buffer)
- XX ("next-buffer", "Next buffer", on_next_buffer)
- XX ("goto-buffer", "Go to buffer", on_goto_buffer)
- XX ("switch-buffer", "Switch buffer", on_switch_buffer)
- XX ("goto-highlight", "Go to highlight", on_goto_highlight)
- XX ("goto-activity", "Go to activity", on_goto_activity)
- XX ("display-backlog", "Show backlog", on_display_backlog)
- XX ("display-full-log", "Show full log", on_display_full_log)
- XX ("edit-input", "Edit input", on_edit_input)
- XX ("redraw-screen", "Redraw screen", on_redraw_screen)
- XX ("insert-attribute", "mIRC formatting", on_insert_attribute)
- XX ("start-paste-mode", "Bracketed paste", on_start_paste_mode)
+ XX ("previous-buffer", "Previous buffer", on_previous_buffer)
+ XX ("next-buffer", "Next buffer", on_next_buffer)
+ XX ("goto-buffer", "Go to buffer", on_goto_buffer)
+ XX ("switch-buffer", "Switch buffer", on_switch_buffer)
+ XX ("goto-highlight", "Go to highlight", on_goto_highlight)
+ XX ("goto-activity", "Go to activity", on_goto_activity)
+ XX ("move-buffer-left", "Move buffer left", on_move_buffer_left)
+ XX ("move-buffer-right", "Move buffer right", on_move_buffer_right)
+ XX ("display-backlog", "Show backlog", on_display_backlog)
+ XX ("display-full-log", "Show full log", on_display_full_log)
+ XX ("edit-input", "Edit input", on_edit_input)
+ XX ("redraw-screen", "Redraw screen", on_redraw_screen)
+ XX ("insert-attribute", "mIRC formatting", on_insert_attribute)
+ XX ("start-paste-mode", "Bracketed paste", on_start_paste_mode)
#undef XX
}
--
cgit v1.2.3-70-g09d2