summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNicholas Marriott <nicholas.marriott@gmail.com>2011-04-06 22:24:01 +0000
committerNicholas Marriott <nicholas.marriott@gmail.com>2011-04-06 22:24:01 +0000
commit4e452a2e11af9bf6cb8582d141c041a72e933c01 (patch)
treee83b35f9da3bd480c83f61b9c320419c65fa4849
parentae5954d30a049ff2be00061e36c72219ea60afc4 (diff)
|PatchSet 882
|Date: 2011/04/05 20:37:01 |Author: nicm |Branch: HEAD |Tag: (none) |Log: |Add a flag to cmd_find_session so that attach-session can prefer |unattached sessions when choosing the most recently used (if -t is not |given). Suggested by claudio@.
-rw-r--r--cmd-attach-session.c4
-rw-r--r--cmd-has-session.c4
-rw-r--r--cmd-kill-session.c4
-rw-r--r--cmd-list-panes.c4
-rw-r--r--cmd-list-windows.c4
-rw-r--r--cmd-lock-server.c4
-rw-r--r--cmd-new-session.c4
-rw-r--r--cmd-rename-session.c4
-rw-r--r--cmd-select-window.c4
-rw-r--r--cmd-set-environment.c4
-rw-r--r--cmd-set-option.c4
-rw-r--r--cmd-show-buffer.c4
-rw-r--r--cmd-show-environment.c4
-rw-r--r--cmd-show-options.c4
-rw-r--r--cmd-switch-client.c4
-rw-r--r--cmd.c34
-rw-r--r--tmux.113
-rw-r--r--tmux.h6
18 files changed, 65 insertions, 48 deletions
diff --git a/cmd-attach-session.c b/cmd-attach-session.c
index 3272c78e..74806547 100644
--- a/cmd-attach-session.c
+++ b/cmd-attach-session.c
@@ -1,4 +1,4 @@
-/* $Id: cmd-attach-session.c,v 1.39 2011-01-07 14:45:33 tcunha Exp $ */
+/* $Id: cmd-attach-session.c,v 1.40 2011-04-06 22:24:00 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -51,7 +51,7 @@ cmd_attach_session_exec(struct cmd *self, struct cmd_ctx *ctx)
return (-1);
}
- if ((s = cmd_find_session(ctx, args_get(args, 't'))) == NULL)
+ if ((s = cmd_find_session(ctx, args_get(args, 't'), 1)) == NULL)
return (-1);
if (ctx->cmdclient == NULL && ctx->curclient == NULL)
diff --git a/cmd-has-session.c b/cmd-has-session.c
index 0d4c4178..047c9567 100644
--- a/cmd-has-session.c
+++ b/cmd-has-session.c
@@ -1,4 +1,4 @@
-/* $Id: cmd-has-session.c,v 1.16 2011-01-07 14:45:34 tcunha Exp $ */
+/* $Id: cmd-has-session.c,v 1.17 2011-04-06 22:24:00 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -41,7 +41,7 @@ cmd_has_session_exec(struct cmd *self, struct cmd_ctx *ctx)
{
struct args *args = self->args;
- if (cmd_find_session(ctx, args_get(args, 't')) == NULL)
+ if (cmd_find_session(ctx, args_get(args, 't'), 0) == NULL)
return (-1);
return (0);
diff --git a/cmd-kill-session.c b/cmd-kill-session.c
index 43c24e3a..27754c64 100644
--- a/cmd-kill-session.c
+++ b/cmd-kill-session.c
@@ -1,4 +1,4 @@
-/* $Id: cmd-kill-session.c,v 1.19 2011-01-07 14:45:34 tcunha Exp $ */
+/* $Id: cmd-kill-session.c,v 1.20 2011-04-06 22:24:00 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -45,7 +45,7 @@ cmd_kill_session_exec(struct cmd *self, struct cmd_ctx *ctx)
struct args *args = self->args;
struct session *s;
- if ((s = cmd_find_session(ctx, args_get(args, 't'))) == NULL)
+ if ((s = cmd_find_session(ctx, args_get(args, 't'), 0)) == NULL)
return (-1);
server_destroy_session(s);
diff --git a/cmd-list-panes.c b/cmd-list-panes.c
index 61b0de70..e34386fa 100644
--- a/cmd-list-panes.c
+++ b/cmd-list-panes.c
@@ -1,4 +1,4 @@
-/* $Id: cmd-list-panes.c,v 1.9 2011-04-06 22:20:16 nicm Exp $ */
+/* $Id: cmd-list-panes.c,v 1.10 2011-04-06 22:24:00 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -52,7 +52,7 @@ cmd_list_panes_exec(struct cmd *self, struct cmd_ctx *ctx)
if (args_has(args, 'a'))
cmd_list_panes_server(ctx);
else if (args_has(args, 's')) {
- s = cmd_find_session(ctx, args_get(args, 't'));
+ s = cmd_find_session(ctx, args_get(args, 't'), 0);
if (s == NULL)
return (-1);
cmd_list_panes_session(s, ctx);
diff --git a/cmd-list-windows.c b/cmd-list-windows.c
index 04bccfab..9e259adf 100644
--- a/cmd-list-windows.c
+++ b/cmd-list-windows.c
@@ -1,4 +1,4 @@
-/* $Id: cmd-list-windows.c,v 1.46 2011-04-06 22:20:16 nicm Exp $ */
+/* $Id: cmd-list-windows.c,v 1.47 2011-04-06 22:24:00 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -50,7 +50,7 @@ cmd_list_windows_exec(struct cmd *self, struct cmd_ctx *ctx)
if (args_has(args, 'a'))
cmd_list_windows_server(ctx);
else {
- s = cmd_find_session(ctx, args_get(args, 't'));
+ s = cmd_find_session(ctx, args_get(args, 't'), 0);
if (s == NULL)
return (-1);
cmd_list_windows_session(s, ctx);
diff --git a/cmd-lock-server.c b/cmd-lock-server.c
index 53efbfdb..383f9dbf 100644
--- a/cmd-lock-server.c
+++ b/cmd-lock-server.c
@@ -1,4 +1,4 @@
-/* $Id: cmd-lock-server.c,v 1.11 2011-01-07 15:02:38 tcunha Exp $ */
+/* $Id: cmd-lock-server.c,v 1.12 2011-04-06 22:24:00 nicm Exp $ */
/*
* Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -71,7 +71,7 @@ cmd_lock_server_exec(struct cmd *self, unused struct cmd_ctx *ctx)
if (self->entry == &cmd_lock_server_entry)
server_lock();
else if (self->entry == &cmd_lock_session_entry) {
- if ((s = cmd_find_session(ctx, args_get(args, 't'))) == NULL)
+ if ((s = cmd_find_session(ctx, args_get(args, 't'), 0)) == NULL)
return (-1);
server_lock_session(s);
} else {
diff --git a/cmd-new-session.c b/cmd-new-session.c
index b1d851a4..175a3d41 100644
--- a/cmd-new-session.c
+++ b/cmd-new-session.c
@@ -1,4 +1,4 @@
-/* $Id: cmd-new-session.c,v 1.84 2011-02-15 15:25:48 tcunha Exp $ */
+/* $Id: cmd-new-session.c,v 1.85 2011-04-06 22:24:00 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -75,7 +75,7 @@ cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx)
target = args_get(args, 't');
if (target != NULL) {
- groupwith = cmd_find_session(ctx, target);
+ groupwith = cmd_find_session(ctx, target, 0);
if (groupwith == NULL)
return (-1);
} else
diff --git a/cmd-rename-session.c b/cmd-rename-session.c
index 779ff7ff..af6d8e9c 100644
--- a/cmd-rename-session.c
+++ b/cmd-rename-session.c
@@ -1,4 +1,4 @@
-/* $Id: cmd-rename-session.c,v 1.22 2011-01-07 14:45:34 tcunha Exp $ */
+/* $Id: cmd-rename-session.c,v 1.23 2011-04-06 22:24:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -51,7 +51,7 @@ cmd_rename_session_exec(struct cmd *self, struct cmd_ctx *ctx)
return (-1);
}
- if ((s = cmd_find_session(ctx, args_get(args, 't'))) == NULL)
+ if ((s = cmd_find_session(ctx, args_get(args, 't'), 0)) == NULL)
return (-1);
RB_REMOVE(sessions, &sessions, s);
diff --git a/cmd-select-window.c b/cmd-select-window.c
index 61176d35..cb3a0d7d 100644
--- a/cmd-select-window.c
+++ b/cmd-select-window.c
@@ -1,4 +1,4 @@
-/* $Id: cmd-select-window.c,v 1.26 2011-01-07 15:02:38 tcunha Exp $ */
+/* $Id: cmd-select-window.c,v 1.27 2011-04-06 22:24:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -102,7 +102,7 @@ cmd_select_window_exec(struct cmd *self, struct cmd_ctx *ctx)
last = 1;
if (next || previous || last) {
- s = cmd_find_session(ctx, args_get(args, 't'));
+ s = cmd_find_session(ctx, args_get(args, 't'), 0);
if (s == NULL)
return (-1);
diff --git a/cmd-set-environment.c b/cmd-set-environment.c
index 3970d9b5..46b6a8e2 100644
--- a/cmd-set-environment.c
+++ b/cmd-set-environment.c
@@ -1,4 +1,4 @@
-/* $Id: cmd-set-environment.c,v 1.4 2011-01-07 14:45:34 tcunha Exp $ */
+/* $Id: cmd-set-environment.c,v 1.5 2011-04-06 22:24:01 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -65,7 +65,7 @@ cmd_set_environment_exec(struct cmd *self, struct cmd_ctx *ctx)
if (args_has(self->args, 'g'))
env = &global_environ;
else {
- if ((s = cmd_find_session(ctx, args_get(args, 't'))) == NULL)
+ if ((s = cmd_find_session(ctx, args_get(args, 't'), 0)) == NULL)
return (-1);
env = &s->environ;
}
diff --git a/cmd-set-option.c b/cmd-set-option.c
index 18ad5420..bac7d935 100644
--- a/cmd-set-option.c
+++ b/cmd-set-option.c
@@ -1,4 +1,4 @@
-/* $Id: cmd-set-option.c,v 1.111 2011-04-06 22:22:49 nicm Exp $ */
+/* $Id: cmd-set-option.c,v 1.112 2011-04-06 22:24:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -164,7 +164,7 @@ cmd_set_option_exec(struct cmd *self, struct cmd_ctx *ctx)
if (args_has(self->args, 'g'))
oo = &global_s_options;
else {
- s = cmd_find_session(ctx, args_get(args, 't'));
+ s = cmd_find_session(ctx, args_get(args, 't'), 0);
if (s == NULL)
return (-1);
oo = &s->options;
diff --git a/cmd-show-buffer.c b/cmd-show-buffer.c
index b6359ee0..abdba9db 100644
--- a/cmd-show-buffer.c
+++ b/cmd-show-buffer.c
@@ -1,4 +1,4 @@
-/* $Id: cmd-show-buffer.c,v 1.14 2011-01-07 14:45:34 tcunha Exp $ */
+/* $Id: cmd-show-buffer.c,v 1.15 2011-04-06 22:24:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -47,7 +47,7 @@ cmd_show_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
size_t size, len;
u_int width;
- if ((s = cmd_find_session(ctx, NULL)) == NULL)
+ if ((s = cmd_find_session(ctx, NULL, 0)) == NULL)
return (-1);
if (!args_has(args, 'b')) {
diff --git a/cmd-show-environment.c b/cmd-show-environment.c
index 8aa31fab..73e2485c 100644
--- a/cmd-show-environment.c
+++ b/cmd-show-environment.c
@@ -1,4 +1,4 @@
-/* $Id: cmd-show-environment.c,v 1.3 2011-01-07 14:45:34 tcunha Exp $ */
+/* $Id: cmd-show-environment.c,v 1.4 2011-04-06 22:24:01 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -50,7 +50,7 @@ cmd_show_environment_exec(struct cmd *self, struct cmd_ctx *ctx)
if (args_has(self->args, 'g'))
env = &global_environ;
else {
- if ((s = cmd_find_session(ctx, args_get(args, 't'))) == NULL)
+ if ((s = cmd_find_session(ctx, args_get(args, 't'), 0)) == NULL)
return (-1);
env = &s->environ;
}
diff --git a/cmd-show-options.c b/cmd-show-options.c
index 4b44ab26..6be6dea9 100644
--- a/cmd-show-options.c
+++ b/cmd-show-options.c
@@ -1,4 +1,4 @@
-/* $Id: cmd-show-options.c,v 1.24 2011-01-07 15:02:38 tcunha Exp $ */
+/* $Id: cmd-show-options.c,v 1.25 2011-04-06 22:24:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -79,7 +79,7 @@ cmd_show_options_exec(struct cmd *self, struct cmd_ctx *ctx)
if (args_has(self->args, 'g'))
oo = &global_s_options;
else {
- s = cmd_find_session(ctx, args_get(args, 't'));
+ s = cmd_find_session(ctx, args_get(args, 't'), 0);
if (s == NULL)
return (-1);
oo = &s->options;
diff --git a/cmd-switch-client.c b/cmd-switch-client.c
index 20197aa1..4c0ae445 100644
--- a/cmd-switch-client.c
+++ b/cmd-switch-client.c
@@ -1,4 +1,4 @@
-/* $Id: cmd-switch-client.c,v 1.25 2011-01-07 14:45:34 tcunha Exp $ */
+/* $Id: cmd-switch-client.c,v 1.26 2011-04-06 22:24:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -86,7 +86,7 @@ cmd_switch_client_exec(struct cmd *self, struct cmd_ctx *ctx)
return (-1);
}
} else
- s = cmd_find_session(ctx, args_get(args, 't'));
+ s = cmd_find_session(ctx, args_get(args, 't'), 0);
if (s == NULL)
return (-1);
diff --git a/cmd.c b/cmd.c
index 2241454c..70c8e3ad 100644
--- a/cmd.c
+++ b/cmd.c
@@ -1,4 +1,4 @@
-/* $Id: cmd.c,v 1.150 2011-04-06 22:16:33 nicm Exp $ */
+/* $Id: cmd.c,v 1.151 2011-04-06 22:24:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -111,7 +111,7 @@ const struct cmd_entry *cmd_table[] = {
};
struct session *cmd_choose_session_list(struct sessionslist *);
-struct session *cmd_choose_session(void);
+struct session *cmd_choose_session(int);
struct client *cmd_choose_client(struct clients *);
struct client *cmd_lookup_client(const char *);
struct session *cmd_lookup_session(const char *, int *);
@@ -315,7 +315,7 @@ cmd_print(struct cmd *cmd, char *buf, size_t len)
* session from all sessions.
*/
struct session *
-cmd_current_session(struct cmd_ctx *ctx)
+cmd_current_session(struct cmd_ctx *ctx, int prefer_unattached)
{
struct msg_command_data *data = ctx->msgdata;
struct client *c = ctx->cmdclient;
@@ -364,19 +364,25 @@ cmd_current_session(struct cmd_ctx *ctx)
return (s);
}
- return (cmd_choose_session());
+ return (cmd_choose_session(prefer_unattached));
}
-/* Find the most recently used session. */
+/*
+ * Find the most recently used session, preferring unattached if the flag is
+ * set.
+ */
struct session *
-cmd_choose_session(void)
+cmd_choose_session(int prefer_unattached)
{
struct session *s, *sbest;
struct timeval *tv = NULL;
sbest = NULL;
RB_FOREACH(s, sessions, &sessions) {
- if (tv == NULL || timercmp(&s->activity_time, tv, >)) {
+ if (tv == NULL || timercmp(&s->activity_time, tv, >) ||
+ (prefer_unattached &&
+ !(sbest->flags & SESSION_UNATTACHED) &&
+ (s->flags & SESSION_UNATTACHED))) {
sbest = s;
tv = &s->activity_time;
}
@@ -427,7 +433,7 @@ cmd_current_client(struct cmd_ctx *ctx)
* No current client set. Find the current session and return the
* newest of its clients.
*/
- s = cmd_current_session(ctx);
+ s = cmd_current_session(ctx, 0);
if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
ARRAY_INIT(&cc);
for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
@@ -670,7 +676,7 @@ cmd_pane_session(struct cmd_ctx *ctx, struct window_pane *wp,
struct winlink *wl;
/* If this pane is in the current session, return that winlink. */
- s = cmd_current_session(ctx);
+ s = cmd_current_session(ctx, 0);
if (s != NULL) {
wl = winlink_find_by_window(&s->windows, wp->window);
if (wl != NULL) {
@@ -695,7 +701,7 @@ cmd_pane_session(struct cmd_ctx *ctx, struct window_pane *wp,
/* Find the target session or report an error and return NULL. */
struct session *
-cmd_find_session(struct cmd_ctx *ctx, const char *arg)
+cmd_find_session(struct cmd_ctx *ctx, const char *arg, int prefer_unattached)
{
struct session *s;
struct window_pane *wp;
@@ -706,7 +712,7 @@ cmd_find_session(struct cmd_ctx *ctx, const char *arg)
/* A NULL argument means the current session. */
if (arg == NULL)
- return (cmd_current_session(ctx));
+ return (cmd_current_session(ctx, prefer_unattached));
tmparg = xstrdup(arg);
/* Lookup as pane id. */
@@ -752,7 +758,7 @@ cmd_find_window(struct cmd_ctx *ctx, const char *arg, struct session **sp)
* Find the current session. There must always be a current session, if
* it can't be found, report an error.
*/
- if ((s = cmd_current_session(ctx)) == NULL) {
+ if ((s = cmd_current_session(ctx, 0)) == NULL) {
ctx->error(ctx, "can't establish current session");
return (NULL);
}
@@ -899,7 +905,7 @@ cmd_find_index(struct cmd_ctx *ctx, const char *arg, struct session **sp)
* Find the current session. There must always be a current session, if
* it can't be found, report an error.
*/
- if ((s = cmd_current_session(ctx)) == NULL) {
+ if ((s = cmd_current_session(ctx, 0)) == NULL) {
ctx->error(ctx, "can't establish current session");
return (-2);
}
@@ -1053,7 +1059,7 @@ cmd_find_pane(struct cmd_ctx *ctx,
u_int idx;
/* Get the current session. */
- if ((s = cmd_current_session(ctx)) == NULL) {
+ if ((s = cmd_current_session(ctx, 0)) == NULL) {
ctx->error(ctx, "can't establish current session");
return (NULL);
}
diff --git a/tmux.1 b/tmux.1
index 918e6b73..334ed3de 100644
--- a/tmux.1
+++ b/tmux.1
@@ -1,4 +1,4 @@
-.\" $Id: tmux.1,v 1.301 2011-04-06 22:23:14 nicm Exp $
+.\" $Id: tmux.1,v 1.302 2011-04-06 22:24:01 nicm Exp $
.\"
.\" Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
.\"
@@ -566,6 +566,17 @@ If no server is started,
.Ic attach-session
will attempt to start it; this will fail unless sessions are created in the
configuration file.
+.Pp
+The
+.Ar target-session
+rules for
+.Ic attach-session
+are slightly adjusted: if
+.Nm
+needs to select the most recently used session, it will prefer the most
+recently used
+.Em unattached
+session.
.It Xo Ic detach-client
.Op Fl P
.Op Fl t Ar target-client
diff --git a/tmux.h b/tmux.h
index c9090d0b..f8152714 100644
--- a/tmux.h
+++ b/tmux.h
@@ -1,4 +1,4 @@
-/* $Id: tmux.h,v 1.616 2011-04-06 22:21:02 nicm Exp $ */
+/* $Id: tmux.h,v 1.617 2011-04-06 22:24:01 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -1483,10 +1483,10 @@ struct cmd *cmd_parse(int, char **, char **);
int cmd_exec(struct cmd *, struct cmd_ctx *);
void cmd_free(struct cmd *);
size_t cmd_print(struct cmd *, char *, size_t);
-struct session *cmd_current_session(struct cmd_ctx *);
+struct session *cmd_current_session(struct cmd_ctx *, int);
struct client *cmd_current_client(struct cmd_ctx *);
struct client *cmd_find_client(struct cmd_ctx *, const char *);
-struct session *cmd_find_session(struct cmd_ctx *, const char *);
+struct session *cmd_find_session(struct cmd_ctx *, const char *, int);
struct winlink *cmd_find_window(
struct cmd_ctx *, const char *, struct session **);
int cmd_find_index(