]> git.sur5r.net Git - i3/i3/blobdiff - src/commands.c
added "toggle" option to "split" command
[i3/i3] / src / commands.c
index f1cc969b300d3848952ef8ecda876b4406b10f8f..e9e5c7dddbb23a7ea7cc95f156a65896dd476f1f 100644 (file)
         }                                               \
     } while (0)
 
+/** If an error occured during parsing of the criteria, we want to exit instead
+ * of relying on fallback behavior. See #2091. */
+#define HANDLE_INVALID_MATCH                                   \
+    do {                                                       \
+        if (current_match->error != NULL) {                    \
+            yerror("Invalid match: %s", current_match->error); \
+            return;                                            \
+        }                                                      \
+    } while (0)
+
 /** When the command did not include match criteria (!), we use the currently
  * focused container. Do not confuse this case with a command which included
  * criteria but which did not match any windows. This macro has to be called in
@@ -49,6 +59,8 @@
  */
 #define HANDLE_EMPTY_MATCH                              \
     do {                                                \
+        HANDLE_INVALID_MATCH;                           \
+                                                        \
         if (match_is_empty(current_match)) {            \
             owindow *ow = smalloc(sizeof(owindow));     \
             ow->con = focused;                          \
@@ -282,33 +294,61 @@ void cmd_criteria_match_windows(I3_CMD) {
         next = TAILQ_NEXT(next, owindows);
 
         DLOG("checking if con %p / %s matches\n", current->con, current->con->name);
+
+        /* We use this flag to prevent matching on window-less containers if
+         * only window-specific criteria were specified. */
+        bool accept_match = false;
+
         if (current_match->con_id != NULL) {
+            accept_match = true;
+
             if (current_match->con_id == current->con) {
-                DLOG("matches container!\n");
-                TAILQ_INSERT_TAIL(&owindows, current, owindows);
+                DLOG("con_id matched.\n");
             } else {
-                DLOG("doesnt match\n");
-                free(current);
+                DLOG("con_id does not match.\n");
+                FREE(current);
+                continue;
             }
-        } else if (current_match->mark != NULL && !TAILQ_EMPTY(&(current->con->marks_head))) {
+        }
+
+        if (current_match->mark != NULL && !TAILQ_EMPTY(&(current->con->marks_head))) {
+            accept_match = true;
+            bool matched_by_mark = false;
+
             mark_t *mark;
             TAILQ_FOREACH(mark, &(current->con->marks_head), marks) {
                 if (!regex_matches(current_match->mark, mark->name))
                     continue;
 
                 DLOG("match by mark\n");
-                TAILQ_INSERT_TAIL(&owindows, current, owindows);
+                matched_by_mark = true;
                 break;
             }
-        } else {
-            if (current->con->window && match_matches_window(current_match, current->con->window)) {
+
+            if (!matched_by_mark) {
+                DLOG("mark does not match.\n");
+                FREE(current);
+                continue;
+            }
+        }
+
+        if (current->con->window != NULL) {
+            if (match_matches_window(current_match, current->con->window)) {
                 DLOG("matches window!\n");
-                TAILQ_INSERT_TAIL(&owindows, current, owindows);
+                accept_match = true;
             } else {
                 DLOG("doesnt match\n");
-                free(current);
+                FREE(current);
+                continue;
             }
         }
+
+        if (accept_match) {
+            TAILQ_INSERT_TAIL(&owindows, current, owindows);
+        } else {
+            FREE(current);
+            continue;
+        }
     }
 
     TAILQ_FOREACH(current, &owindows, owindows) {
@@ -403,16 +443,17 @@ void cmd_move_con_to_workspace_back_and_forth(I3_CMD) {
 }
 
 /*
- * Implementation of 'move [window|container] [to] workspace <name>'.
+ * Implementation of 'move [--no-auto-back-and-forth] [window|container] [to] workspace <name>'.
  *
  */
-void cmd_move_con_to_workspace_name(I3_CMD, const char *name) {
+void cmd_move_con_to_workspace_name(I3_CMD, const char *name, const char *_no_auto_back_and_forth) {
     if (strncasecmp(name, "__", strlen("__")) == 0) {
         LOG("You cannot move containers to i3-internal workspaces (\"%s\").\n", name);
         ysuccess(false);
         return;
     }
 
+    const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
     owindow *current;
 
     /* We have nothing to move:
@@ -432,7 +473,8 @@ void cmd_move_con_to_workspace_name(I3_CMD, const char *name) {
     /* get the workspace */
     Con *ws = workspace_get(name, NULL);
 
-    ws = maybe_auto_back_and_forth_workspace(ws);
+    if (!no_auto_back_and_forth)
+        ws = maybe_auto_back_and_forth_workspace(ws);
 
     HANDLE_EMPTY_MATCH;
 
@@ -447,10 +489,11 @@ void cmd_move_con_to_workspace_name(I3_CMD, const char *name) {
 }
 
 /*
- * Implementation of 'move [window|container] [to] workspace number <name>'.
+ * Implementation of 'move [--no-auto-back-and-forth] [window|container] [to] workspace number <name>'.
  *
  */
-void cmd_move_con_to_workspace_number(I3_CMD, const char *which) {
+void cmd_move_con_to_workspace_number(I3_CMD, const char *which, const char *_no_auto_back_and_forth) {
+    const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
     owindow *current;
 
     /* We have nothing to move:
@@ -483,7 +526,8 @@ void cmd_move_con_to_workspace_number(I3_CMD, const char *which) {
         workspace = workspace_get(which, NULL);
     }
 
-    workspace = maybe_auto_back_and_forth_workspace(workspace);
+    if (!no_auto_back_and_forth)
+        workspace = maybe_auto_back_and_forth_workspace(workspace);
 
     HANDLE_EMPTY_MATCH;
 
@@ -615,7 +659,7 @@ static bool cmd_resize_tiling_width_height(I3_CMD, Con *current, const char *way
 
     while (current->type != CT_WORKSPACE &&
            current->type != CT_FLOATING_CON &&
-           con_orientation(current->parent) != search_orientation)
+           (con_orientation(current->parent) != search_orientation || con_num_children(current->parent) == 1))
         current = current->parent;
 
     /* get the default percentage */
@@ -917,10 +961,11 @@ void cmd_workspace(I3_CMD, const char *which) {
 }
 
 /*
- * Implementation of 'workspace number <name>'
+ * Implementation of 'workspace [--no-auto-back-and-forth] number <name>'
  *
  */
-void cmd_workspace_number(I3_CMD, const char *which) {
+void cmd_workspace_number(I3_CMD, const char *which, const char *_no_auto_back_and_forth) {
+    const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
     Con *output, *workspace = NULL;
 
     if (con_get_fullscreen_con(croot, CF_GLOBAL)) {
@@ -948,7 +993,7 @@ void cmd_workspace_number(I3_CMD, const char *which) {
         cmd_output->needs_tree_render = true;
         return;
     }
-    if (maybe_back_and_forth(cmd_output, workspace->name))
+    if (!no_auto_back_and_forth && maybe_back_and_forth(cmd_output, workspace->name))
         return;
     workspace_show(workspace);
 
@@ -976,10 +1021,12 @@ void cmd_workspace_back_and_forth(I3_CMD) {
 }
 
 /*
- * Implementation of 'workspace <name>'
+ * Implementation of 'workspace [--no-auto-back-and-forth] <name>'
  *
  */
-void cmd_workspace_name(I3_CMD, const char *name) {
+void cmd_workspace_name(I3_CMD, const char *name, const char *_no_auto_back_and_forth) {
+    const bool no_auto_back_and_forth = (_no_auto_back_and_forth != NULL);
+
     if (strncasecmp(name, "__", strlen("__")) == 0) {
         LOG("You cannot switch to the i3-internal workspaces (\"%s\").\n", name);
         ysuccess(false);
@@ -993,7 +1040,7 @@ void cmd_workspace_name(I3_CMD, const char *name) {
     }
 
     DLOG("should switch to workspace %s\n", name);
-    if (maybe_back_and_forth(cmd_output, name))
+    if (!no_auto_back_and_forth && maybe_back_and_forth(cmd_output, name))
         return;
     workspace_show_by_name(name);
 
@@ -1181,18 +1228,35 @@ void cmd_move_workspace_to_output(I3_CMD, const char *name) {
 }
 
 /*
- * Implementation of 'split v|h|vertical|horizontal'.
+ * Implementation of 'split v|h|t|vertical|horizontal|toggle'.
  *
  */
 void cmd_split(I3_CMD, const char *direction) {
+    HANDLE_EMPTY_MATCH;
+
     owindow *current;
-    /* TODO: use matches */
     LOG("splitting in direction %c\n", direction[0]);
-    if (match_is_empty(current_match))
-        tree_split(focused, (direction[0] == 'v' ? VERT : HORIZ));
-    else {
-        TAILQ_FOREACH(current, &owindows, owindows) {
-            DLOG("matching: %p / %s\n", current->con, current->con->name);
+    TAILQ_FOREACH(current, &owindows, owindows) {
+        if (con_is_docked(current->con)) {
+            ELOG("Cannot split a docked container, skipping.\n");
+            continue;
+        }
+
+        DLOG("matching: %p / %s\n", current->con, current->con->name);
+        if (direction[0] == 't') {
+            layout_t current_layout;
+            if (current->con->type == CT_WORKSPACE) {
+                current_layout = current->con->layout;
+            } else {
+                current_layout = current->con->parent->layout;
+            }
+            /* toggling split orientation */
+            if (current_layout == L_SPLITH) {
+                tree_split(current->con, VERT);
+            } else {
+                tree_split(current->con, HORIZ);
+            }
+        } else {
             tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
         }
     }
@@ -1209,7 +1273,6 @@ void cmd_split(I3_CMD, const char *direction) {
 void cmd_kill(I3_CMD, const char *kill_mode_str) {
     if (kill_mode_str == NULL)
         kill_mode_str = "window";
-    owindow *current;
 
     DLOG("kill_mode=%s\n", kill_mode_str);
 
@@ -1224,14 +1287,11 @@ void cmd_kill(I3_CMD, const char *kill_mode_str) {
         return;
     }
 
-    /* check if the match is empty, not if the result is empty */
-    if (match_is_empty(current_match))
-        tree_close_con(kill_mode);
-    else {
-        TAILQ_FOREACH(current, &owindows, owindows) {
-            DLOG("matching: %p / %s\n", current->con, current->con->name);
-            tree_close(current->con, kill_mode, false, false);
-        }
+    HANDLE_EMPTY_MATCH;
+
+    owindow *current;
+    TAILQ_FOREACH(current, &owindows, owindows) {
+        con_close(current->con, kill_mode);
     }
 
     cmd_output->needs_tree_render = true;
@@ -1521,9 +1581,10 @@ void cmd_move_direction(I3_CMD, const char *direction, long move_px) {
  *
  */
 void cmd_layout(I3_CMD, const char *layout_str) {
+    HANDLE_EMPTY_MATCH;
+
     if (strcmp(layout_str, "stacking") == 0)
         layout_str = "stacked";
-    owindow *current;
     layout_t layout;
     /* default is a special case which will be handled in con_set_layout(). */
     if (strcmp(layout_str, "default") == 0)
@@ -1543,14 +1604,15 @@ void cmd_layout(I3_CMD, const char *layout_str) {
 
     DLOG("changing layout to %s (%d)\n", layout_str, layout);
 
-    /* check if the match is empty, not if the result is empty */
-    if (match_is_empty(current_match))
-        con_set_layout(focused, layout);
-    else {
-        TAILQ_FOREACH(current, &owindows, owindows) {
-            DLOG("matching: %p / %s\n", current->con, current->con->name);
-            con_set_layout(current->con, layout);
+    owindow *current;
+    TAILQ_FOREACH(current, &owindows, owindows) {
+        if (con_is_docked(current->con)) {
+            ELOG("cannot change layout of a docked container, skipping it.\n");
+            continue;
         }
+
+        DLOG("matching: %p / %s\n", current->con, current->con->name);
+        con_set_layout(current->con, layout);
     }
 
     cmd_output->needs_tree_render = true;
@@ -1749,29 +1811,43 @@ void cmd_move_window_to_position(I3_CMD, const char *method, long x, long y) {
  *
  */
 void cmd_move_window_to_center(I3_CMD, const char *method) {
-    if (!con_is_floating(focused)) {
-        ELOG("Cannot change position. The window/container is not floating\n");
-        yerror("Cannot change position. The window/container is not floating.");
-        return;
-    }
+    bool has_error = false;
+    HANDLE_EMPTY_MATCH;
+
+    owindow *current;
+    TAILQ_FOREACH(current, &owindows, owindows) {
+        Con *floating_con = con_inside_floating(current->con);
+        if (floating_con == NULL) {
+            ELOG("con %p / %s is not floating, cannot move it to the center.\n",
+                 current->con, current->con->name);
 
-    if (strcmp(method, "absolute") == 0) {
-        DLOG("moving to absolute center\n");
-        floating_center(focused->parent, croot->rect);
+            if (!has_error) {
+                yerror("Cannot change position of a window/container because it is not floating.");
+                has_error = true;
+            }
 
-        floating_maybe_reassign_ws(focused->parent);
-        cmd_output->needs_tree_render = true;
-    }
+            continue;
+        }
 
-    if (strcmp(method, "position") == 0) {
-        DLOG("moving to center\n");
-        floating_center(focused->parent, con_get_workspace(focused)->rect);
+        if (strcmp(method, "absolute") == 0) {
+            DLOG("moving to absolute center\n");
+            floating_center(floating_con, croot->rect);
 
-        cmd_output->needs_tree_render = true;
+            floating_maybe_reassign_ws(floating_con);
+            cmd_output->needs_tree_render = true;
+        }
+
+        if (strcmp(method, "position") == 0) {
+            DLOG("moving to center\n");
+            floating_center(floating_con, con_get_workspace(floating_con)->rect);
+
+            cmd_output->needs_tree_render = true;
+        }
     }
 
     // XXX: default reply for now, make this a better reply
-    ysuccess(true);
+    if (!has_error)
+        ysuccess(true);
 }
 
 /*