degesch: add unbound commands for buffer movement

This commit is contained in:
Přemysl Eric Janouch 2016-03-10 00:07:59 +01:00
parent a1c4a1ef3a
commit 42d88f87f5
1 changed files with 58 additions and 28 deletions

View File

@ -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
}