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.
This commit is contained in:
Tiago Cunha 2012-08-12 19:28:20 +00:00
parent 30b801decf
commit 200b0e5821
5 changed files with 152 additions and 60 deletions

View File

@ -74,6 +74,7 @@ const struct mode_key_cmdstr mode_key_cmdstr_edit[] = {
/* Choice keys command strings. */ /* Choice keys command strings. */
const struct mode_key_cmdstr mode_key_cmdstr_choice[] = { const struct mode_key_cmdstr mode_key_cmdstr_choice[] = {
{ MODEKEYCHOICE_BACKSPACE, "backspace" },
{ MODEKEYCHOICE_CANCEL, "cancel" }, { MODEKEYCHOICE_CANCEL, "cancel" },
{ MODEKEYCHOICE_CHOOSE, "choose" }, { MODEKEYCHOICE_CHOOSE, "choose" },
{ MODEKEYCHOICE_DOWN, "down" }, { MODEKEYCHOICE_DOWN, "down" },
@ -81,6 +82,7 @@ const struct mode_key_cmdstr mode_key_cmdstr_choice[] = {
{ MODEKEYCHOICE_PAGEUP, "page-up" }, { MODEKEYCHOICE_PAGEUP, "page-up" },
{ MODEKEYCHOICE_SCROLLDOWN, "scroll-down" }, { MODEKEYCHOICE_SCROLLDOWN, "scroll-down" },
{ MODEKEYCHOICE_SCROLLUP, "scroll-up" }, { MODEKEYCHOICE_SCROLLUP, "scroll-up" },
{ MODEKEYCHOICE_STARTNUMBERPREFIX, "start-number-prefix" },
{ MODEKEYCHOICE_UP, "up" }, { MODEKEYCHOICE_UP, "up" },
{ 0, NULL } { 0, NULL }
@ -190,6 +192,16 @@ struct mode_key_tree mode_key_tree_vi_edit;
/* vi choice selection keys. */ /* vi choice selection keys. */
const struct mode_key_entry mode_key_vi_choice[] = { 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 }, { '\002' /* C-b */, 0, MODEKEYCHOICE_PAGEUP },
{ '\003' /* C-c */, 0, MODEKEYCHOICE_CANCEL }, { '\003' /* C-c */, 0, MODEKEYCHOICE_CANCEL },
{ '\005' /* C-e */, 0, MODEKEYCHOICE_SCROLLDOWN }, { '\005' /* C-e */, 0, MODEKEYCHOICE_SCROLLDOWN },
@ -199,6 +211,7 @@ const struct mode_key_entry mode_key_vi_choice[] = {
{ 'j', 0, MODEKEYCHOICE_DOWN }, { 'j', 0, MODEKEYCHOICE_DOWN },
{ 'k', 0, MODEKEYCHOICE_UP }, { 'k', 0, MODEKEYCHOICE_UP },
{ 'q', 0, MODEKEYCHOICE_CANCEL }, { 'q', 0, MODEKEYCHOICE_CANCEL },
{ KEYC_BSPACE, 0, MODEKEYCHOICE_BACKSPACE },
{ KEYC_DOWN | KEYC_CTRL,0, MODEKEYCHOICE_SCROLLDOWN }, { KEYC_DOWN | KEYC_CTRL,0, MODEKEYCHOICE_SCROLLDOWN },
{ KEYC_DOWN, 0, MODEKEYCHOICE_DOWN }, { KEYC_DOWN, 0, MODEKEYCHOICE_DOWN },
{ KEYC_NPAGE, 0, MODEKEYCHOICE_PAGEDOWN }, { KEYC_NPAGE, 0, MODEKEYCHOICE_PAGEDOWN },
@ -317,6 +330,16 @@ struct mode_key_tree mode_key_tree_emacs_edit;
/* emacs choice selection keys. */ /* emacs choice selection keys. */
const struct mode_key_entry mode_key_emacs_choice[] = { 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 }, { '\003' /* C-c */, 0, MODEKEYCHOICE_CANCEL },
{ '\016' /* C-n */, 0, MODEKEYCHOICE_DOWN }, { '\016' /* C-n */, 0, MODEKEYCHOICE_DOWN },
{ '\020' /* C-p */, 0, MODEKEYCHOICE_UP }, { '\020' /* C-p */, 0, MODEKEYCHOICE_UP },
@ -325,6 +348,7 @@ const struct mode_key_entry mode_key_emacs_choice[] = {
{ '\r', 0, MODEKEYCHOICE_CHOOSE }, { '\r', 0, MODEKEYCHOICE_CHOOSE },
{ 'q', 0, MODEKEYCHOICE_CANCEL }, { 'q', 0, MODEKEYCHOICE_CANCEL },
{ 'v' | KEYC_ESCAPE, 0, MODEKEYCHOICE_PAGEUP }, { 'v' | KEYC_ESCAPE, 0, MODEKEYCHOICE_PAGEUP },
{ KEYC_BSPACE, 0, MODEKEYCHOICE_BACKSPACE },
{ KEYC_DOWN | KEYC_CTRL,0, MODEKEYCHOICE_SCROLLDOWN }, { KEYC_DOWN | KEYC_CTRL,0, MODEKEYCHOICE_SCROLLDOWN },
{ KEYC_DOWN, 0, MODEKEYCHOICE_DOWN }, { KEYC_DOWN, 0, MODEKEYCHOICE_DOWN },
{ KEYC_NPAGE, 0, MODEKEYCHOICE_PAGEDOWN }, { KEYC_NPAGE, 0, MODEKEYCHOICE_PAGEDOWN },

5
tmux.h
View File

@ -499,6 +499,7 @@ enum mode_key_cmd {
MODEKEYEDIT_TRANSPOSECHARS, MODEKEYEDIT_TRANSPOSECHARS,
/* Menu (choice) keys. */ /* Menu (choice) keys. */
MODEKEYCHOICE_BACKSPACE,
MODEKEYCHOICE_CANCEL, MODEKEYCHOICE_CANCEL,
MODEKEYCHOICE_CHOOSE, MODEKEYCHOICE_CHOOSE,
MODEKEYCHOICE_DOWN, MODEKEYCHOICE_DOWN,
@ -506,6 +507,7 @@ enum mode_key_cmd {
MODEKEYCHOICE_PAGEUP, MODEKEYCHOICE_PAGEUP,
MODEKEYCHOICE_SCROLLDOWN, MODEKEYCHOICE_SCROLLDOWN,
MODEKEYCHOICE_SCROLLUP, MODEKEYCHOICE_SCROLLUP,
MODEKEYCHOICE_STARTNUMBERPREFIX,
MODEKEYCHOICE_UP, MODEKEYCHOICE_UP,
/* Copy keys. */ /* Copy keys. */
@ -847,6 +849,7 @@ struct window_choose_data {
struct client *client; struct client *client;
struct session *session; struct session *session;
struct format_tree *ft; struct format_tree *ft;
struct winlink *wl;
char *ft_template; char *ft_template;
char *command; char *command;
u_int idx; u_int idx;
@ -855,6 +858,7 @@ struct window_choose_data {
struct window_choose_mode_item { struct window_choose_mode_item {
struct window_choose_data *wcd; struct window_choose_data *wcd;
char *name; char *name;
int pos;
}; };
/* Child window structure. */ /* 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 *); struct window_pane *window_pane_find_right(struct window_pane *);
void window_set_name(struct window *, const char *); void window_set_name(struct window *, const char *);
void winlink_clear_flags(struct winlink *); void winlink_clear_flags(struct winlink *);
void window_mode_attrs(struct grid_cell *, struct options *);
/* layout.c */ /* layout.c */
u_int layout_count_cells(struct layout_cell *); u_int layout_count_cells(struct layout_cell *);

View File

@ -40,6 +40,11 @@ void window_choose_write_line(
void window_choose_scroll_up(struct window_pane *); void window_choose_scroll_up(struct window_pane *);
void window_choose_scroll_down(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 = { const struct window_mode window_choose_mode = {
window_choose_init, window_choose_init,
window_choose_free, window_choose_free,
@ -55,27 +60,39 @@ struct window_choose_mode_data {
struct mode_key_data mdata; struct mode_key_data mdata;
ARRAY_DECL(, struct window_choose_mode_item) list; ARRAY_DECL(, struct window_choose_mode_item) list;
int width;
u_int top; u_int top;
u_int selected; u_int selected;
enum window_choose_input_type input_type;
const char *input_prompt;
char *input_str;
void (*callbackfn)(struct window_choose_data *); void (*callbackfn)(struct window_choose_data *);
void (*freefn)(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(int);
int window_choose_index_key(struct window_choose_mode_data *, int); void window_choose_prompt_input(enum window_choose_input_type,
const char *, struct window_pane *, int);
void void
window_choose_add(struct window_pane *wp, struct window_choose_data *wcd) window_choose_add(struct window_pane *wp, struct window_choose_data *wcd)
{ {
struct window_choose_mode_data *data = wp->modedata; struct window_choose_mode_data *data = wp->modedata;
struct window_choose_mode_item *item; struct window_choose_mode_item *item;
int width;
char tmp[10];
ARRAY_EXPAND(&data->list, 1); ARRAY_EXPAND(&data->list, 1);
item = &ARRAY_LAST(&data->list); item = &ARRAY_LAST(&data->list);
item->name = format_expand(wcd->ft, wcd->ft_template); item->name = format_expand(wcd->ft, wcd->ft_template);
item->wcd = wcd; 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 void
@ -107,6 +124,9 @@ window_choose_init(struct window_pane *wp)
data->callbackfn = NULL; data->callbackfn = NULL;
data->freefn = NULL; data->freefn = NULL;
data->input_type = WINDOW_CHOOSE_NORMAL;
data->input_str = xstrdup("");
data->input_prompt = NULL;
ARRAY_INIT(&data->list); ARRAY_INIT(&data->list);
data->top = 0; data->top = 0;
@ -156,6 +176,7 @@ window_choose_free(struct window_pane *wp)
free(item->name); free(item->name);
} }
ARRAY_FREE(&data->list); ARRAY_FREE(&data->list);
free(data->input_str);
screen_free(&data->screen); screen_free(&data->screen);
free(data); free(data);
@ -190,6 +211,24 @@ window_choose_fire_callback(
wp->mode = oldmode; 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 */ /* ARGSUSED */
void void
window_choose_key(struct window_pane *wp, unused struct session *sess, int key) 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 *s = &data->screen;
struct screen_write_ctx ctx; struct screen_write_ctx ctx;
struct window_choose_mode_item *item; struct window_choose_mode_item *item;
u_int items; size_t input_len;
u_int items, n;
int idx; int idx;
items = ARRAY_LENGTH(&data->list); items = ARRAY_LENGTH(&data->list);
@ -209,10 +249,22 @@ window_choose_key(struct window_pane *wp, unused struct session *sess, int key)
window_pane_reset_mode(wp); window_pane_reset_mode(wp);
break; break;
case MODEKEYCHOICE_CHOOSE: case MODEKEYCHOICE_CHOOSE:
switch (data->input_type) {
case WINDOW_CHOOSE_NORMAL:
item = &ARRAY_ITEM(&data->list, data->selected); item = &ARRAY_ITEM(&data->list, data->selected);
window_choose_fire_callback(wp, item->wcd); window_choose_fire_callback(wp, item->wcd);
window_pane_reset_mode(wp); window_pane_reset_mode(wp);
break; 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: case MODEKEYCHOICE_UP:
if (items == 0) if (items == 0)
break; break;
@ -309,8 +361,23 @@ window_choose_key(struct window_pane *wp, unused struct session *sess, int key)
data->top = data->selected; data->top = data->selected;
window_choose_redraw_screen(wp); window_choose_redraw_screen(wp);
break; break;
default: case MODEKEYCHOICE_BACKSPACE:
idx = window_choose_index_key(data, key); 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;
/*
* 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)) if (idx < 0 || (u_int) idx >= ARRAY_LENGTH(&data->list))
break; break;
data->selected = idx; data->selected = idx;
@ -318,6 +385,12 @@ window_choose_key(struct window_pane *wp, unused struct session *sess, int key)
item = &ARRAY_ITEM(&data->list, data->selected); item = &ARRAY_ITEM(&data->list, data->selected);
window_choose_fire_callback(wp, item->wcd); window_choose_fire_callback(wp, item->wcd);
window_pane_reset_mode(wp); window_pane_reset_mode(wp);
} else {
window_choose_prompt_input(
WINDOW_CHOOSE_GOTO_ITEM, "Goto item", wp, key);
}
break;
default:
break; break;
} }
} }
@ -358,65 +431,51 @@ window_choose_write_line(
struct options *oo = &wp->window->options; struct options *oo = &wp->window->options;
struct screen *s = &data->screen; struct screen *s = &data->screen;
struct grid_cell gc; struct grid_cell gc;
int utf8flag, key; size_t last, xoff = 0;
char hdr[32];
int utf8flag;
if (data->callbackfn == NULL) if (data->callbackfn == NULL)
fatalx("called before callback assigned"); fatalx("called before callback assigned");
last = screen_size_y(s) - 1;
utf8flag = options_get_number(&wp->window->options, "utf8"); utf8flag = options_get_number(&wp->window->options, "utf8");
memcpy(&gc, &grid_default_cell, sizeof gc); memcpy(&gc, &grid_default_cell, sizeof gc);
if (data->selected == data->top + py) { if (data->selected == data->top + py)
colour_set_fg(&gc, options_get_number(oo, "mode-fg")); window_mode_attrs(&gc, oo);
colour_set_bg(&gc, options_get_number(oo, "mode-bg"));
gc.attr |= options_get_number(oo, "mode-attr");
}
screen_write_cursormove(ctx, 0, py); screen_write_cursormove(ctx, 0, py);
if (data->top + py < ARRAY_LENGTH(&data->list)) { if (data->top + py < ARRAY_LENGTH(&data->list)) {
item = &ARRAY_ITEM(&data->list, data->top + py); 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, screen_write_nputs(ctx, screen_size_x(s) - 1,
&gc, utf8flag, "(%c) %s", key, item->name); &gc, utf8flag, "(%*d) %s", data->width,
} else { item->pos, item->name);
screen_write_nputs(ctx, screen_size_x(s) - 1,
&gc, utf8flag, " %s", item->name);
}
} }
while (s->cx < screen_size_x(s)) while (s->cx < screen_size_x(s))
screen_write_putc(ctx, &gc, ' '); screen_write_putc(ctx, &gc, ' ');
if (data->input_type != WINDOW_CHOOSE_NORMAL) {
window_mode_attrs(&gc, oo);
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);
}
} }
int int
window_choose_key_index(struct window_choose_mode_data *data, u_int idx) window_choose_index_key(int key)
{ {
static const char keys[] = "0123456789abcdefghijklmnopqrstuvwxyz"; static const char keys[] = "0123456789";
const char *ptr; const char *ptr;
int mkey;
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);
}
return (-1);
}
int
window_choose_index_key(struct window_choose_mode_data *data, int key)
{
static const char keys[] = "0123456789abcdefghijklmnopqrstuvwxyz";
const char *ptr;
int mkey;
u_int idx = 0; u_int idx = 0;
for (ptr = keys; *ptr != '\0'; ptr++) { for (ptr = keys; *ptr != '\0'; ptr++) {
mkey = mode_key_lookup(&data->mdata, *ptr);
if (mkey != MODEKEY_NONE && mkey != MODEKEY_OTHER)
continue;
if (key == *ptr) if (key == *ptr)
return (idx); return (idx);
idx++; idx++;

View File

@ -1115,10 +1115,7 @@ window_copy_write_line(
char hdr[32]; char hdr[32];
size_t last, xoff = 0, size = 0; size_t last, xoff = 0, size = 0;
memcpy(&gc, &grid_default_cell, sizeof gc); window_mode_attrs(&gc, oo);
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");
last = screen_size_y(s) - 1; last = screen_size_y(s) - 1;
if (py == 0) { if (py == 0) {
@ -1232,10 +1229,7 @@ window_copy_update_selection(struct window_pane *wp)
return (0); return (0);
/* Set colours. */ /* Set colours. */
memcpy(&gc, &grid_default_cell, sizeof gc); window_mode_attrs(&gc, oo);
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");
/* Find top of screen. */ /* Find top of screen. */
ty = screen_hsize(data->backing) - data->oy; ty = screen_hsize(data->backing) - data->oy;

View File

@ -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");
}