From: Michael Stapelberg Date: Sun, 8 Apr 2012 17:17:46 +0000 (+0200) Subject: Implement 'workspace number ' to switch to named workspaces X-Git-Tag: 4.2~28 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=72078c704e592719f4f088282675b28431e91f23;p=i3%2Fi3 Implement 'workspace number ' to switch to named workspaces --- diff --git a/docs/userguide b/docs/userguide index 73db645a..667cdde9 100644 --- a/docs/userguide +++ b/docs/userguide @@ -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 workspace’s name +dynamically. [[resizingconfig]] diff --git a/include/commands.h b/include/commands.h index 88521d31..273f32a8 100644 --- a/include/commands.h +++ b/include/commands.h @@ -91,6 +91,12 @@ void cmd_append_layout(I3_CMD, char *path); */ void cmd_workspace(I3_CMD, char *which); +/** + * Implementation of 'workspace number ' + * + */ +void cmd_workspace_number(I3_CMD, char *which); + /** * Implementation of 'workspace back_and_forth'. * diff --git a/parser-specs/commands.spec b/parser-specs/commands.spec index 1a3f99d9..9ea1c55e 100644 --- a/parser-specs/commands.spec +++ b/parser-specs/commands.spec @@ -77,14 +77,21 @@ state APPEND_LAYOUT: # workspace next|prev|next_on_output|prev_on_output # workspace back_and_forth # workspace +# workspace 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 # focus tiling|floating|mode_toggle diff --git a/src/commands.c b/src/commands.c index 0a5abde5..6c36c256 100644 --- a/src/commands.c +++ b/src/commands.c @@ -676,6 +676,43 @@ void cmd_workspace(I3_CMD, char *which) { cmd_output->json_output = sstrdup("{\"success\": true}"); } +/* + * Implementation of 'workspace 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'. * diff --git a/testcases/t/117-workspace.t b/testcases/t/117-workspace.t index 3c52e31e..9f4d270c 100644 --- a/testcases/t/117-workspace.t +++ b/testcases/t/117-workspace.t @@ -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;