]> git.sur5r.net Git - i3/i3/blobdiff - src/commands.c
added "toggle" option to "split" command
[i3/i3] / src / commands.c
index 5b28d03ead938b9384e27faac491b8a15299a020..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) {
@@ -1188,7 +1228,7 @@ 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) {
@@ -1203,7 +1243,22 @@ void cmd_split(I3_CMD, const char *direction) {
         }
 
         DLOG("matching: %p / %s\n", current->con, current->con->name);
-        tree_split(current->con, (direction[0] == 'v' ? VERT : HORIZ));
+        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));
+        }
     }
 
     cmd_output->needs_tree_render = true;
@@ -1218,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);
 
@@ -1233,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;