Implement 'workspace number <number>' to switch to named workspaces

This commit is contained in:
Michael Stapelberg 2012-04-08 19:17:46 +02:00
parent 42bbdbdfc1
commit 72078c704e
5 changed files with 71 additions and 1 deletions

View File

@ -1351,7 +1351,10 @@ bindsym mod+2 workspace 2: www
Note that the workspace will really be named "1: mail". i3 treats workspace
names beginning with a number in a slightly special way. Normally, named
workspaces are ordered the way they appeared. When they start with a number, i3
will order them numerically.
will order them numerically. Also, you will be able to use +workspace number 1+
to switch to the workspace which begins with number 1, regardless of which name
it has. This is useful in case you are changing the workspaces name
dynamically.
[[resizingconfig]]

View File

@ -91,6 +91,12 @@ void cmd_append_layout(I3_CMD, char *path);
*/
void cmd_workspace(I3_CMD, char *which);
/**
* Implementation of 'workspace number <number>'
*
*/
void cmd_workspace_number(I3_CMD, char *which);
/**
* Implementation of 'workspace back_and_forth'.
*

View File

@ -77,14 +77,21 @@ state APPEND_LAYOUT:
# workspace next|prev|next_on_output|prev_on_output
# workspace back_and_forth
# workspace <name>
# workspace number <number>
state WORKSPACE:
direction = 'next_on_output', 'prev_on_output', 'next', 'prev'
-> call cmd_workspace($direction)
'back_and_forth'
-> call cmd_workspace_back_and_forth()
'number'
-> WORKSPACE_NUMBER
workspace = string
-> call cmd_workspace_name($workspace)
state WORKSPACE_NUMBER:
workspace = string
-> call cmd_workspace_number($workspace)
# focus left|right|up|down
# focus output <output>
# focus tiling|floating|mode_toggle

View File

@ -676,6 +676,43 @@ void cmd_workspace(I3_CMD, char *which) {
cmd_output->json_output = sstrdup("{\"success\": true}");
}
/*
* Implementation of 'workspace number <number>'
*
*/
void cmd_workspace_number(I3_CMD, char *which) {
Con *output, *workspace;
char *endptr = NULL;
long parsed_num = strtol(which, &endptr, 10);
if (parsed_num == LONG_MIN ||
parsed_num == LONG_MAX ||
parsed_num < 0 ||
*endptr != '\0') {
LOG("Could not parse \"%s\" as a number.\n", which);
cmd_output->json_output = sstrdup("{\"success\": false, "
"\"error\": \"Could not parse number\"}");
return;
}
TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
GREP_FIRST(workspace, output_get_content(output),
child->num == parsed_num);
if (!workspace) {
LOG("There is no workspace with number %d.\n", parsed_num);
cmd_output->json_output = sstrdup("{\"success\": false, "
"\"error\": \"No such workspace\"}");
return;
}
workspace_show(workspace);
cmd_output->needs_tree_render = true;
// XXX: default reply for now, make this a better reply
cmd_output->json_output = sstrdup("{\"success\": true}");
}
/*
* Implementation of 'workspace back_and_forth'.
*

View File

@ -117,4 +117,21 @@ $ws = get_ws("aa: $tmp");
ok(defined($ws), "workspace aa: $tmp was created");
is($ws->{num}, -1, 'workspace number is -1');
################################################################################
# Check that we can go to workspace "4: foo" with the command
# "workspace number 4".
################################################################################
ok(!workspace_exists('4'), 'workspace 4 does not exist');
ok(!workspace_exists('4: foo'), 'workspace 4: foo does not exist yet');
cmd 'workspace 4: foo';
ok(workspace_exists('4: foo'), 'workspace 4: foo was created');
cmd 'open';
cmd 'workspace 3';
ok(workspace_exists('4: foo'), 'workspace 4: foo still open');
cmd 'workspace number 4';
is(focused_ws(), '4: foo', 'now on workspace 4: foo');
ok(!workspace_exists('4'), 'workspace 4 still does not exist');
done_testing;