15494e770Schristos /* $OpenBSD$ */
2698d5317Sjmmv
3698d5317Sjmmv /*
4ed4e6cd4Schristos * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
5698d5317Sjmmv *
6698d5317Sjmmv * Permission to use, copy, modify, and distribute this software for any
7698d5317Sjmmv * purpose with or without fee is hereby granted, provided that the above
8698d5317Sjmmv * copyright notice and this permission notice appear in all copies.
9698d5317Sjmmv *
10698d5317Sjmmv * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11698d5317Sjmmv * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12698d5317Sjmmv * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13698d5317Sjmmv * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14698d5317Sjmmv * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15698d5317Sjmmv * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16698d5317Sjmmv * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17698d5317Sjmmv */
18698d5317Sjmmv
19698d5317Sjmmv #include <sys/types.h>
20698d5317Sjmmv
21698d5317Sjmmv #include <stdlib.h>
22698d5317Sjmmv
23698d5317Sjmmv #include "tmux.h"
24698d5317Sjmmv
25698d5317Sjmmv /*
26698d5317Sjmmv * Break pane off into a window.
27698d5317Sjmmv */
28698d5317Sjmmv
295494e770Schristos #define BREAK_PANE_TEMPLATE "#{session_name}:#{window_index}.#{pane_index}"
305494e770Schristos
314e179ddaSchristos static enum cmd_retval cmd_break_pane_exec(struct cmd *, struct cmdq_item *);
32698d5317Sjmmv
33698d5317Sjmmv const struct cmd_entry cmd_break_pane_entry = {
34ed4e6cd4Schristos .name = "break-pane",
35ed4e6cd4Schristos .alias = "breakp",
36ed4e6cd4Schristos
376db26757Swiz .args = { "abdPF:n:s:t:", 0, 0, NULL },
389fb66d81Schristos .usage = "[-abdP] [-F format] [-n window-name] [-s src-pane] "
39c9ad075bSchristos "[-t dst-window]",
40ed4e6cd4Schristos
41c9ad075bSchristos .source = { 's', CMD_FIND_PANE, 0 },
42c9ad075bSchristos .target = { 't', CMD_FIND_WINDOW, CMD_FIND_WINDOW_INDEX },
43ed4e6cd4Schristos
44ed4e6cd4Schristos .flags = 0,
45ed4e6cd4Schristos .exec = cmd_break_pane_exec
46698d5317Sjmmv };
47698d5317Sjmmv
484e179ddaSchristos static enum cmd_retval
cmd_break_pane_exec(struct cmd * self,struct cmdq_item * item)494e179ddaSchristos cmd_break_pane_exec(struct cmd *self, struct cmdq_item *item)
50698d5317Sjmmv {
519fb66d81Schristos struct args *args = cmd_get_args(self);
529fb66d81Schristos struct cmd_find_state *current = cmdq_get_current(item);
539fb66d81Schristos struct cmd_find_state *target = cmdq_get_target(item);
549fb66d81Schristos struct cmd_find_state *source = cmdq_get_source(item);
559fb66d81Schristos struct client *tc = cmdq_get_target_client(item);
569fb66d81Schristos struct winlink *wl = source->wl;
579fb66d81Schristos struct session *src_s = source->s;
589fb66d81Schristos struct session *dst_s = target->s;
599fb66d81Schristos struct window_pane *wp = source->wp;
60ed4e6cd4Schristos struct window *w = wl->window;
619fb66d81Schristos char *name, *cause, *cp;
629fb66d81Schristos int idx = target->idx, before;
63928fc495Schristos const char *template;
64698d5317Sjmmv
659fb66d81Schristos before = args_has(args, 'b');
669fb66d81Schristos if (args_has(args, 'a') || before) {
679fb66d81Schristos if (target->wl != NULL)
689fb66d81Schristos idx = winlink_shuffle_up(dst_s, target->wl, before);
699fb66d81Schristos else
709fb66d81Schristos idx = winlink_shuffle_up(dst_s, dst_s->curw, before);
719fb66d81Schristos if (idx == -1)
72928fc495Schristos return (CMD_RETURN_ERROR);
73698d5317Sjmmv }
74928fc495Schristos server_unzoom_window(w);
75928fc495Schristos
769fb66d81Schristos if (window_count_panes(w) == 1) {
779fb66d81Schristos if (server_link_window(src_s, wl, dst_s, idx, 0,
789fb66d81Schristos !args_has(args, 'd'), &cause) != 0) {
799fb66d81Schristos cmdq_error(item, "%s", cause);
809fb66d81Schristos free(cause);
819fb66d81Schristos return (CMD_RETURN_ERROR);
829fb66d81Schristos }
839fb66d81Schristos if (args_has(args, 'n')) {
849fb66d81Schristos window_set_name(w, args_get(args, 'n'));
859fb66d81Schristos options_set_number(w->options, "automatic-rename", 0);
869fb66d81Schristos }
879fb66d81Schristos server_unlink_window(src_s, wl);
889fb66d81Schristos return (CMD_RETURN_NORMAL);
899fb66d81Schristos }
909fb66d81Schristos if (idx != -1 && winlink_find_by_index(&dst_s->windows, idx) != NULL) {
919fb66d81Schristos cmdq_error(item, "index in use: %d", idx);
929fb66d81Schristos return (CMD_RETURN_ERROR);
939fb66d81Schristos }
949fb66d81Schristos
95d530c4d0Sjmmv TAILQ_REMOVE(&w->panes, wp, entry);
969fb66d81Schristos server_client_remove_pane(wp);
975494e770Schristos window_lost_pane(w, wp);
98698d5317Sjmmv layout_close_pane(wp);
99698d5317Sjmmv
100aa83ff61Schristos w = wp->window = window_create(w->sx, w->sy, w->xpixel, w->ypixel);
1016483eba0Schristos options_set_parent(wp->options, w->options);
1026483eba0Schristos wp->flags |= PANE_STYLECHANGED;
103698d5317Sjmmv TAILQ_INSERT_HEAD(&w->panes, wp, entry);
104698d5317Sjmmv w->active = wp;
1059fb66d81Schristos w->latest = tc;
1064e179ddaSchristos
1074e179ddaSchristos if (!args_has(args, 'n')) {
108928fc495Schristos name = default_window_name(w);
109928fc495Schristos window_set_name(w, name);
110928fc495Schristos free(name);
1114e179ddaSchristos } else {
1124e179ddaSchristos window_set_name(w, args_get(args, 'n'));
1134e179ddaSchristos options_set_number(w->options, "automatic-rename", 0);
1144e179ddaSchristos }
1154e179ddaSchristos
116928fc495Schristos layout_init(w, wp);
117ed4e6cd4Schristos wp->flags |= PANE_CHANGED;
118*c23f9150Swiz colour_palette_from_option(&wp->palette, wp->options);
119698d5317Sjmmv
1205494e770Schristos if (idx == -1)
121ed4e6cd4Schristos idx = -1 - options_get_number(dst_s->options, "base-index");
1225494e770Schristos wl = session_attach(dst_s, w, idx, &cause); /* can't fail */
1239fb66d81Schristos if (!args_has(args, 'd')) {
1245494e770Schristos session_select(dst_s, wl->idx);
125c9ad075bSchristos cmd_find_from_session(current, dst_s, 0);
126c9ad075bSchristos }
127698d5317Sjmmv
1285494e770Schristos server_redraw_session(src_s);
1295494e770Schristos if (src_s != dst_s)
1305494e770Schristos server_redraw_session(dst_s);
1315494e770Schristos server_status_session_group(src_s);
1325494e770Schristos if (src_s != dst_s)
1335494e770Schristos server_status_session_group(dst_s);
134698d5317Sjmmv
135928fc495Schristos if (args_has(args, 'P')) {
136928fc495Schristos if ((template = args_get(args, 'F')) == NULL)
137928fc495Schristos template = BREAK_PANE_TEMPLATE;
1389fb66d81Schristos cp = format_single(item, template, tc, dst_s, wl, wp);
1394e179ddaSchristos cmdq_print(item, "%s", cp);
140928fc495Schristos free(cp);
141928fc495Schristos }
142928fc495Schristos return (CMD_RETURN_NORMAL);
143698d5317Sjmmv }
144