Home | History | Annotate | Line # | Download | only in dist
      1 /* $OpenBSD$ */
      2 
      3 /*
      4  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott (at) gmail.com>
      5  *
      6  * Permission to use, copy, modify, and distribute this software for any
      7  * purpose with or without fee is hereby granted, provided that the above
      8  * copyright notice and this permission notice appear in all copies.
      9  *
     10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
     15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
     16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17  */
     18 
     19 #include <sys/types.h>
     20 
     21 #include <stdlib.h>
     22 #include <string.h>
     23 
     24 #include "tmux.h"
     25 
     26 /*
     27  * Switch client to a different session.
     28  */
     29 
     30 static enum cmd_retval	cmd_switch_client_exec(struct cmd *,
     31 			    struct cmdq_item *);
     32 
     33 const struct cmd_entry cmd_switch_client_entry = {
     34 	.name = "switch-client",
     35 	.alias = "switchc",
     36 
     37 	.args = { "lc:EFnpt:rT:Z", 0, 0, NULL },
     38 	.usage = "[-ElnprZ] [-c target-client] [-t target-session] "
     39 		 "[-T key-table]",
     40 
     41 	/* -t is special */
     42 
     43 	.flags = CMD_READONLY|CMD_CLIENT_CFLAG,
     44 	.exec = cmd_switch_client_exec
     45 };
     46 
     47 static enum cmd_retval
     48 cmd_switch_client_exec(struct cmd *self, struct cmdq_item *item)
     49 {
     50 	struct args		*args = cmd_get_args(self);
     51 	struct cmd_find_state	*current = cmdq_get_current(item);
     52 	struct cmd_find_state	 target;
     53 	const char		*tflag = args_get(args, 't');
     54 	enum cmd_find_type	 type;
     55 	int			 flags;
     56 	struct client		*tc = cmdq_get_target_client(item);
     57 	struct session		*s;
     58 	struct winlink		*wl;
     59 	struct window		*w;
     60 	struct window_pane	*wp;
     61 	const char		*tablename;
     62 	struct key_table	*table;
     63 
     64 	if (tflag != NULL &&
     65 	    (tflag[strcspn(tflag, ":.%")] != '\0' || strcmp(tflag, "=") == 0)) {
     66 		type = CMD_FIND_PANE;
     67 		flags = 0;
     68 	} else {
     69 		type = CMD_FIND_SESSION;
     70 		flags = CMD_FIND_PREFER_UNATTACHED;
     71 	}
     72 	if (cmd_find_target(&target, item, tflag, type, flags) != 0)
     73 		return (CMD_RETURN_ERROR);
     74 	s = target.s;
     75 	wl = target.wl;
     76 	wp = target.wp;
     77 
     78 	if (args_has(args, 'r')) {
     79 		if (tc->flags & CLIENT_READONLY)
     80 			tc->flags &= ~(CLIENT_READONLY|CLIENT_IGNORESIZE);
     81 		else
     82 			tc->flags |= (CLIENT_READONLY|CLIENT_IGNORESIZE);
     83 	}
     84 
     85 	tablename = args_get(args, 'T');
     86 	if (tablename != NULL) {
     87 		table = key_bindings_get_table(tablename, 0);
     88 		if (table == NULL) {
     89 			cmdq_error(item, "table %s doesn't exist", tablename);
     90 			return (CMD_RETURN_ERROR);
     91 		}
     92 		table->references++;
     93 		key_bindings_unref_table(tc->keytable);
     94 		tc->keytable = table;
     95 		return (CMD_RETURN_NORMAL);
     96 	}
     97 
     98 	if (args_has(args, 'n')) {
     99 		if ((s = session_next_session(tc->session)) == NULL) {
    100 			cmdq_error(item, "can't find next session");
    101 			return (CMD_RETURN_ERROR);
    102 		}
    103 	} else if (args_has(args, 'p')) {
    104 		if ((s = session_previous_session(tc->session)) == NULL) {
    105 			cmdq_error(item, "can't find previous session");
    106 			return (CMD_RETURN_ERROR);
    107 		}
    108 	} else if (args_has(args, 'l')) {
    109 		if (tc->last_session != NULL && session_alive(tc->last_session))
    110 			s = tc->last_session;
    111 		else
    112 			s = NULL;
    113 		if (s == NULL) {
    114 			cmdq_error(item, "can't find last session");
    115 			return (CMD_RETURN_ERROR);
    116 		}
    117 	} else {
    118 		if (cmdq_get_client(item) == NULL)
    119 			return (CMD_RETURN_NORMAL);
    120 		if (wl != NULL && wp != NULL && wp != wl->window->active) {
    121 			w = wl->window;
    122 			if (window_push_zoom(w, 0, args_has(args, 'Z')))
    123 				server_redraw_window(w);
    124 			window_redraw_active_switch(w, wp);
    125 			window_set_active_pane(w, wp, 1);
    126 			if (window_pop_zoom(w))
    127 				server_redraw_window(w);
    128 		}
    129 		if (wl != NULL) {
    130 			session_set_current(s, wl);
    131 			cmd_find_from_session(current, s, 0);
    132 		}
    133 	}
    134 
    135 	if (!args_has(args, 'E'))
    136 		environ_update(s->options, tc->environ, s->environ);
    137 
    138 	server_client_set_session(tc, s);
    139 	if (~cmdq_get_flags(item) & CMDQ_STATE_REPEAT)
    140 		server_client_set_key_table(tc, NULL);
    141 
    142 	return (CMD_RETURN_NORMAL);
    143 }
    144