From 12da13c9d1e0e015ed19761e352ee6b877b23aa4 Mon Sep 17 00:00:00 2001 From: nicm Date: Tue, 15 Dec 2015 00:00:01 +0000 Subject: [PATCH 1/4] Make the marked pane a cmd_find_state. --- cmd-find.c | 109 +++++++++++++++++++++++++++++----------------- cmd-select-pane.c | 4 +- screen-redraw.c | 4 +- server.c | 56 +++++++----------------- tmux.h | 7 +-- window.c | 4 +- 6 files changed, 95 insertions(+), 89 deletions(-) diff --git a/cmd-find.c b/cmd-find.c index 8e118c00..b7728ea2 100644 --- a/cmd-find.c +++ b/cmd-find.c @@ -792,6 +792,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. @@ -814,17 +875,14 @@ cmd_find_target(struct cmd_find_state *fs, struct cmd_q *cmdq, /* 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; + if (server_check_marked() && (flags & CMD_FIND_DEFAULT_MARKED)) + cmd_find_copy_state(¤t, &marked_pane); + else { + if (cmd_find_current_session(¤t) != 0) { + if (~flags & CMD_FIND_QUIET) + cmdq_error(cmdq, "no current session"); + goto error; + } } /* Clear new state. */ @@ -868,11 +926,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; } @@ -1068,29 +1122,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) diff --git a/cmd-select-pane.c b/cmd-select-pane.c index 7652f608..6ebe753c 100644 --- a/cmd-select-pane.c +++ b/cmd-select-pane.c @@ -94,13 +94,13 @@ cmd_select_pane_exec(struct cmd *self, struct cmd_q *cmdq) if (args_has(args, 'm') || args_has(args, 'M')) { if (args_has(args, 'm') && !window_pane_visible(wp)) return (CMD_RETURN_NORMAL); - lastwp = marked_window_pane; + lastwp = marked_pane.wp; if (args_has(args, 'M') || server_is_marked(s, wl, wp)) server_clear_marked(); else server_set_marked(s, wl, wp); - markedwp = marked_window_pane; + markedwp = marked_pane.wp; if (lastwp != NULL) { server_redraw_window_borders(lastwp->window); diff --git a/screen-redraw.c b/screen-redraw.c index fd4536d0..9958d04a 100644 --- a/screen-redraw.c +++ b/screen-redraw.c @@ -331,9 +331,9 @@ screen_redraw_draw_borders(struct client *c, int status, u_int top) continue; active = screen_redraw_check_is(i, j, type, w, w->active, wp); - if (server_is_marked(s, s->curw, marked_window_pane) && + if (server_is_marked(s, s->curw, marked_pane.wp) && screen_redraw_check_is(i, j, type, w, - marked_window_pane, wp)) { + marked_pane.wp, wp)) { if (active) tty_attributes(tty, &m_active_gc, NULL); else diff --git a/server.c b/server.c index c45e8a2c..0a1146f3 100644 --- a/server.c +++ b/server.c @@ -41,18 +41,14 @@ * Main server functions. */ -struct clients clients; +struct clients clients; -struct tmuxproc *server_proc; -int server_fd; -int server_exit; -struct event server_ev_accept; +struct tmuxproc *server_proc; +int server_fd; +int server_exit; +struct event server_ev_accept; -struct session *marked_session; -struct winlink *marked_winlink; -struct window *marked_window; -struct window_pane *marked_window_pane; -struct layout_cell *marked_layout_cell; +struct cmd_find_state marked_pane; int server_create_socket(void); int server_loop(void); @@ -68,22 +64,18 @@ void server_child_stopped(pid_t, int); void server_set_marked(struct session *s, struct winlink *wl, struct window_pane *wp) { - marked_session = s; - marked_winlink = wl; - marked_window = wl->window; - marked_window_pane = wp; - marked_layout_cell = wp->layout_cell; + cmd_find_clear_state(&marked_pane, NULL, 0); + marked_pane.s = s; + marked_pane.wl = wl; + marked_pane.w = wl->window; + marked_pane.wp = wp; } /* Clear marked pane. */ void server_clear_marked(void) { - marked_session = NULL; - marked_winlink = NULL; - marked_window = NULL; - marked_window_pane = NULL; - marked_layout_cell = NULL; + cmd_find_clear_state(&marked_pane, NULL, 0); } /* Is this the marked pane? */ @@ -92,9 +84,9 @@ server_is_marked(struct session *s, struct winlink *wl, struct window_pane *wp) { if (s == NULL || wl == NULL || wp == NULL) return (0); - if (marked_session != s || marked_winlink != wl) + if (marked_pane.s != s || marked_pane.wl != wl) return (0); - if (marked_window_pane != wp) + if (marked_pane.wp != wp) return (0); return (server_check_marked()); } @@ -103,25 +95,7 @@ server_is_marked(struct session *s, struct winlink *wl, struct window_pane *wp) int server_check_marked(void) { - struct winlink *wl; - - if (marked_window_pane == NULL) - return (0); - if (marked_layout_cell != marked_window_pane->layout_cell) - return (0); - - if (!session_alive(marked_session)) - return (0); - RB_FOREACH(wl, winlinks, &marked_session->windows) { - if (wl->window == marked_window && wl == marked_winlink) - break; - } - if (wl == NULL) - return (0); - - if (!window_has_pane(marked_window, marked_window_pane)) - return (0); - return (window_pane_visible(marked_window_pane)); + return (cmd_find_valid_state(&marked_pane)); } /* Create server socket. */ diff --git a/tmux.h b/tmux.h index c6bd48cf..651990f0 100644 --- a/tmux.h +++ b/tmux.h @@ -1766,6 +1766,9 @@ int cmd_find_target(struct cmd_find_state *, struct cmd_q *, struct client *cmd_find_client(struct cmd_q *, const char *, int); void cmd_find_clear_state(struct cmd_find_state *, struct cmd_q *, int); +int cmd_find_valid_state(struct cmd_find_state *); +void cmd_find_copy_state(struct cmd_find_state *, + struct cmd_find_state *); void cmd_find_log_state(const char *, struct cmd_find_state *); /* cmd.c */ @@ -1844,9 +1847,7 @@ void alerts_check_session(struct session *); /* server.c */ extern struct tmuxproc *server_proc; extern struct clients clients; -extern struct session *marked_session; -extern struct winlink *marked_winlink; -extern struct window_pane *marked_window_pane; +extern struct cmd_find_state marked_pane; void server_set_marked(struct session *, struct winlink *, struct window_pane *); void server_clear_marked(void); diff --git a/window.c b/window.c index 75b99672..f0e57190 100644 --- a/window.c +++ b/window.c @@ -568,7 +568,7 @@ window_add_pane(struct window *w, u_int hlimit) void window_lost_pane(struct window *w, struct window_pane *wp) { - if (wp == marked_window_pane) + if (wp == marked_pane.wp) server_clear_marked(); if (wp == w->active) { @@ -691,7 +691,7 @@ window_printable_flags(struct session *s, struct winlink *wl) flags[pos++] = '*'; if (wl == TAILQ_FIRST(&s->lastw)) flags[pos++] = '-'; - if (server_check_marked() && wl == marked_winlink) + if (server_check_marked() && wl == marked_pane.wl) flags[pos++] = 'M'; if (wl->window->flags & WINDOW_ZOOMED) flags[pos++] = 'Z'; From 56d097cfe053f6cd50db9607503c25a80e3cd989 Mon Sep 17 00:00:00 2001 From: nicm Date: Tue, 15 Dec 2015 00:11:24 +0000 Subject: [PATCH 2/4] Don't copy marked pane when can just point to it. --- cmd-find.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/cmd-find.c b/cmd-find.c index b7728ea2..ea1389a9 100644 --- a/cmd-find.c +++ b/cmd-find.c @@ -873,22 +873,23 @@ 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); + /* Clear new state. */ + cmd_find_clear_state(fs, cmdq, flags); + /* Find current state. */ - cmd_find_clear_state(¤t, cmdq, flags); + fs->current = NULL; if (server_check_marked() && (flags & CMD_FIND_DEFAULT_MARKED)) - cmd_find_copy_state(¤t, &marked_pane); - else { + 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; } - /* Clear new state. */ - cmd_find_clear_state(fs, cmdq, flags); - fs->current = ¤t; - /* An empty or NULL target is the current. */ if (target == NULL || *target == '\0') goto current; @@ -1087,9 +1088,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: From 6f417ec9438de4440522483be02c51803d628313 Mon Sep 17 00:00:00 2001 From: nicm Date: Tue, 15 Dec 2015 00:45:02 +0000 Subject: [PATCH 3/4] We changed somewhat recently to us the pty when tmux was run inside itself to work out the current pane. This is confusing in many cases (particularly notable is that "tmux neww\; splitw" would not split the new window), and the few advantages do not make up for the confusion. So drop this behaviour and return to using the current window and pane; keep the pty check but only use it to limit the list of possible current sessions. --- cmd-find.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/cmd-find.c b/cmd-find.c index ea1389a9..dbfe99ef 100644 --- a/cmd-find.c +++ b/cmd-find.c @@ -249,7 +249,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) @@ -262,11 +266,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) { /* @@ -278,6 +279,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: @@ -290,6 +298,7 @@ unknown_pane: fs->idx = fs->wl->idx; fs->w = fs->wl->window; fs->wp = fs->w->active; + return (0); } From 9d88d82d5e8caa7882a28ac95fda19754e5553e7 Mon Sep 17 00:00:00 2001 From: nicm Date: Tue, 15 Dec 2015 00:52:17 +0000 Subject: [PATCH 4/4] Allow list-keys and list-commands to be run without a running server. --- cmd-list-keys.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd-list-keys.c b/cmd-list-keys.c index 08796e5d..4abe2473 100644 --- a/cmd-list-keys.c +++ b/cmd-list-keys.c @@ -39,7 +39,7 @@ const struct cmd_entry cmd_list_keys_entry = { .args = { "t:T:", 0, 0 }, .usage = "[-t mode-table] [-T key-table]", - .flags = 0, + .flags = CMD_STARTSERVER, .exec = cmd_list_keys_exec }; @@ -50,7 +50,7 @@ const struct cmd_entry cmd_list_commands_entry = { .args = { "", 0, 0 }, .usage = "", - .flags = 0, + .flags = CMD_STARTSERVER, .exec = cmd_list_keys_exec };