Miscellaneous tidying of choose API, including:

- rename client and session to start_client and start_session in
  window_choose_data struct. also add TREE_OTHER define and reorder the
  struct
- rename window_choose_ctx to window_choose_data_run
- don't pass a cmd_ctx into window_choose_create (will let it use a
  different client later). instead take type, session, client
- add window_choose_data_free and use it to dispose of wcd rather than
  each cmd-*.c doing it individually
- change so ref counting is done by wcd_add and wcd_free rather than
  callers. this means 1 ref for each item but what of it :-)
- also add a ref to tree_session - not sure if this is needed?
- all the callbacks except choose-client and find-window are the same so
  remove them and add window_choose_default_callback
- reorder/rename some other bits and pieces for tidyness
This commit is contained in:
Nicholas Marriott 2013-02-10 17:32:58 +00:00
parent 418ba99078
commit 4d382ae8e6
7 changed files with 156 additions and 217 deletions

View File

@ -29,9 +29,6 @@
enum cmd_retval cmd_choose_buffer_exec(struct cmd *, struct cmd_ctx *);
void cmd_choose_buffer_callback(struct window_choose_data *);
void cmd_choose_buffer_free(struct window_choose_data *);
const struct cmd_entry cmd_choose_buffer_entry = {
"choose-buffer", NULL,
"F:t:", 0, 1,
@ -46,6 +43,7 @@ enum cmd_retval
cmd_choose_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
{
struct args *args = self->args;
struct client *c;
struct window_choose_data *cdata;
struct winlink *wl;
struct paste_buffer *pb;
@ -57,6 +55,7 @@ cmd_choose_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
ctx->error(ctx, "must be run interactively");
return (CMD_RETURN_ERROR);
}
c = ctx->curclient;
if ((template = args_get(args, 'F')) == NULL)
template = CHOOSE_BUFFER_TEMPLATE;
@ -77,9 +76,8 @@ cmd_choose_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
idx = 0;
while ((pb = paste_walk_stack(&global_buffers, &idx)) != NULL) {
cdata = window_choose_data_create(ctx);
cdata = window_choose_data_create(TREE_OTHER, c, c->session);
cdata->idx = idx - 1;
cdata->client->references++;
cdata->ft_template = xstrdup(template);
format_add(cdata->ft, "line", "%u", idx - 1);
@ -93,34 +91,7 @@ cmd_choose_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
}
free(action);
window_choose_ready(wl->window->active,
0, cmd_choose_buffer_callback, cmd_choose_buffer_free);
window_choose_ready(wl->window->active, 0, NULL, NULL);
return (CMD_RETURN_NORMAL);
}
void
cmd_choose_buffer_callback(struct window_choose_data *cdata)
{
if (cdata == NULL)
return;
if (cdata->client->flags & CLIENT_DEAD)
return;
window_choose_ctx(cdata);
}
void
cmd_choose_buffer_free(struct window_choose_data *data)
{
struct window_choose_data *cdata = data;
if (cdata == NULL)
return;
cdata->client->references--;
free(cdata->command);
free(cdata->ft_template);
free(cdata);
}

View File

@ -30,7 +30,6 @@
enum cmd_retval cmd_choose_client_exec(struct cmd *, struct cmd_ctx *);
void cmd_choose_client_callback(struct window_choose_data *);
void cmd_choose_client_free(struct window_choose_data *);
const struct cmd_entry cmd_choose_client_entry = {
"choose-client", NULL,
@ -50,9 +49,10 @@ enum cmd_retval
cmd_choose_client_exec(struct cmd *self, struct cmd_ctx *ctx)
{
struct args *args = self->args;
struct client *c;
struct client *c1;
struct window_choose_data *cdata;
struct winlink *wl;
struct client *c;
const char *template;
char *action;
u_int i, idx, cur;
@ -61,6 +61,7 @@ cmd_choose_client_exec(struct cmd *self, struct cmd_ctx *ctx)
ctx->error(ctx, "must be run interactively");
return (CMD_RETURN_ERROR);
}
c = ctx->curclient;
if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL)
return (CMD_RETURN_ERROR);
@ -78,30 +79,29 @@ cmd_choose_client_exec(struct cmd *self, struct cmd_ctx *ctx)
cur = idx = 0;
for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
c = ARRAY_ITEM(&clients, i);
if (c == NULL || c->session == NULL)
c1 = ARRAY_ITEM(&clients, i);
if (c1 == NULL || c1->session == NULL)
continue;
if (c == ctx->curclient)
if (c1 == ctx->curclient)
cur = idx;
idx++;
cdata = window_choose_data_create(ctx);
cdata = window_choose_data_create(TREE_OTHER, c, c->session);
cdata->idx = i;
cdata->client->references++;
cdata->ft_template = xstrdup(template);
format_add(cdata->ft, "line", "%u", i);
format_session(cdata->ft, c->session);
format_client(cdata->ft, c);
format_session(cdata->ft, c1->session);
format_client(cdata->ft, c1);
cdata->command = cmd_template_replace(action, c->tty.path, 1);
cdata->command = cmd_template_replace(action, c1->tty.path, 1);
window_choose_add(wl->window->active, cdata);
}
free(action);
window_choose_ready(wl->window->active,
cur, cmd_choose_client_callback, cmd_choose_client_free);
window_choose_ready(wl->window->active, cur,
cmd_choose_client_callback, NULL);
return (CMD_RETURN_NORMAL);
}
@ -113,7 +113,7 @@ cmd_choose_client_callback(struct window_choose_data *cdata)
if (cdata == NULL)
return;
if (cdata->client->flags & CLIENT_DEAD)
if (cdata->start_client->flags & CLIENT_DEAD)
return;
if (cdata->idx > ARRAY_LENGTH(&clients) - 1)
@ -122,19 +122,5 @@ cmd_choose_client_callback(struct window_choose_data *cdata)
if (c == NULL || c->session == NULL)
return;
window_choose_ctx(cdata);
}
void
cmd_choose_client_free(struct window_choose_data *cdata)
{
if (cdata == NULL)
return;
cdata->client->references--;
free(cdata->ft_template);
free(cdata->command);
format_free(cdata->ft);
free(cdata);
window_choose_data_run(cdata);
}

View File

@ -33,9 +33,6 @@
enum cmd_retval cmd_choose_list_exec(struct cmd *, struct cmd_ctx *);
void cmd_choose_list_callback(struct window_choose_data *);
void cmd_choose_list_free(struct window_choose_data *);
const struct cmd_entry cmd_choose_list_entry = {
"choose-list", NULL,
"l:t:", 0, 1,
@ -92,32 +89,9 @@ cmd_choose_list_exec(struct cmd *self, struct cmd_ctx *ctx)
return (CMD_RETURN_ERROR);
}
window_choose_ready(wl->window->active, 0, cmd_choose_list_callback,
cmd_choose_list_free);
window_choose_ready(wl->window->active, 0, NULL, NULL);
free(template);
return (CMD_RETURN_NORMAL);
}
void
cmd_choose_list_callback(struct window_choose_data *cdata)
{
if (cdata == NULL || (cdata->client->flags & CLIENT_DEAD))
return;
window_choose_ctx(cdata);
}
void
cmd_choose_list_free(struct window_choose_data *cdata)
{
cdata->session->references--;
cdata->client->references--;
free(cdata->ft_template);
free(cdata->command);
format_free(cdata->ft);
free(cdata);
}

View File

@ -34,9 +34,6 @@
enum cmd_retval cmd_choose_tree_exec(struct cmd *, struct cmd_ctx *);
void cmd_choose_tree_callback(struct window_choose_data *);
void cmd_choose_tree_free(struct window_choose_data *);
const struct cmd_entry cmd_choose_tree_entry = {
"choose-tree", NULL,
"S:W:swub:c:t:", 0, 1,
@ -230,35 +227,10 @@ windows_only:
free(final_win_template_middle);
free(final_win_template_last);
window_choose_ready(wl->window->active, cur_win,
cmd_choose_tree_callback, cmd_choose_tree_free);
window_choose_ready(wl->window->active, cur_win, NULL, NULL);
if (args_has(args, 'u'))
window_choose_expand_all(wl->window->active);
return (CMD_RETURN_NORMAL);
}
void
cmd_choose_tree_callback(struct window_choose_data *cdata)
{
if (cdata == NULL)
return;
if (cdata->client->flags & CLIENT_DEAD)
return;
window_choose_ctx(cdata);
}
void
cmd_choose_tree_free(struct window_choose_data *cdata)
{
cdata->session->references--;
cdata->client->references--;
free(cdata->ft_template);
free(cdata->command);
format_free(cdata->ft);
free(cdata);
}

View File

@ -31,7 +31,6 @@
enum cmd_retval cmd_find_window_exec(struct cmd *, struct cmd_ctx *);
void cmd_find_window_callback(struct window_choose_data *);
void cmd_find_window_free(struct window_choose_data *);
/* Flags for determining matching behavior. */
#define CMD_FIND_WINDOW_BY_TITLE 0x1
@ -131,6 +130,7 @@ enum cmd_retval
cmd_find_window_exec(struct cmd *self, struct cmd_ctx *ctx)
{
struct args *args = self->args;
struct client *c;
struct window_choose_data *cdata;
struct session *s;
struct winlink *wl, *wm;
@ -143,7 +143,8 @@ cmd_find_window_exec(struct cmd *self, struct cmd_ctx *ctx)
ctx->error(ctx, "must be run interactively");
return (CMD_RETURN_ERROR);
}
s = ctx->curclient->session;
c = ctx->curclient;
s = c->session;
if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL)
return (CMD_RETURN_ERROR);
@ -180,9 +181,8 @@ cmd_find_window_exec(struct cmd *self, struct cmd_ctx *ctx)
for (i = 0; i < ARRAY_LENGTH(&find_list); i++) {
wm = ARRAY_ITEM(&find_list, i).wl;
cdata = window_choose_data_create(ctx);
cdata = window_choose_data_create(TREE_OTHER, c, c->session);
cdata->idx = wm->idx;
cdata->client->references++;
cdata->wl = wm;
cdata->ft_template = xstrdup(template);
@ -198,8 +198,8 @@ cmd_find_window_exec(struct cmd *self, struct cmd_ctx *ctx)
window_choose_add(wl->window->active, cdata);
}
window_choose_ready(wl->window->active,
0, cmd_find_window_callback, cmd_find_window_free);
window_choose_ready(wl->window->active, 0, cmd_find_window_callback,
NULL);
out:
ARRAY_FREE(&find_list);
@ -215,7 +215,7 @@ cmd_find_window_callback(struct window_choose_data *cdata)
if (cdata == NULL)
return;
s = cdata->session;
s = cdata->start_session;
if (!session_alive(s))
return;
@ -228,16 +228,3 @@ cmd_find_window_callback(struct window_choose_data *cdata)
recalculate_sizes();
}
}
void
cmd_find_window_free(struct window_choose_data *cdata)
{
if (cdata == NULL)
return;
cdata->session->references--;
free(cdata->ft_template);
format_free(cdata->ft);
free(cdata);
}

26
tmux.h
View File

@ -879,18 +879,24 @@ struct window_mode {
/* Structures for choose mode. */
struct window_choose_data {
struct client *client;
struct session *session; /* Session of current client. */
struct session *tree_session; /* Session of items in tree. */
struct format_tree *ft;
struct winlink *wl;
char *ft_template;
char *command;
struct client *start_client;
struct session *start_session;
u_int idx;
int type;
#define TREE_OTHER 0x0
#define TREE_WINDOW 0x1
#define TREE_SESSION 0x2
struct session *tree_session; /* session of items in tree */
struct winlink *wl;
int pane_id;
char *ft_template;
struct format_tree *ft;
char *command;
};
struct window_choose_mode_item {
@ -2194,8 +2200,10 @@ void window_choose_add(struct window_pane *,
void window_choose_ready(struct window_pane *,
u_int, void (*)(struct window_choose_data *),
void (*)(struct window_choose_data *));
struct window_choose_data *window_choose_data_create(struct cmd_ctx *);
void window_choose_ctx(struct window_choose_data *);
struct window_choose_data *window_choose_data_create (int,
struct client *, struct session *);
void window_choose_data_free(struct window_choose_data *);
void window_choose_data_run(struct window_choose_data *);
struct window_choose_data *window_choose_add_window(struct window_pane *,
struct cmd_ctx *, struct session *, struct winlink *,
const char *, char *, u_int);

View File

@ -31,6 +31,8 @@ void window_choose_key(struct window_pane *, struct session *, int);
void window_choose_mouse(
struct window_pane *, struct session *, struct mouse_event *);
void window_choose_default_callback(struct window_choose_data *);
void window_choose_fire_callback(
struct window_pane *, struct window_choose_data *);
void window_choose_redraw_screen(struct window_pane *);
@ -112,6 +114,8 @@ window_choose_ready(struct window_pane *wp, u_int cur,
data->top = ARRAY_LENGTH(&data->list) - screen_size_y(s);
data->callbackfn = callbackfn;
if (data->callbackfn == NULL)
data->callbackfn = window_choose_default_callback;
data->freefn = freefn;
ARRAY_CONCAT(&data->old_list, &data->list);
@ -154,24 +158,95 @@ window_choose_init(struct window_pane *wp)
}
struct window_choose_data *
window_choose_data_create(struct cmd_ctx *ctx)
window_choose_data_create(int type, struct client *c, struct session *s)
{
struct window_choose_data *wcd;
wcd = xmalloc(sizeof *wcd);
wcd->type = type;
wcd->ft = format_create();
wcd->ft_template = NULL;
wcd->command = NULL;
wcd->wl = NULL;
wcd->tree_session = NULL;
wcd->client = ctx->curclient;
wcd->session = ctx->curclient->session;
wcd->pane_id = -1;
wcd->idx = -1;
wcd->type = 0;
wcd->tree_session = NULL;
wcd->start_client = c;
wcd->start_client->references++;
wcd->start_session = s;
wcd->start_session->references++;
return (wcd);
}
void
window_choose_data_free(struct window_choose_data *wcd)
{
wcd->start_client->references--;
wcd->start_session->references--;
if (wcd->tree_session != NULL)
wcd->tree_session->references--;
free(wcd->ft_template);
format_free(wcd->ft);
free(wcd->command);
free(wcd);
}
void
window_choose_data_run(struct window_choose_data *cdata)
{
struct cmd_ctx ctx;
struct cmd_list *cmdlist;
char *cause;
/*
* The command template will have already been replaced. But if it's
* NULL, bail here.
*/
if (cdata->command == NULL)
return;
if (cmd_string_parse(cdata->command, &cmdlist, &cause) != 0) {
if (cause != NULL) {
*cause = toupper((u_char) *cause);
status_message_set(cdata->start_client, "%s", cause);
free(cause);
}
return;
}
ctx.msgdata = NULL;
ctx.curclient = cdata->start_client;
ctx.error = key_bindings_error;
ctx.print = key_bindings_print;
ctx.info = key_bindings_info;
ctx.cmdclient = NULL;
cmd_list_exec(cmdlist, &ctx);
cmd_list_free(cmdlist);
}
void
window_choose_default_callback(struct window_choose_data *wcd)
{
if (wcd == NULL)
return;
if (wcd->start_client->flags & CLIENT_DEAD)
return;
window_choose_data_run(wcd);
}
void
window_choose_free(struct window_pane *wp)
{
@ -183,6 +258,7 @@ window_choose_free(struct window_pane *wp)
item = &ARRAY_ITEM(&data->old_list, i);
if (data->freefn != NULL && item->wcd != NULL)
data->freefn(item->wcd);
window_choose_data_free(item->wcd);
free(item->name);
}
ARRAY_FREE(&data->list);
@ -209,7 +285,7 @@ window_choose_resize(struct window_pane *wp, u_int sx, u_int sy)
void
window_choose_fire_callback(
struct window_pane *wp, struct window_choose_data *wcd)
struct window_pane *wp, struct window_choose_data *wcd)
{
struct window_choose_mode_data *data = wp->modedata;
const struct window_mode *oldmode;
@ -299,7 +375,7 @@ window_choose_collapse_all(struct window_pane *wp)
struct session *s, *chosen;
u_int i;
chosen = ARRAY_ITEM(&data->list, data->selected).wcd->session;
chosen = ARRAY_ITEM(&data->list, data->selected).wcd->start_session;
RB_FOREACH(s, sessions, &sessions)
window_choose_collapse(wp, s);
@ -790,58 +866,24 @@ window_choose_scroll_down(struct window_pane *wp)
screen_write_stop(&ctx);
}
void
window_choose_ctx(struct window_choose_data *cdata)
{
struct cmd_ctx ctx;
struct cmd_list *cmdlist;
char *cause;
/* The command template will have already been replaced. But if it's
* NULL, bail here.
*/
if (cdata->command == NULL)
return;
if (cmd_string_parse(cdata->command, &cmdlist, &cause) != 0) {
if (cause != NULL) {
*cause = toupper((u_char) *cause);
status_message_set(cdata->client, "%s", cause);
free(cause);
}
return;
}
ctx.msgdata = NULL;
ctx.curclient = cdata->client;
ctx.error = key_bindings_error;
ctx.print = key_bindings_print;
ctx.info = key_bindings_info;
ctx.cmdclient = NULL;
cmd_list_exec(cmdlist, &ctx);
cmd_list_free(cmdlist);
}
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 window_choose_data *wcd;
struct client *c = ctx->curclient;
wcd = window_choose_data_create(ctx);
wcd = window_choose_data_create(TREE_SESSION, c, c->session);
wcd->idx = s->idx;
wcd->tree_session = s;
wcd->type = TREE_SESSION;
wcd->command = cmd_template_replace(action, s->name, 1);
wcd->tree_session->references++;
wcd->ft_template = xstrdup(template);
format_add(wcd->ft, "line", "%u", idx);
format_session(wcd->ft, s);
wcd->client->references++;
wcd->session->references++;
wcd->command = cmd_template_replace(action, s->name, 1);
window_choose_add(wp, wcd);
@ -853,29 +895,28 @@ window_choose_add_item(struct window_pane *wp, struct cmd_ctx *ctx,
struct winlink *wl, const char *template, char *action, u_int idx)
{
struct window_choose_data *wcd;
char *action_data;
struct client *c = ctx->curclient;
char *expanded;
wcd = window_choose_data_create(ctx);
wcd = window_choose_data_create(TREE_OTHER, c, c->session);
wcd->idx = wl->idx;
wcd->ft_template = xstrdup(template);
format_add(wcd->ft, "line", "%u", idx);
format_session(wcd->ft, wcd->session);
format_winlink(wcd->ft, wcd->session, wl);
format_session(wcd->ft, wcd->start_session);
format_winlink(wcd->ft, wcd->start_session, wl);
format_window_pane(wcd->ft, wl->window->active);
wcd->client->references++;
wcd->session->references++;
/*
* Interpolate action here, since the data we pass back is the expanded
* template itself.
*/
xasprintf(&expanded, "%s", format_expand(wcd->ft, wcd->ft_template));
wcd->command = cmd_template_replace(action, expanded, 1);
free(expanded);
window_choose_add(wp, wcd);
/*
* Interpolate action_data here, since the data we pass back is the
* expanded template itself.
*/
xasprintf(&action_data, "%s", format_expand(wcd->ft, wcd->ft_template));
wcd->command = cmd_template_replace(action, action_data, 1);
free(action_data);
return (wcd);
}
@ -886,26 +927,26 @@ window_choose_add_window(struct window_pane *wp, struct cmd_ctx *ctx,
char *action, u_int idx)
{
struct window_choose_data *wcd;
char *action_data;
wcd = window_choose_data_create(ctx);
xasprintf(&action_data, "%s:%d", s->name, wl->idx);
wcd->command = cmd_template_replace(action, action_data, 1);
free(action_data);
struct client *c = ctx->curclient;
char *expanded;
wcd = window_choose_data_create(TREE_WINDOW, c, c->session);
wcd->idx = wl->idx;
wcd->wl = wl;
wcd->tree_session = s;
wcd->type = TREE_WINDOW;
wcd->tree_session->references++;
wcd->ft_template = xstrdup(template);
format_add(wcd->ft, "line", "%u", idx);
format_session(wcd->ft, s);
format_winlink(wcd->ft, s, wl);
format_window_pane(wcd->ft, wl->window->active);
wcd->client->references++;
wcd->session->references++;
xasprintf(&expanded, "%s:%d", s->name, wl->idx);
wcd->command = cmd_template_replace(action, expanded, 1);
free(expanded);
window_choose_add(wp, wcd);