Coverage Report

Created: 2026-10-28 06:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/cmd-select-layout.c
Line
Count
Source
1
/* $OpenBSD: cmd-select-layout.c,v 1.46 2026/10/09 13:12:14 nicm Exp $ */
2
3
/*
4
 * Copyright (c) 2009 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
 * Switch window to selected layout.
27
 */
28
29
static enum cmd_retval  cmd_select_layout_exec(struct cmd *,
30
          struct cmdq_item *);
31
32
const struct cmd_entry cmd_select_layout_entry = {
33
  .name = "select-layout",
34
  .description = "Choose or apply a layout.",
35
  .alias = "selectl",
36
37
  .args = { "Enopt:", 0, 1, NULL },
38
  .usage = "[-Enop] " CMD_TARGET_PANE_USAGE " [layout-name]",
39
40
  .target = { 't', CMD_FIND_PANE, 0 },
41
42
  .flags = CMD_AFTERHOOK,
43
  .exec = cmd_select_layout_exec
44
};
45
46
const struct cmd_entry cmd_next_layout_entry = {
47
  .name = "next-layout",
48
  .description = "Select the next layout.",
49
  .alias = "nextl",
50
51
  .args = { "t:", 0, 0, NULL },
52
  .usage = CMD_TARGET_WINDOW_USAGE,
53
54
  .target = { 't', CMD_FIND_WINDOW, 0 },
55
56
  .flags = CMD_AFTERHOOK,
57
  .exec = cmd_select_layout_exec
58
};
59
60
const struct cmd_entry cmd_previous_layout_entry = {
61
  .name = "previous-layout",
62
  .description = "Select the previous layout.",
63
  .alias = "prevl",
64
65
  .args = { "t:", 0, 0, NULL },
66
  .usage = CMD_TARGET_WINDOW_USAGE,
67
68
  .target = { 't', CMD_FIND_WINDOW, 0 },
69
70
  .flags = CMD_AFTERHOOK,
71
  .exec = cmd_select_layout_exec
72
};
73
74
static enum cmd_retval
75
cmd_select_layout_exec(struct cmd *self, struct cmdq_item *item)
76
0
{
77
0
  struct args   *args = cmd_get_args(self);
78
0
  struct cmd_find_state *target = cmdq_get_target(item);
79
0
  struct client   *c = cmdq_get_target_client(item);
80
0
  struct winlink    *wl = target->wl;
81
0
  struct window   *w = wl->window;
82
0
  struct window_pane  *wp = target->wp;
83
0
  const char    *layoutname;
84
0
  char      *oldlayout, *cause = NULL;
85
0
  int      next, previous, layout, flags = 0;
86
87
0
  server_unzoom_window(w);
88
89
0
  next = (cmd_get_entry(self) == &cmd_next_layout_entry);
90
0
  if (args_has(args, 'n'))
91
0
    next = 1;
92
0
  previous = (cmd_get_entry(self) == &cmd_previous_layout_entry);
93
0
  if (args_has(args, 'p'))
94
0
    previous = 1;
95
96
0
  if (c != NULL &&
97
0
      (c->flags & CLIENT_CONTROL) &&
98
0
      (~c->flags & CLIENT_CONTROL_NEWLAYOUTS))
99
0
    flags |= LAYOUT_CUSTOM_OLD_FORMAT;
100
0
  oldlayout = w->old_layout;
101
0
  w->old_layout = layout_dump(w, w->layout_root, flags);
102
103
0
  if (next || previous) {
104
0
    if (next)
105
0
      layout_set_next(w);
106
0
    else
107
0
      layout_set_previous(w);
108
0
    goto changed;
109
0
  }
110
111
0
  if (args_has(args, 'E')) {
112
0
    layout_spread_out(wp);
113
0
    goto changed;
114
0
  }
115
116
0
  if (args_count(args) != 0)
117
0
    layoutname = args_string(args, 0);
118
0
  else if (args_has(args, 'o'))
119
0
    layoutname = oldlayout;
120
0
  else
121
0
    layoutname = NULL;
122
123
0
  if (!args_has(args, 'o')) {
124
0
    if (layoutname == NULL)
125
0
      layout = w->lastlayout;
126
0
    else
127
0
      layout = layout_set_lookup(layoutname);
128
0
    if (layout != -1) {
129
0
      layout_set_select(w, layout);
130
0
      goto changed;
131
0
    }
132
0
  }
133
134
0
  if (layoutname != NULL) {
135
0
    if (layout_parse(w, layoutname, &cause) == -1) {
136
0
      cmdq_error(item, "%s: %s", cause, layoutname);
137
0
      free(cause);
138
0
      goto error;
139
0
    }
140
0
    goto changed;
141
0
  }
142
143
0
  free(oldlayout);
144
0
  return (CMD_RETURN_NORMAL);
145
146
0
changed:
147
0
  free(oldlayout);
148
0
  recalculate_sizes();
149
0
  server_redraw_window(w);
150
0
  events_fire_window("window-layout-changed", w);
151
0
  return (CMD_RETURN_NORMAL);
152
153
0
error:
154
0
  free(w->old_layout);
155
0
  w->old_layout = oldlayout;
156
0
  return (CMD_RETURN_ERROR);
157
0
}