mirror of
				https://github.com/tmux/tmux.git
				synced 2025-10-26 12:27:15 +00:00 
			
		
		
		
	Tidy up new-session and attach-session and change them to work from inside
tmux, switching the current client to the new or requested session.
This commit is contained in:
		| @@ -1,4 +1,4 @@ | |||||||
| /* $Id: cmd-attach-session.c,v 1.27 2009-07-14 06:43:32 nicm Exp $ */ | /* $Id: cmd-attach-session.c,v 1.28 2009-07-18 11:06:09 nicm Exp $ */ | ||||||
|  |  | ||||||
| /* | /* | ||||||
|  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> |  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> | ||||||
| @@ -44,10 +44,9 @@ cmd_attach_session_exec(struct cmd *self, struct cmd_ctx *ctx) | |||||||
| { | { | ||||||
| 	struct cmd_target_data	*data = self->data; | 	struct cmd_target_data	*data = self->data; | ||||||
| 	struct session		*s; | 	struct session		*s; | ||||||
|  | 	struct client		*c; | ||||||
| 	char			*cause; | 	char			*cause; | ||||||
|  | 	u_int			 i; | ||||||
| 	if (ctx->curclient != NULL) |  | ||||||
| 		return (0); |  | ||||||
|  |  | ||||||
| 	if (ARRAY_LENGTH(&sessions) == 0) { | 	if (ARRAY_LENGTH(&sessions) == 0) { | ||||||
| 		ctx->error(ctx, "no sessions"); | 		ctx->error(ctx, "no sessions"); | ||||||
| @@ -56,24 +55,44 @@ cmd_attach_session_exec(struct cmd *self, struct cmd_ctx *ctx) | |||||||
| 	if ((s = cmd_find_session(ctx, data->target)) == NULL) | 	if ((s = cmd_find_session(ctx, data->target)) == NULL) | ||||||
| 		return (-1); | 		return (-1); | ||||||
|  |  | ||||||
| 	if (!(ctx->cmdclient->flags & CLIENT_TERMINAL)) { | 	if (ctx->cmdclient == NULL) { | ||||||
| 		ctx->error(ctx, "not a terminal"); | 		if (data->chflags & CMD_CHFLAG('d')) { | ||||||
| 		return (-1); | 			/*  | ||||||
|  | 			 * Can't use server_write_session in case attaching to | ||||||
|  | 			 * the same session as currently attached to. | ||||||
|  | 			 */ | ||||||
|  | 			for (i = 0; i < ARRAY_LENGTH(&clients); i++) { | ||||||
|  | 				c = ARRAY_ITEM(&clients, i); | ||||||
|  | 				if (c == NULL || c->session != s) | ||||||
|  | 					continue; | ||||||
|  | 				if (c == ctx->curclient) | ||||||
|  | 					continue; | ||||||
|  | 				server_write_client(c, MSG_DETACH, NULL, 0); | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 		 | ||||||
|  | 		ctx->curclient->session = s; | ||||||
|  | 		server_redraw_client(ctx->curclient); | ||||||
|  | 	} else { | ||||||
|  | 		if (!(ctx->cmdclient->flags & CLIENT_TERMINAL)) { | ||||||
|  | 			ctx->error(ctx, "not a terminal"); | ||||||
|  | 			return (-1); | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		if (tty_open(&ctx->cmdclient->tty, &cause) != 0) { | ||||||
|  | 			ctx->error(ctx, "terminal open failed: %s", cause); | ||||||
|  | 			xfree(cause); | ||||||
|  | 			return (-1); | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		if (data->chflags & CMD_CHFLAG('d')) | ||||||
|  | 			server_write_session(s, MSG_DETACH, NULL, 0); | ||||||
|  |  | ||||||
|  | 		ctx->cmdclient->session = s; | ||||||
|  | 		server_write_client(ctx->cmdclient, MSG_READY, NULL, 0); | ||||||
|  | 		server_redraw_client(ctx->cmdclient); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if (tty_open(&ctx->cmdclient->tty, &cause) != 0) { |  | ||||||
| 		ctx->error(ctx, "terminal open failed: %s", cause); |  | ||||||
| 		xfree(cause); |  | ||||||
| 		return (-1); |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	if (data->chflags & CMD_CHFLAG('d')) |  | ||||||
| 		server_write_session(s, MSG_DETACH, NULL, 0); |  | ||||||
| 	ctx->cmdclient->session = s; |  | ||||||
|  |  | ||||||
| 	server_write_client(ctx->cmdclient, MSG_READY, NULL, 0); |  | ||||||
| 	recalculate_sizes(); | 	recalculate_sizes(); | ||||||
| 	server_redraw_client(ctx->cmdclient); |  | ||||||
|  |  | ||||||
| 	return (1); | 	return (1);	/* 1 means don't tell command client to exit */ | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| /* $Id: cmd-new-session.c,v 1.44 2009-07-14 06:43:32 nicm Exp $ */ | /* $Id: cmd-new-session.c,v 1.45 2009-07-18 11:06:09 nicm Exp $ */ | ||||||
|  |  | ||||||
| /* | /* | ||||||
|  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> |  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> | ||||||
| @@ -111,65 +111,80 @@ int | |||||||
| cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx) | cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx) | ||||||
| { | { | ||||||
| 	struct cmd_new_session_data	*data = self->data; | 	struct cmd_new_session_data	*data = self->data; | ||||||
| 	struct client			*c = ctx->cmdclient; |  | ||||||
| 	struct session			*s; | 	struct session			*s; | ||||||
| 	char				*cmd, *cwd, *cause; | 	char				*cmd, *cwd, *cause; | ||||||
| 	u_int				 sx, sy; | 	u_int				 sx, sy; | ||||||
|  |  | ||||||
| 	if (ctx->curclient != NULL) |  | ||||||
| 		return (0); |  | ||||||
|  |  | ||||||
| 	if (!data->flag_detached) { |  | ||||||
| 		if (c == NULL) { |  | ||||||
| 			ctx->error(ctx, "no client to attach to"); |  | ||||||
| 			return (-1); |  | ||||||
| 		} |  | ||||||
| 		if (!(c->flags & CLIENT_TERMINAL)) { |  | ||||||
| 			ctx->error(ctx, "not a terminal"); |  | ||||||
| 			return (-1); |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	if (data->newname != NULL && session_find(data->newname) != NULL) { | 	if (data->newname != NULL && session_find(data->newname) != NULL) { | ||||||
| 		ctx->error(ctx, "duplicate session: %s", data->newname); | 		ctx->error(ctx, "duplicate session: %s", data->newname); | ||||||
| 		return (-1); | 		return (-1); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	cmd = data->cmd; | 	/* | ||||||
| 	if (cmd == NULL) | 	 * There are two cases: | ||||||
| 		cmd = options_get_string(&global_s_options, "default-command"); | 	 * | ||||||
| 	if (c == NULL || c->cwd == NULL) | 	 * 1. If cmdclient is non-NULL, new-session has been called from the | ||||||
| 		cwd = options_get_string(&global_s_options, "default-path"); | 	 *    command-line - cmdclient is to become a new attached, interactive | ||||||
|  | 	 *    client. Unless -d is given, the terminal must be opened and then | ||||||
|  | 	 *    the client sent MSG_READY. | ||||||
|  | 	 * | ||||||
|  | 	 * 2. If cmdclient is NULL, new-session has been called from an | ||||||
|  | 	 *    existing client (such as a key binding). | ||||||
|  | 	 * | ||||||
|  | 	 * In both cases, a new additional session needs to be created and | ||||||
|  | 	 * (unless -d) set as the current session for the client. | ||||||
|  | 	 */ | ||||||
|  |  | ||||||
|  | 	/* Open the terminal if necessary. */ | ||||||
|  | 	if (!data->flag_detached && ctx->cmdclient != NULL) { | ||||||
|  | 		if (!(ctx->cmdclient->flags & CLIENT_TERMINAL)) { | ||||||
|  | 			ctx->error(ctx, "not a terminal"); | ||||||
|  | 			return (-1); | ||||||
|  | 		} | ||||||
|  | 		 | ||||||
|  | 		if (tty_open(&ctx->cmdclient->tty, &cause) != 0) { | ||||||
|  | 			ctx->error(ctx, "open terminal failed: %s", cause); | ||||||
|  | 			xfree(cause); | ||||||
|  | 			return (-1); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	/* Find new session size and options. */ | ||||||
|  | 	if (data->flag_detached) { | ||||||
|  | 		sx = 80; | ||||||
|  | 		sy = 25; | ||||||
|  | 	} else { | ||||||
|  | 		if (ctx->cmdclient != NULL) { | ||||||
|  | 			sx = ctx->cmdclient->tty.sx; | ||||||
|  | 			sy = ctx->cmdclient->tty.sy; | ||||||
|  | 		} else { | ||||||
|  | 			sx = ctx->curclient->tty.sx; | ||||||
|  | 			sy = ctx->curclient->tty.sy; | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	if (sy > 0 && options_get_number(&global_s_options, "status")) | ||||||
|  | 		sy--; | ||||||
|  | 	if (sx == 0) | ||||||
|  | 		sx = 1; | ||||||
|  | 	if (sy == 0) | ||||||
|  | 		sy = 1; | ||||||
|  | 	if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL) | ||||||
|  | 		cwd = ctx->cmdclient->cwd; | ||||||
| 	else | 	else | ||||||
| 		cwd = c->cwd; | 		cwd = options_get_string(&global_s_options, "default-path"); | ||||||
|  | 	if (data->cmd != NULL) | ||||||
| 	sx = 80; | 		cmd = data->cmd; | ||||||
| 	sy = 25; | 	else | ||||||
| 	if (!data->flag_detached) { | 		cmd = options_get_string(&global_s_options, "default-command"); | ||||||
| 		sx = c->tty.sx; |  | ||||||
| 		sy = c->tty.sy; |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	if (options_get_number(&global_s_options, "status")) { |  | ||||||
| 		if (sy == 0) |  | ||||||
| 			sy = 1; |  | ||||||
| 		else |  | ||||||
| 			sy--; |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	if (!data->flag_detached && tty_open(&c->tty, &cause) != 0) { |  | ||||||
| 		ctx->error(ctx, "open terminal failed: %s", cause); |  | ||||||
| 		xfree(cause); |  | ||||||
| 		return (-1); |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  | 	/* Create the new session. */ | ||||||
| 	s = session_create(data->newname, cmd, cwd, sx, sy, &cause); | 	s = session_create(data->newname, cmd, cwd, sx, sy, &cause); | ||||||
| 	if (s == NULL) { | 	if (s == NULL) { | ||||||
| 		ctx->error(ctx, "create session failed: %s", cause); | 		ctx->error(ctx, "create session failed: %s", cause); | ||||||
| 		xfree(cause); | 		xfree(cause); | ||||||
| 		return (-1); | 		return (-1); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if (data->winname != NULL) { | 	if (data->winname != NULL) { | ||||||
| 		xfree(s->curw->window->name); | 		xfree(s->curw->window->name); | ||||||
| 		s->curw->window->name = xstrdup(data->winname); | 		s->curw->window->name = xstrdup(data->winname); | ||||||
| @@ -177,17 +192,31 @@ cmd_new_session_exec(struct cmd *self, struct cmd_ctx *ctx) | |||||||
| 		    &s->curw->window->options, "automatic-rename", 0); | 		    &s->curw->window->options, "automatic-rename", 0); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if (data->flag_detached) { | 	/*  | ||||||
| 		if (c != NULL) | 	 * If a command client exists, it is either taking this session (and | ||||||
| 			server_write_client(c, MSG_EXIT, NULL, 0); | 	 * needs to get MSG_READY and stay around), or -d is given and it needs | ||||||
| 	} else { | 	 * to exit. | ||||||
| 		c->session = s; | 	 */ | ||||||
| 		server_write_client(c, MSG_READY, NULL, 0); | 	if (ctx->cmdclient != NULL) { | ||||||
| 		server_redraw_client(c); | 		if (!data->flag_detached) | ||||||
|  | 			server_write_client(ctx->cmdclient, MSG_READY, NULL, 0); | ||||||
|  | 		else  | ||||||
|  | 			server_write_client(ctx->cmdclient, MSG_EXIT, NULL, 0); | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	/* Set the client to the new session. */ | ||||||
|  |  	if (!data->flag_detached) { | ||||||
|  | 		if (ctx->cmdclient != NULL) { | ||||||
|  |  			ctx->cmdclient->session = s; | ||||||
|  | 			server_redraw_client(ctx->cmdclient); | ||||||
|  | 		} else { | ||||||
|  |  			ctx->curclient->session = s; | ||||||
|  | 			server_redraw_client(ctx->curclient); | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| 	recalculate_sizes(); | 	recalculate_sizes(); | ||||||
|  |  | ||||||
| 	return (1); | 	return (1);	/* 1 means don't tell command client to exit */ | ||||||
| } | } | ||||||
|  |  | ||||||
| void | void | ||||||
|   | |||||||
							
								
								
									
										8
									
								
								tmux.1
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								tmux.1
									
									
									
									
									
								
							| @@ -1,4 +1,4 @@ | |||||||
| .\" $Id: tmux.1,v 1.119 2009-07-18 11:05:13 nicm Exp $ | .\" $Id: tmux.1,v 1.120 2009-07-18 11:06:09 nicm Exp $ | ||||||
| .\" | .\" | ||||||
| .\" Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> | .\" Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> | ||||||
| .\" | .\" | ||||||
| @@ -596,7 +596,11 @@ The following commands are available: | |||||||
| .Op Fl t Ar target-session | .Op Fl t Ar target-session | ||||||
| .Xc | .Xc | ||||||
| .D1 (alias: Ic attach ) | .D1 (alias: Ic attach ) | ||||||
| Create a new client in the current terminal and attach it to a session. | If run from outside | ||||||
|  | .Nm , | ||||||
|  | create a new client in the current terminal and attach it to | ||||||
|  | .Ar target-session . | ||||||
|  | If used from inside, switch the current client. | ||||||
| If | If | ||||||
| .Fl d | .Fl d | ||||||
| is specified, any other clients attached to the session are detached. | is specified, any other clients attached to the session are detached. | ||||||
|   | |||||||
							
								
								
									
										12
									
								
								tmux.h
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								tmux.h
									
									
									
									
									
								
							| @@ -1,4 +1,4 @@ | |||||||
| /* $Id: tmux.h,v 1.369 2009-07-17 18:32:54 tcunha Exp $ */ | /* $Id: tmux.h,v 1.370 2009-07-18 11:06:09 nicm Exp $ */ | ||||||
|  |  | ||||||
| /* | /* | ||||||
|  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> |  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> | ||||||
| @@ -834,8 +834,18 @@ struct client_ctx { | |||||||
| struct cmd_ctx { | struct cmd_ctx { | ||||||
| 	struct client  *cmdclient; | 	struct client  *cmdclient; | ||||||
|  |  | ||||||
|  | 	/* | ||||||
|  | 	 * curclient is the client where this command was executed if inside | ||||||
|  | 	 * tmux. This is NULL if the command came from the command-line. | ||||||
|  | 	 * | ||||||
|  | 	 * cmdclient is the client which sent the MSG_COMMAND to the server, if | ||||||
|  | 	 * any. This is NULL unless the command came from the command-line. | ||||||
|  | 	 * | ||||||
|  | 	 * One of curclient or cmdclient is always NULL and the other not. | ||||||
|  | 	 */ | ||||||
| 	struct client  *curclient; | 	struct client  *curclient; | ||||||
| 	struct session *cursession; | 	struct session *cursession; | ||||||
|  |  | ||||||
| 	struct msg_command_data	*msgdata; | 	struct msg_command_data	*msgdata; | ||||||
|  |  | ||||||
| 	void		(*print)(struct cmd_ctx *, const char *, ...); | 	void		(*print)(struct cmd_ctx *, const char *, ...); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Nicholas Marriott
					Nicholas Marriott