]> git.sur5r.net Git - i3/i3/blob - src/cmdparse.y
Implement 'workspace back_and_forth' (Patch by Michael Walle)
[i3/i3] / src / cmdparse.y
1 %{
2 /*
3  * vim:ts=4:sw=4:expandtab
4  *
5  * i3 - an improved dynamic tiling window manager
6  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
7  *
8  * cmdparse.y: the parser for commands you send to i3 (or bind on keys)
9  *
10
11  */
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
15 #include <fcntl.h>
16 #include <float.h>
17
18 #include "all.h"
19
20 /** When the command did not include match criteria (!), we use the currently
21  * focused command. Do not confuse this case with a command which included
22  * criteria but which did not match any windows. This macro has to be called in
23  * every command.
24  */
25 #define HANDLE_EMPTY_MATCH do { \
26     if (match_is_empty(&current_match)) { \
27         owindow *ow = smalloc(sizeof(owindow)); \
28         ow->con = focused; \
29         TAILQ_INIT(&owindows); \
30         TAILQ_INSERT_TAIL(&owindows, ow, owindows); \
31     } \
32 } while (0)
33
34 typedef struct yy_buffer_state *YY_BUFFER_STATE;
35 extern int cmdyylex(struct context *context);
36 extern int cmdyyparse(void);
37 extern int cmdyylex_destroy(void);
38 extern FILE *cmdyyin;
39 YY_BUFFER_STATE cmdyy_scan_string(const char *);
40
41 static struct context *context;
42 static Match current_match;
43
44 /*
45  * Helper data structure for an operation window (window on which the operation
46  * will be performed). Used to build the TAILQ owindows.
47  *
48  */
49 typedef struct owindow {
50     Con *con;
51     TAILQ_ENTRY(owindow) owindows;
52 } owindow;
53 static TAILQ_HEAD(owindows_head, owindow) owindows;
54
55 /* Holds the JSON which will be returned via IPC or NULL for the default return
56  * message */
57 static char *json_output;
58
59 /* We don’t need yydebug for now, as we got decent error messages using
60  * yyerror(). Should you ever want to extend the parser, it might be handy
61  * to just comment it in again, so it stays here. */
62 //int cmdyydebug = 1;
63
64 void cmdyyerror(const char *error_message) {
65     ELOG("\n");
66     ELOG("CMD: %s\n", error_message);
67     ELOG("CMD: in command:\n");
68     ELOG("CMD:   %s\n", context->line_copy);
69     ELOG("CMD:   ");
70     for (int c = 1; c <= context->last_column; c++)
71         if (c >= context->first_column)
72                 printf("^");
73         else printf(" ");
74     printf("\n");
75     ELOG("\n");
76     context->compact_error = sstrdup(error_message);
77 }
78
79 int cmdyywrap() {
80     return 1;
81 }
82
83 char *parse_cmd(const char *new) {
84     json_output = NULL;
85     LOG("COMMAND: *%s*\n", new);
86     cmdyy_scan_string(new);
87
88     match_init(&current_match);
89     context = scalloc(sizeof(struct context));
90     context->filename = "cmd";
91     if (cmdyyparse() != 0) {
92         fprintf(stderr, "Could not parse command\n");
93         asprintf(&json_output, "{\"success\":false, \"error\":\"%s at position %d\"}",
94                  context->compact_error, context->first_column);
95         FREE(context->line_copy);
96         FREE(context->compact_error);
97         free(context);
98         return json_output;
99     }
100     printf("done, json output = %s\n", json_output);
101
102     cmdyylex_destroy();
103     FREE(context->line_copy);
104     FREE(context->compact_error);
105     free(context);
106     return json_output;
107 }
108
109 /*
110  * Returns true if a is definitely greater than b (using the given epsilon)
111  *
112  */
113 bool definitelyGreaterThan(float a, float b, float epsilon) {
114     return (a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * epsilon);
115 }
116
117 %}
118
119 %error-verbose
120 %lex-param { struct context *context }
121
122 %union {
123     char *string;
124     char chr;
125     int number;
126 }
127
128 %token              TOK_EXEC            "exec"
129 %token              TOK_EXIT            "exit"
130 %token              TOK_RELOAD          "reload"
131 %token              TOK_RESTART         "restart"
132 %token              TOK_KILL            "kill"
133 %token              TOK_WINDOW          "window"
134 %token              TOK_CLIENT          "client"
135 %token              TOK_FULLSCREEN      "fullscreen"
136 %token              TOK_GLOBAL          "global"
137 %token              TOK_LAYOUT          "layout"
138 %token              TOK_DEFAULT         "default"
139 %token              TOK_STACKED         "stacked"
140 %token              TOK_TABBED          "tabbed"
141 %token              TOK_BORDER          "border"
142 %token              TOK_NORMAL          "normal"
143 %token              TOK_NONE            "none"
144 %token              TOK_1PIXEL          "1pixel"
145 %token              TOK_MODE            "mode"
146 %token              TOK_TILING          "tiling"
147 %token              TOK_FLOATING        "floating"
148 %token              TOK_MODE_TOGGLE     "mode_toggle"
149 %token              TOK_ENABLE          "enable"
150 %token              TOK_DISABLE         "disable"
151 %token              TOK_WORKSPACE       "workspace"
152 %token              TOK_OUTPUT          "output"
153 %token              TOK_TOGGLE          "toggle"
154 %token              TOK_FOCUS           "focus"
155 %token              TOK_MOVE            "move"
156 %token              TOK_OPEN            "open"
157 %token              TOK_NEXT            "next"
158 %token              TOK_PREV            "prev"
159 %token              TOK_SPLIT           "split"
160 %token              TOK_HORIZONTAL      "horizontal"
161 %token              TOK_VERTICAL        "vertical"
162 %token              TOK_UP              "up"
163 %token              TOK_DOWN            "down"
164 %token              TOK_LEFT            "left"
165 %token              TOK_RIGHT           "right"
166 %token              TOK_PARENT          "parent"
167 %token              TOK_CHILD           "child"
168 %token              TOK_APPEND_LAYOUT   "append_layout"
169 %token              TOK_MARK            "mark"
170 %token              TOK_RESIZE          "resize"
171 %token              TOK_GROW            "grow"
172 %token              TOK_SHRINK          "shrink"
173 %token              TOK_PX              "px"
174 %token              TOK_OR              "or"
175 %token              TOK_PPT             "ppt"
176 %token              TOK_NOP             "nop"
177 %token              TOK_BACK_AND_FORTH  "back_and_forth"
178
179 %token              TOK_CLASS           "class"
180 %token              TOK_INSTANCE        "instance"
181 %token              TOK_WINDOW_ROLE     "window_role"
182 %token              TOK_ID              "id"
183 %token              TOK_CON_ID          "con_id"
184 %token              TOK_TITLE           "title"
185
186 %token  <string>    STR                 "<string>"
187 %token  <number>    NUMBER              "<number>"
188
189 %type   <number>    direction
190 %type   <number>    split_direction
191 %type   <number>    fullscreen_mode
192 %type   <number>    level
193 %type   <number>    window_mode
194 %type   <number>    boolean
195 %type   <number>    border_style
196 %type   <number>    layout_mode
197 %type   <number>    resize_px
198 %type   <number>    resize_way
199 %type   <number>    resize_tiling
200 %type   <number>    optional_kill_mode
201
202 %%
203
204 commands:
205     commands ';' command
206     | command
207     {
208         owindow *current;
209
210         printf("single command completely parsed, dropping state...\n");
211         while (!TAILQ_EMPTY(&owindows)) {
212             current = TAILQ_FIRST(&owindows);
213             TAILQ_REMOVE(&owindows, current, owindows);
214             free(current);
215         }
216         match_init(&current_match);
217     }
218     ;
219
220 command:
221     match operations
222     ;
223
224 match:
225     | matchstart criteria matchend
226     {
227         printf("match parsed\n");
228     }
229     ;
230
231 matchstart:
232     '['
233     {
234         printf("start\n");
235         match_init(&current_match);
236         TAILQ_INIT(&owindows);
237         /* copy all_cons */
238         Con *con;
239         TAILQ_FOREACH(con, &all_cons, all_cons) {
240             owindow *ow = smalloc(sizeof(owindow));
241             ow->con = con;
242             TAILQ_INSERT_TAIL(&owindows, ow, owindows);
243         }
244     }
245     ;
246
247 matchend:
248     ']'
249     {
250         owindow *next, *current;
251
252         printf("match specification finished, matching...\n");
253         /* copy the old list head to iterate through it and start with a fresh
254          * list which will contain only matching windows */
255         struct owindows_head old = owindows;
256         TAILQ_INIT(&owindows);
257         for (next = TAILQ_FIRST(&old); next != TAILQ_END(&old);) {
258             /* make a copy of the next pointer and advance the pointer to the
259              * next element as we are going to invalidate the element’s
260              * next/prev pointers by calling TAILQ_INSERT_TAIL later */
261             current = next;
262             next = TAILQ_NEXT(next, owindows);
263
264             printf("checking if con %p / %s matches\n", current->con, current->con->name);
265             if (current_match.con_id != NULL) {
266                 if (current_match.con_id == current->con) {
267                     printf("matches container!\n");
268                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
269
270                 }
271             } else if (current_match.mark != NULL && current->con->mark != NULL &&
272                        regex_matches(current_match.mark, current->con->mark)) {
273                 printf("match by mark\n");
274                 TAILQ_INSERT_TAIL(&owindows, current, owindows);
275             } else {
276                 if (current->con->window == NULL)
277                     continue;
278                 if (match_matches_window(&current_match, current->con->window)) {
279                     printf("matches window!\n");
280                     TAILQ_INSERT_TAIL(&owindows, current, owindows);
281                 } else {
282                     printf("doesnt match\n");
283                     free(current);
284                 }
285             }
286         }
287
288         TAILQ_FOREACH(current, &owindows, owindows) {
289             printf("matching: %p / %s\n", current->con, current->con->name);
290         }
291
292     }
293     ;
294
295 criteria:
296     criteria criterion
297     | criterion
298     ;
299
300 criterion:
301     TOK_CLASS '=' STR
302     {
303         printf("criteria: class = %s\n", $3);
304         current_match.class = regex_new($3);
305         free($3);
306     }
307     | TOK_INSTANCE '=' STR
308     {
309         printf("criteria: instance = %s\n", $3);
310         current_match.instance = regex_new($3);
311         free($3);
312     }
313     | TOK_WINDOW_ROLE '=' STR
314     {
315         printf("criteria: window_role = %s\n", $3);
316         current_match.role = regex_new($3);
317         free($3);
318     }
319     | TOK_CON_ID '=' STR
320     {
321         printf("criteria: id = %s\n", $3);
322         char *end;
323         long parsed = strtol($3, &end, 10);
324         if (parsed == LONG_MIN ||
325             parsed == LONG_MAX ||
326             parsed < 0 ||
327             (end && *end != '\0')) {
328             ELOG("Could not parse con id \"%s\"\n", $3);
329         } else {
330             current_match.con_id = (Con*)parsed;
331             printf("id as int = %p\n", current_match.con_id);
332         }
333     }
334     | TOK_ID '=' STR
335     {
336         printf("criteria: window id = %s\n", $3);
337         char *end;
338         long parsed = strtol($3, &end, 10);
339         if (parsed == LONG_MIN ||
340             parsed == LONG_MAX ||
341             parsed < 0 ||
342             (end && *end != '\0')) {
343             ELOG("Could not parse window id \"%s\"\n", $3);
344         } else {
345             current_match.id = parsed;
346             printf("window id as int = %d\n", current_match.id);
347         }
348     }
349     | TOK_MARK '=' STR
350     {
351         printf("criteria: mark = %s\n", $3);
352         current_match.mark = regex_new($3);
353         free($3);
354     }
355     | TOK_TITLE '=' STR
356     {
357         printf("criteria: title = %s\n", $3);
358         current_match.title = regex_new($3);
359         free($3);
360     }
361     ;
362
363 operations:
364     operation
365     | operations ',' operation
366     ;
367
368 operation:
369     exec
370     | exit
371     | restart
372     | reload
373     | border
374     | layout
375     | append_layout
376     | move
377     | workspace
378     | focus
379     | kill
380     | open
381     | fullscreen
382     | split
383     | floating
384     | mark
385     | resize
386     | nop
387     | mode
388     ;
389
390 exec:
391     TOK_EXEC STR
392     {
393         printf("should execute %s\n", $2);
394         start_application($2);
395         free($2);
396     }
397     ;
398
399 exit:
400     TOK_EXIT
401     {
402         printf("exit, bye bye\n");
403         exit(0);
404     }
405     ;
406
407 reload:
408     TOK_RELOAD
409     {
410         printf("reloading\n");
411         kill_configerror_nagbar(false);
412         load_configuration(conn, NULL, true);
413         x_set_i3_atoms();
414         /* Send an IPC event just in case the ws names have changed */
415         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"reload\"}");
416     }
417     ;
418
419 restart:
420     TOK_RESTART
421     {
422         printf("restarting i3\n");
423         i3_restart(false);
424     }
425     ;
426
427 focus:
428     TOK_FOCUS
429     {
430         owindow *current;
431
432         if (match_is_empty(&current_match)) {
433             ELOG("You have to specify which window/container should be focused.\n");
434             ELOG("Example: [class=\"urxvt\" title=\"irssi\"] focus\n");
435
436             asprintf(&json_output, "{\"success\":false, \"error\":\"You have to "
437                      "specify which window/container should be focused\"}");
438             break;
439         }
440
441         int count = 0;
442         TAILQ_FOREACH(current, &owindows, owindows) {
443             Con *ws = con_get_workspace(current->con);
444
445             /* If the container is not on the current workspace,
446              * workspace_show() will switch to a different workspace and (if
447              * enabled) trigger a mouse pointer warp to the currently focused
448              * container (!) on the target workspace.
449              *
450              * Therefore, before calling workspace_show(), we make sure that
451              * 'current' will be focused on the workspace. However, we cannot
452              * just con_focus(current) because then the pointer will not be
453              * warped at all (the code thinks we are already there).
454              *
455              * So we focus 'current' to make it the currently focused window of
456              * the target workspace, then revert focus. */
457             Con *currently_focused = focused;
458             con_focus(current->con);
459             con_focus(currently_focused);
460
461             /* Now switch to the workspace, then focus */
462             workspace_show(ws);
463             LOG("focusing %p / %s\n", current->con, current->con->name);
464             con_focus(current->con);
465             count++;
466         }
467
468         if (count > 1)
469             LOG("WARNING: Your criteria for the focus command matches %d containers, "
470                 "while only exactly one container can be focused at a time.\n", count);
471
472         tree_render();
473     }
474     | TOK_FOCUS direction
475     {
476         int direction = $2;
477         switch (direction) {
478             case TOK_LEFT:
479                 LOG("Focusing left\n");
480                 tree_next('p', HORIZ);
481                 break;
482             case TOK_RIGHT:
483                 LOG("Focusing right\n");
484                 tree_next('n', HORIZ);
485                 break;
486             case TOK_UP:
487                 LOG("Focusing up\n");
488                 tree_next('p', VERT);
489                 break;
490             case TOK_DOWN:
491                 LOG("Focusing down\n");
492                 tree_next('n', VERT);
493                 break;
494             default:
495                 ELOG("Invalid focus direction (%d)\n", direction);
496                 break;
497         }
498
499         tree_render();
500     }
501     | TOK_FOCUS window_mode
502     {
503         printf("should focus: ");
504
505         if ($2 == TOK_TILING)
506             printf("tiling\n");
507         else if ($2 == TOK_FLOATING)
508             printf("floating\n");
509         else printf("mode toggle\n");
510
511         Con *ws = con_get_workspace(focused);
512         Con *current;
513         if (ws != NULL) {
514             int to_focus = $2;
515             if ($2 == TOK_MODE_TOGGLE) {
516                 current = TAILQ_FIRST(&(ws->focus_head));
517                 if (current != NULL && current->type == CT_FLOATING_CON)
518                     to_focus = TOK_TILING;
519                 else to_focus = TOK_FLOATING;
520             }
521             TAILQ_FOREACH(current, &(ws->focus_head), focused) {
522                 if ((to_focus == TOK_FLOATING && current->type != CT_FLOATING_CON) ||
523                     (to_focus == TOK_TILING && current->type == CT_FLOATING_CON))
524                     continue;
525
526                 con_focus(con_descend_focused(current));
527                 break;
528             }
529         }
530
531         tree_render();
532     }
533     | TOK_FOCUS level
534     {
535         if ($2 == TOK_PARENT)
536             level_up();
537         else level_down();
538
539         tree_render();
540     }
541     ;
542
543 window_mode:
544     TOK_TILING        { $$ = TOK_TILING; }
545     | TOK_FLOATING    { $$ = TOK_FLOATING; }
546     | TOK_MODE_TOGGLE { $$ = TOK_MODE_TOGGLE; }
547     ;
548
549 level:
550     TOK_PARENT  { $$ = TOK_PARENT; }
551     | TOK_CHILD { $$ = TOK_CHILD;  }
552     ;
553
554 kill:
555     TOK_KILL optional_kill_mode
556     {
557         owindow *current;
558
559         printf("killing!\n");
560         /* check if the match is empty, not if the result is empty */
561         if (match_is_empty(&current_match))
562             tree_close_con($2);
563         else {
564             TAILQ_FOREACH(current, &owindows, owindows) {
565                 printf("matching: %p / %s\n", current->con, current->con->name);
566                 tree_close(current->con, $2, false, false);
567             }
568         }
569
570         tree_render();
571     }
572     ;
573
574 optional_kill_mode:
575     /* empty */             { $$ = KILL_WINDOW; }
576     | TOK_WINDOW { $$ = KILL_WINDOW; }
577     | TOK_CLIENT { $$ = KILL_CLIENT; }
578     ;
579
580 workspace:
581     TOK_WORKSPACE TOK_NEXT
582     {
583         workspace_show(workspace_next());
584         tree_render();
585     }
586     | TOK_WORKSPACE TOK_PREV
587     {
588         workspace_show(workspace_prev());
589         tree_render();
590     }
591     | TOK_WORKSPACE TOK_BACK_AND_FORTH
592     {
593         workspace_back_and_forth();
594         tree_render();
595     }
596     | TOK_WORKSPACE STR
597     {
598         printf("should switch to workspace %s\n", $2);
599
600         Con *ws = con_get_workspace(focused);
601
602         /* Check if the command wants to switch to the current workspace */
603         if (strcmp(ws->name, $2) == 0) {
604             printf("This workspace is already focused.\n");
605             if (config.workspace_auto_back_and_forth) {
606                 workspace_back_and_forth();
607                 free($2);
608                 tree_render();
609             }
610             break;
611         }
612
613         workspace_show_by_name($2);
614         free($2);
615
616         tree_render();
617     }
618     ;
619
620 open:
621     TOK_OPEN
622     {
623         printf("opening new container\n");
624         Con *con = tree_open_con(NULL, NULL);
625         con_focus(con);
626         asprintf(&json_output, "{\"success\":true, \"id\":%ld}", (long int)con);
627
628         tree_render();
629     }
630     ;
631
632 fullscreen:
633     TOK_FULLSCREEN fullscreen_mode
634     {
635         printf("toggling fullscreen, mode = %s\n", ($2 == CF_OUTPUT ? "normal" : "global"));
636         owindow *current;
637
638         HANDLE_EMPTY_MATCH;
639
640         TAILQ_FOREACH(current, &owindows, owindows) {
641             printf("matching: %p / %s\n", current->con, current->con->name);
642             con_toggle_fullscreen(current->con, $2);
643         }
644
645         tree_render();
646     }
647     ;
648
649 fullscreen_mode:
650     /* empty */  { $$ = CF_OUTPUT; }
651     | TOK_GLOBAL { $$ = CF_GLOBAL; }
652     ;
653
654 split:
655     TOK_SPLIT split_direction
656     {
657         /* TODO: use matches */
658         printf("splitting in direction %c\n", $2);
659         tree_split(focused, ($2 == 'v' ? VERT : HORIZ));
660
661         tree_render();
662     }
663     ;
664
665 split_direction:
666     TOK_HORIZONTAL  { $$ = 'h'; }
667     | 'h'           { $$ = 'h'; }
668     | TOK_VERTICAL  { $$ = 'v'; }
669     | 'v'           { $$ = 'v'; }
670     ;
671
672 floating:
673     TOK_FLOATING boolean
674     {
675         HANDLE_EMPTY_MATCH;
676
677         owindow *current;
678         TAILQ_FOREACH(current, &owindows, owindows) {
679             printf("matching: %p / %s\n", current->con, current->con->name);
680             if ($2 == TOK_TOGGLE) {
681                 printf("should toggle mode\n");
682                 toggle_floating_mode(current->con, false);
683             } else {
684                 printf("should switch mode to %s\n", ($2 == TOK_FLOATING ? "floating" : "tiling"));
685                 if ($2 == TOK_ENABLE) {
686                     floating_enable(current->con, false);
687                 } else {
688                     floating_disable(current->con, false);
689                 }
690             }
691         }
692
693         tree_render();
694     }
695     ;
696
697 boolean:
698     TOK_ENABLE    { $$ = TOK_ENABLE; }
699     | TOK_DISABLE { $$ = TOK_DISABLE; }
700     | TOK_TOGGLE  { $$ = TOK_TOGGLE; }
701     ;
702
703 border:
704     TOK_BORDER border_style
705     {
706         printf("border style should be changed to %d\n", $2);
707         owindow *current;
708
709         HANDLE_EMPTY_MATCH;
710
711         TAILQ_FOREACH(current, &owindows, owindows) {
712             printf("matching: %p / %s\n", current->con, current->con->name);
713             if ($2 == TOK_TOGGLE) {
714                 current->con->border_style++;
715                 current->con->border_style %= 3;
716             } else current->con->border_style = $2;
717         }
718
719         tree_render();
720     }
721     ;
722
723 border_style:
724     TOK_NORMAL      { $$ = BS_NORMAL; }
725     | TOK_NONE      { $$ = BS_NONE; }
726     | TOK_1PIXEL    { $$ = BS_1PIXEL; }
727     | TOK_TOGGLE    { $$ = TOK_TOGGLE; }
728     ;
729
730 move:
731     TOK_MOVE direction
732     {
733         printf("moving in direction %d\n", $2);
734         tree_move($2);
735
736         tree_render();
737     }
738     | TOK_MOVE TOK_WORKSPACE STR
739     {
740         owindow *current;
741
742         printf("should move window to workspace %s\n", $3);
743         /* get the workspace */
744         Con *ws = workspace_get($3, NULL);
745         free($3);
746
747         HANDLE_EMPTY_MATCH;
748
749         TAILQ_FOREACH(current, &owindows, owindows) {
750             printf("matching: %p / %s\n", current->con, current->con->name);
751             con_move_to_workspace(current->con, ws, true, false);
752         }
753
754         tree_render();
755     }
756     | TOK_MOVE TOK_WORKSPACE TOK_NEXT
757     {
758         owindow *current;
759
760         /* get the workspace */
761         Con *ws = workspace_next();
762
763         HANDLE_EMPTY_MATCH;
764
765         TAILQ_FOREACH(current, &owindows, owindows) {
766             printf("matching: %p / %s\n", current->con, current->con->name);
767             con_move_to_workspace(current->con, ws, true, false);
768         }
769
770         tree_render();
771     }
772     | TOK_MOVE TOK_WORKSPACE TOK_PREV
773     {
774         owindow *current;
775
776         /* get the workspace */
777         Con *ws = workspace_prev();
778
779         HANDLE_EMPTY_MATCH;
780
781         TAILQ_FOREACH(current, &owindows, owindows) {
782             printf("matching: %p / %s\n", current->con, current->con->name);
783             con_move_to_workspace(current->con, ws, true, false);
784         }
785
786         tree_render();
787     }
788     | TOK_MOVE TOK_OUTPUT STR
789     {
790         owindow *current;
791
792         printf("should move window to output %s", $3);
793
794         HANDLE_EMPTY_MATCH;
795
796         /* get the output */
797         Output *current_output = NULL;
798         Output *output;
799
800         TAILQ_FOREACH(current, &owindows, owindows)
801             current_output = get_output_containing(current->con->rect.x, current->con->rect.y);
802
803         assert(current_output != NULL);
804
805         if (strcasecmp($3, "up") == 0)
806             output = get_output_next(D_UP, current_output);
807         else if (strcasecmp($3, "down") == 0)
808             output = get_output_next(D_DOWN, current_output);
809         else if (strcasecmp($3, "left") == 0)
810             output = get_output_next(D_LEFT, current_output);
811         else if (strcasecmp($3, "right") == 0)
812             output = get_output_next(D_RIGHT, current_output);
813         else
814             output = get_output_by_name($3);
815         free($3);
816
817         if (!output)
818             break;
819
820         /* get visible workspace on output */
821         Con *ws = NULL;
822         GREP_FIRST(ws, output_get_content(output->con), workspace_is_visible(child));
823         if (!ws)
824             break;
825
826         TAILQ_FOREACH(current, &owindows, owindows) {
827             printf("matching: %p / %s\n", current->con, current->con->name);
828             con_move_to_workspace(current->con, ws, true, false);
829         }
830
831         tree_render();
832     }
833     ;
834
835 append_layout:
836     TOK_APPEND_LAYOUT STR
837     {
838         printf("restoring \"%s\"\n", $2);
839         tree_append_json($2);
840         free($2);
841         tree_render();
842     }
843     ;
844
845 layout:
846     TOK_LAYOUT layout_mode
847     {
848         printf("changing layout to %d\n", $2);
849         owindow *current;
850
851         /* check if the match is empty, not if the result is empty */
852         if (match_is_empty(&current_match))
853             con_set_layout(focused->parent, $2);
854         else {
855             TAILQ_FOREACH(current, &owindows, owindows) {
856                 printf("matching: %p / %s\n", current->con, current->con->name);
857                 con_set_layout(current->con, $2);
858             }
859         }
860
861         tree_render();
862     }
863     ;
864
865 layout_mode:
866     TOK_DEFAULT   { $$ = L_DEFAULT; }
867     | TOK_STACKED { $$ = L_STACKED; }
868     | TOK_TABBED  { $$ = L_TABBED; }
869     ;
870
871 mark:
872     TOK_MARK STR
873     {
874         printf("marking window with str %s\n", $2);
875         owindow *current;
876
877         HANDLE_EMPTY_MATCH;
878
879         TAILQ_FOREACH(current, &owindows, owindows) {
880             printf("matching: %p / %s\n", current->con, current->con->name);
881             current->con->mark = sstrdup($2);
882         }
883
884         free($<string>2);
885
886         tree_render();
887     }
888     ;
889
890 nop:
891     TOK_NOP STR
892     {
893         printf("-------------------------------------------------\n");
894         printf("  NOP: %s\n", $2);
895         printf("-------------------------------------------------\n");
896         free($2);
897     }
898     ;
899
900 resize:
901     TOK_RESIZE resize_way direction resize_px resize_tiling
902     {
903         /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
904         printf("resizing in way %d, direction %d, px %d or ppt %d\n", $2, $3, $4, $5);
905         int direction = $3;
906         int px = $4;
907         int ppt = $5;
908         if ($2 == TOK_SHRINK) {
909             px *= -1;
910             ppt *= -1;
911         }
912
913         if (con_is_floating(focused)) {
914             printf("floating resize\n");
915             if (direction == TOK_UP) {
916                 focused->parent->rect.y -= px;
917                 focused->parent->rect.height += px;
918             } else if (direction == TOK_DOWN) {
919                 focused->parent->rect.height += px;
920             } else if (direction == TOK_LEFT) {
921                 focused->parent->rect.x -= px;
922                 focused->parent->rect.width += px;
923             } else {
924                 focused->parent->rect.width += px;
925             }
926         } else {
927             LOG("tiling resize\n");
928             /* get the appropriate current container (skip stacked/tabbed cons) */
929             Con *current = focused;
930             while (current->parent->layout == L_STACKED ||
931                    current->parent->layout == L_TABBED)
932                 current = current->parent;
933             /* get the default percentage */
934             int children = con_num_children(current->parent);
935             Con *other;
936             LOG("ins. %d children\n", children);
937             double percentage = 1.0 / children;
938             LOG("default percentage = %f\n", percentage);
939
940             orientation_t orientation = current->parent->orientation;
941
942             if ((orientation == HORIZ &&
943                  (direction == TOK_UP || direction == TOK_DOWN)) ||
944                 (orientation == VERT &&
945                  (direction == TOK_LEFT || direction == TOK_RIGHT))) {
946                 LOG("You cannot resize in that direction. Your focus is in a %s split container currently.\n",
947                     (orientation == HORIZ ? "horizontal" : "vertical"));
948                 break;
949             }
950
951             if (direction == TOK_UP || direction == TOK_LEFT) {
952                 other = TAILQ_PREV(current, nodes_head, nodes);
953             } else {
954                 other = TAILQ_NEXT(current, nodes);
955             }
956             if (other == TAILQ_END(workspaces)) {
957                 LOG("No other container in this direction found, cannot resize.\n");
958                 break;
959             }
960             LOG("other->percent = %f\n", other->percent);
961             LOG("current->percent before = %f\n", current->percent);
962             if (current->percent == 0.0)
963                 current->percent = percentage;
964             if (other->percent == 0.0)
965                 other->percent = percentage;
966             double new_current_percent = current->percent + ((double)ppt / 100.0);
967             double new_other_percent = other->percent - ((double)ppt / 100.0);
968             LOG("new_current_percent = %f\n", new_current_percent);
969             LOG("new_other_percent = %f\n", new_other_percent);
970             /* Ensure that the new percentages are positive and greater than
971              * 0.05 to have a reasonable minimum size. */
972             if (definitelyGreaterThan(new_current_percent, 0.05, DBL_EPSILON) &&
973                 definitelyGreaterThan(new_other_percent, 0.05, DBL_EPSILON)) {
974                 current->percent += ((double)ppt / 100.0);
975                 other->percent -= ((double)ppt / 100.0);
976                 LOG("current->percent after = %f\n", current->percent);
977                 LOG("other->percent after = %f\n", other->percent);
978             } else {
979                 LOG("Not resizing, already at minimum size\n");
980             }
981         }
982
983         tree_render();
984     }
985     ;
986
987 resize_px:
988     /* empty */
989     {
990         $$ = 10;
991     }
992     | NUMBER TOK_PX
993     {
994         $$ = $1;
995     }
996     ;
997
998 resize_tiling:
999     /* empty */
1000     {
1001         $$ = 10;
1002     }
1003     | TOK_OR NUMBER TOK_PPT
1004     {
1005         $$ = $2;
1006     }
1007     ;
1008
1009 resize_way:
1010     TOK_GROW        { $$ = TOK_GROW; }
1011     | TOK_SHRINK    { $$ = TOK_SHRINK; }
1012     ;
1013
1014 direction:
1015     TOK_UP          { $$ = TOK_UP; }
1016     | TOK_DOWN      { $$ = TOK_DOWN; }
1017     | TOK_LEFT      { $$ = TOK_LEFT; }
1018     | TOK_RIGHT     { $$ = TOK_RIGHT; }
1019     ;
1020
1021 mode:
1022     TOK_MODE STR
1023     {
1024         switch_mode($2);
1025     }
1026     ;