From d29ca39e0eb6a47986d05ebefce23349e1635c00 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Thu, 15 Jan 2009 19:27:31 +0000 Subject: [PATCH] Two new commands, choose-window and choose-session which work only when bound to a key and allow the window or session to be selected from a list. --- CHANGES | 7 +- GNUmakefile | 5 +- Makefile | 5 +- TODO | 3 + cmd-choose-session.c | 105 +++++++++++++++ cmd-choose-window.c | 102 ++++++++++++++ cmd.c | 4 +- key-bindings.c | 6 +- mode-key.c | 4 +- server.c | 16 ++- tmux.h | 17 ++- window-choose.c | 312 +++++++++++++++++++++++++++++++++++++++++++ 12 files changed, 572 insertions(+), 14 deletions(-) create mode 100644 cmd-choose-session.c create mode 100644 cmd-choose-window.c create mode 100644 window-choose.c diff --git a/CHANGES b/CHANGES index 3a2c91d4..88ce1608 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,8 @@ +15 January 2009 + +* Two new commands, choose-window and choose-session which work only when bound + to a key and allow the window or session to be selected from a list. + 14 January 2009 * Rework the prefix-time stuff. The option is now called repeat-time and @@ -923,7 +928,7 @@ (including mutt, emacs). No status bar yet and no key remapping or other customisation. -$Id: CHANGES,v 1.210 2009-01-14 23:00:18 nicm Exp $ +$Id: CHANGES,v 1.211 2009-01-15 19:27:31 nicm Exp $ LocalWords: showw utf UTF fulvio ciriaco joshe OSC APC gettime abc DEF OA clr LocalWords: rivo nurges lscm Erdely eol smysession mysession ek dstname RB ms diff --git a/GNUmakefile b/GNUmakefile index a650d4e3..833e0674 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -1,4 +1,4 @@ -# $Id: GNUmakefile,v 1.55 2009-01-14 23:08:23 nicm Exp $ +# $Id: GNUmakefile,v 1.56 2009-01-15 19:27:31 nicm Exp $ .PHONY: clean @@ -35,8 +35,9 @@ SRCS= tmux.c server.c server-msg.c server-fn.c buffer.c buffer-poll.c status.c \ cmd-clock-mode.c cmd-lock-server.c cmd-set-password.c \ cmd-save-buffer.c cmd-select-pane.c cmd-split-window.c \ cmd-resize-pane-up.c cmd-resize-pane-down.c cmd-kill-pane.c \ - cmd-up-pane.c cmd-down-pane.c \ + cmd-up-pane.c cmd-down-pane.c cmd-choose-window.c cmd-choose-session.c \ window-clock.c window-scroll.c window-more.c window-copy.c \ + window-choose.c \ options.c options-cmd.c paste.c colour.c utf8.c clock.c \ tty.c tty-term.c tty-keys.c tty-write.c diff --git a/Makefile b/Makefile index 4ffad3d9..cd199c96 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -# $Id: Makefile,v 1.93 2009-01-14 19:56:55 nicm Exp $ +# $Id: Makefile,v 1.94 2009-01-15 19:27:31 nicm Exp $ .SUFFIXES: .c .o .y .h .PHONY: clean update-index.html upload-index.html @@ -39,8 +39,9 @@ SRCS= tmux.c server.c server-msg.c server-fn.c buffer.c buffer-poll.c status.c \ cmd-clock-mode.c cmd-lock-server.c cmd-set-password.c \ cmd-save-buffer.c cmd-select-pane.c cmd-split-window.c \ cmd-resize-pane-up.c cmd-resize-pane-down.c cmd-kill-pane.c \ - cmd-up-pane.c cmd-down-pane.c \ + cmd-up-pane.c cmd-down-pane.c cmd-choose-window.c cmd-choose-session.c \ window-clock.c window-scroll.c window-more.c window-copy.c \ + window-choose.c \ options.c options-cmd.c paste.c colour.c utf8.c clock.c \ tty.c tty-term.c tty-keys.c tty-write.c diff --git a/TODO b/TODO index 28e4b520..a1210dbc 100644 --- a/TODO +++ b/TODO @@ -71,6 +71,9 @@ - fix rxvt cursor fg issue (text under cursor has non-white fg) - key handling sucks a bit and needs to be reworked - window selection mode a la C-a " in screen + generic choice code, then: choose-window and choose-session options + which build a list of options (name, idx), pass them choice code, + then convert the return idx back into pointer and select the item for 0.6: - document OPTIONS section in man page with description of new option handling diff --git a/cmd-choose-session.c b/cmd-choose-session.c new file mode 100644 index 00000000..8c70aa0b --- /dev/null +++ b/cmd-choose-session.c @@ -0,0 +1,105 @@ +/* $Id: cmd-choose-session.c,v 1.1 2009-01-15 19:27:31 nicm Exp $ */ + +/* + * Copyright (c) 2009 Nicholas Marriott + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER + * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING + * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#include "tmux.h" + +/* + * Enter choice mode to choose a session. + */ + +void cmd_choose_session_exec(struct cmd *, struct cmd_ctx *); + +void cmd_choose_session_callback(void *, int); + +const struct cmd_entry cmd_choose_session_entry = { + "choose-session", NULL, + CMD_TARGET_WINDOW_USAGE, + 0, + cmd_target_init, + cmd_target_parse, + cmd_choose_session_exec, + cmd_target_send, + cmd_target_recv, + cmd_target_free, + cmd_target_print +}; + +struct cmd_choose_session_data { + u_int client; +}; + +void +cmd_choose_session_exec(struct cmd *self, struct cmd_ctx *ctx) +{ + struct cmd_target_data *data = self->data; + struct cmd_choose_session_data *cdata; + struct winlink *wl; + struct session *s; + u_int i, idx, cur; + + if (ctx->curclient == NULL) + return; + + if ((wl = cmd_find_window(ctx, data->target, NULL)) == NULL) + return; + + if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0) + return; + + idx = 0; + for (i = 0; i < ARRAY_LENGTH(&sessions); i++) { + s = ARRAY_ITEM(&sessions, i); + if (s == NULL) + continue; + if (s == ctx->curclient->session) + cur = idx; + idx++; + + window_choose_add(wl->window->active, i, + "%s: %u windows [%ux%u]", s->name, + winlink_count(&s->windows), s->sx, s->sy); + } + + cdata = xmalloc(sizeof *cdata); + cdata->client = server_client_index(ctx->curclient); + + window_choose_ready( + wl->window->active, cur, cmd_choose_session_callback, cdata); + + if (ctx->cmdclient != NULL) + server_write_client(ctx->cmdclient, MSG_EXIT, NULL, 0); +} + +void +cmd_choose_session_callback(void *data, int idx) +{ + struct cmd_choose_session_data *cdata = data; + struct client *c; + + if (idx != -1 && cdata->client <= ARRAY_LENGTH(&clients) - 1) { + c = ARRAY_ITEM(&clients, cdata->client); + if (c != NULL && (u_int) idx <= ARRAY_LENGTH(&sessions) - 1) { + c->session = ARRAY_ITEM(&sessions, idx); + recalculate_sizes(); + server_redraw_client(c); + } + } + xfree(cdata); +} diff --git a/cmd-choose-window.c b/cmd-choose-window.c new file mode 100644 index 00000000..948a32d9 --- /dev/null +++ b/cmd-choose-window.c @@ -0,0 +1,102 @@ +/* $Id: cmd-choose-window.c,v 1.1 2009-01-15 19:27:31 nicm Exp $ */ + +/* + * Copyright (c) 2009 Nicholas Marriott + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER + * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING + * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#include "tmux.h" + +/* + * Enter choice mode to chosoe a window. + */ + +void cmd_choose_window_exec(struct cmd *, struct cmd_ctx *); + +void cmd_choose_window_callback(void *, int); + +const struct cmd_entry cmd_choose_window_entry = { + "choose-window", NULL, + CMD_TARGET_WINDOW_USAGE, + 0, + cmd_target_init, + cmd_target_parse, + cmd_choose_window_exec, + cmd_target_send, + cmd_target_recv, + cmd_target_free, + cmd_target_print +}; + +struct cmd_choose_window_data { + u_int session; +}; + +void +cmd_choose_window_exec(struct cmd *self, struct cmd_ctx *ctx) +{ + struct cmd_target_data *data = self->data; + struct cmd_choose_window_data *cdata; + struct session *s = ctx->curclient->session; + struct winlink *wl, *wm; + struct window *w; + u_int idx, cur; + + if (ctx->curclient == NULL) + return; + + if ((wl = cmd_find_window(ctx, data->target, NULL)) == NULL) + return; + + if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0) + return; + + idx = 0; + RB_FOREACH(wm, winlinks, &s->windows) { + w = wm->window; + + if (wm == s->curw) + cur = idx; + idx++; + + window_choose_add(wl->window->active, + wm->idx, "%3d: %s [%ux%u]", wm->idx, w->name, w->sx, w->sy); + } + + cdata = xmalloc(sizeof *cdata); + if (session_index(s, &cdata->session) != 0) + fatalx("session not found"); + + window_choose_ready( + wl->window->active, cur, cmd_choose_window_callback, cdata); + + if (ctx->cmdclient != NULL) + server_write_client(ctx->cmdclient, MSG_EXIT, NULL, 0); +} + +void +cmd_choose_window_callback(void *data, int idx) +{ + struct cmd_choose_window_data *cdata = data; + struct session *s; + + if (idx != -1 && cdata->session <= ARRAY_LENGTH(&sessions) - 1) { + s = ARRAY_ITEM(&sessions, cdata->session); + if (s != NULL && session_select(s, idx) != -1) + server_redraw_session(s); + } + xfree(cdata); +} diff --git a/cmd.c b/cmd.c index e4b45243..0b110bef 100644 --- a/cmd.c +++ b/cmd.c @@ -1,4 +1,4 @@ -/* $Id: cmd.c,v 1.78 2009-01-14 19:56:55 nicm Exp $ */ +/* $Id: cmd.c,v 1.79 2009-01-15 19:27:31 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -28,6 +28,8 @@ const struct cmd_entry *cmd_table[] = { &cmd_attach_session_entry, &cmd_bind_key_entry, + &cmd_choose_session_entry, + &cmd_choose_window_entry, &cmd_clock_mode_entry, &cmd_command_prompt_entry, &cmd_copy_mode_entry, diff --git a/key-bindings.c b/key-bindings.c index 2358fa62..eab6b503 100644 --- a/key-bindings.c +++ b/key-bindings.c @@ -1,4 +1,4 @@ -/* $Id: key-bindings.c,v 1.51 2009-01-14 22:14:51 nicm Exp $ */ +/* $Id: key-bindings.c,v 1.52 2009-01-15 19:27:31 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -105,9 +105,9 @@ key_bindings_init(void) { 'o', &cmd_down_pane_entry }, { 'p', &cmd_previous_window_entry }, { 'r', &cmd_refresh_client_entry }, - { 's', &cmd_list_sessions_entry }, + { 's', &cmd_choose_session_entry }, { 't', &cmd_clock_mode_entry }, - { 'w', &cmd_list_windows_entry }, + { 'w', &cmd_choose_window_entry }, { 'x', &cmd_kill_pane_entry, }, { KEYC_UP, &cmd_up_pane_entry }, { KEYC_DOWN, &cmd_down_pane_entry }, diff --git a/mode-key.c b/mode-key.c index 4526a6f4..f5663eee 100644 --- a/mode-key.c +++ b/mode-key.c @@ -1,4 +1,4 @@ -/* $Id: mode-key.c,v 1.5 2009-01-09 16:45:58 nicm Exp $ */ +/* $Id: mode-key.c,v 1.6 2009-01-15 19:27:31 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott @@ -32,6 +32,7 @@ const struct mode_key_entry mode_key_table_vi[] = { { MODEKEY_COPYSEL, '\r' }, { MODEKEY_DOWN, 'j' }, { MODEKEY_DOWN, KEYC_DOWN }, + { MODEKEY_ENTER, '\r' }, { MODEKEY_EOL, '$' }, { MODEKEY_LEFT, 'h' }, { MODEKEY_LEFT, KEYC_LEFT }, @@ -55,6 +56,7 @@ const struct mode_key_entry mode_key_table_emacs[] = { { MODEKEY_COPYSEL, '\027' }, { MODEKEY_COPYSEL, KEYC_ADDESC('w') }, { MODEKEY_DOWN, KEYC_DOWN }, + { MODEKEY_ENTER, '\r' }, { MODEKEY_EOL, '\005' }, { MODEKEY_LEFT, '\002' }, { MODEKEY_LEFT, KEYC_LEFT }, diff --git a/server.c b/server.c index 25b27ef9..95dc60f3 100644 --- a/server.c +++ b/server.c @@ -1,4 +1,4 @@ -/* $Id: server.c,v 1.102 2009-01-14 22:29:28 nicm Exp $ */ +/* $Id: server.c,v 1.103 2009-01-15 19:27:31 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -58,6 +58,18 @@ void server_check_timers(struct client *); void server_second_timers(void); int server_update_socket(const char *); +int +server_client_index(struct client *c) +{ + u_int i; + + for (i = 0; i < ARRAY_LENGTH(&clients); i++) { + if (c == ARRAY_ITEM(&clients, i)) + return (i); + } + return (-1); +} + /* Fork new server. */ int server_start(const char *path) @@ -645,7 +657,7 @@ server_handle_client(struct client *c) /* Dispatch the command. */ key_bindings_dispatch(bd, c); - } + } wp = wl->window->active; /* could die - reset again */ /* Ensure the cursor is in the right place and correctly on or off. */ diff --git a/tmux.h b/tmux.h index ab9d30ba..8da7d8cf 100644 --- a/tmux.h +++ b/tmux.h @@ -1,4 +1,4 @@ -/* $Id: tmux.h,v 1.234 2009-01-14 22:16:57 nicm Exp $ */ +/* $Id: tmux.h,v 1.235 2009-01-15 19:27:31 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -19,7 +19,7 @@ #ifndef TMUX_H #define TMUX_H -#define PROTOCOL_VERSION -7 +#define PROTOCOL_VERSION -8 /* Shut up gcc warnings about empty if bodies. */ #define RB_AUGMENT(x) do {} while (0) @@ -904,6 +904,7 @@ enum mode_key { MODEKEY_CLEARSEL, MODEKEY_COPYSEL, MODEKEY_DOWN, + MODEKEY_ENTER, MODEKEY_EOL, MODEKEY_LEFT, MODEKEY_NONE, @@ -1082,6 +1083,8 @@ struct winlink *cmd_find_window( extern const struct cmd_entry *cmd_table[]; extern const struct cmd_entry cmd_attach_session_entry; extern const struct cmd_entry cmd_bind_key_entry; +extern const struct cmd_entry cmd_choose_session_entry; +extern const struct cmd_entry cmd_choose_window_entry; extern const struct cmd_entry cmd_clock_mode_entry; extern const struct cmd_entry cmd_command_prompt_entry; extern const struct cmd_entry cmd_copy_mode_entry; @@ -1227,6 +1230,7 @@ const char *key_string_lookup_key(int); /* server.c */ extern struct clients clients; +int server_client_index(struct client *); int server_start(const char *); /* server-msg.c */ @@ -1428,6 +1432,15 @@ extern const struct window_mode window_more_mode; void window_more_vadd(struct window_pane *, const char *, va_list); void printflike2 window_more_add(struct window_pane *, const char *, ...); +/* window-choose.c */ +extern const struct window_mode window_choose_mode; +void window_choose_vadd( + struct window_pane *, int, const char *, va_list); +void printflike3 window_choose_add( + struct window_pane *, int, const char *, ...); +void window_choose_ready(struct window_pane *, + u_int, void (*)(void *, int), void *); + /* session.c */ extern struct sessions sessions; void session_alert_add(struct session *, struct window *, int); diff --git a/window-choose.c b/window-choose.c new file mode 100644 index 00000000..b753cb53 --- /dev/null +++ b/window-choose.c @@ -0,0 +1,312 @@ +/* $Id: window-choose.c,v 1.1 2009-01-15 19:27:31 nicm Exp $ */ + +/* + * Copyright (c) 2009 Nicholas Marriott + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER + * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING + * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#include + +#include "tmux.h" + +struct screen *window_choose_init(struct window_pane *); +void window_choose_free(struct window_pane *); +void window_choose_resize(struct window_pane *, u_int, u_int); +void window_choose_key(struct window_pane *, struct client *, int); + +void window_choose_redraw_screen(struct window_pane *); +void window_choose_write_line( + struct window_pane *, struct screen_write_ctx *, u_int); + +void window_choose_scroll_up(struct window_pane *); +void window_choose_scroll_down(struct window_pane *); + +const struct window_mode window_choose_mode = { + window_choose_init, + window_choose_free, + window_choose_resize, + window_choose_key, + NULL +}; + +struct window_choose_mode_item { + char *name; + int idx; +}; + +struct window_choose_mode_data { + struct screen screen; + + ARRAY_DECL(, struct window_choose_mode_item) list; + u_int top; + u_int selected; + + void (*callback)(void *, int); + void *data; +}; + +void +window_choose_vadd(struct window_pane *wp, int idx, const char *fmt, va_list ap) +{ + struct window_choose_mode_data *data = wp->modedata; + struct window_choose_mode_item *item; + + ARRAY_EXPAND(&data->list, 1); + item = &ARRAY_LAST(&data->list); + xvasprintf(&item->name, fmt, ap); + item->idx = idx; +} + +void printflike3 +window_choose_add(struct window_pane *wp, int idx, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + window_choose_vadd(wp, idx, fmt, ap); + va_end(ap); +} + +void +window_choose_ready(struct window_pane *wp, + u_int cur, void (*callback)(void *, int), void *cdata) +{ + struct window_choose_mode_data *data = wp->modedata; + struct screen *s = &data->screen; + + data->selected = cur; + if (data->selected > screen_size_y(s) - 1) + data->top = ARRAY_LENGTH(&data->list) - screen_size_y(s); + + data->callback = callback; + data->data = cdata; + + window_choose_redraw_screen(wp); +} + +struct screen * +window_choose_init(struct window_pane *wp) +{ + struct window_choose_mode_data *data; + struct screen *s; + + wp->modedata = data = xmalloc(sizeof *data); + data->callback = NULL; + ARRAY_INIT(&data->list); + data->top = 0; + + s = &data->screen; + screen_init(s, screen_size_x(&wp->base), screen_size_y(&wp->base), 0); + s->mode &= ~MODE_CURSOR; + + return (s); +} + +void +window_choose_free(struct window_pane *wp) +{ + struct window_choose_mode_data *data = wp->modedata; + u_int i; + + for (i = 0; i < ARRAY_LENGTH(&data->list); i++) + xfree(ARRAY_ITEM(&data->list, i).name); + ARRAY_FREE(&data->list); + + screen_free(&data->screen); + xfree(data); +} + +void +window_choose_resize(struct window_pane *wp, u_int sx, u_int sy) +{ + struct window_choose_mode_data *data = wp->modedata; + struct screen *s = &data->screen; + + data->top = 0; + if (data->selected > sy - 1) + data->top = data->selected - (sy - 1); + + screen_resize(s, sx, sy); + window_choose_redraw_screen(wp); +} + +void +window_choose_key(struct window_pane *wp, unused struct client *c, int key) +{ + struct window_choose_mode_data *data = wp->modedata; + struct screen *s = &data->screen; + struct screen_write_ctx ctx; + int table; + u_int items; + + items = ARRAY_LENGTH(&data->list); + + table = options_get_number(&wp->window->options, "mode-keys"); + switch (mode_key_lookup(table, key)) { + case MODEKEY_QUIT: + data->callback(data->data, -1); + window_pane_reset_mode(wp); + break; + case MODEKEY_ENTER: + data->callback(data->data, data->selected); + window_pane_reset_mode(wp); + break; + case MODEKEY_UP: + if (items == 0) + break; + if (data->selected == 0) { + data->selected = items - 1; + if (data->selected > screen_size_y(s) - 1) + data->top = items - screen_size_y(s); + window_choose_redraw_screen(wp); + break; + } + data->selected--; + if (data->selected < data->top) + window_choose_scroll_up(wp); + else { + screen_write_start(&ctx, wp, NULL); + window_choose_write_line( + wp, &ctx, data->selected - data->top); + window_choose_write_line( + wp, &ctx, data->selected + 1 - data->top); + screen_write_stop(&ctx); + } + break; + case MODEKEY_DOWN: + if (items == 0) + break; + if (data->selected == items - 1) { + data->selected = 0; + if (data->top != 0) + data->top = 0; + window_choose_redraw_screen(wp); + break; + } + data->selected++; + if (data->selected >= data->top + screen_size_y(&data->screen)) + window_choose_scroll_down(wp); + else { + screen_write_start(&ctx, wp, NULL); + window_choose_write_line( + wp, &ctx, data->selected - data->top); + window_choose_write_line( + wp, &ctx, data->selected - 1 - data->top); + screen_write_stop(&ctx); + } + break; + case MODEKEY_PPAGE: + if (data->top < screen_size_y(s)) + data->top = 0; + else + data->top -= screen_size_y(s); + window_choose_redraw_screen(wp); + break; + case MODEKEY_NONE: + if (key != ' ') + break; + /* FALLTHROUGH */ + case MODEKEY_NPAGE: + if (data->top + screen_size_y(s) > ARRAY_LENGTH(&data->list)) + data->top = ARRAY_LENGTH(&data->list); + else + data->top += screen_size_y(s); + window_choose_redraw_screen(wp); + break; + } +} + +void +window_choose_write_line( + struct window_pane *wp, struct screen_write_ctx *ctx, u_int py) +{ + struct window_choose_mode_data *data = wp->modedata; + struct window_choose_mode_item *item; + struct screen *s = &data->screen; + struct grid_cell gc; + + if (data->callback == NULL) + fatalx("called before callback assigned"); + + memcpy(&gc, &grid_default_cell, sizeof gc); + if (data->selected == data->top + py) { + gc.fg = options_get_number(&wp->window->options, "mode-fg"); + gc.bg = options_get_number(&wp->window->options, "mode-bg"); + } + + screen_write_cursormove(ctx, 0, py); + if (data->top + py < ARRAY_LENGTH(&data->list)) { + item = &ARRAY_ITEM(&data->list, data->top + py); + screen_write_puts( + ctx, &gc, "%.*s", (int) screen_size_x(s), item->name); + } + while (s->cx < screen_size_x(s)) + screen_write_putc(ctx, &gc, ' '); + +} + +void +window_choose_redraw_screen(struct window_pane *wp) +{ + struct window_choose_mode_data *data = wp->modedata; + struct screen *s = &data->screen; + struct screen_write_ctx ctx; + u_int i; + + screen_write_start(&ctx, wp, NULL); + for (i = 0; i < screen_size_y(s); i++) + window_choose_write_line(wp, &ctx, i); + screen_write_stop(&ctx); +} + +void +window_choose_scroll_up(struct window_pane *wp) +{ + struct window_choose_mode_data *data = wp->modedata; + struct screen_write_ctx ctx; + + if (data->top == 0) + return; + data->top--; + + screen_write_start(&ctx, wp, NULL); + screen_write_cursormove(&ctx, 0, 0); + screen_write_insertline(&ctx, 1); + window_choose_write_line(wp, &ctx, 0); + if (screen_size_y(&data->screen) > 1) + window_choose_write_line(wp, &ctx, 1); + screen_write_stop(&ctx); +} + +void +window_choose_scroll_down(struct window_pane *wp) +{ + struct window_choose_mode_data *data = wp->modedata; + struct screen *s = &data->screen; + struct screen_write_ctx ctx; + + if (data->top >= ARRAY_LENGTH(&data->list)) + return; + data->top++; + + screen_write_start(&ctx, wp, NULL); + screen_write_cursormove(&ctx, 0, 0); + screen_write_deleteline(&ctx, 1); + window_choose_write_line(wp, &ctx, screen_size_y(s) - 1); + if (screen_size_y(&data->screen) > 1) + window_choose_write_line(wp, &ctx, screen_size_y(s) - 2); + screen_write_stop(&ctx); +}