Sync OpenBSD patchset 543:

In choose mode, assign each item a number or lowercase letter from those
available and accept that as a shortcut key for the item.
This commit is contained in:
Tiago Cunha 2009-11-18 01:27:33 +00:00
parent 1c97866a5f
commit 4c2e037046

View File

@ -1,4 +1,4 @@
/* $Id: window-choose.c,v 1.24 2009-10-12 00:18:19 tcunha Exp $ */ /* $Id: window-choose.c,v 1.25 2009-11-18 01:27:33 tcunha Exp $ */
/* /*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@ -64,6 +64,9 @@ struct window_choose_mode_data {
void *data; void *data;
}; };
int window_choose_key_index(struct window_choose_mode_data *, u_int);
int window_choose_index_key(struct window_choose_mode_data *, int);
void void
window_choose_vadd(struct window_pane *wp, int idx, const char *fmt, va_list ap) window_choose_vadd(struct window_pane *wp, int idx, const char *fmt, va_list ap)
{ {
@ -174,6 +177,7 @@ window_choose_key(struct window_pane *wp, unused struct client *c, int key)
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; u_int items;
int idx;
items = ARRAY_LENGTH(&data->list); items = ARRAY_LENGTH(&data->list);
@ -259,6 +263,14 @@ window_choose_key(struct window_pane *wp, unused struct client *c, int key)
window_choose_redraw_screen(wp); window_choose_redraw_screen(wp);
break; break;
default: default:
idx = window_choose_index_key(data, key);
if (idx < 0 || (u_int) idx >= ARRAY_LENGTH(&data->list))
break;
data->selected = idx;
item = &ARRAY_ITEM(&data->list, data->selected);
data->callbackfn(data->data, item->idx);
window_pane_reset_mode(wp);
break; break;
} }
} }
@ -299,6 +311,7 @@ window_choose_write_line(
struct screen *s = &data->screen; struct screen *s = &data->screen;
struct grid_cell gc; struct grid_cell gc;
int utf8flag; int utf8flag;
char key;
if (data->callbackfn == NULL) if (data->callbackfn == NULL)
fatalx("called before callback assigned"); fatalx("called before callback assigned");
@ -314,13 +327,56 @@ window_choose_write_line(
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);
screen_write_nputs( key = window_choose_key_index(data, data->top + py);
ctx, screen_size_x(s) - 1, &gc, utf8flag, "%s", item->name); 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);
}
} }
while (s->cx < screen_size_x(s)) while (s->cx < screen_size_x(s))
screen_write_putc(ctx, &gc, ' '); 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;
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;
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++;
}
return (-1);
}
void void
window_choose_redraw_screen(struct window_pane *wp) window_choose_redraw_screen(struct window_pane *wp)
{ {