diff --git a/cmd-find.c b/cmd-find.c index 07a3c7a3..3d129875 100644 --- a/cmd-find.c +++ b/cmd-find.c @@ -50,6 +50,7 @@ struct cmd_find_state { 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); int cmd_find_session_better(struct session *, struct session *, @@ -108,6 +109,33 @@ const char *cmd_find_pane_table[][2] = { { NULL, NULL } }; +/* Get session from TMUX if present. */ +struct session * +cmd_find_try_TMUX(struct client *c, struct window *w) +{ + struct environ_entry *envent; + char tmp[256]; + long long pid; + u_int session; + struct session *s; + + envent = environ_find(&c->environ, "TMUX"); + if (envent == NULL) + return (NULL); + + if (sscanf(envent->value, "%255[^,],%lld,%d", tmp, &pid, &session) != 3) + return (NULL); + if (pid != getpid()) + return (NULL); + log_debug("client %d TMUX is %s (session @%u)", c->ibuf.fd, + envent->value, session); + + s = session_find_by_id(session); + if (s == NULL || (w != NULL && !session_has(s, w))) + return (NULL); + return (s); +} + /* Is this client better? */ int cmd_find_client_better(struct client *c, struct client *than) @@ -191,6 +219,12 @@ cmd_find_best_session_with_window(struct cmd_find_state *fs) u_int ssize; struct session *s; + if (fs->cmdq->client != NULL) { + fs->s = cmd_find_try_TMUX(fs->cmdq->client, fs->w); + if (fs->s != NULL) + return (cmd_find_best_winlink_with_window(fs)); + } + ssize = 0; RB_FOREACH(s, sessions, &sessions) { if (!session_has(s, fs->w)) @@ -276,7 +310,9 @@ cmd_find_current_session_with_client(struct cmd_find_state *fs) return (0); unknown_pane: - fs->s = cmd_find_best_session(NULL, 0, fs->flags); + fs->s = cmd_find_try_TMUX(fs->cmdq->client, NULL); + if (fs->s == NULL) + fs->s = cmd_find_best_session(NULL, 0, fs->flags); if (fs->s == NULL) return (-1); fs->wl = fs->s->curw; diff --git a/cmd-new-session.c b/cmd-new-session.c index 7687398e..b3bf3c74 100644 --- a/cmd-new-session.c +++ b/cmd-new-session.c @@ -133,7 +133,7 @@ cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq) strerror(errno)); return (CMD_RETURN_ERROR); } - } else if (cp != NULL) + } else free(cp); cwd = fd; } else if (c != NULL && c->session == NULL) diff --git a/cmd-new-window.c b/cmd-new-window.c index 9cead449..893fe6e2 100644 --- a/cmd-new-window.c +++ b/cmd-new-window.c @@ -107,7 +107,7 @@ cmd_new_window_exec(struct cmd *self, struct cmd_q *cmdq) strerror(errno)); return (CMD_RETURN_ERROR); } - } else if (cp != NULL) + } else free(cp); cwd = fd; } else if (cmdq->client != NULL && cmdq->client->session == NULL) diff --git a/cmd-split-window.c b/cmd-split-window.c index 50799cff..99da5f1c 100644 --- a/cmd-split-window.c +++ b/cmd-split-window.c @@ -100,7 +100,7 @@ cmd_split_window_exec(struct cmd *self, struct cmd_q *cmdq) strerror(errno)); return (CMD_RETURN_ERROR); } - } else if (cp != NULL) + } else free(cp); cwd = fd; } else if (cmdq->client != NULL && cmdq->client->session == NULL) diff --git a/format.c b/format.c index acfcb531..4b15e0fa 100644 --- a/format.c +++ b/format.c @@ -1022,7 +1022,7 @@ format_defaults_pane(struct format_tree *ft, struct window_pane *wp) { struct grid *gd = wp->base.grid; u_int idx; - int status; + int status, scroll_position; if (ft->w == NULL) ft->w = wp->window; @@ -1070,6 +1070,10 @@ format_defaults_pane(struct format_tree *ft, struct window_pane *wp) format_add(ft, "scroll_region_upper", "%u", wp->base.rupper); format_add(ft, "scroll_region_lower", "%u", wp->base.rlower); + scroll_position = window_copy_scroll_position(wp); + if (scroll_position != -1) + format_add(ft, "scroll_position", "%d", scroll_position); + format_add(ft, "alternate_on", "%d", wp->saved_grid ? 1 : 0); format_add(ft, "alternate_saved_x", "%u", wp->saved_cx); format_add(ft, "alternate_saved_y", "%u", wp->saved_cy); diff --git a/server-client.c b/server-client.c index 960c5630..08bf5024 100644 --- a/server-client.c +++ b/server-client.c @@ -597,7 +597,7 @@ server_client_handle_key(struct client *c, int key) m->valid = 0; /* Treat everything as a regular key when pasting is detected. */ - if (server_client_assume_paste(s)) { + if (!KEYC_IS_MOUSE(key) && server_client_assume_paste(s)) { if (!(c->flags & CLIENT_READONLY)) window_pane_key(wp, c, s, key, m); return; diff --git a/server.c b/server.c index 8664bd06..139505d4 100644 --- a/server.c +++ b/server.c @@ -177,6 +177,10 @@ server_start(struct event_base *base, int lockfd, char *lockfile) } close(pair[0]); + if (pledge("stdio rpath wpath cpath fattr unix recvfd proc exec tty " + "ps", NULL) != 0) + fatal("pledge failed"); + /* * Must daemonise before loading configuration as the PID changes so * $TMUX would be wrong for sessions created in the config file. diff --git a/tmux.1 b/tmux.1 index 4fdb7f77..5d21caab 100644 --- a/tmux.1 +++ b/tmux.1 @@ -3411,6 +3411,7 @@ The following variables are available, where appropriate: .It Li "pid" Ta "" Ta "Server PID" .It Li "scroll_region_lower" Ta "" Ta "Bottom of scroll region in pane" .It Li "scroll_region_upper" Ta "" Ta "Top of scroll region in pane" +.It Li "scroll_position" Ta "" Ta "Scroll position in copy mode" .It Li "session_alerts" Ta "" Ta "List of window indexes with alerts" .It Li "session_attached" Ta "" Ta "Number of clients session is attached to" .It Li "session_activity" Ta "" Ta "Integer time of session last activity" diff --git a/tmux.c b/tmux.c index dff7952c..6e1de6f6 100644 --- a/tmux.c +++ b/tmux.c @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -260,6 +261,10 @@ main(int argc, char **argv) if (shell_cmd != NULL && argc != 0) usage(); + if (pledge("stdio rpath wpath cpath flock fattr unix sendfd recvfd " + "proc exec tty ps", NULL) != 0) + err(1, "pledge"); + if (!(flags & CLIENT_UTF8)) { /* * If the user has set whichever of LC_ALL, LC_CTYPE or LANG diff --git a/tmux.h b/tmux.h index 15d08611..001d2929 100644 --- a/tmux.h +++ b/tmux.h @@ -2083,6 +2083,7 @@ void printflike(2, 3) window_copy_add(struct window_pane *, const char *, ...); void window_copy_vadd(struct window_pane *, const char *, va_list); void window_copy_pageup(struct window_pane *); void window_copy_start_drag(struct client *, struct mouse_event *); +int window_copy_scroll_position(struct window_pane *); /* window-choose.c */ extern const struct window_mode window_choose_mode; diff --git a/tty.c b/tty.c index 7be952c8..3d6f29ca 100644 --- a/tty.c +++ b/tty.c @@ -228,7 +228,7 @@ tty_start_tty(struct tty *tty) if (tty_term_has(tty->term, TTYC_KMOUS)) tty_puts(tty, "\033[?1000l\033[?1002l\033[?1006l\033[?1005l"); - if (tty_term_has(tty->term, TTYC_XT)) { + if (tty_term_flag(tty->term, TTYC_XT)) { if (options_get_number(&global_options, "focus-events")) { tty->flags |= TTY_FOCUS; tty_puts(tty, "\033[?1004h"); @@ -293,7 +293,7 @@ tty_stop_tty(struct tty *tty) if (tty_term_has(tty->term, TTYC_KMOUS)) tty_raw(tty, "\033[?1000l\033[?1002l\033[?1006l\033[?1005l"); - if (tty_term_has(tty->term, TTYC_XT)) { + if (tty_term_flag(tty->term, TTYC_XT)) { if (tty->flags & TTY_FOCUS) { tty->flags &= ~TTY_FOCUS; tty_raw(tty, "\033[?1004l"); @@ -338,10 +338,8 @@ tty_free(struct tty *tty) tty_close(tty); free(tty->ccolour); - if (tty->path != NULL) - free(tty->path); - if (tty->termname != NULL) - free(tty->termname); + free(tty->path); + free(tty->termname); } void @@ -1647,6 +1645,13 @@ tty_try_256(struct tty *tty, u_char colour, const char *type) { char s[32]; + /* + * If the user has specified -2 to the client, setaf and setab may not + * work (or they may not want to use them), so send the usual sequence. + */ + if (tty->term_flags & TERM_256COLOURS) + goto fallback; + /* * If the terminfo entry has 256 colours and setaf and setab exist, * assume that they work correctly. @@ -1664,13 +1669,6 @@ tty_try_256(struct tty *tty, u_char colour, const char *type) return (0); } - /* - * If the user has specified -2 to the client, setaf and setab may not - * work, so send the usual sequence. - */ - if (tty->term_flags & TERM_256COLOURS) - goto fallback; - return (-1); fallback: diff --git a/window-copy.c b/window-copy.c index c7d360de..f81a2a16 100644 --- a/window-copy.c +++ b/window-copy.c @@ -2219,6 +2219,16 @@ window_copy_scroll_down(struct window_pane *wp, u_int ny) screen_write_stop(&ctx); } +int +window_copy_scroll_position(struct window_pane *wp) +{ + struct window_copy_mode_data *data = wp->modedata; + + if (wp->mode != &window_copy_mode) + return (-1); + return (data->oy); +} + void window_copy_rectangle_toggle(struct window_pane *wp) {