diff options
Diffstat (limited to 'cmd-find.c')
-rw-r--r-- | cmd-find.c | 143 |
1 files changed, 92 insertions, 51 deletions
@@ -248,7 +248,11 @@ cmd_find_current_session_with_client(struct cmd_find_state *fs) { struct window_pane *wp; - /* If this is running in a pane, that's great. */ + /* + * If this is running in a pane, we can use that to limit the list of + * sessions to those containing that pane (we still use the current + * window in the best session). + */ if (fs->cmdq->client->tty.path != NULL) { RB_FOREACH(wp, window_pane_tree, &all_window_panes) { if (strcmp(wp->tty, fs->cmdq->client->tty.path) == 0) @@ -261,11 +265,8 @@ cmd_find_current_session_with_client(struct cmd_find_state *fs) if (wp == NULL) goto unknown_pane; - /* We now know the window and pane. */ + /* Find the best session and winlink containing this pane. */ fs->w = wp->window; - fs->wp = wp; - - /* Find the best session and winlink. */ if (cmd_find_best_session_with_window(fs) != 0) { if (wp != NULL) { /* @@ -277,6 +278,13 @@ cmd_find_current_session_with_client(struct cmd_find_state *fs) } return (-1); } + + /* Use the current window and pane from this session. */ + fs->wl = fs->s->curw; + fs->idx = fs->wl->idx; + fs->w = fs->wl->window; + fs->wp = fs->w->active; + return (0); unknown_pane: @@ -289,6 +297,7 @@ unknown_pane: fs->idx = fs->wl->idx; fs->w = fs->wl->window; fs->wp = fs->w->active; + return (0); } @@ -791,6 +800,67 @@ cmd_find_clear_state(struct cmd_find_state *fs, struct cmd_q *cmdq, int flags) fs->idx = -1; } +/* Check if a state if valid. */ +int +cmd_find_valid_state(struct cmd_find_state *fs) +{ + struct winlink *wl; + + if (fs->s == NULL || fs->wl == NULL || fs->w == NULL || fs->wp == NULL) + return (0); + + if (!session_alive(fs->s)) + return (0); + + RB_FOREACH(wl, winlinks, &fs->s->windows) { + if (wl->window == fs->w && wl == fs->wl) + break; + } + if (wl == NULL) + return (0); + + if (fs->w != fs->wl->window) + return (0); + + if (!window_has_pane(fs->w, fs->wp)) + return (0); + return (window_pane_visible(fs->wp)); +} + +/* Copy a state. */ +void +cmd_find_copy_state(struct cmd_find_state *dst, struct cmd_find_state *src) +{ + dst->s = src->s; + dst->wl = src->wl; + dst->idx = dst->wl->idx; + dst->w = dst->wl->window; + dst->wp = src->wp; +} + +/* Log the result. */ +void +cmd_find_log_state(const char *prefix, struct cmd_find_state *fs) +{ + if (fs->s != NULL) + log_debug("%s: s=$%u", prefix, fs->s->id); + else + log_debug("%s: s=none", prefix); + if (fs->wl != NULL) { + log_debug("%s: wl=%u %d w=@%u %s", prefix, fs->wl->idx, + fs->wl->window == fs->w, fs->w->id, fs->w->name); + } else + log_debug("%s: wl=none", prefix); + if (fs->wp != NULL) + log_debug("%s: wp=%%%u", prefix, fs->wp->id); + else + log_debug("%s: wp=none", prefix); + if (fs->idx != -1) + log_debug("%s: idx=%d", prefix, fs->idx); + else + log_debug("%s: idx=none", prefix); +} + /* * Split target into pieces and resolve for the given type. Fills in the given * state. Returns 0 on success or -1 on error. @@ -811,24 +881,22 @@ cmd_find_target(struct cmd_find_state *fs, struct cmd_q *cmdq, log_debug("%s: target %s, type %d", __func__, target, type); log_debug("%s: cmdq %p, flags %#x", __func__, cmdq, flags); - /* Find current state. */ - cmd_find_clear_state(¤t, cmdq, flags); - if (server_check_marked() && (flags & CMD_FIND_DEFAULT_MARKED)) { - current.s = marked_session; - current.wl = marked_winlink; - current.idx = current.wl->idx; - current.w = current.wl->window; - current.wp = marked_window_pane; - } - if (current.s == NULL && cmd_find_current_session(¤t) != 0) { - if (~flags & CMD_FIND_QUIET) - cmdq_error(cmdq, "no current session"); - goto error; - } - /* Clear new state. */ cmd_find_clear_state(fs, cmdq, flags); - fs->current = ¤t; + + /* Find current state. */ + fs->current = NULL; + if (server_check_marked() && (flags & CMD_FIND_DEFAULT_MARKED)) + fs->current = &marked_pane; + if (fs->current == NULL) { + cmd_find_clear_state(¤t, cmdq, flags); + if (cmd_find_current_session(¤t) != 0) { + if (~flags & CMD_FIND_QUIET) + cmdq_error(cmdq, "no current session"); + goto error; + } + fs->current = ¤t; + } /* An empty or NULL target is the current. */ if (target == NULL || *target == '\0') @@ -867,11 +935,7 @@ cmd_find_target(struct cmd_find_state *fs, struct cmd_q *cmdq, cmdq_error(cmdq, "no marked target"); goto error; } - fs->s = marked_session; - fs->wl = marked_winlink; - fs->idx = fs->wl->idx; - fs->w = fs->wl->window; - fs->wp = marked_window_pane; + cmd_find_copy_state(fs, &marked_pane); goto found; } @@ -1032,9 +1096,9 @@ cmd_find_target(struct cmd_find_state *fs, struct cmd_q *cmdq, current: /* Use the current session. */ + cmd_find_copy_state(fs, fs->current); if (flags & CMD_FIND_WINDOW_INDEX) - current.idx = -1; - memcpy(fs, ¤t, sizeof *fs); + fs->idx = -1; goto found; error: @@ -1067,29 +1131,6 @@ no_pane: goto error; } -/* Log the result. */ -void -cmd_find_log_state(const char *prefix, struct cmd_find_state *fs) -{ - if (fs->s != NULL) - log_debug("%s: s=$%u", prefix, fs->s->id); - else - log_debug("%s: s=none", prefix); - if (fs->wl != NULL) { - log_debug("%s: wl=%u %d w=@%u %s", prefix, fs->wl->idx, - fs->wl->window == fs->w, fs->w->id, fs->w->name); - } else - log_debug("%s: wl=none", prefix); - if (fs->wp != NULL) - log_debug("%s: wp=%%%u", prefix, fs->wp->id); - else - log_debug("%s: wp=none", prefix); - if (fs->idx != -1) - log_debug("%s: idx=%d", prefix, fs->idx); - else - log_debug("%s: idx=none", prefix); -} - /* 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) |