2018-04-23 05:04:16 +02:00
|
|
|
#define _XOPEN_SOURCE 500
|
2018-08-12 01:32:13 +02:00
|
|
|
#include <ctype.h>
|
2018-04-23 05:04:16 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include "log.h"
|
|
|
|
#include "stringop.h"
|
|
|
|
#include "sway/commands.h"
|
|
|
|
#include "sway/config.h"
|
|
|
|
#include "sway/ipc-server.h"
|
2018-08-18 13:54:09 +02:00
|
|
|
#include "sway/output.h"
|
2018-04-23 05:04:16 +02:00
|
|
|
#include "sway/tree/container.h"
|
|
|
|
#include "sway/tree/workspace.h"
|
|
|
|
|
|
|
|
static const char* expected_syntax =
|
|
|
|
"Expected 'rename workspace <old_name> to <new_name>' or "
|
|
|
|
"'rename workspace to <new_name>'";
|
|
|
|
|
|
|
|
struct cmd_results *cmd_rename(int argc, char **argv) {
|
|
|
|
struct cmd_results *error = NULL;
|
|
|
|
if ((error = checkarg(argc, "rename", EXPECTED_AT_LEAST, 3))) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
if (strcasecmp(argv[0], "workspace") != 0) {
|
|
|
|
return cmd_results_new(CMD_INVALID, "rename", expected_syntax);
|
|
|
|
}
|
|
|
|
|
|
|
|
int argn = 1;
|
|
|
|
struct sway_container *workspace;
|
|
|
|
|
|
|
|
if (strcasecmp(argv[1], "to") == 0) {
|
|
|
|
// 'rename workspace to new_name'
|
|
|
|
workspace = config->handler_context.current_container;
|
|
|
|
if (workspace->type != C_WORKSPACE) {
|
|
|
|
workspace = container_parent(workspace, C_WORKSPACE);
|
|
|
|
}
|
|
|
|
} else if (strcasecmp(argv[1], "number") == 0) {
|
|
|
|
// 'rename workspace number x to new_name'
|
2018-08-12 01:32:13 +02:00
|
|
|
if (!isdigit(argv[2][0])) {
|
|
|
|
return cmd_results_new(CMD_INVALID, "rename",
|
|
|
|
"Invalid workspace number '%s'", argv[2]);
|
|
|
|
}
|
2018-04-23 05:04:16 +02:00
|
|
|
workspace = workspace_by_number(argv[2]);
|
|
|
|
while (argn < argc && strcasecmp(argv[argn], "to") != 0) {
|
|
|
|
++argn;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// 'rename workspace old_name to new_name'
|
|
|
|
int end = argn;
|
|
|
|
while (end < argc && strcasecmp(argv[end], "to") != 0) {
|
|
|
|
++end;
|
|
|
|
}
|
|
|
|
char *old_name = join_args(argv + argn, end - argn);
|
|
|
|
workspace = workspace_by_name(old_name);
|
|
|
|
free(old_name);
|
|
|
|
argn = end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!workspace) {
|
|
|
|
return cmd_results_new(CMD_INVALID, "rename",
|
|
|
|
"There is no workspace with that name");
|
|
|
|
}
|
|
|
|
|
|
|
|
++argn; // move past "to"
|
|
|
|
|
|
|
|
if (argn >= argc) {
|
|
|
|
return cmd_results_new(CMD_INVALID, "rename", expected_syntax);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *new_name = join_args(argv + argn, argc - argn);
|
2018-08-06 13:46:28 +02:00
|
|
|
if (strcasecmp(new_name, "next") == 0 ||
|
|
|
|
strcasecmp(new_name, "prev") == 0 ||
|
|
|
|
strcasecmp(new_name, "next_on_output") == 0 ||
|
|
|
|
strcasecmp(new_name, "prev_on_output") == 0 ||
|
|
|
|
strcasecmp(new_name, "back_and_forth") == 0 ||
|
2018-08-12 01:32:13 +02:00
|
|
|
strcasecmp(new_name, "current") == 0 ||
|
|
|
|
strcasecmp(new_name, "number") == 0) {
|
2018-08-06 13:46:28 +02:00
|
|
|
free(new_name);
|
|
|
|
return cmd_results_new(CMD_INVALID, "rename",
|
|
|
|
"Cannot use special workspace name '%s'", argv[argn]);
|
|
|
|
}
|
2018-04-23 05:04:16 +02:00
|
|
|
struct sway_container *tmp_workspace = workspace_by_name(new_name);
|
|
|
|
if (tmp_workspace) {
|
|
|
|
free(new_name);
|
|
|
|
return cmd_results_new(CMD_INVALID, "rename",
|
|
|
|
"Workspace already exists");
|
|
|
|
}
|
|
|
|
|
2018-07-09 23:54:30 +02:00
|
|
|
wlr_log(WLR_DEBUG, "renaming workspace '%s' to '%s'", workspace->name, new_name);
|
2018-04-23 05:04:16 +02:00
|
|
|
free(workspace->name);
|
|
|
|
workspace->name = new_name;
|
|
|
|
|
2018-08-18 13:54:09 +02:00
|
|
|
output_sort_workspaces(workspace->parent);
|
2018-04-23 05:04:16 +02:00
|
|
|
ipc_event_workspace(NULL, workspace, "rename");
|
|
|
|
|
|
|
|
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
|
|
|
}
|