From 085ec9f3e3755e974916d011bf7004a39154f840 Mon Sep 17 00:00:00 2001
From: Daniel Mueller <deso@posteo.net>
Date: Fri, 29 Sep 2017 21:25:30 -0700
Subject: [PATCH 03/14] Allow for creation of new workspace when selecting next
 one

Until now, i3 when asked to select the next workspace, would cycle
through the existing workspaces. That behavior is not the desired one.
Instead we would like it to switch to the workspace with the next higher
number or create it if it does not exist. A cycling through workspaces
is not needed.
---
 src/workspace.c | 31 +++++++++----------------------
 1 file changed, 9 insertions(+), 22 deletions(-)

diff --git a/src/workspace.c b/src/workspace.c
index bda067..25ab17 100644
--- a/src/workspace.c
+++ b/src/workspace.c
@@ -731,30 +731,17 @@ Con *workspace_next_on_output(void) {
         /* If currently a named workspace, find next named workspace. */
         next = TAILQ_NEXT(current, nodes);
     } else {
-        /* If currently a numbered workspace, find next numbered workspace. */
-        bool found_current = false;
-        NODES_FOREACH (output_get_content(output)) {
-            if (child->type != CT_WORKSPACE) {
-                continue;
-            }
-            if (child->num == -1) {
-                break;
-            }
-            /* Need to check child against current and next because we are
-             * traversing multiple lists and thus are not guaranteed the
-             * relative order between the list of workspaces. */
-            if (current->num < child->num && (!next || child->num < next->num)) {
-                next = child;
-            }
+        int result;
+        char num_str[3];
+        long num = current->num + 1;
 
-            /* If two workspaces have the same number, but different names
-             * (eg '5:a', '5:b') then just take the next one. */
-            if (child == current) {
-                found_current = true;
-            } else if (found_current && current->num == child->num) {
-                return child;
-            }
+        result = snprintf(num_str, sizeof(num_str), "%ld", num);
+        if (result >= sizeof(num_str)) {
+            ELOG("Workspace number too big: %ld\n", num);
+            /* Ups, that will make boom somewhere. */
+            return NULL;
         }
+        next = workspace_get(num_str);
     }
 
     /* Find next named workspace. */
-- 
2.52.0

