From 200b0e5821d83de8c83fcc564fe7eda9253939e0 Mon Sep 17 00:00:00 2001 From: Tiago Cunha Date: Sun, 12 Aug 2012 19:28:20 +0000 Subject: [PATCH] Sync OpenBSD patchset 1156: Instead of numbering choose mode items 0-9a-z and then nothing, number them all and if there are more than 10 use a prompt when 0-9 is pressed. From Thomas Adam. --- mode-key.c | 24 ++++++++ tmux.h | 9 ++- window-choose.c | 159 +++++++++++++++++++++++++++++++++--------------- window-copy.c | 10 +-- window.c | 10 +++ 5 files changed, 152 insertions(+), 60 deletions(-) diff --git a/mode-key.c b/mode-key.c index b82e9b9b..a5540f1c 100644 --- a/mode-key.c +++ b/mode-key.c @@ -74,6 +74,7 @@ const struct mode_key_cmdstr mode_key_cmdstr_edit[] = { /* Choice keys command strings. */ const struct mode_key_cmdstr mode_key_cmdstr_choice[] = { + { MODEKEYCHOICE_BACKSPACE, "backspace" }, { MODEKEYCHOICE_CANCEL, "cancel" }, { MODEKEYCHOICE_CHOOSE, "choose" }, { MODEKEYCHOICE_DOWN, "down" }, @@ -81,6 +82,7 @@ const struct mode_key_cmdstr mode_key_cmdstr_choice[] = { { MODEKEYCHOICE_PAGEUP, "page-up" }, { MODEKEYCHOICE_SCROLLDOWN, "scroll-down" }, { MODEKEYCHOICE_SCROLLUP, "scroll-up" }, + { MODEKEYCHOICE_STARTNUMBERPREFIX, "start-number-prefix" }, { MODEKEYCHOICE_UP, "up" }, { 0, NULL } @@ -190,6 +192,16 @@ struct mode_key_tree mode_key_tree_vi_edit; /* vi choice selection keys. */ const struct mode_key_entry mode_key_vi_choice[] = { + { '0', 0, MODEKEYCHOICE_STARTNUMBERPREFIX }, + { '1', 0, MODEKEYCHOICE_STARTNUMBERPREFIX }, + { '2', 0, MODEKEYCHOICE_STARTNUMBERPREFIX }, + { '3', 0, MODEKEYCHOICE_STARTNUMBERPREFIX }, + { '4', 0, MODEKEYCHOICE_STARTNUMBERPREFIX }, + { '5', 0, MODEKEYCHOICE_STARTNUMBERPREFIX }, + { '6', 0, MODEKEYCHOICE_STARTNUMBERPREFIX }, + { '7', 0, MODEKEYCHOICE_STARTNUMBERPREFIX }, + { '8', 0, MODEKEYCHOICE_STARTNUMBERPREFIX }, + { '9', 0, MODEKEYCHOICE_STARTNUMBERPREFIX }, { '\002' /* C-b */, 0, MODEKEYCHOICE_PAGEUP }, { '\003' /* C-c */, 0, MODEKEYCHOICE_CANCEL }, { '\005' /* C-e */, 0, MODEKEYCHOICE_SCROLLDOWN }, @@ -199,6 +211,7 @@ const struct mode_key_entry mode_key_vi_choice[] = { { 'j', 0, MODEKEYCHOICE_DOWN }, { 'k', 0, MODEKEYCHOICE_UP }, { 'q', 0, MODEKEYCHOICE_CANCEL }, + { KEYC_BSPACE, 0, MODEKEYCHOICE_BACKSPACE }, { KEYC_DOWN | KEYC_CTRL,0, MODEKEYCHOICE_SCROLLDOWN }, { KEYC_DOWN, 0, MODEKEYCHOICE_DOWN }, { KEYC_NPAGE, 0, MODEKEYCHOICE_PAGEDOWN }, @@ -317,6 +330,16 @@ struct mode_key_tree mode_key_tree_emacs_edit; /* emacs choice selection keys. */ const struct mode_key_entry mode_key_emacs_choice[] = { + { '0', 0, MODEKEYCHOICE_STARTNUMBERPREFIX }, + { '1', 0, MODEKEYCHOICE_STARTNUMBERPREFIX }, + { '2', 0, MODEKEYCHOICE_STARTNUMBERPREFIX }, + { '3', 0, MODEKEYCHOICE_STARTNUMBERPREFIX }, + { '4', 0, MODEKEYCHOICE_STARTNUMBERPREFIX }, + { '5', 0, MODEKEYCHOICE_STARTNUMBERPREFIX }, + { '6', 0, MODEKEYCHOICE_STARTNUMBERPREFIX }, + { '7', 0, MODEKEYCHOICE_STARTNUMBERPREFIX }, + { '8', 0, MODEKEYCHOICE_STARTNUMBERPREFIX }, + { '9', 0, MODEKEYCHOICE_STARTNUMBERPREFIX }, { '\003' /* C-c */, 0, MODEKEYCHOICE_CANCEL }, { '\016' /* C-n */, 0, MODEKEYCHOICE_DOWN }, { '\020' /* C-p */, 0, MODEKEYCHOICE_UP }, @@ -325,6 +348,7 @@ const struct mode_key_entry mode_key_emacs_choice[] = { { '\r', 0, MODEKEYCHOICE_CHOOSE }, { 'q', 0, MODEKEYCHOICE_CANCEL }, { 'v' | KEYC_ESCAPE, 0, MODEKEYCHOICE_PAGEUP }, + { KEYC_BSPACE, 0, MODEKEYCHOICE_BACKSPACE }, { KEYC_DOWN | KEYC_CTRL,0, MODEKEYCHOICE_SCROLLDOWN }, { KEYC_DOWN, 0, MODEKEYCHOICE_DOWN }, { KEYC_NPAGE, 0, MODEKEYCHOICE_PAGEDOWN }, diff --git a/tmux.h b/tmux.h index 223d5e94..452ef1ee 100644 --- a/tmux.h +++ b/tmux.h @@ -499,6 +499,7 @@ enum mode_key_cmd { MODEKEYEDIT_TRANSPOSECHARS, /* Menu (choice) keys. */ + MODEKEYCHOICE_BACKSPACE, MODEKEYCHOICE_CANCEL, MODEKEYCHOICE_CHOOSE, MODEKEYCHOICE_DOWN, @@ -506,6 +507,7 @@ enum mode_key_cmd { MODEKEYCHOICE_PAGEUP, MODEKEYCHOICE_SCROLLDOWN, MODEKEYCHOICE_SCROLLUP, + MODEKEYCHOICE_STARTNUMBERPREFIX, MODEKEYCHOICE_UP, /* Copy keys. */ @@ -847,14 +849,16 @@ struct window_choose_data { struct client *client; struct session *session; struct format_tree *ft; + struct winlink *wl; char *ft_template; char *command; u_int idx; }; struct window_choose_mode_item { - struct window_choose_data *wcd; - char *name; + struct window_choose_data *wcd; + char *name; + int pos; }; /* Child window structure. */ @@ -2083,6 +2087,7 @@ struct window_pane *window_pane_find_left(struct window_pane *); struct window_pane *window_pane_find_right(struct window_pane *); void window_set_name(struct window *, const char *); void winlink_clear_flags(struct winlink *); +void window_mode_attrs(struct grid_cell *, struct options *); /* layout.c */ u_int layout_count_cells(struct layout_cell *); diff --git a/window-choose.c b/window-choose.c index f3857633..43b5a7b1 100644 --- a/window-choose.c +++ b/window-choose.c @@ -40,6 +40,11 @@ void window_choose_write_line( void window_choose_scroll_up(struct window_pane *); void window_choose_scroll_down(struct window_pane *); +enum window_choose_input_type { + WINDOW_CHOOSE_NORMAL = -1, + WINDOW_CHOOSE_GOTO_ITEM, +}; + const struct window_mode window_choose_mode = { window_choose_init, window_choose_free, @@ -55,27 +60,39 @@ struct window_choose_mode_data { struct mode_key_data mdata; ARRAY_DECL(, struct window_choose_mode_item) list; + int width; u_int top; u_int selected; + enum window_choose_input_type input_type; + const char *input_prompt; + char *input_str; void (*callbackfn)(struct window_choose_data *); void (*freefn)(struct window_choose_data *); }; -int window_choose_key_index(struct window_choose_mode_data *, u_int); -int window_choose_index_key(struct window_choose_mode_data *, int); +int window_choose_index_key(int); +void window_choose_prompt_input(enum window_choose_input_type, + const char *, struct window_pane *, int); void window_choose_add(struct window_pane *wp, struct window_choose_data *wcd) { struct window_choose_mode_data *data = wp->modedata; struct window_choose_mode_item *item; + int width; + char tmp[10]; ARRAY_EXPAND(&data->list, 1); item = &ARRAY_LAST(&data->list); item->name = format_expand(wcd->ft, wcd->ft_template); item->wcd = wcd; + item->pos = ARRAY_LENGTH(&data->list) - 1; + + width = snprintf (tmp, sizeof tmp, "%u", item->pos); + if (width > data->width) + data->width = width; } void @@ -107,6 +124,9 @@ window_choose_init(struct window_pane *wp) data->callbackfn = NULL; data->freefn = NULL; + data->input_type = WINDOW_CHOOSE_NORMAL; + data->input_str = xstrdup(""); + data->input_prompt = NULL; ARRAY_INIT(&data->list); data->top = 0; @@ -156,6 +176,7 @@ window_choose_free(struct window_pane *wp) free(item->name); } ARRAY_FREE(&data->list); + free(data->input_str); screen_free(&data->screen); free(data); @@ -190,6 +211,24 @@ window_choose_fire_callback( wp->mode = oldmode; } +void +window_choose_prompt_input(enum window_choose_input_type input_type, + const char *prompt, struct window_pane *wp, int key) +{ + struct window_choose_mode_data *data = wp->modedata; + size_t input_len; + + data->input_type = input_type; + data->input_prompt = prompt; + input_len = strlen(data->input_str) + 2; + + data->input_str = xrealloc(data->input_str, 1, input_len); + data->input_str[input_len - 2] = key; + data->input_str[input_len - 1] = '\0'; + + window_choose_redraw_screen(wp); +} + /* ARGSUSED */ void window_choose_key(struct window_pane *wp, unused struct session *sess, int key) @@ -198,7 +237,8 @@ window_choose_key(struct window_pane *wp, unused struct session *sess, int key) struct screen *s = &data->screen; struct screen_write_ctx ctx; struct window_choose_mode_item *item; - u_int items; + size_t input_len; + u_int items, n; int idx; items = ARRAY_LENGTH(&data->list); @@ -209,9 +249,21 @@ window_choose_key(struct window_pane *wp, unused struct session *sess, int key) window_pane_reset_mode(wp); break; case MODEKEYCHOICE_CHOOSE: - item = &ARRAY_ITEM(&data->list, data->selected); - window_choose_fire_callback(wp, item->wcd); - window_pane_reset_mode(wp); + switch (data->input_type) { + case WINDOW_CHOOSE_NORMAL: + item = &ARRAY_ITEM(&data->list, data->selected); + window_choose_fire_callback(wp, item->wcd); + window_pane_reset_mode(wp); + break; + case WINDOW_CHOOSE_GOTO_ITEM: + n = strtonum(data->input_str, 0, INT_MAX, NULL); + if (n > items - 1) + break; + item = &ARRAY_ITEM(&data->list, n); + window_choose_fire_callback(wp, item->wcd); + window_pane_reset_mode(wp); + break; + } break; case MODEKEYCHOICE_UP: if (items == 0) @@ -309,15 +361,36 @@ window_choose_key(struct window_pane *wp, unused struct session *sess, int key) data->top = data->selected; window_choose_redraw_screen(wp); break; - default: - idx = window_choose_index_key(data, key); - if (idx < 0 || (u_int) idx >= ARRAY_LENGTH(&data->list)) + case MODEKEYCHOICE_BACKSPACE: + input_len = strlen(data->input_str); + if (input_len > 0) + data->input_str[input_len - 1] = '\0'; + window_choose_redraw_screen(wp); + break; + case MODEKEYCHOICE_STARTNUMBERPREFIX: + if (key < '0' && key > '9') break; - data->selected = idx; - item = &ARRAY_ITEM(&data->list, data->selected); - window_choose_fire_callback(wp, item->wcd); - window_pane_reset_mode(wp); + /* + * If there's less than ten items (0-9) then pressing a number + * will automatically select that item; otherwise, prompt for + * the item to go to. + */ + if (ARRAY_LENGTH(&data->list) - 1 <= 9) { + idx = window_choose_index_key(key); + if (idx < 0 || (u_int) idx >= ARRAY_LENGTH(&data->list)) + break; + data->selected = idx; + + item = &ARRAY_ITEM(&data->list, data->selected); + window_choose_fire_callback(wp, item->wcd); + window_pane_reset_mode(wp); + } else { + window_choose_prompt_input( + WINDOW_CHOOSE_GOTO_ITEM, "Goto item", wp, key); + } + break; + default: break; } } @@ -358,65 +431,51 @@ window_choose_write_line( struct options *oo = &wp->window->options; struct screen *s = &data->screen; struct grid_cell gc; - int utf8flag, key; + size_t last, xoff = 0; + char hdr[32]; + int utf8flag; if (data->callbackfn == NULL) fatalx("called before callback assigned"); + last = screen_size_y(s) - 1; utf8flag = options_get_number(&wp->window->options, "utf8"); memcpy(&gc, &grid_default_cell, sizeof gc); - if (data->selected == data->top + py) { - colour_set_fg(&gc, options_get_number(oo, "mode-fg")); - colour_set_bg(&gc, options_get_number(oo, "mode-bg")); - gc.attr |= options_get_number(oo, "mode-attr"); - } + if (data->selected == data->top + py) + window_mode_attrs(&gc, oo); screen_write_cursormove(ctx, 0, py); if (data->top + py < ARRAY_LENGTH(&data->list)) { item = &ARRAY_ITEM(&data->list, data->top + py); - key = window_choose_key_index(data, data->top + py); - if (key != -1) { - screen_write_nputs(ctx, screen_size_x(s) - 1, - &gc, utf8flag, "(%c) %s", key, item->name); - } else { - screen_write_nputs(ctx, screen_size_x(s) - 1, - &gc, utf8flag, " %s", item->name); - } + screen_write_nputs(ctx, screen_size_x(s) - 1, + &gc, utf8flag, "(%*d) %s", data->width, + item->pos, item->name); } while (s->cx < screen_size_x(s)) screen_write_putc(ctx, &gc, ' '); -} -int -window_choose_key_index(struct window_choose_mode_data *data, u_int idx) -{ - static const char keys[] = "0123456789abcdefghijklmnopqrstuvwxyz"; - const char *ptr; - int mkey; + if (data->input_type != WINDOW_CHOOSE_NORMAL) { + window_mode_attrs(&gc, oo); - for (ptr = keys; *ptr != '\0'; ptr++) { - mkey = mode_key_lookup(&data->mdata, *ptr); - if (mkey != MODEKEY_NONE && mkey != MODEKEY_OTHER) - continue; - if (idx-- == 0) - return (*ptr); + xoff = xsnprintf(hdr, sizeof hdr, + "%s: %s", data->input_prompt, data->input_str); + screen_write_cursormove(ctx, 0, last); + screen_write_puts(ctx, &gc, "%s", hdr); + screen_write_cursormove(ctx, xoff, py); + memcpy(&gc, &grid_default_cell, sizeof gc); } - return (-1); + } int -window_choose_index_key(struct window_choose_mode_data *data, int key) +window_choose_index_key(int key) { - static const char keys[] = "0123456789abcdefghijklmnopqrstuvwxyz"; + static const char keys[] = "0123456789"; const char *ptr; - int mkey; u_int idx = 0; for (ptr = keys; *ptr != '\0'; ptr++) { - mkey = mode_key_lookup(&data->mdata, *ptr); - if (mkey != MODEKEY_NONE && mkey != MODEKEY_OTHER) - continue; if (key == *ptr) return (idx); idx++; @@ -514,7 +573,7 @@ window_choose_ctx(struct window_choose_data *cdata) struct window_choose_data * window_choose_add_session(struct window_pane *wp, struct cmd_ctx *ctx, - struct session *s, const char *template, char *action, u_int idx) + struct session *s, const char *template, char *action, u_int idx) { struct window_choose_data *wcd; @@ -535,8 +594,8 @@ window_choose_add_session(struct window_pane *wp, struct cmd_ctx *ctx, struct window_choose_data * window_choose_add_window(struct window_pane *wp, struct cmd_ctx *ctx, - struct session *s, struct winlink *wl, const char *template, - char *action, u_int idx) + struct session *s, struct winlink *wl, const char *template, + char *action, u_int idx) { struct window_choose_data *wcd; char *action_data; diff --git a/window-copy.c b/window-copy.c index 35742a6f..6d18dd42 100644 --- a/window-copy.c +++ b/window-copy.c @@ -1115,10 +1115,7 @@ window_copy_write_line( char hdr[32]; size_t last, xoff = 0, size = 0; - memcpy(&gc, &grid_default_cell, sizeof gc); - colour_set_fg(&gc, options_get_number(oo, "mode-fg")); - colour_set_bg(&gc, options_get_number(oo, "mode-bg")); - gc.attr |= options_get_number(oo, "mode-attr"); + window_mode_attrs(&gc, oo); last = screen_size_y(s) - 1; if (py == 0) { @@ -1232,10 +1229,7 @@ window_copy_update_selection(struct window_pane *wp) return (0); /* Set colours. */ - memcpy(&gc, &grid_default_cell, sizeof gc); - colour_set_fg(&gc, options_get_number(oo, "mode-fg")); - colour_set_bg(&gc, options_get_number(oo, "mode-bg")); - gc.attr |= options_get_number(oo, "mode-attr"); + window_mode_attrs(&gc, oo); /* Find top of screen. */ ty = screen_hsize(data->backing) - data->oy; diff --git a/window.c b/window.c index a832f4f3..d3d8c186 100644 --- a/window.c +++ b/window.c @@ -1193,3 +1193,13 @@ winlink_clear_flags(struct winlink *wl) } } } + +/* Set the grid_cell with fg/bg/attr information when window is in a mode. */ +void +window_mode_attrs(struct grid_cell *gc, struct options *oo) +{ + memcpy(gc, &grid_default_cell, sizeof gc); + colour_set_fg(gc, options_get_number(oo, "mode-fg")); + colour_set_bg(gc, options_get_number(oo, "mode-bg")); + gc->attr |= options_get_number(oo, "mode-attr"); +}