diff options
-rw-r--r-- | include/sway/commands.h | 1 | ||||
-rw-r--r-- | sway/commands.c | 1 | ||||
-rw-r--r-- | sway/commands/rename.c | 78 | ||||
-rw-r--r-- | sway/meson.build | 1 |
4 files changed, 81 insertions, 0 deletions
diff --git a/include/sway/commands.h b/include/sway/commands.h index 7b8c949b..75534163 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h | |||
@@ -131,6 +131,7 @@ sway_cmd cmd_output; | |||
131 | sway_cmd cmd_permit; | 131 | sway_cmd cmd_permit; |
132 | sway_cmd cmd_reject; | 132 | sway_cmd cmd_reject; |
133 | sway_cmd cmd_reload; | 133 | sway_cmd cmd_reload; |
134 | sway_cmd cmd_rename; | ||
134 | sway_cmd cmd_resize; | 135 | sway_cmd cmd_resize; |
135 | sway_cmd cmd_scratchpad; | 136 | sway_cmd cmd_scratchpad; |
136 | sway_cmd cmd_seamless_mouse; | 137 | sway_cmd cmd_seamless_mouse; |
diff --git a/sway/commands.c b/sway/commands.c index 2115bd8c..6af3c5d0 100644 --- a/sway/commands.c +++ b/sway/commands.c | |||
@@ -167,6 +167,7 @@ static struct cmd_handler command_handlers[] = { | |||
167 | { "move", cmd_move }, | 167 | { "move", cmd_move }, |
168 | { "opacity", cmd_opacity }, | 168 | { "opacity", cmd_opacity }, |
169 | { "reload", cmd_reload }, | 169 | { "reload", cmd_reload }, |
170 | { "rename", cmd_rename }, | ||
170 | { "resize", cmd_resize }, | 171 | { "resize", cmd_resize }, |
171 | { "split", cmd_split }, | 172 | { "split", cmd_split }, |
172 | { "splith", cmd_splith }, | 173 | { "splith", cmd_splith }, |
diff --git a/sway/commands/rename.c b/sway/commands/rename.c new file mode 100644 index 00000000..4be912a9 --- /dev/null +++ b/sway/commands/rename.c | |||
@@ -0,0 +1,78 @@ | |||
1 | #define _XOPEN_SOURCE 500 | ||
2 | #include <string.h> | ||
3 | #include <strings.h> | ||
4 | #include "log.h" | ||
5 | #include "stringop.h" | ||
6 | #include "sway/commands.h" | ||
7 | #include "sway/config.h" | ||
8 | #include "sway/ipc-server.h" | ||
9 | #include "sway/tree/container.h" | ||
10 | #include "sway/tree/workspace.h" | ||
11 | |||
12 | static const char* expected_syntax = | ||
13 | "Expected 'rename workspace <old_name> to <new_name>' or " | ||
14 | "'rename workspace to <new_name>'"; | ||
15 | |||
16 | struct cmd_results *cmd_rename(int argc, char **argv) { | ||
17 | struct cmd_results *error = NULL; | ||
18 | if ((error = checkarg(argc, "rename", EXPECTED_AT_LEAST, 3))) { | ||
19 | return error; | ||
20 | } | ||
21 | if (strcasecmp(argv[0], "workspace") != 0) { | ||
22 | return cmd_results_new(CMD_INVALID, "rename", expected_syntax); | ||
23 | } | ||
24 | |||
25 | int argn = 1; | ||
26 | struct sway_container *workspace; | ||
27 | |||
28 | if (strcasecmp(argv[1], "to") == 0) { | ||
29 | // 'rename workspace to new_name' | ||
30 | workspace = config->handler_context.current_container; | ||
31 | if (workspace->type != C_WORKSPACE) { | ||
32 | workspace = container_parent(workspace, C_WORKSPACE); | ||
33 | } | ||
34 | } else if (strcasecmp(argv[1], "number") == 0) { | ||
35 | // 'rename workspace number x to new_name' | ||
36 | workspace = workspace_by_number(argv[2]); | ||
37 | while (argn < argc && strcasecmp(argv[argn], "to") != 0) { | ||
38 | ++argn; | ||
39 | } | ||
40 | } else { | ||
41 | // 'rename workspace old_name to new_name' | ||
42 | int end = argn; | ||
43 | while (end < argc && strcasecmp(argv[end], "to") != 0) { | ||
44 | ++end; | ||
45 | } | ||
46 | char *old_name = join_args(argv + argn, end - argn); | ||
47 | workspace = workspace_by_name(old_name); | ||
48 | free(old_name); | ||
49 | argn = end; | ||
50 | } | ||
51 | |||
52 | if (!workspace) { | ||
53 | return cmd_results_new(CMD_INVALID, "rename", | ||
54 | "There is no workspace with that name"); | ||
55 | } | ||
56 | |||
57 | ++argn; // move past "to" | ||
58 | |||
59 | if (argn >= argc) { | ||
60 | return cmd_results_new(CMD_INVALID, "rename", expected_syntax); | ||
61 | } | ||
62 | |||
63 | char *new_name = join_args(argv + argn, argc - argn); | ||
64 | struct sway_container *tmp_workspace = workspace_by_name(new_name); | ||
65 | if (tmp_workspace) { | ||
66 | free(new_name); | ||
67 | return cmd_results_new(CMD_INVALID, "rename", | ||
68 | "Workspace already exists"); | ||
69 | } | ||
70 | |||
71 | wlr_log(L_DEBUG, "renaming workspace '%s' to '%s'", workspace->name, new_name); | ||
72 | free(workspace->name); | ||
73 | workspace->name = new_name; | ||
74 | |||
75 | ipc_event_workspace(NULL, workspace, "rename"); | ||
76 | |||
77 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
78 | } | ||
diff --git a/sway/meson.build b/sway/meson.build index 67dbe3dd..f3c319ed 100644 --- a/sway/meson.build +++ b/sway/meson.build | |||
@@ -45,6 +45,7 @@ sway_sources = files( | |||
45 | 'commands/move.c', | 45 | 'commands/move.c', |
46 | 'commands/output.c', | 46 | 'commands/output.c', |
47 | 'commands/reload.c', | 47 | 'commands/reload.c', |
48 | 'commands/rename.c', | ||
48 | 'commands/resize.c', | 49 | 'commands/resize.c', |
49 | 'commands/seat.c', | 50 | 'commands/seat.c', |
50 | 'commands/seat/attach.c', | 51 | 'commands/seat/attach.c', |