summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@cvs.openbsd.org>2009-10-05 18:30:55 +0000
committerNicholas Marriott <nicm@cvs.openbsd.org>2009-10-05 18:30:55 +0000
commitf7709a16f4d20f26d84497c07c17feb8817d5ac4 (patch)
tree9f635b12cceca5cf07f0f21f5f8cf6c6985adde0
parentb1f0999c04e72d7ec869947978955972b2daf1c1 (diff)
If no target client is specified to commands which accept one, try to guess the
current client, in a similar manner to how sessions already work: if the current session can be established and has only one client, use that; otherwise use the most recently created client.
-rw-r--r--usr.bin/tmux/cmd.c53
-rw-r--r--usr.bin/tmux/server.c5
-rw-r--r--usr.bin/tmux/tmux.h3
3 files changed, 56 insertions, 5 deletions
diff --git a/usr.bin/tmux/cmd.c b/usr.bin/tmux/cmd.c
index e1fa8f89c0b..13cd5f41045 100644
--- a/usr.bin/tmux/cmd.c
+++ b/usr.bin/tmux/cmd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: cmd.c,v 1.19 2009/09/24 14:17:09 nicm Exp $ */
+/* $OpenBSD: cmd.c,v 1.20 2009/10/05 18:30:54 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -110,6 +110,7 @@ const struct cmd_entry *cmd_table[] = {
};
struct session *cmd_newest_session(struct sessions *);
+struct client *cmd_newest_client(void);
struct client *cmd_lookup_client(const char *);
struct session *cmd_lookup_session(const char *, int *);
struct winlink *cmd_lookup_window(struct session *, const char *, int *);
@@ -369,17 +370,63 @@ cmd_newest_session(struct sessions *ss)
return (snewest);
}
+/* Find the newest client. */
+struct client *
+cmd_newest_client(void)
+{
+ struct client *c, *cnewest;
+ struct timeval *tv = NULL;
+ u_int i;
+
+ cnewest = NULL;
+ for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
+ if ((c = ARRAY_ITEM(&clients, i)) == NULL)
+ continue;
+ if (c->session == NULL)
+ continue;
+
+ if (tv == NULL || timercmp(&c->tv, tv, >)) {
+ cnewest = c;
+ tv = &c->tv;
+ }
+ }
+
+ return (cnewest);
+}
+
/* Find the target client or report an error and return NULL. */
struct client *
cmd_find_client(struct cmd_ctx *ctx, const char *arg)
{
struct client *c;
+ struct session *s;
char *tmparg;
size_t arglen;
+ u_int i;
/* A NULL argument means the current client. */
- if (arg == NULL)
- return (ctx->curclient);
+ if (arg == NULL) {
+ if (ctx->curclient != NULL)
+ return (ctx->curclient);
+ /*
+ * No current client set. Find the current session and see if
+ * it has only has one client.
+ */
+ s = cmd_current_session(ctx);
+ if (s != NULL) {
+ c = NULL;
+ for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
+ if (ARRAY_ITEM(&clients, i)->session == s) {
+ if (c != NULL)
+ break;
+ c = ARRAY_ITEM(&clients, i);
+ }
+ }
+ if (i == ARRAY_LENGTH(&clients) && c != NULL)
+ return (c);
+ }
+ return (cmd_newest_client());
+ }
tmparg = xstrdup(arg);
/* Trim a single trailing colon if any. */
diff --git a/usr.bin/tmux/server.c b/usr.bin/tmux/server.c
index e46c713eb5e..c60347bf458 100644
--- a/usr.bin/tmux/server.c
+++ b/usr.bin/tmux/server.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: server.c,v 1.43 2009/09/24 07:02:56 nicm Exp $ */
+/* $OpenBSD: server.c,v 1.44 2009/10/05 18:30:54 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -90,6 +90,9 @@ server_create_client(int fd)
c = xcalloc(1, sizeof *c);
c->references = 0;
imsg_init(&c->ibuf, fd);
+
+ if (gettimeofday(&c->tv, NULL) != 0)
+ fatal("gettimeofday failed");
ARRAY_INIT(&c->prompt_hdata);
diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h
index 5996e364a9a..600ff4ce362 100644
--- a/usr.bin/tmux/tmux.h
+++ b/usr.bin/tmux/tmux.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: tmux.h,v 1.118 2009/09/24 14:17:09 nicm Exp $ */
+/* $OpenBSD: tmux.h,v 1.119 2009/10/05 18:30:54 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -922,6 +922,7 @@ struct tty_ctx {
/* Client connection. */
struct client {
struct imsgbuf ibuf;
+ struct timeval tv;
struct environ environ;