Coverage Report

Created: 2026-03-12 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/cmd-move-window.c
Line
Count
Source
1
/* $OpenBSD$ */
2
3
/*
4
 * Copyright (c) 2008 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
23
#include "tmux.h"
24
25
/*
26
 * Move a window.
27
 */
28
29
static enum cmd_retval  cmd_move_window_exec(struct cmd *, struct cmdq_item *);
30
31
const struct cmd_entry cmd_move_window_entry = {
32
  .name = "move-window",
33
  .alias = "movew",
34
35
  .args = { "abdkrs:t:", 0, 0, NULL },
36
  .usage = "[-abdkr] " CMD_SRCDST_WINDOW_USAGE,
37
38
  .source = { 's', CMD_FIND_WINDOW, 0 },
39
  /* -t is special */
40
41
  .flags = 0,
42
  .exec = cmd_move_window_exec
43
};
44
45
const struct cmd_entry cmd_link_window_entry = {
46
  .name = "link-window",
47
  .alias = "linkw",
48
49
  .args = { "abdks:t:", 0, 0, NULL },
50
  .usage = "[-abdk] " CMD_SRCDST_WINDOW_USAGE,
51
52
  .source = { 's', CMD_FIND_WINDOW, 0 },
53
  /* -t is special */
54
55
  .flags = 0,
56
  .exec = cmd_move_window_exec
57
};
58
59
static enum cmd_retval
60
cmd_move_window_exec(struct cmd *self, struct cmdq_item *item)
61
0
{
62
0
  struct args   *args = cmd_get_args(self);
63
0
  struct cmd_find_state *source = cmdq_get_source(item);
64
0
  struct cmd_find_state  target;
65
0
  const char    *tflag = args_get(args, 't');
66
0
  struct session    *src = source->s;
67
0
  struct session    *dst;
68
0
  struct winlink    *wl = source->wl;
69
0
  char      *cause;
70
0
  int      idx, kflag, dflag, sflag, before;
71
72
0
  if (args_has(args, 'r')) {
73
0
    if (cmd_find_target(&target, item, tflag, CMD_FIND_SESSION,
74
0
        CMD_FIND_QUIET) != 0)
75
0
      return (CMD_RETURN_ERROR);
76
77
0
    session_renumber_windows(target.s);
78
0
    recalculate_sizes();
79
0
    server_status_session(target.s);
80
81
0
    return (CMD_RETURN_NORMAL);
82
0
  }
83
0
  if (cmd_find_target(&target, item, tflag, CMD_FIND_WINDOW,
84
0
      CMD_FIND_WINDOW_INDEX) != 0)
85
0
    return (CMD_RETURN_ERROR);
86
0
  dst = target.s;
87
0
  idx = target.idx;
88
89
0
  kflag = args_has(args, 'k');
90
0
  dflag = args_has(args, 'd');
91
0
  sflag = args_has(args, 's');
92
93
0
  before = args_has(args, 'b');
94
0
  if (args_has(args, 'a') || before) {
95
0
    if (target.wl != NULL)
96
0
      idx = winlink_shuffle_up(dst, target.wl, before);
97
0
    else
98
0
      idx = winlink_shuffle_up(dst, dst->curw, before);
99
0
    if (idx == -1)
100
0
      return (CMD_RETURN_ERROR);
101
0
  }
102
103
0
  if (server_link_window(src, wl, dst, idx, kflag, !dflag, &cause) != 0) {
104
0
    cmdq_error(item, "%s", cause);
105
0
    free(cause);
106
0
    return (CMD_RETURN_ERROR);
107
0
  }
108
0
  if (cmd_get_entry(self) == &cmd_move_window_entry)
109
0
    server_unlink_window(src, wl);
110
111
  /*
112
   * Renumber the winlinks in the src session only, the destination
113
   * session already has the correct winlink id to us, either
114
   * automatically or specified by -s.
115
   */
116
0
  if (!sflag && options_get_number(src->options, "renumber-windows"))
117
0
    session_renumber_windows(src);
118
119
0
  recalculate_sizes();
120
121
0
  return (CMD_RETURN_NORMAL);
122
0
}