/src/tmux/cmd-switch-client.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD$ */ |
2 | | |
3 | | /* |
4 | | * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@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 | 0 | { |
50 | 0 | struct args *args = cmd_get_args(self); |
51 | 0 | struct cmd_find_state *current = cmdq_get_current(item); |
52 | 0 | struct cmd_find_state target; |
53 | 0 | const char *tflag = args_get(args, 't'); |
54 | 0 | enum cmd_find_type type; |
55 | 0 | int flags; |
56 | 0 | struct client *tc = cmdq_get_target_client(item); |
57 | 0 | struct session *s; |
58 | 0 | struct winlink *wl; |
59 | 0 | struct window *w; |
60 | 0 | struct window_pane *wp; |
61 | 0 | const char *tablename; |
62 | 0 | struct key_table *table; |
63 | |
|
64 | 0 | if (tflag != NULL && tflag[strcspn(tflag, ":.%")] != '\0') { |
65 | 0 | type = CMD_FIND_PANE; |
66 | 0 | flags = 0; |
67 | 0 | } else { |
68 | 0 | type = CMD_FIND_SESSION; |
69 | 0 | flags = CMD_FIND_PREFER_UNATTACHED; |
70 | 0 | } |
71 | 0 | if (cmd_find_target(&target, item, tflag, type, flags) != 0) |
72 | 0 | return (CMD_RETURN_ERROR); |
73 | 0 | s = target.s; |
74 | 0 | wl = target.wl; |
75 | 0 | wp = target.wp; |
76 | |
|
77 | 0 | if (args_has(args, 'r')) { |
78 | 0 | if (tc->flags & CLIENT_READONLY) |
79 | 0 | tc->flags &= ~(CLIENT_READONLY|CLIENT_IGNORESIZE); |
80 | 0 | else |
81 | 0 | tc->flags |= (CLIENT_READONLY|CLIENT_IGNORESIZE); |
82 | 0 | } |
83 | |
|
84 | 0 | tablename = args_get(args, 'T'); |
85 | 0 | if (tablename != NULL) { |
86 | 0 | table = key_bindings_get_table(tablename, 0); |
87 | 0 | if (table == NULL) { |
88 | 0 | cmdq_error(item, "table %s doesn't exist", tablename); |
89 | 0 | return (CMD_RETURN_ERROR); |
90 | 0 | } |
91 | 0 | table->references++; |
92 | 0 | key_bindings_unref_table(tc->keytable); |
93 | 0 | tc->keytable = table; |
94 | 0 | return (CMD_RETURN_NORMAL); |
95 | 0 | } |
96 | | |
97 | 0 | if (args_has(args, 'n')) { |
98 | 0 | if ((s = session_next_session(tc->session)) == NULL) { |
99 | 0 | cmdq_error(item, "can't find next session"); |
100 | 0 | return (CMD_RETURN_ERROR); |
101 | 0 | } |
102 | 0 | } else if (args_has(args, 'p')) { |
103 | 0 | if ((s = session_previous_session(tc->session)) == NULL) { |
104 | 0 | cmdq_error(item, "can't find previous session"); |
105 | 0 | return (CMD_RETURN_ERROR); |
106 | 0 | } |
107 | 0 | } else if (args_has(args, 'l')) { |
108 | 0 | if (tc->last_session != NULL && session_alive(tc->last_session)) |
109 | 0 | s = tc->last_session; |
110 | 0 | else |
111 | 0 | s = NULL; |
112 | 0 | if (s == NULL) { |
113 | 0 | cmdq_error(item, "can't find last session"); |
114 | 0 | return (CMD_RETURN_ERROR); |
115 | 0 | } |
116 | 0 | } else { |
117 | 0 | if (cmdq_get_client(item) == NULL) |
118 | 0 | return (CMD_RETURN_NORMAL); |
119 | 0 | if (wl != NULL && wp != NULL && wp != wl->window->active) { |
120 | 0 | w = wl->window; |
121 | 0 | if (window_push_zoom(w, 0, args_has(args, 'Z'))) |
122 | 0 | server_redraw_window(w); |
123 | 0 | window_redraw_active_switch(w, wp); |
124 | 0 | window_set_active_pane(w, wp, 1); |
125 | 0 | if (window_pop_zoom(w)) |
126 | 0 | server_redraw_window(w); |
127 | 0 | } |
128 | 0 | if (wl != NULL) { |
129 | 0 | session_set_current(s, wl); |
130 | 0 | cmd_find_from_session(current, s, 0); |
131 | 0 | } |
132 | 0 | } |
133 | | |
134 | 0 | if (!args_has(args, 'E')) |
135 | 0 | environ_update(s->options, tc->environ, s->environ); |
136 | |
|
137 | 0 | server_client_set_session(tc, s); |
138 | 0 | if (~cmdq_get_flags(item) & CMDQ_STATE_REPEAT) |
139 | 0 | server_client_set_key_table(tc, NULL); |
140 | |
|
141 | 0 | return (CMD_RETURN_NORMAL); |
142 | 0 | } |