aboutsummaryrefslogtreecommitdiff
path: root/cmd-find.c
diff options
context:
space:
mode:
authorThomas Adam <thomas@xteddy.org>2015-12-15 02:01:14 +0000
committerThomas Adam <thomas@xteddy.org>2015-12-15 02:01:14 +0000
commit1a33ea9671895258cee37cf89a209e56ecf578fb (patch)
tree73ab6a6db7131c90bfeba60813423bbe316b31da /cmd-find.c
parent6ab17e3e15f8cbaf12fa3a9ea038a4e65f19ba85 (diff)
parent9d88d82d5e8caa7882a28ac95fda19754e5553e7 (diff)
downloadrtmux-1a33ea9671895258cee37cf89a209e56ecf578fb.tar.gz
rtmux-1a33ea9671895258cee37cf89a209e56ecf578fb.tar.bz2
rtmux-1a33ea9671895258cee37cf89a209e56ecf578fb.zip
Merge branch 'obsd-master'
Diffstat (limited to 'cmd-find.c')
-rw-r--r--cmd-find.c143
1 files changed, 92 insertions, 51 deletions
diff --git a/cmd-find.c b/cmd-find.c
index 813bfbc9..959b7111 100644
--- a/cmd-find.c
+++ b/cmd-find.c
@@ -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(&current, 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(&current) != 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 = &current;
+
+ /* 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(&current, cmdq, flags);
+ if (cmd_find_current_session(&current) != 0) {
+ if (~flags & CMD_FIND_QUIET)
+ cmdq_error(cmdq, "no current session");
+ goto error;
+ }
+ fs->current = &current;
+ }
/* 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, &current, 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)