diff --git a/cmd-find.c b/cmd-find.c index f85f0707..8059957b 100644 --- a/cmd-find.c +++ b/cmd-find.c @@ -27,31 +27,6 @@ #include "tmux.h" -#define CMD_FIND_PREFER_UNATTACHED 0x1 -#define CMD_FIND_QUIET 0x2 -#define CMD_FIND_WINDOW_INDEX 0x4 -#define CMD_FIND_DEFAULT_MARKED 0x8 -#define CMD_FIND_EXACT_SESSION 0x10 -#define CMD_FIND_EXACT_WINDOW 0x20 - -enum cmd_find_type { - CMD_FIND_PANE, - CMD_FIND_WINDOW, - CMD_FIND_SESSION, -}; - -struct cmd_find_state { - struct cmd_q *cmdq; - int flags; - struct cmd_find_state *current; - - struct session *s; - struct winlink *wl; - struct window *w; - struct window_pane *wp; - int idx; -}; - struct session *cmd_find_try_TMUX(struct client *, struct window *); int cmd_find_client_better(struct client *, struct client *); struct client *cmd_find_best_client(struct client **, u_int); @@ -78,9 +53,6 @@ int cmd_find_get_pane_with_window(struct cmd_find_state *, const char *); void cmd_find_clear_state(struct cmd_find_state *, struct cmd_q *, int); void cmd_find_log_state(const char *, struct cmd_find_state *); -struct cmd_find_state *cmd_find_target(struct cmd_q *, const char *, - enum cmd_find_type, int); - const char *cmd_find_session_table[][2] = { { NULL, NULL } }; @@ -1112,106 +1084,6 @@ cmd_find_log_state(const char *prefix, struct cmd_find_state *fs) log_debug("%s: idx=none", prefix); } -/* Find the current session. */ -struct session * -cmd_find_current(struct cmd_q *cmdq) -{ - struct cmd_find_state *fs; - int flags = CMD_FIND_QUIET; - - fs = cmd_find_target(cmdq, NULL, CMD_FIND_SESSION, flags); - if (fs == NULL) - return (NULL); - - return (fs->s); -} - -/* Find the target session or report an error and return NULL. */ -struct session * -cmd_find_session(struct cmd_q *cmdq, const char *target, int prefer_unattached) -{ - struct cmd_find_state *fs; - int flags = 0; - - if (prefer_unattached) - flags |= CMD_FIND_PREFER_UNATTACHED; - - fs = cmd_find_target(cmdq, target, CMD_FIND_SESSION, flags); - if (fs == NULL) - return (NULL); - - return (fs->s); -} - -/* Find the target window or report an error and return NULL. */ -struct winlink * -cmd_find_window(struct cmd_q *cmdq, const char *target, struct session **sp) -{ - struct cmd_find_state *fs; - - fs = cmd_find_target(cmdq, target, CMD_FIND_WINDOW, 0); - if (fs == NULL) - return (NULL); - - if (sp != NULL) - *sp = fs->s; - return (fs->wl); -} - -/* Find the target window, defaulting to marked rather than current. */ -struct winlink * -cmd_find_window_marked(struct cmd_q *cmdq, const char *target, - struct session **sp) -{ - struct cmd_find_state *fs; - int flags = CMD_FIND_DEFAULT_MARKED; - - fs = cmd_find_target(cmdq, target, CMD_FIND_WINDOW, flags); - if (fs == NULL) - return (NULL); - - if (sp != NULL) - *sp = fs->s; - return (fs->wl); -} - -/* Find the target pane and report an error and return NULL. */ -struct winlink * -cmd_find_pane(struct cmd_q *cmdq, const char *target, struct session **sp, - struct window_pane **wpp) -{ - struct cmd_find_state *fs; - - fs = cmd_find_target(cmdq, target, CMD_FIND_PANE, 0); - if (fs == NULL) - return (NULL); - - if (sp != NULL) - *sp = fs->s; - if (wpp != NULL) - *wpp = fs->wp; - return (fs->wl); -} - -/* Find the target pane, defaulting to marked rather than current. */ -struct winlink * -cmd_find_pane_marked(struct cmd_q *cmdq, const char *target, - struct session **sp, struct window_pane **wpp) -{ - struct cmd_find_state *fs; - int flags = CMD_FIND_DEFAULT_MARKED; - - fs = cmd_find_target(cmdq, target, CMD_FIND_PANE, flags); - if (fs == NULL) - return (NULL); - - if (sp != NULL) - *sp = fs->s; - if (wpp != NULL) - *wpp = fs->wp; - return (fs->wl); -} - /* Find the target client or report an error and return NULL. */ struct client * cmd_find_client(struct cmd_q *cmdq, const char *target, int quiet) @@ -1261,24 +1133,3 @@ cmd_find_client(struct cmd_q *cmdq, const char *target, int quiet) log_debug("%s: target %s, return %p", __func__, target, c); return (c); } - -/* - * Find the target session and window index, whether or not it exists in the - * session. Return -2 on error or -1 if no window index is specified. This is - * used when parsing an argument for a window target that may not exist (for - * example if it is going to be created). - */ -int -cmd_find_index(struct cmd_q *cmdq, const char *target, struct session **sp) -{ - struct cmd_find_state *fs; - int flags = CMD_FIND_WINDOW_INDEX; - - fs = cmd_find_target(cmdq, target, CMD_FIND_WINDOW, flags); - if (fs == NULL) - return (-2); - - if (sp != NULL) - *sp = fs->s; - return (fs->idx); -} diff --git a/cmd.c b/cmd.c index fc483740..0bb804ef 100644 --- a/cmd.c +++ b/cmd.c @@ -433,13 +433,14 @@ cmd_set_state_flag(struct cmd *cmd, struct cmd_q *cmdq, char c) struct cmd_state_flag *statef = NULL; const char *flag; int flags = cmd->entry->flags, everything = 0; - int allflags = 0; - int prefer = !!(flags & CMD_PREFERUNATTACHED); + int allflags = 0, targetflags; struct session *s; struct window *w; struct winlink *wl; struct window_pane *wp; + struct cmd_find_state *fs; + /* Set up state for either -t or -s. */ if (c == 't') { statef = &cmdq->state.tflag; allflags = CMD_ALL_T; @@ -469,26 +470,35 @@ cmd_set_state_flag(struct cmd *cmd, struct cmd_q *cmdq, char c) goto complete_everything; /* Fill in state using command (current or base) flags. */ + if (flags & CMD_PREFERUNATTACHED) + targetflags = CMD_FIND_PREFER_UNATTACHED; + else + targetflags = 0; switch (cmd->entry->flags & allflags) { case 0: break; case CMD_SESSION_T|CMD_PANE_T: case CMD_SESSION_S|CMD_PANE_S: if (flag != NULL && flag[strcspn(flag, ":.")] != '\0') { - statef->wl = cmd_find_pane(cmdq, flag, &statef->s, - &statef->wp); - if (statef->wl == NULL) + fs = cmd_find_target(cmdq, flag, CMD_FIND_PANE, + targetflags); + if (fs == NULL) return (-1); + statef->s = fs->s; + statef->wl = fs->wl; + statef->wp = fs->wp; } else { - statef->s = cmd_find_session(cmdq, flag, prefer); - if (statef->s == NULL) + fs = cmd_find_target(cmdq, flag, CMD_FIND_SESSION, + targetflags); + if (fs == NULL) return (-1); + statef->s = fs->s; - s = statef->s; if (flag == NULL) { - statef->wl = s->curw; - statef->wp = s->curw->window->active; + statef->wl = statef->s->curw; + statef->wp = statef->s->curw->window->active; } else { + s = statef->s; if ((w = window_find_by_id_str(flag)) != NULL) wp = w->active; else { @@ -506,50 +516,58 @@ cmd_set_state_flag(struct cmd *cmd, struct cmd_q *cmdq, char c) break; case CMD_MOVEW_R|CMD_INDEX_T: case CMD_MOVEW_R|CMD_INDEX_S: - statef->s = cmd_find_session(cmdq, flag, prefer); - if (statef->s == NULL) { - statef->idx = cmd_find_index(cmdq, flag, &statef->s); - if (statef->idx == -2) + fs = cmd_find_target(cmdq, flag, CMD_FIND_SESSION, targetflags); + if (fs != NULL) + statef->s = fs->s; + else { + fs = cmd_find_target(cmdq, flag, CMD_FIND_WINDOW, + CMD_FIND_WINDOW_INDEX); + if (fs == NULL) return (-1); + statef->s = fs->s; + statef->idx = fs->idx; } break; case CMD_SESSION_T: case CMD_SESSION_S: - statef->s = cmd_find_session(cmdq, flag, prefer); - if (statef->s == NULL) - return (-1); - break; - case CMD_WINDOW_T: - case CMD_WINDOW_S: - statef->wl = cmd_find_window(cmdq, flag, &statef->s); - if (statef->wl == NULL) + fs = cmd_find_target(cmdq, flag, CMD_FIND_SESSION, targetflags); + if (fs == NULL) return (-1); + statef->s = fs->s; break; case CMD_WINDOW_MARKED_T: case CMD_WINDOW_MARKED_S: - statef->wl = cmd_find_window_marked(cmdq, flag, &statef->s); - if (statef->wl == NULL) - return (-1); - break; - case CMD_PANE_T: - case CMD_PANE_S: - statef->wl = cmd_find_pane(cmdq, flag, &statef->s, - &statef->wp); - if (statef->wl == NULL) + targetflags |= CMD_FIND_DEFAULT_MARKED; + /* FALLTHROUGH */ + case CMD_WINDOW_T: + case CMD_WINDOW_S: + fs = cmd_find_target(cmdq, flag, CMD_FIND_WINDOW, targetflags); + if (fs == NULL) return (-1); + statef->s = fs->s; + statef->wl = fs->wl; break; case CMD_PANE_MARKED_T: case CMD_PANE_MARKED_S: - statef->wl = cmd_find_pane_marked(cmdq, flag, &statef->s, - &statef->wp); - if (statef->wl == NULL) + targetflags |= CMD_FIND_DEFAULT_MARKED; + /* FALLTHROUGH */ + case CMD_PANE_T: + case CMD_PANE_S: + fs = cmd_find_target(cmdq, flag, CMD_FIND_PANE, targetflags); + if (fs == NULL) return (-1); + statef->s = fs->s; + statef->wl = fs->wl; + statef->wp = fs->wp; break; case CMD_INDEX_T: case CMD_INDEX_S: - statef->idx = cmd_find_index(cmdq, flag, &statef->s); - if (statef->idx == -2) + fs = cmd_find_target(cmdq, flag, CMD_FIND_WINDOW, + CMD_FIND_WINDOW_INDEX); + if (fs == NULL) return (-1); + statef->s = fs->s; + statef->idx = fs->idx; break; default: fatalx("too many -%c for %s", c, cmd->entry->name); @@ -567,21 +585,34 @@ complete_everything: if (statef->s == NULL) { if (state->c != NULL) statef->s = state->c->session; - if (statef->s == NULL) - statef->s = cmd_find_current(cmdq); + if (statef->s == NULL) { + fs = cmd_find_target(cmdq, NULL, CMD_FIND_SESSION, + CMD_FIND_QUIET); + if (fs != NULL) + statef->s = fs->s; + } if (statef->s == NULL) { if (flags & CMD_CANFAIL) return (0); - cmdq_error(cmdq, "no current session"); return (-1); } } - if (statef->wl == NULL) - statef->wl = cmd_find_window(cmdq, flag, &statef->s); - if (statef->wp == NULL) - statef->wl = cmd_find_pane(cmdq, flag, &statef->s, &statef->wp); - + if (statef->wl == NULL) { + fs = cmd_find_target(cmdq, flag, CMD_FIND_WINDOW, 0); + if (fs != NULL) { + statef->s = fs->s; + statef->wl = fs->wl; + } + } + if (statef->wp == NULL) { + fs = cmd_find_target(cmdq, flag, CMD_FIND_PANE, 0); + if (fs != NULL) { + statef->s = fs->s; + statef->wl = fs->wl; + statef->wp = fs->wp; + } + } return (0); } diff --git a/tmux.h b/tmux.h index 5924470e..7a30fc71 100644 --- a/tmux.h +++ b/tmux.h @@ -1407,6 +1407,32 @@ struct cmd_entry { #define CMD_ALL_S (CMD_SESSION_S|CMD_WINDOW_S|CMD_PANE_S|CMD_INDEX_S| \ CMD_PANE_MARKED_S|CMD_WINDOW_MARKED_S) +/* Command find structures. */ +enum cmd_find_type { + CMD_FIND_PANE, + CMD_FIND_WINDOW, + CMD_FIND_SESSION, +}; +struct cmd_find_state { + struct cmd_q *cmdq; + int flags; + struct cmd_find_state *current; + + struct session *s; + struct winlink *wl; + struct window *w; + struct window_pane *wp; + int idx; +}; + +/* Command fine flags. */ +#define CMD_FIND_PREFER_UNATTACHED 0x1 +#define CMD_FIND_QUIET 0x2 +#define CMD_FIND_WINDOW_INDEX 0x4 +#define CMD_FIND_DEFAULT_MARKED 0x8 +#define CMD_FIND_EXACT_SESSION 0x10 +#define CMD_FIND_EXACT_WINDOW 0x20 + /* Key binding and key table. */ struct key_binding { key_code key; @@ -1734,19 +1760,9 @@ long long args_strtonum(struct args *, u_char, long long, long long, char **); /* cmd-find.c */ -struct session *cmd_find_current(struct cmd_q *); -struct session *cmd_find_session(struct cmd_q *, const char *, int); -struct winlink *cmd_find_window(struct cmd_q *, const char *, - struct session **); -struct winlink *cmd_find_window_marked(struct cmd_q *, const char *, - struct session **); -struct winlink *cmd_find_pane(struct cmd_q *, const char *, struct session **, - struct window_pane **); -struct winlink *cmd_find_pane_marked(struct cmd_q *, const char *, - struct session **, struct window_pane **); +struct cmd_find_state *cmd_find_target(struct cmd_q *, const char *, + enum cmd_find_type, int); struct client *cmd_find_client(struct cmd_q *, const char *, int); -int cmd_find_index(struct cmd_q *, const char *, - struct session **); /* cmd.c */ int cmd_pack_argv(int, char **, char *, size_t);