Skip to content

Commit

Permalink
Instead of every command resolving the target (-t or -s) itself, prepare
Browse files Browse the repository at this point in the history
the state (client, session, winlink, pane) for it it before entering the
command. Each command provides some flags that tell the prepare step
what it is expecting.

This is a requirement for having hooks on commands (for example, if you
hook "select-window -t1:2", the hook command should to operate on window
1:2 not whatever it thinks is the current window), and should allow some
other target improvements.

The old cmd_find_* functions remain for the moment but that layer will
be dropped later.

Joint work with Thomas Adam.
  • Loading branch information
nicm committed Dec 13, 2015
1 parent 5ed17e8 commit 4a4daf1
Show file tree
Hide file tree
Showing 56 changed files with 605 additions and 598 deletions.
43 changes: 10 additions & 33 deletions cmd-attach-session.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,48 +36,27 @@ const struct cmd_entry cmd_attach_session_entry = {
"attach-session", "attach",
"c:dErt:", 0, 0,
"[-dEr] [-c working-directory] " CMD_TARGET_SESSION_USAGE,
CMD_STARTSERVER,
CMD_STARTSERVER|CMD_SESSION_T|CMD_PANE_T|CMD_PREFERUNATTACHED,
cmd_attach_session_exec
};

enum cmd_retval
cmd_attach_session(struct cmd_q *cmdq, const char *tflag, int dflag, int rflag,
const char *cflag, int Eflag)
cmd_attach_session(struct cmd_q *cmdq, int dflag, int rflag, const char *cflag,
int Eflag)
{
struct session *s;
struct session *s = cmdq->state.tflag.s;
struct client *c = cmdq->client, *c_loop;
struct winlink *wl = NULL;
struct window *w = NULL;
struct window_pane *wp = NULL;
struct winlink *wl = cmdq->state.tflag.wl;
struct window_pane *wp = cmdq->state.tflag.wp;
const char *update;
char *cause;
char *cause, *cwd;
struct format_tree *ft;
char *cwd;

if (RB_EMPTY(&sessions)) {
cmdq_error(cmdq, "no sessions");
return (CMD_RETURN_ERROR);
}

if (tflag == NULL) {
if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL)
return (CMD_RETURN_ERROR);
} else if (tflag[strcspn(tflag, ":.")] != '\0') {
if ((wl = cmd_find_pane(cmdq, tflag, &s, &wp)) == NULL)
return (CMD_RETURN_ERROR);
} else {
if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL)
return (CMD_RETURN_ERROR);
w = window_find_by_id_str(tflag);
if (w == NULL) {
wp = window_pane_find_by_id_str(tflag);
if (wp != NULL)
w = wp->window;
}
if (w != NULL)
wl = winlink_find_by_window(&s->windows, w);
}

if (c == NULL)
return (CMD_RETURN_NORMAL);
if (server_client_check_nested(c)) {
Expand All @@ -94,8 +73,7 @@ cmd_attach_session(struct cmd_q *cmdq, const char *tflag, int dflag, int rflag,

if (cflag != NULL) {
ft = format_create(cmdq, 0);
format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s,
NULL, NULL);
format_defaults(ft, c, s, wl, wp);
cwd = format_expand(ft, cflag);
format_free(ft);

Expand Down Expand Up @@ -176,7 +154,6 @@ cmd_attach_session_exec(struct cmd *self, struct cmd_q *cmdq)
{
struct args *args = self->args;

return (cmd_attach_session(cmdq, args_get(args, 't'),
args_has(args, 'd'), args_has(args, 'r'), args_get(args, 'c'),
args_has(args, 'E')));
return (cmd_attach_session(cmdq, args_has(args, 'd'),
args_has(args, 'r'), args_get(args, 'c'), args_has(args, 'E')));
}
23 changes: 8 additions & 15 deletions cmd-break-pane.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,30 @@ const struct cmd_entry cmd_break_pane_entry = {
"break-pane", "breakp",
"dPF:s:t:", 0, 0,
"[-dP] [-F format] " CMD_SRCDST_PANE_USAGE,
0,
CMD_PANE_S|CMD_INDEX_T,
cmd_break_pane_exec
};

enum cmd_retval
cmd_break_pane_exec(struct cmd *self, struct cmd_q *cmdq)
{
struct args *args = self->args;
struct winlink *wl;
struct session *src_s;
struct session *dst_s;
struct window_pane *wp;
struct window *w;
struct winlink *wl = cmdq->state.sflag.wl;
struct session *src_s = cmdq->state.sflag.s;
struct session *dst_s = cmdq->state.tflag.s;
struct window_pane *wp = cmdq->state.sflag.wp;
struct window *w = wl->window;
char *name;
char *cause;
int idx;
int idx = cmdq->state.tflag.idx;
struct format_tree *ft;
const char *template;
char *cp;

wl = cmd_find_pane(cmdq, args_get(args, 's'), &src_s, &wp);
if (wl == NULL)
return (CMD_RETURN_ERROR);
if ((idx = cmd_find_index(cmdq, args_get(args, 't'), &dst_s)) == -2)
return (CMD_RETURN_ERROR);
if (idx != -1 && winlink_find_by_index(&dst_s->windows, idx) != NULL) {
cmdq_error(cmdq, "index %d already in use", idx);
return (CMD_RETURN_ERROR);
}
w = wl->window;

if (window_count_panes(w) == 1) {
cmdq_error(cmdq, "can't break with only one pane");
Expand Down Expand Up @@ -102,8 +96,7 @@ cmd_break_pane_exec(struct cmd *self, struct cmd_q *cmdq)
template = BREAK_PANE_TEMPLATE;

ft = format_create(cmdq, 0);
format_defaults(ft, cmd_find_client(cmdq, NULL, 1), dst_s, wl,
wp);
format_defaults(ft, cmdq->state.c, dst_s, wl, wp);

cp = format_expand(ft, template);
cmdq_print(cmdq, "%s", cp);
Expand Down
7 changes: 2 additions & 5 deletions cmd-capture-pane.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const struct cmd_entry cmd_capture_pane_entry = {
"ab:CeE:JpPqS:t:", 0, 0,
"[-aCeJpPq] " CMD_BUFFER_USAGE " [-E end-line] [-S start-line]"
CMD_TARGET_PANE_USAGE,
0,
CMD_PANE_T,
cmd_capture_pane_exec
};

Expand Down Expand Up @@ -175,14 +175,11 @@ cmd_capture_pane_exec(struct cmd *self, struct cmd_q *cmdq)
{
struct args *args = self->args;
struct client *c;
struct window_pane *wp;
struct window_pane *wp = cmdq->state.tflag.wp;
char *buf, *cause;
const char *bufname;
size_t len;

if (cmd_find_pane(cmdq, args_get(args, 't'), NULL, &wp) == NULL)
return (CMD_RETURN_ERROR);

len = 0;
if (args_has(args, 'P'))
buf = cmd_capture_pane_pending(args, wp, &len);
Expand Down
11 changes: 4 additions & 7 deletions cmd-choose-buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,30 @@ const struct cmd_entry cmd_choose_buffer_entry = {
"choose-buffer", NULL,
"F:t:", 0, 1,
CMD_TARGET_WINDOW_USAGE " [-F format] [template]",
0,
CMD_WINDOW_T,
cmd_choose_buffer_exec
};

enum cmd_retval
cmd_choose_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
{
struct args *args = self->args;
struct client *c;
struct client *c = cmdq->state.c;
struct winlink *wl = cmdq->state.tflag.wl;
struct window_choose_data *cdata;
struct winlink *wl;
struct paste_buffer *pb;
char *action, *action_data;
const char *template;
u_int idx;

if ((c = cmd_find_client(cmdq, NULL, 1)) == NULL) {
if (c == NULL) {
cmdq_error(cmdq, "no client available");
return (CMD_RETURN_ERROR);
}

if ((template = args_get(args, 'F')) == NULL)
template = CHOOSE_BUFFER_TEMPLATE;

if ((wl = cmd_find_window(cmdq, args_get(args, 't'), NULL)) == NULL)
return (CMD_RETURN_ERROR);

if (paste_get_top(NULL) == NULL)
return (CMD_RETURN_NORMAL);

Expand Down
11 changes: 4 additions & 7 deletions cmd-choose-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const struct cmd_entry cmd_choose_client_entry = {
"choose-client", NULL,
"F:t:", 0, 1,
CMD_TARGET_WINDOW_USAGE " [-F format] [template]",
0,
CMD_WINDOW_T,
cmd_choose_client_exec
};

Expand All @@ -53,22 +53,19 @@ enum cmd_retval
cmd_choose_client_exec(struct cmd *self, struct cmd_q *cmdq)
{
struct args *args = self->args;
struct client *c;
struct client *c = cmdq->state.c;
struct client *c1;
struct window_choose_data *cdata;
struct winlink *wl;
struct winlink *wl = cmdq->state.tflag.wl;
const char *template;
char *action;
u_int idx, cur;

if ((c = cmd_find_client(cmdq, NULL, 1)) == NULL) {
if (c == NULL) {
cmdq_error(cmdq, "no client available");
return (CMD_RETURN_ERROR);
}

if ((wl = cmd_find_window(cmdq, args_get(args, 't'), NULL)) == NULL)
return (CMD_RETURN_ERROR);

if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
return (CMD_RETURN_NORMAL);

Expand Down
17 changes: 7 additions & 10 deletions cmd-choose-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,33 @@ const struct cmd_entry cmd_choose_tree_entry = {
"S:W:swub:c:t:", 0, 1,
"[-suw] [-b session-template] [-c window template] [-S format] " \
"[-W format] " CMD_TARGET_WINDOW_USAGE,
0,
CMD_WINDOW_T,
cmd_choose_tree_exec
};

const struct cmd_entry cmd_choose_session_entry = {
"choose-session", NULL,
"F:t:", 0, 1,
CMD_TARGET_WINDOW_USAGE " [-F format] [template]",
0,
CMD_WINDOW_T,
cmd_choose_tree_exec
};

const struct cmd_entry cmd_choose_window_entry = {
"choose-window", NULL,
"F:t:", 0, 1,
CMD_TARGET_WINDOW_USAGE "[-F format] [template]",
0,
CMD_WINDOW_T,
cmd_choose_tree_exec
};

enum cmd_retval
cmd_choose_tree_exec(struct cmd *self, struct cmd_q *cmdq)
{
struct args *args = self->args;
struct winlink *wl, *wm;
struct session *s, *s2;
struct client *c;
struct client *c = cmdq->state.c;
struct winlink *wl = cmdq->state.tflag.wl, *wm;
struct session *s = cmdq->state.tflag.s, *s2;
struct window_choose_data *wcd = NULL;
const char *ses_template, *win_template;
char *final_win_action, *cur_win_template;
Expand All @@ -87,14 +87,11 @@ cmd_choose_tree_exec(struct cmd *self, struct cmd_q *cmdq)
ses_template = win_template = NULL;
ses_action = win_action = NULL;

if ((c = cmd_find_client(cmdq, NULL, 1)) == NULL) {
if (c == NULL) {
cmdq_error(cmdq, "no client available");
return (CMD_RETURN_ERROR);
}

if ((wl = cmd_find_window(cmdq, args_get(args, 't'), &s)) == NULL)
return (CMD_RETURN_ERROR);

if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
return (CMD_RETURN_NORMAL);

Expand Down
11 changes: 4 additions & 7 deletions cmd-clear-history.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,17 @@ const struct cmd_entry cmd_clear_history_entry = {
"clear-history", "clearhist",
"t:", 0, 0,
CMD_TARGET_PANE_USAGE,
0,
CMD_PANE_T,
cmd_clear_history_exec
};

enum cmd_retval
cmd_clear_history_exec(struct cmd *self, struct cmd_q *cmdq)
cmd_clear_history_exec(__unused struct cmd *self, struct cmd_q *cmdq)
{
struct args *args = self->args;
struct window_pane *wp;
struct window_pane *wp = cmdq->state.tflag.wp;
struct grid *gd;

if (cmd_find_pane(cmdq, args_get(args, 't'), NULL, &wp) == NULL)
return (CMD_RETURN_ERROR);
gd = wp->base.grid;
gd = cmdq->state.tflag.wp->base.grid;

if (wp->mode == &window_copy_mode)
window_pane_reset_mode(wp);
Expand Down
7 changes: 2 additions & 5 deletions cmd-command-prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const struct cmd_entry cmd_command_prompt_entry = {
"command-prompt", NULL,
"I:p:t:", 0, 1,
"[-I inputs] [-p prompts] " CMD_TARGET_CLIENT_USAGE " [template]",
0,
CMD_CLIENT_T,
cmd_command_prompt_exec
};

Expand All @@ -58,13 +58,10 @@ cmd_command_prompt_exec(struct cmd *self, struct cmd_q *cmdq)
struct args *args = self->args;
const char *inputs, *prompts;
struct cmd_command_prompt_cdata *cdata;
struct client *c;
struct client *c = cmdq->state.c;
char *prompt, *ptr, *input = NULL;
size_t n;

if ((c = cmd_find_client(cmdq, args_get(args, 't'), 0)) == NULL)
return (CMD_RETURN_ERROR);

if (c->prompt_string != NULL)
return (CMD_RETURN_NORMAL);

Expand Down
7 changes: 2 additions & 5 deletions cmd-confirm-before.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const struct cmd_entry cmd_confirm_before_entry = {
"confirm-before", "confirm",
"p:t:", 1, 1,
"[-p prompt] " CMD_TARGET_CLIENT_USAGE " command",
0,
CMD_CLIENT_T,
cmd_confirm_before_exec
};

Expand All @@ -51,13 +51,10 @@ cmd_confirm_before_exec(struct cmd *self, struct cmd_q *cmdq)
{
struct args *args = self->args;
struct cmd_confirm_before_data *cdata;
struct client *c;
struct client *c = cmdq->state.c;
char *cmd, *copy, *new_prompt, *ptr;
const char *prompt;

if ((c = cmd_find_client(cmdq, args_get(args, 't'), 0)) == NULL)
return (CMD_RETURN_ERROR);

if ((prompt = args_get(args, 'p')) != NULL)
xasprintf(&new_prompt, "%s ", prompt);
else {
Expand Down
9 changes: 4 additions & 5 deletions cmd-copy-mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ enum cmd_retval cmd_copy_mode_exec(struct cmd *, struct cmd_q *);
const struct cmd_entry cmd_copy_mode_entry = {
"copy-mode", NULL,
"Met:u", 0, 0,
"[-Meu] " CMD_TARGET_PANE_USAGE,
0,
"[-Mu] " CMD_TARGET_PANE_USAGE,
CMD_PANE_T,
cmd_copy_mode_exec
};

Expand All @@ -48,15 +48,14 @@ cmd_copy_mode_exec(struct cmd *self, struct cmd_q *cmdq)
struct args *args = self->args;
struct client *c = cmdq->client;
struct session *s;
struct window_pane *wp;
struct window_pane *wp = cmdq->state.tflag.wp;

if (args_has(args, 'M')) {
if ((wp = cmd_mouse_pane(&cmdq->item->mouse, &s, NULL)) == NULL)
return (CMD_RETURN_NORMAL);
if (c == NULL || c->session != s)
return (CMD_RETURN_NORMAL);
} else if (cmd_find_pane(cmdq, args_get(args, 't'), NULL, &wp) == NULL)
return (CMD_RETURN_ERROR);
}

if (self->entry == &cmd_clock_mode_entry) {
window_pane_set_mode(wp, &window_clock_mode);
Expand Down
Loading

0 comments on commit 4a4daf1

Please sign in to comment.